Semaphore

abstract class Semaphore[F[_]]

A purely functional semaphore.

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 semantic blocking until a permit becomes available.

Blocking acquires are cancelable if the semaphore is created with Semaphore.apply (and hence, with a Concurrent[F] instance). Blocking acquires are non-cancelable if the semaphore is created with Semaphore.async (and hence, with an Async[F] instance).

Companion
object
class Object
trait Matchable
class Any

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).

Value Params
n

number of permits to acquire - must be >= 0

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.

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.

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.

Value Params
n

number of permits to release - must be >= 0

def tryAcquireN(n: Long): F[Boolean]

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.

Value Params
n

number of permits to acquire - must be >= 0

def withPermit[A](t: F[A]): F[A]

Returns an effect that acquires a permit, runs the supplied effect, and then releases the permit.

Returns an effect that acquires a permit, runs the supplied effect, and then releases the permit.

Concrete methods

def acquire: F[Unit]

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

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

def imapK[G[_]](f: FunctionK[F, G], g: FunctionK[G, F]): Semaphore[G]

Modify the context F using natural isomorphism f with g.

Modify the context F using natural isomorphism f with g.

def release: F[Unit]

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

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

def tryAcquire: F[Boolean]

Alias for [[tryAcquireN]](1).

Alias for [[tryAcquireN]](1).