2019-02-22 11:40:27 +01:00
|
|
|
// package chainlock implements a mutex whose Lock and Unlock
|
|
|
|
// methods return the lock itself, to enable chaining.
|
|
|
|
//
|
|
|
|
// Intended Usage
|
|
|
|
//
|
2022-10-24 22:09:02 +02:00
|
|
|
// defer s.lock().unlock()
|
|
|
|
// // drop lock while waiting for wait group
|
|
|
|
// func() {
|
|
|
|
// defer a.l.Unlock().Lock()
|
|
|
|
// fssesDone.Wait()
|
|
|
|
// }()
|
2019-02-22 11:40:27 +01:00
|
|
|
package chainlock
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
|
|
type L struct {
|
|
|
|
mtx sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *L {
|
|
|
|
return &L{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *L) Lock() *L {
|
|
|
|
l.mtx.Lock()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *L) Unlock() *L {
|
|
|
|
l.mtx.Unlock()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *L) NewCond() *sync.Cond {
|
|
|
|
return sync.NewCond(&l.mtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *L) DropWhile(f func()) {
|
|
|
|
defer l.Unlock().Lock()
|
|
|
|
f()
|
2019-03-22 19:41:12 +01:00
|
|
|
}
|
2020-04-07 23:45:20 +02:00
|
|
|
|
|
|
|
func (l *L) HoldWhile(f func()) {
|
|
|
|
defer l.Lock().Unlock()
|
|
|
|
f()
|
|
|
|
}
|