import { SafeInteger } from "../numbers"; /** * Limits the number of concurrent asynchronous processes that can access a resource. * * @remarks * A Semaphore is a synchronization object that maintains a count between zero and an optional, specified maximum value. * The count is decremented each time a caller enters the Semaphore, and incremented each time a caller releases the Semaphore. * * To enter the Semaphore, call {@link wait} or {@link waitAsync}. To release the Semaphore, call {@link release}. * When {@link count} reaches zero, subsequent calls to {@link waitAsync} will block asynchronously until the Semaphore is released elsewhere. * If multiple callers are blocked asynchronously, there is no guaranteed order, such as FIFO or LIFO, that controls who enter the Semaphore. */ export declare class Semaphore { private _count; private readonly _maxCount?; private readonly _waiters; constructor(initialCount: SafeInteger, maxCount?: SafeInteger); get count(): SafeInteger; get maxCount(): SafeInteger | undefined; /** * Tries to enter the Semaphore immediately. * * @returns `true` if the caller has successfully entered the Semaphore; `false` otherwise. * @remarks * * If the caller has successfully entered the Sempahore, {@link count} will be decreased by 1. * * This method is synchronous and does not block the caller, as JavaScript is single-threaded. * Blocking the caller causes deadlocks. * @see {release} */ wait(): boolean; /** * Blocks the caller asynchronously until it can enter the Semaphore, reached the specified timeout, or has been aborted. * * @param timeoutMs maximum time in milliseconds to wait. * @param signal a signal used to cancel the wait. * @returns `true` if the caller has entered Semaphore successfully; `false` if the specified timeout has been reched. * @throws {DOMException} the specified `signal` has been aborted. If the signal has been aborted with explicit reason, * the {@link AbortSignal.reason} will be thrown. * @remarks * If the caller has successfully entered the Sempahore, {@link count} will be decreased by 1. * @see {release} */ waitAsync(timeoutMs?: number, signal?: AbortSignal): Promise; /** * Blocks the caller asynchronously until it can enter the Semaphore or has reached the specified timeout. * * @param signal a signal used to cancel the wait. * @returns always `true`, as the returned {@link Promise} won't fulfill until it enters the Semaphore or has been aborted, * which will result in rejection. * @throws {DOMException} the specified `signal` has been aborted. If the signal has been aborted with explicit reason, * the {@link AbortSignal.reason} will be thrown. * @remarks * If the caller has successfully entered the Sempahore, {@link count} will be decreased by 1. * @see {release} */ waitAsync(signal?: AbortSignal): Promise; /** * Blocks the caller asynchronously until it can enter the Semaphore. * * @returns always `true`, as the returned {@link Promise} won't fulfill until it enters the Semaphore. * @remarks * If the caller has successfully entered the Sempahore, {@link count} will be decreased by 1. * @see {release} */ waitAsync(): Promise; /** * Releases the Semaphore a specified number of times. * * @param releaseCount The number of times to exit the Semaphore. `undefined` will exit the Semaphore once (1). * @throws {InvalidOperationError} Adding the specified count to the Semaphore would cause it to exceed {@link maxCount}. * @remarks * * A call to this function increments the {@link count} by `releaseCount`. If {@link count} is already `0` before this function * is called, the function also allows `releaseCount` {@link Promise}s blocked by {@link waitAsync} to enter the Semaphore. * * If there are blocked Promises waiting to enter the Semaphore, this function _does not_ guarantee the order in which * the blocked Promises enters the Semaphore (e.g. FIFO). That is, the order does not have to be deterministic and is subject * to changes in a later version of the implementation. * * When deciding whether the specified `releaseCount` will cause Semaphore to exceed {@link maxCount}, Promises waiting to * enter the Semaphore are not counted in. This behavior is currently just to align with the Win32 * [`ReleaseSemaphore`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-releasesemaphore) * API. */ release(releaseCount?: SafeInteger): SafeInteger; toString(): string; readonly [Symbol.toStringTag] = "Semaphore"; } //# sourceMappingURL=semaphore.d.ts.map