rust/src/lib/comm.rs

80 lines
1.5 KiB
Rust
Raw Normal View History

import sys;
import ptr;
import unsafe;
2011-08-09 18:07:49 -05:00
import task;
import task::task_id;
export _chan;
export _port;
export chan_handle;
export mk_port;
2011-08-09 18:07:49 -05:00
export send;
2011-08-16 19:12:46 -05:00
export recv;
export chan;
export port;
native "rust" mod rustrt {
type void;
type rust_port;
fn chan_id_send<~T>(target_task : task_id, target_port : port_id,
data : -T);
fn new_port(unit_sz : uint) -> *rust_port;
fn del_port(po : *rust_port);
fn drop_port(po : *rust_port);
2011-08-09 18:07:49 -05:00
fn get_port_id(po : *rust_port) -> port_id;
}
native "rust-intrinsic" mod rusti {
fn recv<~T>(port : *rustrt::rust_port) -> T;
}
2011-08-09 18:07:49 -05:00
type port_id = int;
type chan_handle<~T> = { task : task_id, port : port_id};
tag chan<~T> { chan_t(chan_handle<T>); }
2011-08-16 19:12:46 -05:00
type _chan<~T> = chan<T>;
2011-08-09 18:07:49 -05:00
resource port_ptr(po: *rustrt::rust_port) {
rustrt::drop_port(po);
rustrt::del_port(po);
}
tag port<~T> { port_t(@port_ptr); }
2011-08-16 19:12:46 -05:00
obj port_obj<~T>(raw_port : port<T>) {
fn mk_chan() -> chan<T> {
chan(raw_port)
2011-08-09 18:07:49 -05:00
}
fn recv() -> T {
2011-08-16 19:12:46 -05:00
recv(raw_port)
2011-07-29 20:52:16 -05:00
}
}
2011-08-16 19:12:46 -05:00
type _port<~T> = port_obj<T>;
2011-07-29 20:52:16 -05:00
fn mk_port<~T>() -> _port<T> {
2011-08-16 19:12:46 -05:00
ret port_obj::<T>(port::<T>());
}
2011-08-09 18:07:49 -05:00
fn send<~T>(ch : &chan<T>, data : -T) {
2011-08-09 18:07:49 -05:00
rustrt::chan_id_send(ch.task, ch.port, data);
}
2011-08-16 19:12:46 -05:00
fn port<~T>() -> port<T> {
port_t(@port_ptr(rustrt::new_port(sys::size_of::<T>())))
2011-08-16 19:12:46 -05:00
}
fn recv<~T>(p : &port<T>) -> T {
ret rusti::recv(***p)
2011-08-16 19:12:46 -05:00
}
fn chan<~T>(p : &port<T>) -> chan<T> {
chan_t({
2011-08-16 19:12:46 -05:00
task: task::get_task_id(),
port: rustrt::get_port_id(***p)
})
2011-08-16 19:12:46 -05:00
}