gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/lock/lock.go (about)

     1  package lock
     2  
     3  import "sync"
     4  
     5  type RLocker interface {
     6  	RLock()
     7  
     8  	RUnlock()
     9  }
    10  
    11  func LockDo(locker sync.Locker, f func()) {
    12  	locker.Lock()
    13  	defer locker.Unlock()
    14  	f()
    15  }
    16  
    17  func LockGet[V any](locker sync.Locker, f func() V) V {
    18  	locker.Lock()
    19  	defer locker.Unlock()
    20  	return f()
    21  }
    22  
    23  func LockGetDouble[V1, V2 any](locker sync.Locker, f func() (V1, V2)) (V1, V2) {
    24  	locker.Lock()
    25  	defer locker.Unlock()
    26  	return f()
    27  }
    28  
    29  func LockGetTriple[V1, V2, V3 any](locker sync.Locker, f func() (V1, V2, V3)) (V1, V2, V3) {
    30  	locker.Lock()
    31  	defer locker.Unlock()
    32  	return f()
    33  }
    34  
    35  func RLockDo(locker RLocker, f func()) {
    36  	locker.RLock()
    37  	defer locker.RUnlock()
    38  	f()
    39  }
    40  
    41  func RLockGet[V any](locker RLocker, f func() V) V {
    42  	locker.RLock()
    43  	defer locker.RUnlock()
    44  	return f()
    45  }
    46  
    47  func RLockGetDouble[V1, V2 any](locker RLocker, f func() (V1, V2)) (V1, V2) {
    48  	locker.RLock()
    49  	defer locker.RUnlock()
    50  	return f()
    51  }
    52  
    53  func RLockGetTriple[V1, V2, V3 any](locker RLocker, f func() (V1, V2, V3)) (V1, V2, V3) {
    54  	locker.RLock()
    55  	defer locker.RUnlock()
    56  	return f()
    57  }