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(); }
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new RwLock():
RwLock
Returns
Section titled “Returns”RwLock
Methods
Section titled “Methods”readLock()
Section titled “readLock()”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.
Parameters
Section titled “Parameters”signal?
Section titled “signal?”AbortSignal | null
Optional AbortSignal to cancel the wait.
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”Error if the lock cannot be acquired due to cancellation.
readToWriteLock()
Section titled “readToWriteLock()”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.
Parameters
Section titled “Parameters”signal?
Section titled “signal?”AbortSignal | null
Optional AbortSignal to cancel the wait.
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”Error if the upgrade cannot be completed due to cancellation.
readUnlock()
Section titled “readUnlock()”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.
Returns
Section titled “Returns”void
tryReadLock()
Section titled “tryReadLock()”tryReadLock():
boolean
Defined in: packages/async/src/synchro/rw-lock.ts:80
Attempt to acquire a read lock without waiting. Returns true if successful.
Returns
Section titled “Returns”boolean
True if the read lock was acquired, false otherwise.
tryWriteLock()
Section titled “tryWriteLock()”tryWriteLock():
boolean
Defined in: packages/async/src/synchro/rw-lock.ts:105
Attempt to acquire a write lock without waiting. Returns true if successful.
Returns
Section titled “Returns”boolean
True if the write lock was acquired, false otherwise.
writeLock()
Section titled “writeLock()”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.
Parameters
Section titled “Parameters”signal?
Section titled “signal?”AbortSignal | null
Optional AbortSignal to cancel the wait.
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”Error if the lock cannot be acquired due to cancellation.
writeToReadLock()
Section titled “writeToReadLock()”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.
Returns
Section titled “Returns”void
writeUnlock()
Section titled “writeUnlock()”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.
Returns
Section titled “Returns”void