UNPKG

composable-locks

Version:

Composable concurrency locks for Javascript.

47 lines (46 loc) 1.39 kB
import type { ILock, Releaser } from "./interfaces"; declare type Queued = { id: symbol; reentrants: number; releaser: Promise<Releaser>; }; /** * A re-entrant Mutex. * * Usage: * ``` * const lock = new ReentrantMutex() * const domain = Symbol() * * const release1 = await lock.acquire(domain) * const release2 = await lock.acquire(domain) * release1() * release2() * ``` */ export declare class ReentrantMutex<A extends unknown[]> implements ILock<[symbol, ...A]> { private readonly greedy; protected latest: Queued | null; protected lockMap: Record<symbol, Queued>; protected lock: ILock<A>; constructor(newLock: () => ILock<A>, greedy?: boolean); /** * Acquire the lock * @param id The domain identifier. * @returns A function to release the lock. A domain *must* call all releasers before exiting. */ acquire(id: symbol, ...args: A): Promise<() => void>; /** * Get a queued domain object * @param id The domain to get the queued information for * @param args Passed to the underlying mutex * @returns The queued information, and a boolean that is true if the domain existed. */ private getQueued; private getQueuedGreedy; private getQueuedUngreedy; private createQueued; private cleanup; private release; } export {};