Package: effect
Module: Effect
A semaphore is a synchronization mechanism used to manage access to a shared resource. In Effect, semaphores help control resource access or coordinate tasks within asynchronous, concurrent operations.
A semaphore acts as a generalized mutex, allowing a set number of permits to be held and released concurrently. Permits act like tickets, giving tasks or fibers controlled access to a shared resource. When no permits are available, tasks trying to acquire one will wait until a permit is released.
Signature
export interface Semaphore {
/**
* Adjusts the number of permits available in the semaphore.
*/
resize(permits: number): Effect<void>
/**
* Runs an effect with the given number of permits and releases the permits
* when the effect completes.
*
* **Details**
*
* This function acquires the specified number of permits before executing
* the provided effect. Once the effect finishes, the permits are released.
* If insufficient permits are available, the function will wait until they
* are released by other tasks.
*/
withPermits(permits: number): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R>
/**
* Runs an effect only if the specified number of permits are immediately
* available.
*
* **Details**
*
* This function attempts to acquire the specified number of permits. If they
* are available, it runs the effect and releases the permits after the effect
* completes. If permits are not available, the effect does not execute, and
* the result is `Option.none`.
*/
withPermitsIfAvailable(permits: number): <A, E, R>(self: Effect<A, E, R>) => Effect<Option.Option<A>, E, R>
/**
* Acquires the specified number of permits and returns the resulting
* available permits, suspending the task if they are not yet available.
* Concurrent pending `take` calls are processed in a first-in, first-out manner.
*/
take(permits: number): Effect<number>
/**
* Releases the specified number of permits and returns the resulting
* available permits.
*/
release(permits: number): Effect<number>
/**
* Releases all permits held by this semaphore and returns the resulting available permits.
*/
releaseAll: Effect<number>
}
Since v2.0.0