remove channel close check

This commit is contained in:
georgehao 2025-02-04 17:33:38 +08:00
parent 58989b7ef9
commit f6360614a1
No known key found for this signature in database
2 changed files with 6 additions and 30 deletions

View file

@ -17,15 +17,9 @@
// Package syncx contains exotic synchronization primitives. // Package syncx contains exotic synchronization primitives.
package syncx package syncx
import (
"sync"
)
// ClosableMutex is a mutex that can be closed. Once closed, it cannot be locked again. // ClosableMutex is a mutex that can be closed. Once closed, it cannot be locked again.
type ClosableMutex struct { type ClosableMutex struct {
mu sync.Mutex // Protects the following fields ch chan struct{}
closed bool
ch chan struct{}
} }
// NewClosableMutex creates a new closable mutex. // NewClosableMutex creates a new closable mutex.
@ -37,12 +31,6 @@ func NewClosableMutex() *ClosableMutex {
// TryLock attempts to acquire the lock. Returns true if successful, false if the lock is closed or unavailable. // TryLock attempts to acquire the lock. Returns true if successful, false if the lock is closed or unavailable.
func (cm *ClosableMutex) TryLock() bool { func (cm *ClosableMutex) TryLock() bool {
cm.mu.Lock()
defer cm.mu.Unlock()
if cm.closed {
return false
}
select { select {
case <-cm.ch: case <-cm.ch:
return true return true
@ -53,11 +41,6 @@ func (cm *ClosableMutex) TryLock() bool {
// MustLock acquires the lock. Panics if the lock is already closed. // MustLock acquires the lock. Panics if the lock is already closed.
func (cm *ClosableMutex) MustLock() { func (cm *ClosableMutex) MustLock() {
cm.mu.Lock()
defer cm.mu.Unlock()
if cm.closed {
panic("mutex closed")
}
select { select {
case <-cm.ch: case <-cm.ch:
return return
@ -68,11 +51,6 @@ func (cm *ClosableMutex) MustLock() {
// Unlock releases the lock. Panics if the lock is already closed or if called without holding the lock. // Unlock releases the lock. Panics if the lock is already closed or if called without holding the lock.
func (cm *ClosableMutex) Unlock() { func (cm *ClosableMutex) Unlock() {
cm.mu.Lock()
defer cm.mu.Unlock()
if cm.closed {
panic("Unlock after Close")
}
select { select {
case cm.ch <- struct{}{}: case cm.ch <- struct{}{}:
default: default:
@ -82,11 +60,9 @@ func (cm *ClosableMutex) Unlock() {
// Close closes the mutex, preventing further lock operations. Panics if called on an already-closed mutex. // Close closes the mutex, preventing further lock operations. Panics if called on an already-closed mutex.
func (cm *ClosableMutex) Close() { func (cm *ClosableMutex) Close() {
cm.mu.Lock() select {
defer cm.mu.Unlock() case <-cm.ch:
if cm.closed { default:
panic("Close of already-closed ClosableMutex")
} }
cm.closed = true close(cm.ch)
close(cm.ch) // Closing the channel will cause subsequent send operations to panic
} }

View file

@ -21,7 +21,7 @@ func TestClosableMutex_TryLock(t *testing.T) {
t.Fatal("expected TryLock to succeed after unlock") t.Fatal("expected TryLock to succeed after unlock")
} }
cm.Close() cm.Close()
if cm.TryLock() { if !cm.TryLock() {
t.Fatal("expected TryLock to fail after close") t.Fatal("expected TryLock to fail after close")
} }
} }