github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/xsync/mutex.go (about)

     1  package xsync
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  type Mutex struct { //nolint:gocritic
     8  	sync.Mutex
     9  }
    10  
    11  func (l *Mutex) WithLock(f func()) {
    12  	l.Lock()
    13  	defer l.Unlock()
    14  
    15  	f()
    16  }
    17  
    18  type RWMutex struct { //nolint:gocritic
    19  	sync.RWMutex
    20  }
    21  
    22  func (l *RWMutex) WithLock(f func()) {
    23  	l.Lock()
    24  	defer l.Unlock()
    25  
    26  	f()
    27  }
    28  
    29  func (l *RWMutex) WithRLock(f func()) {
    30  	l.RLock()
    31  	defer l.RUnlock()
    32  
    33  	f()
    34  }
    35  
    36  type (
    37  	mutex interface {
    38  		Lock()
    39  		Unlock()
    40  	}
    41  	rwMutex interface {
    42  		RLock()
    43  		RUnlock()
    44  	}
    45  )
    46  
    47  func WithLock[T any](l mutex, f func() T) T {
    48  	l.Lock()
    49  	defer l.Unlock()
    50  
    51  	return f()
    52  }
    53  
    54  func WithRLock[T any](l rwMutex, f func() T) T {
    55  	l.RLock()
    56  	defer l.RUnlock()
    57  
    58  	return f()
    59  }