go-zero/core/syncx/barrier.go

21 lines
362 B
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package syncx
import "sync"
2021-02-28 16:16:22 +08:00
// A Barrier is used to facility the barrier on a resource.
2020-07-26 17:09:05 +08:00
type Barrier struct {
lock sync.Mutex
}
2021-02-28 16:16:22 +08:00
// Guard guards the given fn on the resource.
2020-07-26 17:09:05 +08:00
func (b *Barrier) Guard(fn func()) {
2021-04-13 00:04:19 +08:00
Guard(&b.lock, fn)
}
// Guard guards the given fn with lock.
func Guard(lock sync.Locker, fn func()) {
lock.Lock()
defer lock.Unlock()
2020-07-26 17:09:05 +08:00
fn()
}