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
|
|
|
import "time"
|
|
|
|
|
|
|
|
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
|
|
|
// 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-05 21:58:27 +08:00
|
|
|
// it returns false. The parameter <expire> specifies the max duration it locks.
|
2019-06-19 09:06:52 +08:00
|
|
|
func TryLock(key string, expire ...time.Duration) bool {
|
|
|
|
return locker.TryLock(key, expire...)
|
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.
|
|
|
|
// The parameter <expire> specifies the max duration it locks.
|
2019-06-19 09:06:52 +08:00
|
|
|
func Lock(key string, expire ...time.Duration) {
|
|
|
|
locker.Lock(key, expire...)
|
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
|
|
|
// 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-01-18 11:30:52 +08:00
|
|
|
func TryRLock(key string) bool {
|
2019-06-19 09:06:52 +08:00
|
|
|
return locker.TryRLock(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-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
|
|
|
// 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-05 23:50:24 +08:00
|
|
|
//
|
|
|
|
// It releases the lock after <f> is executed.
|
|
|
|
//
|
|
|
|
// The parameter <expire> specifies the max duration it locks.
|
2019-06-19 09:06:52 +08:00
|
|
|
func TryLockFunc(key string, f func(), expire ...time.Duration) bool {
|
2019-06-05 23:50:24 +08:00
|
|
|
return locker.TryLockFunc(key, f, expire...)
|
|
|
|
}
|
|
|
|
|
2019-06-18 17:31:48 +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-05 23:50:24 +08:00
|
|
|
//
|
|
|
|
// It releases the lock after <f> is executed.
|
|
|
|
//
|
|
|
|
// The parameter <expire> specifies the max duration it locks.
|
|
|
|
func TryRLockFunc(key string, f func()) bool {
|
|
|
|
return locker.TryRLockFunc(key, f)
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
//
|
|
|
|
// The parameter <expire> specifies the max duration it locks.
|
2019-06-19 09:06:52 +08:00
|
|
|
func LockFunc(key string, f func(), expire ...time.Duration) {
|
2019-06-05 20:22:57 +08:00
|
|
|
locker.LockFunc(key, f, expire...)
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
//
|
|
|
|
// The parameter <expire> specifies the max duration it locks.
|
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
|
|
|
}
|