Semaphore
Defined in: packages/async/src/synchro/semaphore.ts:19
A general/counting strong semaphore implementation.
A semaphore maintains a set of permits. Each acquire call blocks if
necessary until a permit is available, and then takes it. Each release call
adds a permit, potentially releasing a blocking acquirer.
The semaphore is initialized with a given number of permits. The number of permits can be increased up to a maximum value.
The order of permit acquisition is guaranteed to be FIFO.
Usage: Only meaningful when the critical section contains at least one
await — synchronous operations cannot be interleaved in JS’s
single-threaded model.
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Semaphore(
maxValue,initialValue?):Semaphore
Defined in: packages/async/src/synchro/semaphore.ts:32
Creates a new semaphore with the given initial number of permits and maximum number of permits.
Parameters
Section titled “Parameters”maxValue
Section titled “maxValue”number
The maximum number of permits.
initialValue?
Section titled “initialValue?”number
The initial number of permits available. Defaults to
maxValue.
Returns
Section titled “Returns”Semaphore
Accessors
Section titled “Accessors”Get Signature
Section titled “Get Signature”get value():
number
Defined in: packages/async/src/synchro/semaphore.ts:53
The current number of available permits.
Returns
Section titled “Returns”number
The current number of available permits.
Methods
Section titled “Methods”acquire()
Section titled “acquire()”acquire(
count?,signal?):Promise<void>
Defined in: packages/async/src/synchro/semaphore.ts:92
Acquires a permit from the semaphore, waiting if necessary until one is available.
Parameters
Section titled “Parameters”count?
Section titled “count?”number = 1
signal?
Section titled “signal?”AbortSignal | null
An optional AbortSignal to cancel the acquire operation.
Returns
Section titled “Returns”Promise<void>
A promise that resolves to a lease when the permits are acquired.
getMaxValue()
Section titled “getMaxValue()”getMaxValue():
number
Defined in: packages/async/src/synchro/semaphore.ts:62
The maximum number of permits.
Returns
Section titled “Returns”number
The maximum number of permits.
release()
Section titled “release()”release(
count?):void
Defined in: packages/async/src/synchro/semaphore.ts:114
Releases permits back to the semaphore.
Any pending acquisitions will be processed asynchronously after this call.
Parameters
Section titled “Parameters”count?
Section titled “count?”number = 1
The number of permits to release.
Returns
Section titled “Returns”void
tryAcquire()
Section titled “tryAcquire()”tryAcquire(
count?):boolean
Defined in: packages/async/src/synchro/semaphore.ts:72
Tries to acquire a permit from the semaphore immediately, without waiting.
Parameters
Section titled “Parameters”count?
Section titled “count?”number = 1
The number of permits to acquire.
Returns
Section titled “Returns”boolean
true if the permits were acquired, false otherwise.