gf/g/os/gmlock/gmlock.go

96 lines
3.0 KiB
Go
Raw Normal View History

// Copyright 2018 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
// Package gmlock implements a concurrent-safe memory-based locker.
package gmlock
import "time"
var (
// Default locker.
2019-06-19 09:06:52 +08:00
locker = New()
)
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...)
}
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...)
}
2019-06-18 17:31:48 +08:00
// Unlock unlocks the writing lock of the <key>.
func Unlock(key string) {
2019-06-19 09:06:52 +08:00
locker.Unlock(key)
}
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.
func TryRLock(key string) bool {
2019-06-19 09:06:52 +08:00
return locker.TryRLock(key)
}
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.
func RLock(key string) {
2019-06-19 09:06:52 +08:00
locker.RLock(key)
}
2019-06-18 17:31:48 +08:00
// RUnlock unlocks the reading lock of the <key>.
func RUnlock(key string) {
2019-06-19 09:06:52 +08:00
locker.RUnlock(key)
}
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.
//
// 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 {
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.
//
// 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) {
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.
func RLockFunc(key string, f func()) {
locker.RLockFunc(key, f)
2019-06-19 09:06:52 +08:00
}