2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). 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
|
|
|
|
2021-10-21 18:22:47 +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)
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// TryLock tries locking the `key` with writing lock,
|
|
|
|
// it returns true if success, or if there's a write/reading lock the `key`,
|
2019-06-21 22:23:07 +08:00
|
|
|
// it returns false.
|
|
|
|
func TryLock(key string) bool {
|
|
|
|
return locker.TryLock(key)
|
2018-08-30 00:00:15 +08:00
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +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
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// RLock locks the `key` with reading lock.
|
|
|
|
// If there's a writing lock on `key`,
|
2019-06-18 17:31:48 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +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.
|
2019-06-21 22:23:07 +08:00
|
|
|
func TryRLock(key string) bool {
|
|
|
|
return locker.TryRLock(key)
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +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
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +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.
|
|
|
|
//
|
2021-10-21 18:22:47 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +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.
|
|
|
|
//
|
2021-10-21 18:22:47 +08:00
|
|
|
// 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
|
|
|
|
2021-10-21 18:22:47 +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.
|
2019-06-21 22:23:07 +08:00
|
|
|
//
|
2021-10-21 18:22:47 +08:00
|
|
|
// It releases the lock after `f` is executed.
|
2019-06-21 22:23:07 +08:00
|
|
|
func TryLockFunc(key string, f func()) bool {
|
|
|
|
return locker.TryLockFunc(key, f)
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// 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.
|
2019-06-21 22:23:07 +08:00
|
|
|
//
|
2021-10-21 18:22:47 +08:00
|
|
|
// It releases the lock after `f` is executed.
|
2019-06-21 22:23:07 +08:00
|
|
|
func TryRLockFunc(key string, f func()) bool {
|
|
|
|
return locker.TryRLockFunc(key, f)
|
|
|
|
}
|
2019-06-22 11:45:58 +08:00
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// Remove removes mutex with given `key`.
|
2019-06-22 11:45:58 +08:00
|
|
|
func Remove(key string) {
|
|
|
|
locker.Remove(key)
|
|
|
|
}
|