gf/g/os/gmlock/gmlock.go

46 lines
1.4 KiB
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.

// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
// Package gmlock implements a thread-safe memory locker.
//
// 内存锁.
package gmlock
import "time"
var (
locker = New()
)
// 内存写锁如果锁成功返回true失败则返回false;过期时间单位为秒默认为0表示不过期
func TryLock(key string, expire...time.Duration) bool {
return locker.TryLock(key, expire...)
}
// 内存写锁锁成功返回true失败时阻塞当失败时表示有其他写锁存在;过期时间单位为秒默认为0表示不过期
func Lock(key string, expire...time.Duration) {
locker.Lock(key, expire...)
}
// 解除基于内存锁的写锁
func Unlock(key string) {
locker.Unlock(key)
}
// 内存读锁如果锁成功返回true失败则返回false; 过期时间单位为秒默认为0表示不过期
func TryRLock(key string) bool {
return locker.TryRLock(key)
}
// 内存写锁锁成功返回true失败时阻塞当失败时表示有写锁存在; 过期时间单位为秒默认为0表示不过期
func RLock(key string) {
locker.RLock(key)
}
// 解除基于内存锁的读锁
func RUnlock(key string) {
locker.RUnlock(key)
}