gmlock.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gmlock implements a concurrent-safe memory-based locker.
  7. package gmlock
  8. var (
  9. // Default locker.
  10. locker = New()
  11. )
  12. // Lock locks the <key> with writing lock.
  13. // If there's a write/reading lock the <key>,
  14. // it will blocks until the lock is released.
  15. func Lock(key string) {
  16. locker.Lock(key)
  17. }
  18. // TryLock tries locking the <key> with writing lock,
  19. // it returns true if success, or if there's a write/reading lock the <key>,
  20. // it returns false.
  21. func TryLock(key string) bool {
  22. return locker.TryLock(key)
  23. }
  24. // Unlock unlocks the writing lock of the <key>.
  25. func Unlock(key string) {
  26. locker.Unlock(key)
  27. }
  28. // RLock locks the <key> with reading lock.
  29. // If there's a writing lock on <key>,
  30. // it will blocks until the writing lock is released.
  31. func RLock(key string) {
  32. locker.RLock(key)
  33. }
  34. // TryRLock tries locking the <key> with reading lock.
  35. // It returns true if success, or if there's a writing lock on <key>, it returns false.
  36. func TryRLock(key string) bool {
  37. return locker.TryRLock(key)
  38. }
  39. // RUnlock unlocks the reading lock of the <key>.
  40. func RUnlock(key string) {
  41. locker.RUnlock(key)
  42. }
  43. // LockFunc locks the <key> with writing lock and callback function <f>.
  44. // If there's a write/reading lock the <key>,
  45. // it will blocks until the lock is released.
  46. //
  47. // It releases the lock after <f> is executed.
  48. func LockFunc(key string, f func()) {
  49. locker.LockFunc(key, f)
  50. }
  51. // RLockFunc locks the <key> with reading lock and callback function <f>.
  52. // If there's a writing lock the <key>,
  53. // it will blocks until the lock is released.
  54. //
  55. // It releases the lock after <f> is executed.
  56. func RLockFunc(key string, f func()) {
  57. locker.RLockFunc(key, f)
  58. }
  59. // TryLockFunc locks the <key> with writing lock and callback function <f>.
  60. // It returns true if success, or else if there's a write/reading lock the <key>, it return false.
  61. //
  62. // It releases the lock after <f> is executed.
  63. func TryLockFunc(key string, f func()) bool {
  64. return locker.TryLockFunc(key, f)
  65. }
  66. // TryRLockFunc locks the <key> with reading lock and callback function <f>.
  67. // It returns true if success, or else if there's a writing lock the <key>, it returns false.
  68. //
  69. // It releases the lock after <f> is executed.
  70. func TryRLockFunc(key string, f func()) bool {
  71. return locker.TryRLockFunc(key, f)
  72. }
  73. // Remove removes mutex with given <key>.
  74. func Remove(key string) {
  75. locker.Remove(key)
  76. }