Skip to content

RwLock

Defined in: packages/async/src/synchro/rw-lock.ts:24

An asynchronous read-write lock (RwLock) for coordinating access to shared resources.

Allows multiple concurrent readers or exclusive access for a single writer.

All lock/unlock operations are asynchronous and must be awaited.

WARNING: Deadlock is possible if: - A task attempts to acquire a lock it already holds (no reentrancy). - Locks are acquired in inconsistent order across tasks. - A task forgets to unlock after acquiring.

Best practices: - Always use try/finally to ensure unlock. - Avoid holding locks across await points that may block indefinitely. - Never call lock methods from within unlock callbacks.

Example usage: await rwLock.readLock(); try { // read shared resource } finally { rwLock.readUnlock(); }

new RwLock(): RwLock

RwLock

readLock(signal?): Promise<void>

Defined in: packages/async/src/synchro/rw-lock.ts:38

Acquire a read lock. Multiple readers may hold the lock concurrently unless a writer is waiting or active.

AbortSignal | null

Optional AbortSignal to cancel the wait.

Promise<void>

Error if the lock cannot be acquired due to cancellation.


readToWriteLock(signal?): Promise<void>

Defined in: packages/async/src/synchro/rw-lock.ts:130

Upgrade a held read lock to a write lock. The caller must already hold a read lock.

AbortSignal | null

Optional AbortSignal to cancel the wait.

Promise<void>

Error if the upgrade cannot be completed due to cancellation.


readUnlock(): void

Defined in: packages/async/src/synchro/rw-lock.ts:182

Release a previously acquired read lock.

WARNING: Failing to call readUnlock() after readLock() will cause deadlock.

void


tryReadLock(): boolean

Defined in: packages/async/src/synchro/rw-lock.ts:80

Attempt to acquire a read lock without waiting. Returns true if successful.

boolean

True if the read lock was acquired, false otherwise.


tryWriteLock(): boolean

Defined in: packages/async/src/synchro/rw-lock.ts:105

Attempt to acquire a write lock without waiting. Returns true if successful.

boolean

True if the write lock was acquired, false otherwise.


writeLock(signal?): Promise<void>

Defined in: packages/async/src/synchro/rw-lock.ts:59

Acquire a write lock. Only one writer may hold the lock, and no readers may be active.

AbortSignal | null

Optional AbortSignal to cancel the wait.

Promise<void>

Error if the lock cannot be acquired due to cancellation.


writeToReadLock(): void

Defined in: packages/async/src/synchro/rw-lock.ts:167

Downgrade a held write lock to a read lock. The caller must already hold the write lock.

void


writeUnlock(): void

Defined in: packages/async/src/synchro/rw-lock.ts:199

Release a previously acquired write lock.

WARNING: Failing to call writeUnlock() after writeLock() will cause deadlock.

void