Yet another comm interface.

This commit is contained in:
Eric Holk 2011-08-16 17:12:46 -07:00
parent 72b2f8207d
commit efac7c9a19
3 changed files with 36 additions and 17 deletions

View File

@ -10,16 +10,14 @@ export _port;
export mk_port;
export chan_from_unsafe_ptr;
export send;
export recv;
export chan;
export port;
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);
fn chan_id_send<~T>(target_task : task_id, target_port : port_id,
data : -T);
@ -35,34 +33,50 @@ native "rust-intrinsic" mod rusti {
type port_id = int;
type _chan<~T> = {
type chan<~T> = {
task : task_id,
port : port_id
};
type _chan<~T> = chan<T>;
resource port_ptr(po: *rustrt::rust_port) {
rustrt::drop_port(po);
rustrt::del_port(po);
}
obj _port<~T>(raw_port : @port_ptr) {
type port<~T> = @port_ptr;
obj port_obj<~T>(raw_port : port<T>) {
// FIXME: rename this to chan once chan is not a keyword.
fn mk_chan() -> _chan<T> {
{
task: task::get_task_id(),
port: rustrt::get_port_id(**raw_port)
}
chan::<T>(raw_port)
}
fn recv() -> T {
ret rusti::recv(**raw_port)
recv(raw_port)
}
}
type _port<~T> = port_obj<T>;
fn mk_port<~T>() -> _port<T> {
_port(@port_ptr(rustrt::new_port(sys::size_of::<T>())))
ret port_obj::<T>(port::<T>());
}
fn send<~T>(ch : _chan<T>, data : -T) {
fn send<~T>(ch : chan<T>, data : -T) {
rustrt::chan_id_send(ch.task, ch.port, data);
}
fn port<~T>() -> port<T> {
@port_ptr(rustrt::new_port(sys::size_of::<T>()))
}
fn recv<~T>(p : port<T>) -> T {
ret rusti::recv(**p)
}
fn chan<~T>(p : port<T>) -> chan<T> {
{
task: task::get_task_id(),
port: rustrt::get_port_id(**p)
}
}

View File

@ -16,14 +16,12 @@ debug_box
debug_fn
debug_obj
debug_opaque
del_chan
del_port
debug_ptrcast
debug_tag
debug_trap
debug_tydesc
do_gc
drop_chan
drop_port
drop_task
get_port_id
@ -77,7 +75,6 @@ str_from_cstr
str_from_ivec
str_push_byte
str_slice
take_chan
task_sleep
task_yield
task_join

View File

@ -17,3 +17,11 @@ fn send_recv() {
log_err v;
assert(42 == v);
}
#[test]
fn send_recv_fn() {
let p = comm::port::<int>();
let c = comm::chan::<int>(p);
comm::send(c, 42);
assert(comm::recv(p) == 42);
}