-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
channellock.go
33 lines (28 loc) · 925 Bytes
/
channellock.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
package paychmgr
import "sync"
type rwlock interface {
RLock()
RUnlock()
}
// channelLock manages locking for a specific channel.
// Some operations update the state of a single channel, and need to block
// other operations only on the same channel's state.
// Some operations update state that affects all channels, and need to block
// any operation against any channel.
type channelLock struct {
globalLock rwlock
chanLock sync.Mutex
}
func (l *channelLock) Lock() {
// Wait for other operations by this channel to finish.
// Exclusive per-channel (no other ops by this channel allowed).
l.chanLock.Lock()
// Wait for operations affecting all channels to finish.
// Allows ops by other channels in parallel, but blocks all operations
// if global lock is taken exclusively (eg when adding a channel)
l.globalLock.RLock()
}
func (l *channelLock) Unlock() {
l.globalLock.RUnlock()
l.chanLock.Unlock()
}