diff options
| author | Eric Holk <eholk@mozilla.com> | 2011-08-09 16:07:49 -0700 |
|---|---|---|
| committer | Eric Holk <eholk@mozilla.com> | 2011-08-15 09:26:51 -0700 |
| commit | 39b16077bbcac64eb62f484486c703aed405ef2f (patch) | |
| tree | 883cd01d305130374bb0438475716982c1c483c2 /src/lib | |
| parent | 04af99ecb0dee1cb3df0032f7e7ba08ffc6c5bd4 (diff) | |
| download | rust-39b16077bbcac64eb62f484486c703aed405ef2f.tar.gz rust-39b16077bbcac64eb62f484486c703aed405ef2f.zip | |
Port ID-based channels.
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/comm.rs | 38 | ||||
| -rw-r--r-- | src/lib/task.rs | 8 |
2 files changed, 41 insertions, 5 deletions
diff --git a/src/lib/comm.rs b/src/lib/comm.rs index 798daf1db12..451c69dafd9 100644 --- a/src/lib/comm.rs +++ b/src/lib/comm.rs @@ -1,12 +1,15 @@ import sys; import ptr; import unsafe; +import task; +import task::task_id; export _chan; export _port; export mk_port; export chan_from_unsafe_ptr; +export send; native "rust" mod rustrt { type void; @@ -17,16 +20,28 @@ native "rust" mod rustrt { fn take_chan(ch : *rust_chan); fn drop_chan(ch : *rust_chan); fn chan_send(ch: *rust_chan, v : *void); + // 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); 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; } native "rust-intrinsic" mod rusti { - fn recv[T](port : *rustrt::rust_port) -> T; + fn recv[~T](port : *rustrt::rust_port) -> T; } +type port_id = int; + +type chan_t[~T] = { + task : task_id, + port : port_id +}; + resource chan_ptr(ch: *rustrt::rust_chan) { rustrt::drop_chan(ch); } @@ -36,7 +51,7 @@ resource port_ptr(po: *rustrt::rust_port) { rustrt::del_port(po); } -obj _chan[T](raw_chan : @chan_ptr) { +obj _chan[~T](raw_chan : @chan_ptr) { fn send(v : &T) { rustrt::chan_send(**raw_chan, unsafe::reinterpret_cast(ptr::addr_of(v))); @@ -49,20 +64,33 @@ obj _chan[T](raw_chan : @chan_ptr) { } } -fn chan_from_unsafe_ptr[T](ch : *u8) -> _chan[T] { +fn chan_from_unsafe_ptr[~T](ch : *u8) -> _chan[T] { _chan(@chan_ptr(unsafe::reinterpret_cast(ch))) } -obj _port[T](raw_port : @port_ptr) { +obj _port[~T](raw_port : @port_ptr) { fn mk_chan() -> _chan[T] { _chan(@chan_ptr(rustrt::new_chan(**raw_port))) } + // 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) + } + } + fn recv() -> T { ret rusti::recv(**raw_port) } } -fn mk_port[T]() -> _port[T] { +fn mk_port[~T]() -> _port[T] { _port(@port_ptr(rustrt::new_port(sys::size_of[T]()))) } + +// 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); +} \ No newline at end of file diff --git a/src/lib/task.rs b/src/lib/task.rs index 1936cf20971..a4c164658a7 100644 --- a/src/lib/task.rs +++ b/src/lib/task.rs @@ -5,6 +5,7 @@ native "rust" mod rustrt { fn unsupervise(); fn pin_task(); fn unpin_task(); + fn get_task_id() -> task_id; fn clone_chan(c: *rust_chan) -> *rust_chan; type rust_chan; @@ -12,6 +13,12 @@ native "rust" mod rustrt { fn set_min_stack(stack_size: uint); } +type task_id = int; + +fn get_task_id() -> task_id { + rustrt::get_task_id() +} + /** * Hints the scheduler to yield this task for a specified ammount of time. * @@ -33,6 +40,7 @@ fn pin() { rustrt::pin_task(); } fn unpin() { rustrt::unpin_task(); } +// FIXME: remove this fn clone_chan[T](c: chan[T]) -> chan[T] { let cloned = rustrt::clone_chan(unsafe::reinterpret_cast(c)); ret unsafe::reinterpret_cast(cloned); |
