Class Semaphore

A semaphore synchronization primitive.
Inheritance
This class must not be extended.

Class summary


constructor --count/int= --limit/int=
Constructs a semaphore with an initial count and an optional limit.

Constructors

constructor --count/int=0 --limit/int=null
Constructs a semaphore with an initial count and an optional limit.
When the limit is reached, further attempts to increment the counter using up are ignored and leaves the counter unchanged.

Methods

await [condition] -> none
Yields until the condition block returns true.
This should be called from a locked method.
The condition is not allowed to change the state, only query it.

The current count of the semaphore.

down -> none
Decrements an internal counter.
This method blocks until the counter is non-zero.
Originally called the P operation.

operator == other/any -> bool
Whether this object is equal to the other.
By default, identical is used for equality.
Inheritance
Classes overwrite this operator to get an equality specific to their needs. Equality operators often compare the type and field contents. For example:

class Pin:
  number/int

  constructor .number:

  operator == other:
    if other is not Pin: return false
    return number == other.number
A class doesn't have to follow the above format, but it must keep the operator in sync with any hash-code method. That is, if a class has a hash-code member, then the equality and hash-code must agree. If two instances are equal (a == b), then their hash codes must also be equal (a.hash-code == b.hash-code).

Stringifies this object.
Inheritance
Objects that need a human-friendly string representation should overwrite this method. The default string is based on the internal class-ID.

try-await --deadline/int [condition] -> any
Yields until the condition block returns true or until the given deadline is reached.
Returns true if the condition has been met or false if we have reached the deadline.
This should be called from a locked method.
The condition is not allowed to change the state, only query it.

up -> none
Increments an internal counter.
Originally called the V operation.