This commit is contained in:
2025-02-06 11:14:33 +08:00
commit 07847a2d9e
535 changed files with 65131 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package lockkey
import (
"fmt"
"sync"
)
type LockByKey struct {
m map[string]*sync.RWMutex
l sync.Mutex
}
func NewLockByKey() *LockByKey {
return &LockByKey{m: make(map[string]*sync.RWMutex)}
}
func (lk *LockByKey) Lock(key string) {
lk.l.Lock()
defer lk.l.Unlock()
mu, ok := lk.m[key]
if !ok {
mu = &sync.RWMutex{}
lk.m[key] = mu
}
mu.Lock()
}
func (lk *LockByKey) Unlock(key string) {
lk.l.Lock()
defer lk.l.Unlock()
mu, ok := lk.m[key]
if !ok {
panic(fmt.Sprintf("unlock non-existent key: %v", key))
}
mu.Unlock()
}