barrier.go 362 B

1234567891011121314151617181920
  1. package syncx
  2. import "sync"
  3. // A Barrier is used to facility the barrier on a resource.
  4. type Barrier struct {
  5. lock sync.Mutex
  6. }
  7. // Guard guards the given fn on the resource.
  8. func (b *Barrier) Guard(fn func()) {
  9. Guard(&b.lock, fn)
  10. }
  11. // Guard guards the given fn with lock.
  12. func Guard(lock sync.Locker, fn func()) {
  13. lock.Lock()
  14. defer lock.Unlock()
  15. fn()
  16. }