final class TSemaphore extends Serializable
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.
- Alphabetic
- By Inheritance
- TSemaphore
- Serializable
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
acquire: USTM[Unit]
Acquires a single permit in transactional context.
-
def
acquireBetween(min: Long, max: Long): USTM[Long]
Acquire at least
minpermits and at mostmaxpermits in a transactional context. -
def
acquireN(n: Long): USTM[Unit]
Acquires the specified number of permits in a transactional context.
-
def
acquireUpTo(max: Long): USTM[Long]
Acquire at most
maxpermits in a transactional context. -
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
available: USTM[Long]
Returns the number of available permits in a transactional context.
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native() @HotSpotIntrinsicCandidate()
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- val permits: TRef[Long]
-
def
release: USTM[Unit]
Releases a single permit in a transactional context.
-
def
releaseN(n: Long): USTM[Unit]
Releases the specified number of permits in a transactional context
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
def
tryAcquire: USTM[Boolean]
Tries to acquire a single permit in a transactional context.
Tries to acquire a single permit in a transactional context. Returns
trueif the permit was acquired, otherwisefalse. -
def
tryAcquireN(n: Long): USTM[Boolean]
Tries to acquire the specified number of permits in a transactional context.
Tries to acquire the specified number of permits in a transactional context. Returns
trueif the permits were acquired, otherwisefalse. -
def
tryWithPermit[R, E, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, Option[A]]
Executes the specified effect, acquiring
1permit if available and releasing them after execution.Executes the specified effect, acquiring
1permit if available and releasing them after execution. ReturnsNoneif no permits were available. -
def
tryWithPermits[R, E, A](n: Long)(zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, Option[A]]
Executes the specified effect, acquiring
npermits if available and releasing them after execution.Executes the specified effect, acquiring
npermits if available and releasing them after execution. ReturnsNoneif no permits were available. -
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
def
withPermit[R, E, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A]
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.
-
def
withPermitScoped(implicit trace: Trace): ZIO[Scope, Nothing, Unit]
Returns a scoped effect that describes acquiring a permit as the
acquireaction and releasing it as thereleaseaction. -
def
withPermits[R, E, A](n: Long)(zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A]
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.
-
def
withPermitsBetween[R, E, A](min: Long, max: Long)(zio: (Long) ⇒ ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A]
Executes the specified effect, acquiring at least
minand at mostmaxpermits immediately before the effect begins execution and releasing them immediately after the effect completes execution, whether by success, failure, or interruption. -
def
withPermitsBetweenScoped(min: Long, max: Long)(implicit trace: Trace): ZIO[Scope, Nothing, Long]
Returns a scoped effect that describes acquiring at least
minand at mostmaxpermits and releasing them when the scope is closed. -
def
withPermitsScoped(n: Long)(implicit trace: Trace): ZIO[Scope, Nothing, Unit]
Returns a scoped effect that describes acquiring the specified number of permits and releasing them when the scope is closed.
-
def
withPermitsUpTo[R, E, A](max: Long)(zio: (Long) ⇒ ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A]
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.
-
def
withPermitsUpToScoped(max: Long)(implicit trace: Trace): ZIO[Scope, Nothing, Long]
Returns a scoped effect that describes acquiring at most
maxpermits and releasing them when the scope is closed.