A TSemaphore is a semaphore that can be composed transactionally. Because
of the extremely high performance of ZIO's implementation of software
transactional memory TSemaphore can support both controlling access to some
resource on a standalone basis as well as composing with other STM data
structures to solve more advanced concurrency problems.
For basic use cases, the most idiomatic way to work with a semaphore is to
use the withPermit operator, which acquires a permit before executing some
ZIO effect and release the permit immediately afterward. The permit is
guaranteed to be released immediately after the effect completes execution,
whether by success, failure, or interruption. Attempting to acquire a permit
when a sufficient number of permits are not available will semantically block
until permits become available without blocking any underlying operating
system threads. If you want to acquire more than one permit at a time you can
use withPermits, which allows specifying a number of permits to acquire.
You can also use withPermitScoped or withPermitsScoped to acquire and
release permits within the context of a scoped effect for composing with
other resources.
For more advanced concurrency problems you can use the acquire and
release operators directly, or their variants acquireN and releaseN,
all of which return STM transactions. Thus, they can be composed to form
larger STM transactions, for example acquiring permits from two different
semaphores transactionally and later releasing them transactionally to safely
synchronize on access to two different mutable variables.
- Companion:
- object
Value members
Concrete methods
Acquires a single permit in transactional context.
Acquires a single permit in transactional context.
Acquire at least min permits and at most max permits in a transactional
context.
Acquire at least min permits and at most max permits in a transactional
context.
Acquires the specified number of permits in a transactional context.
Acquires the specified number of permits in a transactional context.
Acquire at most max permits in a transactional context.
Acquire at most max permits in a transactional context.
Returns the number of available permits in a transactional context.
Returns the number of available permits in a transactional context.
Releases a single permit in a transactional context.
Releases a single permit in a transactional context.
Releases the specified number of permits in a transactional context
Releases the specified number of permits in a transactional context
Executes the specified effect, acquiring a permit immediately before the effect begins execution and releasing it immediately after the effect completes execution, whether by success, failure, or interruption.
Executes the specified effect, acquiring a permit immediately before the effect begins execution and releasing it immediately after the effect completes execution, whether by success, failure, or interruption.
Returns a scoped effect that describes acquiring a permit as the acquire
action and releasing it as the release action.
Returns a scoped effect that describes acquiring a permit as the acquire
action and releasing it as the release action.
Executes the specified effect, acquiring the specified number of permits immediately before the effect begins execution and releasing them immediately after the effect completes execution, whether by success, failure, or interruption.
Executes the specified effect, acquiring the specified number of permits immediately before the effect begins execution and releasing them immediately after the effect completes execution, whether by success, failure, or interruption.
Executes the specified effect, acquiring at least min and at most max
permits immediately before the effect begins execution and releasing them
immediately after the effect completes execution, whether by success,
failure, or interruption.
Executes the specified effect, acquiring at least min and at most max
permits immediately before the effect begins execution and releasing them
immediately after the effect completes execution, whether by success,
failure, or interruption.
Returns a scoped effect that describes acquiring at least min and at most
max permits and releasing them when the scope is closed.
Returns a scoped effect that describes acquiring at least min and at most
max permits and releasing them when the scope is closed.
Returns a scoped effect that describes acquiring the specified number of permits and releasing them when the scope is closed.
Returns a scoped effect that describes acquiring the specified number of permits and releasing them when the scope is closed.
Executes the specified effect, acquiring at most the specified number of permits immediately before the effect begins execution and releasing them immediately after the effect completes execution, whether by success, failure, or interruption.
Executes the specified effect, acquiring at most the specified number of permits immediately before the effect begins execution and releasing them immediately after the effect completes execution, whether by success, failure, or interruption.