optimize mutex lock fn

This commit is contained in:
xing 2023-12-28 20:42:11 +08:00
parent 568ab15a34
commit 12b75b9d82

5
cache/locks.go vendored
View File

@ -43,13 +43,16 @@ func (l *Locks[K]) GetLock(ctx context.Context, gMut *sync.Mutex, keys ...K) *sy
if ok { if ok {
return lo return lo
} }
num := l.numFn()
if num == 1 {
return gMut
}
gMut.Lock() gMut.Lock()
defer gMut.Unlock() defer gMut.Unlock()
lo, ok = l.m.Load(k) lo, ok = l.m.Load(k)
if ok { if ok {
return lo return lo
} }
num := l.numFn()
if num <= 0 { if num <= 0 {
lo = &sync.Mutex{} lo = &sync.Mutex{}
l.m.Store(k, lo) l.m.Store(k, lo)