]> Sergey Matveev's repositories - btrtrc.git/blob - deferrwl.go
Coalesce piece state change notifications on client unlock
[btrtrc.git] / deferrwl.go
1 package torrent
2
3 import "sync"
4
5 // Runs deferred actions on Unlock. Note that actions are assumed to be the results of changes that
6 // would only occur with a write lock at present. The race detector should catch instances of defers
7 // without the write lock being held.
8 type lockWithDeferreds struct {
9         internal      sync.RWMutex
10         unlockActions []func()
11 }
12
13 func (me *lockWithDeferreds) Lock() {
14         me.internal.Lock()
15 }
16
17 func (me *lockWithDeferreds) Unlock() {
18         for _, a := range me.unlockActions {
19                 a()
20         }
21         me.unlockActions = me.unlockActions[:0]
22         me.internal.Unlock()
23 }
24
25 func (me *lockWithDeferreds) RLock() {
26         me.internal.RLock()
27 }
28
29 func (me *lockWithDeferreds) RUnlock() {
30         me.internal.RUnlock()
31 }
32
33 func (me *lockWithDeferreds) Defer(action func()) {
34         me.unlockActions = append(me.unlockActions, action)
35 }