2019-12-13 04:55:56 +00:00
|
|
|
package torrent
|
|
|
|
|
2019-12-17 09:49:40 +00:00
|
|
|
import "github.com/anacrolix/sync"
|
2019-12-13 04:55:56 +00:00
|
|
|
|
|
|
|
// 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() {
|
2021-09-17 03:08:00 +00:00
|
|
|
unlockActions := me.unlockActions
|
|
|
|
for i := 0; i < len(unlockActions); i += 1 {
|
|
|
|
unlockActions[i]()
|
2019-12-13 04:55:56 +00:00
|
|
|
}
|
2021-09-17 03:08:00 +00:00
|
|
|
me.unlockActions = unlockActions[:0]
|
2019-12-13 04:55:56 +00:00
|
|
|
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)
|
|
|
|
}
|