Semaphore

gears.async.Semaphore
See theSemaphore companion object
class Semaphore(initialValue: Int) extends Source[Guard]

A semaphore that manages a number of grants. One can wait to obtain a grant (with acquire) and return it to the semaphore (with release).

Value parameters

initialValue

the initial counter of this semaphore

Attributes

Companion
object
Graph
Supertypes
trait Source[Guard]
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

inline def acquire()(using Async): Guard

Decrease the number of grants available from this semaphore, possibly waiting if none is available.

Decrease the number of grants available from this semaphore, possibly waiting if none is available.

Value parameters

a

the async capability used for waiting

Attributes

override def dropListener(k: Listener[Guard]): Unit

Signal that listener k is dead (i.e. will always fail to acquire locks from now on), and should be removed from onComplete queues.

Signal that listener k is dead (i.e. will always fail to acquire locks from now on), and should be removed from onComplete queues.

This permits original, (i.e. non-derived) sources like futures or channels to drop the listener from their waiting sets.

Attributes

Definition Classes
override def onComplete(k: Listener[Guard]): Unit

Once data is available, pass it to the listener k. onComplete is always non-blocking.

Once data is available, pass it to the listener k. onComplete is always non-blocking.

Note that k's methods will be executed on the same thread as the Source, usually in sequence. It is hence important that the listener itself does not perform expensive operations.

Attributes

Definition Classes
override def poll(k: Listener[Guard]): Boolean

Checks whether data is available at present and pass it to k if so. Calls to poll are always synchronous and non-blocking.

Checks whether data is available at present and pass it to k if so. Calls to poll are always synchronous and non-blocking.

The process is as follows:

  • If no data is immediately available, return false immediately.
  • If there is data available, attempt to lock k.
    • If k is no longer available, true is returned to signal this source's general availability.
    • If locking k succeeds:
      • If data is still available, complete k and return true.
      • Otherwise, unlock k and return false.

Note that in all cases, a return value of false indicates that k should be put into onComplete to receive data in a later point in time.

Attributes

Returns

Whether poll was able to pass data to k. Note that this is regardless of k being available to receive the data. In most cases, one should pass k into Source!.onComplete if poll returns false.

Definition Classes
override def poll(): Option[Guard]

Similar to poll, but instead of passing in a listener, directly return the value T if it is available.

Similar to poll, but instead of passing in a listener, directly return the value T if it is available.

Attributes

Definition Classes

Inherited methods

final def awaitResult(using ac: Async): Guard

Waits for an item to arrive from the source. Suspends until an item returns.

Waits for an item to arrive from the source. Suspends until an item returns.

This is an utility method for direct waiting with Async, instead of going through listeners.

Attributes

Inherited from:
Source