2011-07-29 19:53:02 -05:00
|
|
|
import sys;
|
2011-08-01 16:57:17 -05:00
|
|
|
import ptr;
|
|
|
|
import unsafe;
|
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
|
|
|
|
|
|
|
native "rust" mod rustrt {
|
2011-08-01 16:57:17 -05:00
|
|
|
type void;
|
2011-07-29 19:53:02 -05:00
|
|
|
type rust_port;
|
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
fn chan_id_send<~T>(target_task: task::task, target_port: port_id,
|
2011-09-12 05:39:38 -05:00
|
|
|
-data: T);
|
2011-07-29 19:53:02 -05:00
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
fn new_port(unit_sz: uint) -> *rust_port;
|
|
|
|
fn del_port(po: *rust_port);
|
|
|
|
fn drop_port(po: *rust_port);
|
|
|
|
fn get_port_id(po: *rust_port) -> port_id;
|
2011-08-02 14:29:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
native "rust-intrinsic" mod rusti {
|
2011-08-19 17:16:48 -05:00
|
|
|
fn recv<~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.
|
|
|
|
tag chan<~T> { chan_t(task::task, port_id); }
|
2011-08-09 18:07:49 -05:00
|
|
|
|
2011-07-29 19:53:02 -05:00
|
|
|
resource port_ptr(po: *rustrt::rust_port) {
|
|
|
|
rustrt::drop_port(po);
|
|
|
|
rustrt::del_port(po);
|
|
|
|
}
|
|
|
|
|
2011-08-17 19:13:11 -05:00
|
|
|
tag port<~T> { port_t(@port_ptr); }
|
2011-08-16 19:12:46 -05:00
|
|
|
|
2011-09-12 05:39:38 -05:00
|
|
|
fn send<~T>(ch: chan<T>, -data: T) {
|
2011-08-25 13:20:43 -05:00
|
|
|
let chan_t(t, p) = ch;
|
|
|
|
rustrt::chan_id_send(t, p, data);
|
2011-08-10 16:38:49 -05:00
|
|
|
}
|
2011-08-16 19:12:46 -05:00
|
|
|
|
|
|
|
fn port<~T>() -> port<T> {
|
2011-08-17 19:13:11 -05:00
|
|
|
port_t(@port_ptr(rustrt::new_port(sys::size_of::<T>())))
|
2011-08-16 19:12:46 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn recv<~T>(p: port<T>) -> T { ret rusti::recv(***p) }
|
2011-08-16 19:12:46 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn chan<~T>(p: port<T>) -> chan<T> {
|
2011-08-25 13:20:43 -05:00
|
|
|
chan_t(task::get_task_id(), rustrt::get_port_id(***p))
|
2011-08-16 19:12:46 -05:00
|
|
|
}
|