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.
let po = comm::port();
let ch = comm::chan(po);
do task::spawn {
comm::send(ch, "Hello, World");
}
io::println(comm::recv(p));
chan
- A communication endpoint that can send messagesport
- A communication endpoint that can receive messagesmethods for port<T>
methods for chan<T>
chan
- Constructs a channellisten
- Open a new receiving channel for the duration of a functionpeek
- Returns true if there are messages availableport
- Constructs a portrecv
- Receive from a portselect2
- Receive on one of two portssend
- Sends data over a channelchan
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.
chan_t(port_id)
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 chan
s.
port_t(@port_ptr<T>)
methods for port<T>
chan
fn chan() -> chan<T>
send
fn send(+v: T)
recv
fn recv() -> T
peek
fn peek() -> bool
methods for chan<T>
chan
fn chan() -> chan<T>
send
fn send(+v: T)
recv
fn recv() -> T
peek
fn peek() -> bool
chan
fn chan<T: send>(p: port<T>) -> chan<T>
Constructs a channel. The channel is bound to the port used to construct it.
listen
fn listen<T: send, U>(f: fn(chan<T>) -> U) -> U
Open a new receiving channel for the duration of a function
peek
fn peek<T: send>(p: port<T>) -> bool
Returns true if there are messages available
port
fn port<T: send>() -> port<T>
Constructs a port
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.
select2
fn select2<A: send, B: send>(p_a: port<A>, p_b: port<B>) -> either<A, B>
Receive on one of two ports
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.