MCPcopy Create free account
hub / github.com/Effect-TS/effect / acquireRead

Function acquireRead

packages/effect/src/TxReentrantLock.ts:147–167  ·  view source on GitHub ↗
(self: TxReentrantLock)

Source from the content-addressed store, hash-verified

145 * @since 2.0.0
146 */
147export 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.

Callers 2

readLockFunction · 0.85
TxReentrantLock.tsFile · 0.85

Calls 3

pipeMethod · 0.65
getMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected