2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-08-30 00:00:15 +08:00
|
|
|
//
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-08-30 00:00:15 +08:00
|
|
|
|
2019-06-05 23:50:24 +08:00
|
|
|
// Package gmlock implements a concurrent-safe memory-based locker.
|
2018-08-30 00:00:15 +08:00
|
|
|
package gmlock
|
|
|
|
|
2019-01-18 11:30:52 +08:00
|
|
|
var (
|
2019-06-05 23:50:24 +08:00
|
|
|
// Default locker.
|
2019-06-19 09:06:52 +08:00
|
|
|
locker = New()
|
2019-01-18 11:30:52 +08:00
|
|
|
)
|
2018-08-30 00:00:15 +08:00
|
|
|
|
2019-06-18 17:31:48 +08:00
|
|
|
// Lock locks the <key> with writing lock.
|
|
|
|
// If there's a write/reading lock the <key>,
|
2019-06-05 21:58:27 +08:00
|
|
|
// it will blocks until the lock is released.
|
2019-06-21 22:23:07 +08:00
|
|
|
func Lock(key string) {
|
|
|
|
locker.Lock(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryLock tries locking the <key> with writing lock,
|
|
|
|
// it returns true if success, or if there's a write/reading lock the <key>,
|
|
|
|
// it returns false.
|
|
|
|
func TryLock(key string) bool {
|
|
|
|
return locker.TryLock(key)
|
2018-08-30 00:00:15 +08:00
|
|
|
}
|
|
|
|
|
2019-06-18 17:31:48 +08:00
|
|
|
// Unlock unlocks the writing lock of the <key>.
|
2018-08-30 00:00:15 +08:00
|
|
|
func Unlock(key string) {
|
2019-06-19 09:06:52 +08:00
|
|
|
locker.Unlock(key)
|
2018-08-30 00:00:15 +08:00
|
|
|
}
|
|
|
|
|
2019-06-18 17:31:48 +08:00
|
|
|
// RLock locks the <key> with reading lock.
|
|
|
|
// If there's a writing lock on <key>,
|
|
|
|
// it will blocks until the writing lock is released.
|
2019-01-18 11:30:52 +08:00
|
|
|
func RLock(key string) {
|
2019-06-19 09:06:52 +08:00
|
|
|
locker.RLock(key)
|
2018-08-30 00:00:15 +08:00
|
|
|
}
|
|
|
|
|
2019-06-21 22:23:07 +08:00
|
|
|
// TryRLock tries locking the <key> with reading lock.
|
|
|
|
// It returns true if success, or if there's a writing lock on <key>, it returns false.
|
|
|
|
func TryRLock(key string) bool {
|
|
|
|
return locker.TryRLock(key)
|
|
|
|
}
|
|
|
|
|
2019-06-18 17:31:48 +08:00
|
|
|
// RUnlock unlocks the reading lock of the <key>.
|
2018-08-30 00:00:15 +08:00
|
|
|
func RUnlock(key string) {
|
2019-06-19 09:06:52 +08:00
|
|
|
locker.RUnlock(key)
|
2019-06-05 20:22:57 +08:00
|
|
|
}
|
|
|
|
|
2019-06-18 17:31:48 +08:00
|
|
|
// LockFunc locks the <key> with writing lock and callback function <f>.
|
|
|
|
// If there's a write/reading lock the <key>,
|
2019-06-05 21:58:27 +08:00
|
|
|
// it will blocks until the lock is released.
|
|
|
|
//
|
|
|
|
// It releases the lock after <f> is executed.
|
2019-06-21 22:23:07 +08:00
|
|
|
func LockFunc(key string, f func()) {
|
|
|
|
locker.LockFunc(key, f)
|
2019-06-05 20:22:57 +08:00
|
|
|
}
|
|
|
|
|
2019-06-18 17:31:48 +08:00
|
|
|
// RLockFunc locks the <key> with reading lock and callback function <f>.
|
|
|
|
// If there's a writing lock the <key>,
|
2019-06-05 21:58:27 +08:00
|
|
|
// it will blocks until the lock is released.
|
|
|
|
//
|
|
|
|
// It releases the lock after <f> is executed.
|
2019-06-05 20:22:57 +08:00
|
|
|
func RLockFunc(key string, f func()) {
|
|
|
|
locker.RLockFunc(key, f)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-06-21 22:23:07 +08:00
|
|
|
|
|
|
|
// TryLockFunc locks the <key> with writing lock and callback function <f>.
|
|
|
|
// It returns true if success, or else if there's a write/reading lock the <key>, it return false.
|
|
|
|
//
|
|
|
|
// It releases the lock after <f> is executed.
|
|
|
|
func TryLockFunc(key string, f func()) bool {
|
|
|
|
return locker.TryLockFunc(key, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryRLockFunc locks the <key> with reading lock and callback function <f>.
|
|
|
|
// It returns true if success, or else if there's a writing lock the <key>, it returns false.
|
|
|
|
//
|
|
|
|
// It releases the lock after <f> is executed.
|
|
|
|
func TryRLockFunc(key string, f func()) bool {
|
|
|
|
return locker.TryRLockFunc(key, f)
|
|
|
|
}
|
2019-06-22 11:45:58 +08:00
|
|
|
|
|
|
|
// Remove removes mutex with given <key>.
|
|
|
|
func Remove(key string) {
|
|
|
|
locker.Remove(key)
|
|
|
|
}
|