Semaphore

cats.effect.std.Semaphore
See theSemaphore companion object
abstract class Semaphore[F[_]]

A purely functional semaphore.

A semaphore has a non-negative number of permits available. Acquiring a permit decrements the current number of permits and releasing a permit increases the current number of permits. An acquire that occurs when there are no permits available results in fiber blocking until a permit becomes available.

Attributes

Companion:
object
Source:
Semaphore.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Concise view

Value members

Abstract methods

def acquireN(n: Long): F[Unit]

Acquires n permits.

Acquires n permits.

The returned effect semantically blocks until all requested permits are available. Note that acquires are statisfied in strict FIFO order, so given s: Semaphore[F] with 2 permits available, an acquireN(3) will always be satisfied before a later call to acquireN(1).

This method is interruptible, and in case of interruption it will take care of restoring any permits it has acquired. If it does succeed however, managing permits correctly is the user's responsibility. Use [[permit]] for a safer but less flexible alternative, when you are using Semaphore merely as a lock.

Attributes

n

number of permits to acquire - must be >= 0

Source:
Semaphore.scala
def available: F[Long]

Returns the number of permits currently available. Always non-negative.

Returns the number of permits currently available. Always non-negative.

May be out of date the instant after it is retrieved. Use [[tryAcquire]] or [[tryAcquireN]] if you wish to attempt an acquire, returning immediately if the current count is not high enough to satisfy the request.

Attributes

Source:
Semaphore.scala
def count: F[Long]

Obtains a snapshot of the current count. May be negative.

Obtains a snapshot of the current count. May be negative.

Like available when permits are available but returns the number of permits callers are waiting for when there are no permits available.

Attributes

Source:
Semaphore.scala
def mapK[G[_]](f: FunctionK[F, G])(implicit G: MonadCancel[G, _]): Semaphore[G]

Modify the context F using natural transformation f.

Modify the context F using natural transformation f.

Attributes

Source:
Semaphore.scala

Returns a cats.effect.kernel.Resource that acquires a permit, holds it for the lifetime of the resource, then releases the permit.

Returns a cats.effect.kernel.Resource that acquires a permit, holds it for the lifetime of the resource, then releases the permit.

Attributes

Source:
Semaphore.scala
def releaseN(n: Long): F[Unit]

Releases n permits, potentially unblocking up to n outstanding acquires.

Releases n permits, potentially unblocking up to n outstanding acquires.

Attributes

n

number of permits to release - must be >= 0

Source:
Semaphore.scala

Acquires n permits now and returns true, or returns false immediately. Error if n < 0.

Acquires n permits now and returns true, or returns false immediately. Error if n < 0.

Attributes

n

number of permits to acquire - must be >= 0

Source:
Semaphore.scala

Concrete methods

def acquire: F[Unit]

Acquires a single permit. Alias for [[acquireN]](1).

Acquires a single permit. Alias for [[acquireN]](1).

Attributes

Source:
Semaphore.scala
def release: F[Unit]

Releases a single permit. Alias for [[releaseN]](1).

Releases a single permit. Alias for [[releaseN]](1).

Attributes

Source:
Semaphore.scala

Alias for [[tryAcquireN]](1).

Alias for [[tryAcquireN]](1).

Attributes

Source:
Semaphore.scala
def tryPermit(implicit F: Applicative[F]): Resource[F, Boolean]

Returns a cats.effect.kernel.Resource that contains a boolean that indicates whether we acquired a permit or not. If the permit was acquired then it is guaranteed to be released at the end of the Resource lifetime

Returns a cats.effect.kernel.Resource that contains a boolean that indicates whether we acquired a permit or not. If the permit was acquired then it is guaranteed to be released at the end of the Resource lifetime

Attributes

Source:
Semaphore.scala