Communication between tasks

Communication between tasks is facilitated by ports (in the receiving task), and channels (in the sending task). Any number of channels may feed into a single port. Ports and channels may only transmit values of unique types; that is, values that are statically guaranteed to be accessed by a single 'owner' at a time. Unique types include scalars, vectors, strings, and records, tags, tuples and unique boxes (~T) thereof. Most notably, shared boxes (@T) may not be transmitted across channels.

Example

let po = comm::port();
let ch = comm::chan(po);

do task::spawn {
    comm::send(ch, "Hello, World");
}

io::println(comm::recv(p));

Enum chan

A communication endpoint that can send messages

Each channel is bound to a port when the channel is constructed, so the destination port for a channel must exist before the channel itself. Channels are weak: a channel does not keep the port it is bound to alive. If a channel attempts to send data to a dead port that data will be silently dropped. Channels may be duplicated and themselves transmitted over other channels.

Variants

Enum port

A communication endpoint that can receive messages

Each port has a unique per-task identity and may not be replicated or transmitted. If a port value is copied, both copies refer to the same port. Ports may be associated with multiple chans.

Variants

Implementation methods for port<T>

Method chan

fn chan() -> chan<T>

Method send

fn send(+v: T)

Method recv

fn recv() -> T

Method peek

fn peek() -> bool

Implementation methods for chan<T>

Method chan

fn chan() -> chan<T>

Method send

fn send(+v: T)

Method recv

fn recv() -> T

Method peek

fn peek() -> bool

Function chan

fn chan<T: send>(p: port<T>) -> chan<T>

Constructs a channel. The channel is bound to the port used to construct it.

Function listen

fn listen<T: send, U>(f: fn(chan<T>) -> U) -> U

Open a new receiving channel for the duration of a function

Function peek

fn peek<T: send>(p: port<T>) -> bool

Returns true if there are messages available

Function port

fn port<T: send>() -> port<T>

Constructs a port

Function recv

fn recv<T: send>(p: port<T>) -> T

Receive from a port. If no data is available on the port then the task will block until data becomes available.

Function select2

fn select2<A: send, B: send>(p_a: port<A>, p_b: port<B>) -> either<A, B>

Receive on one of two ports

Function send

fn send<T: send>(ch: chan<T>, -data: T)

Sends data over a channel. The sent data is moved into the channel, whereupon the caller loses access to it.