daShangDao_planA/controlState/lock/lock.go

37 lines
957 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package lock
import "sync"
// 用sync.Map替代原生map天然支持并发安全
var lock sync.Map
// GetLock 获取锁返回true表示已上锁false表示未上锁
func GetLock(key string) bool {
v, ok := lock.Load(key)
if !ok {
return false
}
// 断言为bool类型确保存储的是布尔值
locked, ok := v.(bool)
return ok && locked
}
// SetLock 设置锁(原子操作)
func SetLock(key string) {
lock.Store(key, true)
}
// DestroyLock 销毁锁(原子操作)
func DestroyLock(key string) {
lock.Delete(key)
}
// TryLock 尝试加锁(核心:检查+设置原子化)
// 返回true表示加锁成功false表示已被上锁
func TryLock(key string) bool {
// LoadOrStore如果key不存在则存储值返回false如果已存在则返回true
_, loaded := lock.LoadOrStore(key, true)
// loaded为true表示已上锁返回falseloaded为false表示加锁成功返回true
return !loaded
}