-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority_lock.go
61 lines (53 loc) · 1.85 KB
/
priority_lock.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package priority_lock
import (
"sync"
)
type PriorityLock interface {
Lock()
Unlock()
HighPriorityLock()
HighPriorityUnlock()
}
// PriorityPreferenceLock implements a simple triple-mutex priority lock
// patterns are like:
// Low Priority would do: lock lowPriorityMutex, wait for high priority groups, lock nextToAccess, lock dataMutex, unlock nextToAccess, do stuff, unlock dataMutex, unlock lowPriorityMutex
// High Priority would do: increment high priority waiting, lock nextToAccess, lock dataMutex, unlock nextToAccess, do stuff, unlock dataMutex, decrement high priority waiting
type PriorityPreferenceLock struct {
dataMutex sync.Mutex
nextToAccess sync.Mutex
lowPriorityMutex sync.Mutex
highPriorityWaiting sync.WaitGroup
}
func NewPriorityPreferenceLock() *PriorityPreferenceLock {
lock := PriorityPreferenceLock{
highPriorityWaiting: sync.WaitGroup{},
}
return &lock
}
// Lock will acquire a low-priority lock
// it must wait until both low priority and all high priority lock holders are released.
func (lock *PriorityPreferenceLock) Lock() {
lock.lowPriorityMutex.Lock()
lock.highPriorityWaiting.Wait()
lock.nextToAccess.Lock()
lock.dataMutex.Lock()
lock.nextToAccess.Unlock()
}
// Unlock will unlock the low-priority lock
func (lock *PriorityPreferenceLock) Unlock() {
lock.dataMutex.Unlock()
lock.lowPriorityMutex.Unlock()
}
// HighPriorityLock will acquire a high-priority lock
// it must still wait until a low-priority lock has been released and then potentially other high priority lock contenders.
func (lock *PriorityPreferenceLock) HighPriorityLock() {
lock.highPriorityWaiting.Add(1)
lock.nextToAccess.Lock()
lock.dataMutex.Lock()
lock.nextToAccess.Unlock()
}
// HighPriorityUnlock will unlock the high-priority lock
func (lock *PriorityPreferenceLock) HighPriorityUnlock() {
lock.dataMutex.Unlock()
lock.highPriorityWaiting.Done()
}