import { Synchronizer } from "../synchronizer.interface"; /** * A mutex to manage "concurrency in Javascript" (as mentioned in the mozilla documentation). * * Implementation's note: the mutex simply use an internal semaphore. * * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#promise_concurrency */ export declare class Mutex implements Synchronizer { /** * The internal semaphore for mutex implementation */ private readonly semaphore; /** * @returns if the current mutex is currently locked */ get isLocked(): boolean; /** * @inheritDoc */ get queueLength(): number; /** * Locks this mutex * * [lockWith]{@link lockWith} is the preferred choice. * * @throws {ConcurrencyInterruptedException} when the mutex is interrupted * @returns a promise when the lock has been set */ lock(): Promise; /** * Locks this mutex within a time limit * * Throws an error if the given time exceeds * * [tryLockWith]{@link tryLockWith} is the preferred choice. * * @param timeout maximum time (in ms) to lock * @throws {ConcurrencyExceedTimeoutException} when the time limit exceeds * @throws {ConcurrencyInterruptedException} when the mutex is interrupted * @returns a promise when the lock has been set */ tryLock(timeout: number): Promise; /** * Unlocks this mutex * * [lockWith]{@link lockWith} or [tryLockWith]{@link tryLockWith} are the preferred choices. */ unlock(): void; /** * Locks this mutex and run the critical section function * Then, automatically unlocks it. * * @param cs function to run once the locked is acquired (critical section) * @throws {ConcurrencyInterruptedException} when the mutex is interrupted * @returns a Promise after the mutex locked and unlocked and what has been return from critical section */ lockWith(cs: () => Promise | T): Promise; /** * Locks this mutex within a time limit and run the critical section function * Then, automatically unlocks it. * * @param timeout maximum time (in ms) to lock * @param cs function to run once the locked is acquired (critical section) * @throws {ConcurrencyExceedTimeoutException} when the time limit exceeds * @throws {ConcurrencyInterruptedException} when the mutex is interrupted * @returns a Promise after the mutex locked and unlocked and what has been return from `fn` */ tryLockWith(timeout: number, cs: () => Promise | T): Promise; /** * Interrupts all awaiting "Threads" with an [exception]{@link ConcurrencyInterruptedException}. * * @param reason The reason why this mutex is being interrupted */ interrupt(reason: unknown): void; }