2011-10-24 17:25:41 -05:00
|
|
|
/*
|
|
|
|
Module: comm
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2011-10-27 15:14:56 -05:00
|
|
|
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.
|
|
|
|
|
2011-10-24 17:25:41 -05:00
|
|
|
Example:
|
|
|
|
|
2011-11-10 17:18:01 -06:00
|
|
|
> use std::{task, comm, io};
|
2011-10-24 17:25:41 -05:00
|
|
|
>
|
|
|
|
> let p = comm::port();
|
|
|
|
> task::spawn(comm::chan(p), fn (c: chan<str>) {
|
|
|
|
> comm::send(c, "Hello, World");
|
|
|
|
> });
|
|
|
|
>
|
2011-11-10 17:18:01 -06:00
|
|
|
> io::println(comm::recv(p));
|
2011-10-24 17:25:41 -05:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2011-07-29 19:53:02 -05:00
|
|
|
import sys;
|
2011-08-09 18:07:49 -05:00
|
|
|
import task;
|
2011-07-29 19:53:02 -05:00
|
|
|
|
2011-08-09 18:07:49 -05:00
|
|
|
export send;
|
2011-08-16 19:12:46 -05:00
|
|
|
export recv;
|
|
|
|
export chan;
|
|
|
|
export port;
|
2011-07-29 19:53:02 -05:00
|
|
|
|
2011-11-09 20:44:12 -06:00
|
|
|
native "cdecl" mod rustrt {
|
2011-08-01 16:57:17 -05:00
|
|
|
type void;
|
2011-07-29 19:53:02 -05:00
|
|
|
type rust_port;
|
|
|
|
|
2011-10-28 08:09:12 -05:00
|
|
|
fn chan_id_send<uniq T>(t: *sys::type_desc,
|
|
|
|
target_task: task::task, target_port: port_id,
|
2011-11-14 15:49:56 -06:00
|
|
|
data: T) -> ctypes::uintptr_t;
|
2011-07-29 19:53:02 -05:00
|
|
|
|
2011-10-19 16:54:53 -05:00
|
|
|
fn new_port(unit_sz: uint) -> *rust_port;
|
|
|
|
fn del_port(po: *rust_port);
|
|
|
|
fn get_port_id(po: *rust_port) -> port_id;
|
2011-11-13 17:28:59 -06:00
|
|
|
fn rust_port_size(po: *rust_port) -> ctypes::size_t;
|
2011-08-02 14:29:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
native "rust-intrinsic" mod rusti {
|
2011-10-28 08:09:12 -05:00
|
|
|
fn recv<uniq T>(port: *rustrt::rust_port) -> T;
|
2011-07-29 19:53:02 -05:00
|
|
|
}
|
|
|
|
|
2011-08-09 18:07:49 -05:00
|
|
|
type port_id = int;
|
|
|
|
|
2011-08-25 13:20:43 -05:00
|
|
|
// It's critical that this only have one variant, so it has a record
|
|
|
|
// layout, and will work in the rust_task structure in task.rs.
|
2011-10-24 17:25:41 -05:00
|
|
|
/*
|
|
|
|
Type: chan
|
|
|
|
|
2011-10-27 13:15:28 -05:00
|
|
|
A communication endpoint that can send messages. Channels send
|
|
|
|
messages to ports.
|
2011-10-24 17:25:41 -05:00
|
|
|
|
2011-10-27 13:15:28 -05:00
|
|
|
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.
|
2011-10-24 17:25:41 -05:00
|
|
|
*/
|
2011-10-28 08:09:12 -05:00
|
|
|
tag chan<uniq T> {
|
2011-10-24 17:25:41 -05:00
|
|
|
chan_t(task::task, port_id);
|
|
|
|
}
|
2011-08-09 18:07:49 -05:00
|
|
|
|
2011-11-13 17:28:59 -06:00
|
|
|
resource port_ptr<uniq T>(po: *rustrt::rust_port) {
|
|
|
|
// Drain the port so that all the still-enqueued items get dropped
|
|
|
|
while rustrt::rust_port_size(po) > 0u {
|
|
|
|
// FIXME: For some reason if we don't assign to something here
|
|
|
|
// we end up with invalid reads in the drop glue.
|
2011-11-13 18:36:47 -06:00
|
|
|
let _t = rusti::recv::<T>(po);
|
2011-11-13 17:28:59 -06:00
|
|
|
}
|
2011-10-19 16:54:53 -05:00
|
|
|
rustrt::del_port(po);
|
2011-07-29 19:53:02 -05:00
|
|
|
}
|
|
|
|
|
2011-10-24 17:25:41 -05:00
|
|
|
/*
|
|
|
|
Type: port
|
|
|
|
|
2011-10-27 13:15:28 -05:00
|
|
|
A communication endpoint that can receive messages. Ports receive
|
|
|
|
messages from channels.
|
|
|
|
|
|
|
|
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.
|
2011-10-24 17:25:41 -05:00
|
|
|
|
|
|
|
Ports may be associated with multiple <chan>s.
|
|
|
|
*/
|
2011-11-13 17:28:59 -06:00
|
|
|
tag port<uniq T> { port_t(@port_ptr<T>); }
|
2011-08-16 19:12:46 -05:00
|
|
|
|
2011-10-24 17:25:41 -05:00
|
|
|
/*
|
|
|
|
Function: send
|
|
|
|
|
|
|
|
Sends data over a channel.
|
|
|
|
|
|
|
|
The sent data is moved into the channel, whereupon the caller loses access
|
|
|
|
to it.
|
|
|
|
*/
|
2011-10-28 08:09:12 -05:00
|
|
|
fn send<uniq T>(ch: chan<T>, -data: T) {
|
2011-08-25 13:20:43 -05:00
|
|
|
let chan_t(t, p) = ch;
|
2011-11-14 15:49:56 -06:00
|
|
|
let res = rustrt::chan_id_send(sys::get_type_desc::<T>(), t, p, data);
|
|
|
|
if res != 0u unsafe {
|
|
|
|
// Data sent successfully
|
|
|
|
unsafe::leak(data);
|
|
|
|
}
|
2011-10-19 16:47:50 -05:00
|
|
|
task::yield();
|
2011-08-10 16:38:49 -05:00
|
|
|
}
|
2011-08-16 19:12:46 -05:00
|
|
|
|
2011-10-24 17:25:41 -05:00
|
|
|
/*
|
|
|
|
Function: port
|
|
|
|
|
|
|
|
Constructs a port.
|
|
|
|
*/
|
2011-10-28 08:09:12 -05:00
|
|
|
fn port<uniq T>() -> port<T> {
|
2011-10-24 17:25:41 -05:00
|
|
|
port_t(@port_ptr(rustrt::new_port(sys::size_of::<T>())))
|
2011-08-16 19:12:46 -05:00
|
|
|
}
|
|
|
|
|
2011-10-24 17:25:41 -05:00
|
|
|
/*
|
|
|
|
Function: recv
|
|
|
|
|
|
|
|
Receive from a port.
|
2011-10-27 13:15:28 -05:00
|
|
|
|
|
|
|
If no data is available on the port then the task will block until data
|
|
|
|
becomes available.
|
2011-10-24 17:25:41 -05:00
|
|
|
*/
|
2011-10-28 08:09:12 -05:00
|
|
|
fn recv<uniq T>(p: port<T>) -> T { ret rusti::recv(***p) }
|
2011-10-24 17:25:41 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
Function: chan
|
2011-08-16 19:12:46 -05:00
|
|
|
|
2011-10-24 17:25:41 -05:00
|
|
|
Constructs a channel.
|
2011-10-27 13:15:28 -05:00
|
|
|
|
|
|
|
The channel is bound to the port used to construct it.
|
2011-10-24 17:25:41 -05:00
|
|
|
*/
|
2011-10-28 08:09:12 -05:00
|
|
|
fn chan<uniq T>(p: port<T>) -> chan<T> {
|
2011-10-27 13:17:39 -05:00
|
|
|
chan_t(task::get_task(), rustrt::get_port_id(***p))
|
2011-08-16 19:12:46 -05:00
|
|
|
}
|