rust/src/lib/comm.rs

69 lines
1.4 KiB
Rust
Raw Normal View History

import sys;
import ptr;
import unsafe;
2011-08-09 16:07:49 -07:00
import task;
import task::task_id;
export _chan;
export _port;
export mk_port;
export chan_from_unsafe_ptr;
2011-08-09 16:07:49 -07:00
export send;
native "rust" mod rustrt {
type void;
type rust_chan;
type rust_port;
fn new_chan(po : *rust_port) -> *rust_chan;
fn take_chan(ch : *rust_chan);
fn drop_chan(ch : *rust_chan);
fn chan_send(ch: *rust_chan, v : *void);
2011-08-09 16:07:49 -07:00
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 16:07:49 -07:00
fn get_port_id(po : *rust_port) -> port_id;
}
native "rust-intrinsic" mod rusti {
2011-08-09 16:07:49 -07:00
fn recv[~T](port : *rustrt::rust_port) -> T;
}
2011-08-09 16:07:49 -07:00
type port_id = int;
2011-08-12 17:36:52 -07:00
type _chan[~T] = {
2011-08-09 16:07:49 -07:00
task : task_id,
port : port_id
};
resource port_ptr(po: *rustrt::rust_port) {
rustrt::drop_port(po);
rustrt::del_port(po);
}
2011-08-09 16:07:49 -07:00
obj _port[~T](raw_port : @port_ptr) {
// FIXME: rename this to chan once chan is not a keyword.
2011-08-12 17:36:52 -07:00
fn mk_chan() -> _chan[T] {
2011-08-09 16:07:49 -07:00
{
task: task::get_task_id(),
port: rustrt::get_port_id(**raw_port)
}
}
fn recv() -> T {
ret rusti::recv(**raw_port)
2011-07-29 18:52:16 -07:00
}
}
fn mk_port[~T]() -> _port<T> {
2011-07-29 18:52:16 -07:00
_port(@port_ptr(rustrt::new_port(sys::size_of[T]())))
}
2011-08-09 16:07:49 -07:00
2011-08-12 17:36:52 -07:00
fn send[~T](ch : _chan[T], data : -T) {
2011-08-09 16:07:49 -07:00
rustrt::chan_id_send(ch.task, ch.port, data);
}