1 Star 1 Fork 0

Breeze / bzv2

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
lock.go 959 Bytes
一键复制 编辑 原始数据 按行查看 历史
Breeze 提交于 2023-08-13 23:58 . U:gin.context_value
package bzlock
import (
"gitee.com/breezeHub/bzv2/pkg/data/bzgncmap"
"sync"
)
var (
locker = bzgncmap.New[string, *sync.Mutex]()
rwLocker = bzgncmap.New[string, *sync.RWMutex]()
)
func Lock(key string) {
mtx, _ := locker.GetOrPut(key, &sync.Mutex{})
mtx.Lock()
}
func UnLock(key string) {
mtx, ok := locker.Get(key)
if ok {
mtx.Unlock()
}
}
func RLock(key string) {
mtx, _ := rwLocker.GetOrPut(key, &sync.RWMutex{})
mtx.RLock()
}
func UnRLock(key string) {
mtx, ok := rwLocker.Get(key)
if ok {
mtx.RUnlock()
}
}
func WLock(key string) {
mtx, _ := rwLocker.GetOrPut(key, &sync.RWMutex{})
mtx.Lock()
}
func UnWLock(key string) {
mtx, ok := rwLocker.Get(key)
if ok {
mtx.Unlock()
}
}
func LockFunc(key string, fn func()) {
Lock(key)
defer UnLock(key)
fn()
}
func RLockFunc(key string, fn func()) {
RLock(key)
defer UnRLock(key)
fn()
}
func WLockFunc(key string, fn func()) {
WLock(key)
defer UnWLock(key)
fn()
}
Go
1
https://gitee.com/breezeHub/bzv2.git
git@gitee.com:breezeHub/bzv2.git
breezeHub
bzv2
bzv2
v0.0.5

搜索帮助