status-go/vendor/github.com/anacrolix/torrent/deferrwl.go

37 lines
885 B
Go
Raw Normal View History

2022-03-10 09:44:48 +00:00
package torrent
import "github.com/anacrolix/sync"
// Runs deferred actions on Unlock. Note that actions are assumed to be the results of changes that
// would only occur with a write lock at present. The race detector should catch instances of defers
// without the write lock being held.
type lockWithDeferreds struct {
internal sync.RWMutex
unlockActions []func()
}
func (me *lockWithDeferreds) Lock() {
me.internal.Lock()
}
func (me *lockWithDeferreds) Unlock() {
unlockActions := me.unlockActions
for i := 0; i < len(unlockActions); i += 1 {
unlockActions[i]()
}
me.unlockActions = unlockActions[:0]
me.internal.Unlock()
}
func (me *lockWithDeferreds) RLock() {
me.internal.RLock()
}
func (me *lockWithDeferreds) RUnlock() {
me.internal.RUnlock()
}
func (me *lockWithDeferreds) Defer(action func()) {
me.unlockActions = append(me.unlockActions, action)
}