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;
|
|
|
|
import task::task_id;
|
2011-07-29 19:53:02 -05:00
|
|
|
|
|
|
|
export _chan;
|
|
|
|
export _port;
|
|
|
|
|
|
|
|
export mk_port;
|
2011-08-05 20:43:33 -05:00
|
|
|
export chan_from_unsafe_ptr;
|
2011-08-09 18:07:49 -05:00
|
|
|
export send;
|
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_chan;
|
|
|
|
type rust_port;
|
|
|
|
|
|
|
|
fn new_chan(po : *rust_port) -> *rust_chan;
|
2011-08-05 20:43:33 -05:00
|
|
|
fn take_chan(ch : *rust_chan);
|
2011-07-29 19:53:02 -05:00
|
|
|
fn drop_chan(ch : *rust_chan);
|
2011-08-01 16:57:17 -05:00
|
|
|
fn chan_send(ch: *rust_chan, v : *void);
|
2011-08-09 18:07:49 -05:00
|
|
|
// FIXME: data should be -T, not &T, but this doesn't seem to be
|
|
|
|
// supported yet.
|
|
|
|
fn chan_id_send[~T](target_task : task_id, target_port : port_id,
|
|
|
|
data : &T);
|
2011-07-29 19:53:02 -05:00
|
|
|
|
|
|
|
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;
|
2011-08-02 14:29:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
native "rust-intrinsic" mod rusti {
|
2011-08-09 18:07:49 -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;
|
|
|
|
|
|
|
|
type chan_t[~T] = {
|
|
|
|
task : task_id,
|
|
|
|
port : port_id
|
|
|
|
};
|
|
|
|
|
2011-07-29 19:53:02 -05:00
|
|
|
resource chan_ptr(ch: *rustrt::rust_chan) {
|
|
|
|
rustrt::drop_chan(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
resource port_ptr(po: *rustrt::rust_port) {
|
|
|
|
rustrt::drop_port(po);
|
|
|
|
rustrt::del_port(po);
|
|
|
|
}
|
|
|
|
|
2011-08-09 18:07:49 -05:00
|
|
|
obj _chan[~T](raw_chan : @chan_ptr) {
|
2011-07-29 20:52:16 -05:00
|
|
|
fn send(v : &T) {
|
2011-08-01 16:57:17 -05:00
|
|
|
rustrt::chan_send(**raw_chan,
|
|
|
|
unsafe::reinterpret_cast(ptr::addr_of(v)));
|
2011-07-29 20:52:16 -05:00
|
|
|
}
|
2011-08-05 20:43:33 -05:00
|
|
|
|
|
|
|
// Use this to get something we can send over a channel.
|
|
|
|
fn unsafe_ptr() -> *u8 {
|
|
|
|
rustrt::take_chan(**raw_chan);
|
|
|
|
ret unsafe::reinterpret_cast(**raw_chan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-09 18:07:49 -05:00
|
|
|
fn chan_from_unsafe_ptr[~T](ch : *u8) -> _chan[T] {
|
2011-08-05 20:43:33 -05:00
|
|
|
_chan(@chan_ptr(unsafe::reinterpret_cast(ch)))
|
2011-07-29 19:53:02 -05:00
|
|
|
}
|
|
|
|
|
2011-08-09 18:07:49 -05:00
|
|
|
obj _port[~T](raw_port : @port_ptr) {
|
2011-07-29 20:52:16 -05:00
|
|
|
fn mk_chan() -> _chan[T] {
|
|
|
|
_chan(@chan_ptr(rustrt::new_chan(**raw_port)))
|
2011-07-29 19:53:02 -05:00
|
|
|
}
|
2011-07-29 20:52:16 -05:00
|
|
|
|
2011-08-09 18:07:49 -05:00
|
|
|
// FIXME: rename this to chan once chan is not a keyword.
|
|
|
|
fn mk_chan2() -> chan_t[T] {
|
|
|
|
{
|
|
|
|
task: task::get_task_id(),
|
|
|
|
port: rustrt::get_port_id(**raw_port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-02 14:29:10 -05:00
|
|
|
fn recv() -> T {
|
|
|
|
ret rusti::recv(**raw_port)
|
2011-07-29 20:52:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-09 18:07:49 -05:00
|
|
|
fn mk_port[~T]() -> _port[T] {
|
2011-07-29 20:52:16 -05:00
|
|
|
_port(@port_ptr(rustrt::new_port(sys::size_of[T]())))
|
2011-07-29 19:53:02 -05:00
|
|
|
}
|
2011-08-09 18:07:49 -05:00
|
|
|
|
|
|
|
// FIXME: make data move-mode once the snapshot is updated.
|
|
|
|
fn send[~T](ch : chan_t[T], data : &T) {
|
|
|
|
rustrt::chan_id_send(ch.task, ch.port, data);
|
|
|
|
}
|