Creates a semaphore.
The number of permits, i.e. things being allowed to run in parallel. To create a lock that only lets one thing run at a time, set this to 1. This number can also be negative.
Alias for Semaphore.wait
.
A promise that gets resolved when execution is allowed to proceed.
Acquires all permits that are currently available and returns the number of acquired permits.
Number of acquired permits.
Schedules func to be called once a permit becomes available. Returns a promise that resolves to the return value of func.
The return type of func.
The function to be executed.
A promise that gets resolved with the return value of the function.
Returns the number of available permits.
The number of available permits.
Alias for Semaphore.signal
.
Increases the number of permits by one. If there are other functions waiting, one of them will continue to execute in a future iteration of the event loop.
Synchronous function that tries to acquire a permit and returns true if successful, false otherwise.
Whether a permit could be acquired.
Returns a promise used to wait for a permit to become available. This method should be awaited on.
A promise that gets resolved when execution is allowed to proceed.
Same as Semaphore.wait
except the promise returned gets resolved with false if no permit becomes available in time.
The time spent waiting before the wait is aborted. This is a lower bound, don't rely on it being precise.
A promise that gets resolved with true when execution is allowed to proceed or false if the time given elapses before a permit becomes available.
Generated using TypeDoc
Class representing a semaphore Semaphores are initialized with a number of permits that get aquired and released over the lifecycle of the Semaphore. These permits limit the number of simultaneous executions of the code that the Semaphore synchronizes. Functions can wait and stop executing until a permit becomes available.
Locks that only allow one execution of a critical section are a special case of Semaphores. To construct a lock, initialize a Semaphore with a permit count of 1.
This Semaphore class is implemented with the help of promises that get returned by functions that wait for permits to become available. This makes it possible to use async/await to synchronize your code.