/
mutex.go
75 lines (61 loc) · 1.55 KB
/
mutex.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package sync
import (
"sync"
"time"
)
type PriorityMutex struct {
name string
mutex *PrioritySemaphore
lock *sync.Mutex
}
func (m *PriorityMutex) getCurrentPending() []string {
return m.mutex.getCurrentPending()
}
var _ Semaphore = &PriorityMutex{}
// NewMutex creates new mutex lock object
// name of the mutex
// callbackFunc is a release notification function.
func NewMutex(name string, nextWorkflow NextWorkflow) *PriorityMutex {
return &PriorityMutex{
name: name,
lock: &sync.Mutex{},
mutex: NewSemaphore(name, 1, nextWorkflow, "mutex"),
}
}
func (m *PriorityMutex) getName() string {
return m.name
}
func (m *PriorityMutex) getLimit() int {
return m.mutex.limit
}
func (m *PriorityMutex) getCurrentHolders() []string {
return m.mutex.getCurrentHolders()
}
func (m *PriorityMutex) resize(n int) bool {
return false
}
func (m *PriorityMutex) release(key string) bool {
m.lock.Lock()
defer m.lock.Unlock()
return m.mutex.release(key)
}
func (m *PriorityMutex) acquire(holderKey string) bool {
m.lock.Lock()
defer m.lock.Unlock()
return m.mutex.acquire(holderKey)
}
func (m *PriorityMutex) addToQueue(holderKey string, priority int32, creationTime time.Time) {
m.lock.Lock()
defer m.lock.Unlock()
m.mutex.addToQueue(holderKey, priority, creationTime)
}
func (m *PriorityMutex) removeFromQueue(holderKey string) {
m.lock.Lock()
defer m.lock.Unlock()
m.mutex.removeFromQueue(holderKey)
}
func (m *PriorityMutex) tryAcquire(holderKey string) (bool, string) {
m.lock.Lock()
defer m.lock.Unlock()
return m.mutex.tryAcquire(holderKey)
}