Class Channel

A one-way communication channel between tasks.
Multiple messages (objects) can be sent, and the capacity indicates how many unreceived message it can buffer.
Inheritance
This class must not be extended.

Class summary


constructor capacity/any
Constructs a channel with a buffer of the given capacity.

Constructors

constructor capacity/any
Constructs a channel with a buffer of the given capacity.

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 capacity of the channel.

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

receive --blocking/bool=true -> any
Receives a message from the channel.
If no message is ready, and blocking is true (the default), blocks until another tasks sends a message through send.
If no message is ready, and blocking is false, returns null.
If multiple tasks are blocked waiting for a new value, then a send call only unblocks one waiting task.
The order in which waiting tasks are unblocked is unspecified.

send value/any -> none
Sends a message with the value on the channel.
This operation may block if the buffer capacity has been reached. In that case, this task waits until another task calls receive.
If there are tasks blocked waiting for a value (with receive), then one of them is woken up and receives the value.

send [block] -> none
Sends a message with the result of calling the given block.
This operation may block if the buffer capacity has been reached. In that case, this task waits until another task calls receive. The block is only called when the channel has the capacity to buffer a message.
If there are tasks blocked waiting for a value (with receive), then one of them is woken up and receives the sent value.

The amount of messages that are currently queued in the channel.

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.

try-send value/any -> bool
Tries to send a message with the value on the channel. This operation never blocks.
If there are tasks blocked waiting for a value (with receive), then one of them is woken up and receives the value.
Returns true if the message was successfully delivered to the channel. Returns false if the channel is full and the message was not delivered