mirror of
https://gitee.com/dromara/mayfly-go
synced 2026-05-19 01:15:40 +08:00
34 lines
657 B
Go
34 lines
657 B
Go
package guac
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// CountedLock counts how many goroutines are waiting on the lock
|
|
type CountedLock struct {
|
|
core sync.Mutex
|
|
numLocks int32
|
|
}
|
|
|
|
// Lock locks the mutex
|
|
func (r *CountedLock) Lock() {
|
|
atomic.AddInt32(&r.numLocks, 1)
|
|
r.core.Lock()
|
|
}
|
|
|
|
// Unlock unlocks the mutex
|
|
func (r *CountedLock) Unlock() {
|
|
// 先检查计数,避免重复解锁导致 panic
|
|
if atomic.LoadInt32(&r.numLocks) <= 0 {
|
|
return
|
|
}
|
|
atomic.AddInt32(&r.numLocks, -1)
|
|
r.core.Unlock()
|
|
}
|
|
|
|
// HasQueued returns true if a goroutine is waiting on the lock
|
|
func (r *CountedLock) HasQueued() bool {
|
|
return atomic.LoadInt32(&r.numLocks) > 1
|
|
}
|