github.com/zhongdalu/gf@v1.0.0/g/os/gmlock/gmlock.go (about)

     1  // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf.
     6  
     7  // Package gmlock implements a concurrent-safe memory-based locker.
     8  package gmlock
     9  
    10  var (
    11  	// Default locker.
    12  	locker = New()
    13  )
    14  
    15  // Lock locks the <key> with writing lock.
    16  // If there's a write/reading lock the <key>,
    17  // it will blocks until the lock is released.
    18  func Lock(key string) {
    19  	locker.Lock(key)
    20  }
    21  
    22  // TryLock tries locking the <key> with writing lock,
    23  // it returns true if success, or if there's a write/reading lock the <key>,
    24  // it returns false.
    25  func TryLock(key string) bool {
    26  	return locker.TryLock(key)
    27  }
    28  
    29  // Unlock unlocks the writing lock of the <key>.
    30  func Unlock(key string) {
    31  	locker.Unlock(key)
    32  }
    33  
    34  // RLock locks the <key> with reading lock.
    35  // If there's a writing lock on <key>,
    36  // it will blocks until the writing lock is released.
    37  func RLock(key string) {
    38  	locker.RLock(key)
    39  }
    40  
    41  // TryRLock tries locking the <key> with reading lock.
    42  // It returns true if success, or if there's a writing lock on <key>, it returns false.
    43  func TryRLock(key string) bool {
    44  	return locker.TryRLock(key)
    45  }
    46  
    47  // RUnlock unlocks the reading lock of the <key>.
    48  func RUnlock(key string) {
    49  	locker.RUnlock(key)
    50  }
    51  
    52  // LockFunc locks the <key> with writing lock and callback function <f>.
    53  // If there's a write/reading lock the <key>,
    54  // it will blocks until the lock is released.
    55  //
    56  // It releases the lock after <f> is executed.
    57  func LockFunc(key string, f func()) {
    58  	locker.LockFunc(key, f)
    59  }
    60  
    61  // RLockFunc locks the <key> with reading lock and callback function <f>.
    62  // If there's a writing lock the <key>,
    63  // it will blocks until the lock is released.
    64  //
    65  // It releases the lock after <f> is executed.
    66  func RLockFunc(key string, f func()) {
    67  	locker.RLockFunc(key, f)
    68  }
    69  
    70  // TryLockFunc locks the <key> with writing lock and callback function <f>.
    71  // It returns true if success, or else if there's a write/reading lock the <key>, it return false.
    72  //
    73  // It releases the lock after <f> is executed.
    74  func TryLockFunc(key string, f func()) bool {
    75  	return locker.TryLockFunc(key, f)
    76  }
    77  
    78  // TryRLockFunc locks the <key> with reading lock and callback function <f>.
    79  // It returns true if success, or else if there's a writing lock the <key>, it returns false.
    80  //
    81  // It releases the lock after <f> is executed.
    82  func TryRLockFunc(key string, f func()) bool {
    83  	return locker.TryRLockFunc(key, f)
    84  }
    85  
    86  // Remove removes mutex with given <key>.
    87  func Remove(key string) {
    88  	locker.Remove(key)
    89  }