Atlas › Test

TestTryLock_Low

Exact test identity: go.temporal.io/server/common/locks/TestPriorityMutexSuite/TestTryLock_Low

Package
go.temporal.io/server/common/locks
Suite / test hierarchy
TestPriorityMutexSuite/TestTryLock_Low
Test
TestTryLock_Low
Introduced at
priority_mutex_impl.go ×1 Frontier kind: Joint frontier
Covered ranges
11
Covered lines
57
Covered files
2

Covered source

Expand a file to inspect source; the > gutter marks covered lines.

go.temporal.io/server/common/locks/priority_mutex_impl.go 46 covered LOC · 9 ranges

Open complete file

29 var _ PriorityMutex = (*PriorityMutexImpl)(nil)
30
31 > func NewPriorityMutex() *PriorityMutexImpl { priority_mutex_impl.go
32 > lock := &sync.Mutex{}
33 > syncCV := NewConditionVariable(lock)
34 > asyncCV := NewConditionVariable(lock)
35 > return &PriorityMutexImpl{
36 > locker: lock,
37 > highCV: syncCV,
38 > lowCV: asyncCV,
39 > highWait: 0,
40 > lowWait: 0,
41 >
42 > lockState: PriorityMutexStateUnlocked,
43 > }
44 > }
45
46 // LockHigh try to lock with high priority, use LockHigh / UnlockHigh pair to lock / unlock
88 }
89
90 > func (c *PriorityMutexImpl) TryLockHigh() bool { priority_mutex_impl.go
91 > c.locker.Lock()
92 > defer c.locker.Unlock()
93 >
94 > if c.lockState != PriorityMutexStateUnlocked {
95 > return false
96 > }
97
98 c.lockState = PriorityMutexStateLockedByHigh
100 }
101
102 > func (c *PriorityMutexImpl) TryLockLow() bool { priority_mutex_impl.go
103 > c.locker.Lock()
104 > defer c.locker.Unlock()
105 >
106 > if c.lockState != PriorityMutexStateUnlocked {
107 > return false
108 > }
109
110 > c.lockState = PriorityMutexStateLockedByLow priority_mutex_impl.go
111 > return true
112 }
113
126
127 // UnlockLow unlock with low priority, use LockLow / UnlockLow pair to lock / unlock
128 > func (c *PriorityMutexImpl) UnlockLow() { priority_mutex_impl.go
129 > c.locker.Lock()
130 > defer c.locker.Unlock()
131 >
132 > if c.lockState != PriorityMutexStateLockedByLow {
133 panic(fmt.Sprintf("unable to unlock high priority, state: %v\n", c.lockState))
134 }
135
136 > c.lockState = PriorityMutexStateUnlocked priority_mutex_impl.go
137 > c.notify()
138 }
139
140 > func (c *PriorityMutexImpl) IsLocked() bool { priority_mutex_impl.go
141 > c.locker.Lock()
142 > defer c.locker.Unlock()
143 >
144 > return c.lockState != PriorityMutexStateUnlocked
145 > }
146
147 > func (c *PriorityMutexImpl) notify() { priority_mutex_impl.go
148 > if c.highWait > 0 {
149 c.highCV.Signal()
150 > } else if c.lowWait > 0 { priority_mutex_impl.go
151 c.lowCV.Signal()
152 }
go.temporal.io/server/common/locks/condition_variable_impl.go 11 covered LOC · 2 ranges

Open complete file

18 func NewConditionVariable(
19 lock Locker,
20 > ) *ConditionVariableImpl { condition_variable_impl.go
21 > return &ConditionVariableImpl{
22 > lock: lock,
23 >
24 > chanLock: sync.Mutex{},
25 > channel: newCVChannel(),
26 > }
27 > }
28
29 // Signal wakes one goroutine waiting on this condition variable, if there is any.