1
0
forked from x/icebergs
icebergs/lock.go
2022-07-25 18:12:32 +08:00

37 lines
582 B
Go

package ice
import (
"sync"
kit "shylinux.com/x/toolkits"
)
var lock = map[string]*sync.RWMutex{}
var _lock = sync.Mutex{}
func (m *Message) _lock(key string) *sync.RWMutex {
if key == "" {
key = m.PrefixKey()
}
_lock.Lock()
defer _lock.Unlock()
l, ok := lock[key]
if !ok {
l = &sync.RWMutex{}
lock[key] = l
}
return l
}
func (m *Message) Lock(arg ...Any) func() {
l := m._lock(kit.Keys(arg...))
l.Lock()
return func() { l.Unlock() }
}
func (m *Message) RLock(arg ...Any) func() {
l := m._lock(kit.Keys(arg...))
l.RLock()
return func() { l.RUnlock() }
}