// 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 thread-safe memory locker. package gmlock import "time" var ( locker = New() ) // TryLock tries locking the with write lock, // it returns true if success, or if there's a write/read lock the , // it returns false. The parameter specifies the max duration it locks. func TryLock(key string, expire...time.Duration) bool { return locker.TryLock(key, expire...) } // Lock locks the with write lock. // If there's a write/read lock the , // it will blocks until the lock is released. // The parameter specifies the max duration it locks. func Lock(key string, expire...time.Duration) { locker.Lock(key, expire...) } // Unlock unlocks the write lock of the . func Unlock(key string) { locker.Unlock(key) } // TryRLock tries locking the with read lock. // It returns true if success, or if there's a write lock on , it returns false. func TryRLock(key string) bool { return locker.TryRLock(key) } // RLock locks the with read lock. // If there's a write lock on , // it will blocks until the write lock is released. func RLock(key string) { locker.RLock(key) } // RUnlock unlocks the read lock of the . func RUnlock(key string) { locker.RUnlock(key) } // LockFunc locks the with write lock and callback function . // If there's a write/read lock the , // it will blocks until the lock is released. // // It releases the lock after is executed. // // The parameter specifies the max duration it locks. func LockFunc(key string, f func(), expire...time.Duration) { locker.LockFunc(key, f, expire...) } // RLockFunc locks the with read lock and callback function . // If there's a write lock the , // it will blocks until the lock is released. // // It releases the lock after is executed. // // The parameter specifies the max duration it locks. func RLockFunc(key string, f func()) { locker.RLockFunc(key, f) }