(self: TxReentrantLock)
| 145 | * @since 2.0.0 |
| 146 | */ |
| 147 | export const acquireRead = (self: TxReentrantLock): Effect.Effect<number> => |
| 148 | Effect.withFiber((fiber) => |
| 149 | Effect.gen(function*() { |
| 150 | const state = yield* TxRef.get(self.stateRef) |
| 151 | const fiberId = fiber.id |
| 152 | |
| 153 | // If another fiber holds the write lock, retry |
| 154 | if (Option.isSome(state.writer) && state.writer.value[0] !== fiberId) { |
| 155 | return yield* Effect.txRetry |
| 156 | } |
| 157 | |
| 158 | // Grant read lock |
| 159 | const currentCount = Option.getOrElse(HashMap.get(state.readers, fiberId), () => 0) |
| 160 | const newCount = currentCount + 1 |
| 161 | yield* TxRef.set(self.stateRef, { |
| 162 | ...state, |
| 163 | readers: HashMap.set(state.readers, fiberId, newCount) |
| 164 | }) |
| 165 | return newCount |
| 166 | }).pipe(Effect.tx) |
| 167 | ) |
| 168 | |
| 169 | /** |
| 170 | * Acquires the write lock for the current fiber. |
no test coverage detected