(self: TReentrantLock.TReentrantLock, delta: number)
| 154 | } |
| 155 | |
| 156 | const adjustRead = (self: TReentrantLock.TReentrantLock, delta: number): STM.STM<number> => |
| 157 | core.withSTMRuntime((runtime) => { |
| 158 | const lock = tRef.unsafeGet(self.state, runtime.journal) |
| 159 | if (isReadLock(lock)) { |
| 160 | const result = adjustReadLock(lock, runtime.fiberId, delta) |
| 161 | tRef.unsafeSet(self.state, result, runtime.journal) |
| 162 | return core.succeed(result.readLocksHeld(runtime.fiberId)) |
| 163 | } |
| 164 | if (isWriteLock(lock) && Equal.equals(runtime.fiberId)(lock.fiberId)) { |
| 165 | const newTotal = lock.readLocks + delta |
| 166 | if (newTotal < 0) { |
| 167 | throw new Error( |
| 168 | `Defect: Fiber ${ |
| 169 | FiberId.threadName(runtime.fiberId) |
| 170 | } releasing read locks it does not hold, newTotal: ${newTotal}` |
| 171 | ) |
| 172 | } |
| 173 | tRef.unsafeSet( |
| 174 | self.state, |
| 175 | new WriteLock(newTotal, lock.writeLocks, runtime.fiberId), |
| 176 | runtime.journal |
| 177 | ) |
| 178 | return core.succeed(newTotal) |
| 179 | } |
| 180 | return core.retry |
| 181 | }) |
| 182 | |
| 183 | /** @internal */ |
| 184 | export const acquireRead = (self: TReentrantLock.TReentrantLock): STM.STM<number> => adjustRead(self, 1) |
no test coverage detected