go.temporal.io/server@v1.23.0/common/locks/priority_mutex_impl.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package locks 26 27 import ( 28 "context" 29 "fmt" 30 "sync" 31 ) 32 33 const ( 34 PriorityMutexStateUnlocked PriorityMutexState = 0 35 PriorityMutexStateLockedByHigh PriorityMutexState = 1 36 PriorityMutexStateLockedByLow PriorityMutexState = 2 37 ) 38 39 type ( 40 PriorityMutexState int 41 42 PriorityMutexImpl struct { 43 locker sync.Locker 44 highCV ConditionVariable 45 lowCV ConditionVariable 46 highWait int 47 lowWait int 48 49 lockState PriorityMutexState 50 } 51 ) 52 53 var _ PriorityMutex = (*PriorityMutexImpl)(nil) 54 55 func NewPriorityMutex() *PriorityMutexImpl { 56 lock := &sync.Mutex{} 57 syncCV := NewConditionVariable(lock) 58 asyncCV := NewConditionVariable(lock) 59 return &PriorityMutexImpl{ 60 locker: lock, 61 highCV: syncCV, 62 lowCV: asyncCV, 63 highWait: 0, 64 lowWait: 0, 65 66 lockState: PriorityMutexStateUnlocked, 67 } 68 } 69 70 // LockHigh try to lock with high priority, use LockHigh / UnlockHigh pair to lock / unlock 71 func (c *PriorityMutexImpl) LockHigh( 72 ctx context.Context, 73 ) error { 74 c.locker.Lock() 75 defer c.locker.Unlock() 76 77 c.highWait += 1 78 defer func() { c.highWait -= 1 }() 79 80 for c.lockState != PriorityMutexStateUnlocked && ctx.Err() == nil { 81 c.highCV.Wait(ctx.Done()) 82 } 83 84 if ctx.Err() != nil { 85 return ctx.Err() 86 } 87 88 c.lockState = PriorityMutexStateLockedByHigh 89 return nil 90 } 91 92 // LockLow try to lock with low priority, use LockLow / UnlockLow pair to lock / unlock 93 func (c *PriorityMutexImpl) LockLow( 94 ctx context.Context, 95 ) error { 96 c.locker.Lock() 97 defer c.locker.Unlock() 98 99 c.lowWait += 1 100 defer func() { c.lowWait -= 1 }() 101 102 for c.lockState != PriorityMutexStateUnlocked && ctx.Err() == nil { 103 c.lowCV.Wait(ctx.Done()) 104 } 105 106 if ctx.Err() != nil { 107 return ctx.Err() 108 } 109 110 c.lockState = PriorityMutexStateLockedByLow 111 return nil 112 } 113 114 func (c *PriorityMutexImpl) TryLockHigh() bool { 115 c.locker.Lock() 116 defer c.locker.Unlock() 117 118 if c.lockState != PriorityMutexStateUnlocked { 119 return false 120 } 121 122 c.lockState = PriorityMutexStateLockedByHigh 123 return true 124 } 125 126 func (c *PriorityMutexImpl) TryLockLow() bool { 127 c.locker.Lock() 128 defer c.locker.Unlock() 129 130 if c.lockState != PriorityMutexStateUnlocked { 131 return false 132 } 133 134 c.lockState = PriorityMutexStateLockedByLow 135 return true 136 } 137 138 // UnlockHigh unlock with high priority, use LockHigh / UnlockHigh pair to lock / unlock 139 func (c *PriorityMutexImpl) UnlockHigh() { 140 c.locker.Lock() 141 defer c.locker.Unlock() 142 143 if c.lockState != PriorityMutexStateLockedByHigh { 144 panic(fmt.Sprintf("unable to unlock high priority, state: %v\n", c.lockState)) 145 } 146 147 c.lockState = PriorityMutexStateUnlocked 148 c.notify() 149 } 150 151 // UnlockLow unlock with low priority, use LockLow / UnlockLow pair to lock / unlock 152 func (c *PriorityMutexImpl) UnlockLow() { 153 c.locker.Lock() 154 defer c.locker.Unlock() 155 156 if c.lockState != PriorityMutexStateLockedByLow { 157 panic(fmt.Sprintf("unable to unlock high priority, state: %v\n", c.lockState)) 158 } 159 160 c.lockState = PriorityMutexStateUnlocked 161 c.notify() 162 } 163 164 func (c *PriorityMutexImpl) IsLocked() bool { 165 c.locker.Lock() 166 defer c.locker.Unlock() 167 168 return c.lockState != PriorityMutexStateUnlocked 169 } 170 171 func (c *PriorityMutexImpl) notify() { 172 if c.highWait > 0 { 173 c.highCV.Signal() 174 } else if c.lowWait > 0 { 175 c.lowCV.Signal() 176 } 177 }