Hyperlinkv0.8.0-beta.28

TxReentrantLock

TxReentrantLock.releaseReadconsteffect/TxReentrantLock.ts:260
(self: TxReentrantLock): Effect.Effect<number>

Releases one read lock held by the current fiber.

When to use

Use to leave a manually acquired read lock.

Details

Returns the remaining number of read locks held by this fiber.

Example (Releasing a read lock)

import { Effect, TxReentrantLock } from "effect"

const program = Effect.gen(function*() {
  const lock = yield* TxReentrantLock.make()
  yield* TxReentrantLock.acquireRead(lock)
  const remaining = yield* TxReentrantLock.releaseRead(lock)
  console.log(remaining) // 0
})
mutations
export const releaseRead = (self: TxReentrantLock): Effect.Effect<number> =>
  Effect.withFiber((fiber) =>
    Effect.gen(function*() {
      const state = yield* TxRef.get(self.stateRef)
      const fiberId = fiber.id
      const currentCount = Option.getOrElse(HashMap.get(state.readers, fiberId), () => 0)

      if (currentCount <= 0) return 0

      const newCount = currentCount - 1
      const newReaders = newCount === 0
        ? HashMap.remove(state.readers, fiberId)
        : HashMap.set(state.readers, fiberId, newCount)

      yield* TxRef.set(self.stateRef, { ...state, readers: newReaders })
      return newCount
    }).pipe(Effect.tx)
  )
Referenced by 2 symbols