Files
mayfly-go/server/internal/machine/guac/counted_lock.go
zongyangleo 8ba87c1895 !149 fix:连接池修复和guacd自动重连
* 连接池修复和guacd自动重连
2026-04-07 13:29:31 +00:00

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
}