forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deferrwl.go
36 lines (29 loc) · 885 Bytes
/
deferrwl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)
}