import { TAcquireParams, TExclusiveCallback, IReadWriteLock, IReleaser, TReadLockToken, TRWLockConstructorProps, TWriteLockToken } from "./types"; /** * ReadWriteLock (short for Read-Write Lock) is a synchronization mechanism that allows multiple threads to read from a resource simultaneously, * but prohibits writing if someone is reading, and vice versa: if writing is in progress, no one can read or write. * * The main principles of ReadWriteLock operation: * • Multiple threads can read simultaneously if there is no writing. * • Only one thread can write, and during writing, no reads are allowed. * • This improves performance with a large number of read operations and infrequent writes. * * This implementation handles locks in the order they are requested: * • Readers can acquire locks concurrently as long as no writer is active * • Writers must wait for all active readers to release their locks before acquiring the write lock * • New readers can acquire locks even if writers are waiting, as long as no writer is currently active * * Explanation in simple terms: * 1. There are multiple threads reading data — that's fine, they can do it simultaneously. * 2. At some point, a thread wants to write — it calls acquireWrite(): * • It cannot write while others are reading, so it queues and waits. * 3. Meanwhile, new read requests can still be granted while writers are waiting. * * Lock state is owned by this class rather than delegated to two independent * semaphores: deciding whether a lock is available and taking it must happen in * a single synchronous step, otherwise a reader and a writer that start * concurrently can both pass their checks and end up holding the lock together. */ export declare class ReadWriteLock implements IReadWriteLock { /** * Synchronous counterpart of maxReaders(), needed wherever the limit has to be * inspected outside of an async context. */ readonly maxReadersCount: number; private activeReaderCount; private writerActive; private readQueue; private writeQueue; constructor(props?: TRWLockConstructorProps); maxReaders(): Promise; activeReaders(): Promise; acquireRead(params?: TAcquireParams): Promise>; acquireWrite(params?: TAcquireParams): Promise>; /** * Same as acquireRead(), but resolves to null instead of throwing * TimeoutLockingError, and timeoutMs defaults to 0. */ tryAcquireRead(params?: TAcquireParams): Promise | null>; /** * Same as acquireWrite(), but resolves to null instead of throwing * TimeoutLockingError, and timeoutMs defaults to 0. */ tryAcquireWrite(params?: TAcquireParams): Promise | null>; /** * Cancels every pending acquisition. Locks that are already held stay held - * their owners are still inside their critical sections. */ cancelAll(errMessage?: string): Promise; withReadLock(fn: TExclusiveCallback): Promise; withReadLock(params: TAcquireParams, fn: TExclusiveCallback): Promise; withWriteLock(fn: TExclusiveCallback): Promise; withWriteLock(params: TAcquireParams, fn: TExclusiveCallback): Promise; isWriteLocked(): Promise; isReadLocked(): Promise; private canTakeRead; private canTakeWrite; private enqueue; private createReleaser; private releaseRead; private releaseWrite; private dispatch; } //# sourceMappingURL=read-write-lock.d.ts.map