1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package lock
- import (
- "sync"
- "sync/atomic"
- )
- var _ Locker = (*locker)(nil)
- type Locker interface {
- condition() bool
- Lock()
- Unlock()
- }
- type locker struct {
- lock *sync.Mutex
- cond *sync.Cond
- v *atomic.Bool
- }
- func NewLocker() Locker {
- lock := new(sync.Mutex)
- return &locker{
- lock: lock,
- cond: sync.NewCond(lock),
- v: new(atomic.Bool),
- }
- }
- func (l *locker) condition() bool {
- return l.v.Load()
- }
- func (l *locker) Lock() {
- l.cond.L.Lock()
- for l.condition() {
- l.cond.Wait()
- }
- l.v.Store(true)
- }
- func (l *locker) Unlock() {
- l.v.Store(false)
- l.cond.L.Unlock()
- l.cond.Signal()
- }
|