github.com/tursom/GoCollections@v0.3.10/concurrent/Lock.go (about)

     1  /*
     2   * Copyright (c) 2022 tursom. All rights reserved.
     3   * Use of this source code is governed by a GPL-3
     4   * license that can be found in the LICENSE file.
     5   */
     6  
     7  package concurrent
     8  
     9  type (
    10  	Lock interface {
    11  		Lock()
    12  		TryLock() bool
    13  		Unlock()
    14  	}
    15  	RWLock interface {
    16  		Lock
    17  		RLock()
    18  		TryRLock() bool
    19  		RUnlock()
    20  	}
    21  )
    22  
    23  func WithLock[R any](lock Lock, f func() R) R {
    24  	lock.Lock()
    25  	defer lock.Unlock()
    26  	return f()
    27  }
    28  
    29  func WithRLock[R any](lock RWLock, f func() R) R {
    30  	lock.RLock()
    31  	defer lock.RUnlock()
    32  	return f()
    33  }