2012-08-01 13:56:46 -05:00
|
|
|
/*! Runtime support for message passing with protocol enforcement.
|
|
|
|
|
|
|
|
|
|
|
|
Pipes consist of two endpoints. One endpoint can send messages and
|
|
|
|
the other can receive messages. The set of legal messages and which
|
|
|
|
directions they can flow at any given point are determined by a
|
|
|
|
protocol. Below is an example protocol.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
proto! pingpong {
|
|
|
|
ping: send {
|
|
|
|
ping -> pong
|
|
|
|
}
|
|
|
|
pong: recv {
|
|
|
|
pong -> ping
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~~~
|
|
|
|
|
|
|
|
The `proto!` syntax extension will convert this into a module called
|
|
|
|
`pingpong`, which includes a set of types and functions that can be
|
|
|
|
used to write programs that follow the pingpong protocol.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* IMPLEMENTATION NOTES
|
|
|
|
|
|
|
|
The initial design for this feature is available at:
|
|
|
|
|
|
|
|
https://github.com/eholk/rust/wiki/Proposal-for-channel-contracts
|
|
|
|
|
|
|
|
Much of the design in that document is still accurate. There are
|
|
|
|
several components for the pipe implementation. First of all is the
|
|
|
|
syntax extension. To see how that works, it is best see comments in
|
|
|
|
libsyntax/ext/pipes.rs.
|
|
|
|
|
|
|
|
This module includes two related pieces of the runtime
|
2012-08-13 12:55:01 -05:00
|
|
|
implementation: support for unbounded and bounded
|
2012-08-01 13:56:46 -05:00
|
|
|
protocols. The main difference between the two is the type of the
|
|
|
|
buffer that is carried along in the endpoint data structures.
|
|
|
|
|
|
|
|
|
2012-08-13 12:55:01 -05:00
|
|
|
The heart of the implementation is the packet type. It contains a
|
|
|
|
header and a payload field. Much of the code in this module deals with
|
|
|
|
the header field. This is where the synchronization information is
|
|
|
|
stored. In the case of a bounded protocol, the header also includes a
|
|
|
|
pointer to the buffer the packet is contained in.
|
|
|
|
|
|
|
|
Packets represent a single message in a protocol. The payload field
|
|
|
|
gets instatiated at the type of the message, which is usually an enum
|
|
|
|
generated by the pipe compiler. Packets are conceptually single use,
|
|
|
|
although in bounded protocols they are reused each time around the
|
|
|
|
loop.
|
|
|
|
|
|
|
|
|
|
|
|
Packets are usually handled through a send_packet_buffered or
|
|
|
|
recv_packet_buffered object. Each packet is referenced by one
|
|
|
|
send_packet and one recv_packet, and these wrappers enforce that only
|
|
|
|
one end can send and only one end can receive. The structs also
|
|
|
|
include a destructor that marks packets are terminated if the sender
|
|
|
|
or receiver destroys the object before sending or receiving a value.
|
|
|
|
|
|
|
|
The *_packet_buffered structs take two type parameters. The first is
|
|
|
|
the message type for the current packet (or state). The second
|
|
|
|
represents the type of the whole buffer. For bounded protocols, the
|
|
|
|
protocol compiler generates a struct with a field for each protocol
|
|
|
|
state. This generated struct is used as the buffer type parameter. For
|
|
|
|
unbounded protocols, the buffer is simply one packet, so there is a
|
|
|
|
shorthand struct called send_packet and recv_packet, where the buffer
|
|
|
|
type is just `packet<T>`. Using the same underlying structure for both
|
|
|
|
bounded and unbounded protocols allows for less code duplication.
|
2012-08-03 17:20:38 -05:00
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
*/
|
2012-06-29 20:15:28 -05:00
|
|
|
|
2012-08-14 18:39:57 -05:00
|
|
|
// NB: transitionary, de-mode-ing.
|
|
|
|
#[forbid(deprecated_mode)];
|
|
|
|
#[forbid(deprecated_pattern)];
|
|
|
|
|
2012-07-09 15:53:55 -05:00
|
|
|
import unsafe::{forget, reinterpret_cast, transmute};
|
2012-08-14 18:54:13 -05:00
|
|
|
import either::{Either, Left, Right};
|
2012-07-10 13:58:43 -05:00
|
|
|
import option::unwrap;
|
2012-07-12 18:44:09 -05:00
|
|
|
|
2012-07-18 17:03:46 -05:00
|
|
|
// Things used by code generated by the pipe compiler.
|
2012-07-20 21:06:32 -05:00
|
|
|
export entangle, get_buffer, drop_buffer;
|
2012-07-23 13:28:38 -05:00
|
|
|
export send_packet_buffered, recv_packet_buffered;
|
2012-07-24 18:58:48 -05:00
|
|
|
export packet, mk_packet, entangle_buffer, has_buffer, buffer_header;
|
2012-07-23 13:28:38 -05:00
|
|
|
|
|
|
|
// export these so we can find them in the buffer_resource
|
2012-08-01 13:56:46 -05:00
|
|
|
// destructor. This is probably a symptom of #3005.
|
2012-07-23 13:28:38 -05:00
|
|
|
export atomic_add_acq, atomic_sub_rel;
|
2012-07-18 17:03:46 -05:00
|
|
|
|
|
|
|
// User-level things
|
|
|
|
export send_packet, recv_packet, send, recv, try_recv, peek;
|
|
|
|
export select, select2, selecti, select2i, selectable;
|
|
|
|
export spawn_service, spawn_service_recv;
|
|
|
|
export stream, port, chan, shared_chan, port_set, channel;
|
2012-08-14 19:48:37 -05:00
|
|
|
export oneshot, chan_one, port_one;
|
|
|
|
export recv_one, try_recv_one, send_one, try_send_one;
|
2012-07-18 17:03:46 -05:00
|
|
|
|
2012-08-15 18:22:40 -05:00
|
|
|
// Functions used by the protocol compiler
|
|
|
|
export rt;
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-24 11:35:44 -05:00
|
|
|
const SPIN_COUNT: uint = 0;
|
|
|
|
|
2012-07-31 19:31:24 -05:00
|
|
|
macro_rules! move_it {
|
2012-07-12 18:44:09 -05:00
|
|
|
{ $x:expr } => { unsafe { let y <- *ptr::addr_of($x); y } }
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-06-29 20:15:28 -05:00
|
|
|
enum state {
|
|
|
|
empty,
|
|
|
|
full,
|
|
|
|
blocked,
|
|
|
|
terminated
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
struct buffer_header {
|
2012-07-20 21:06:32 -05:00
|
|
|
// Tracks whether this buffer needs to be freed. We can probably
|
|
|
|
// get away with restricting it to 0 or 1, if we're careful.
|
|
|
|
let mut ref_count: int;
|
|
|
|
|
2012-07-23 13:28:38 -05:00
|
|
|
new() { self.ref_count = 0; }
|
2012-07-20 21:06:32 -05:00
|
|
|
|
|
|
|
// We may want a drop, and to be careful about stringing this
|
|
|
|
// thing along.
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is for protocols to associate extra data to thread around.
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-20 21:06:32 -05:00
|
|
|
type buffer<T: send> = {
|
|
|
|
header: buffer_header,
|
|
|
|
data: T,
|
2012-07-03 19:33:20 -05:00
|
|
|
};
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
struct packet_header {
|
2012-07-20 21:06:32 -05:00
|
|
|
let mut state: state;
|
2012-08-03 20:57:43 -05:00
|
|
|
let mut blocked_task: *rust_task;
|
2012-07-20 21:06:32 -05:00
|
|
|
|
|
|
|
// This is a reinterpret_cast of a ~buffer, that can also be cast
|
|
|
|
// to a buffer_header if need be.
|
|
|
|
let mut buffer: *libc::c_void;
|
|
|
|
|
|
|
|
new() {
|
|
|
|
self.state = empty;
|
2012-08-03 20:57:43 -05:00
|
|
|
self.blocked_task = ptr::null();
|
2012-07-20 21:06:32 -05:00
|
|
|
self.buffer = ptr::null();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the old state.
|
|
|
|
unsafe fn mark_blocked(this: *rust_task) -> state {
|
2012-08-03 20:57:43 -05:00
|
|
|
rustrt::rust_task_ref(this);
|
2012-08-14 18:39:57 -05:00
|
|
|
let old_task = swap_task(&mut self.blocked_task, this);
|
2012-08-03 20:57:43 -05:00
|
|
|
assert old_task.is_null();
|
2012-08-14 18:39:57 -05:00
|
|
|
swap_state_acq(&mut self.state, blocked)
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn unblock() {
|
2012-08-14 18:39:57 -05:00
|
|
|
let old_task = swap_task(&mut self.blocked_task, ptr::null());
|
2012-08-03 20:57:43 -05:00
|
|
|
if !old_task.is_null() { rustrt::rust_task_deref(old_task) }
|
2012-08-14 18:39:57 -05:00
|
|
|
match swap_state_acq(&mut self.state, empty) {
|
2012-08-03 21:59:04 -05:00
|
|
|
empty | blocked => (),
|
|
|
|
terminated => self.state = terminated,
|
|
|
|
full => self.state = full
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unsafe because this can do weird things to the space/time
|
|
|
|
// continuum. It ends making multiple unique pointers to the same
|
|
|
|
// thing. You'll proobably want to forget them when you're done.
|
|
|
|
unsafe fn buf_header() -> ~buffer_header {
|
|
|
|
assert self.buffer.is_not_null();
|
|
|
|
reinterpret_cast(self.buffer)
|
|
|
|
}
|
2012-07-23 13:28:38 -05:00
|
|
|
|
|
|
|
fn set_buffer<T: send>(b: ~buffer<T>) unsafe {
|
|
|
|
self.buffer = reinterpret_cast(b);
|
|
|
|
}
|
2012-07-11 14:45:54 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-20 21:06:32 -05:00
|
|
|
type packet<T: send> = {
|
2012-07-03 19:33:20 -05:00
|
|
|
header: packet_header,
|
2012-07-20 21:06:32 -05:00
|
|
|
mut payload: option<T>,
|
2012-06-29 20:15:28 -05:00
|
|
|
};
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-23 15:50:12 -05:00
|
|
|
trait has_buffer {
|
|
|
|
fn set_buffer(b: *libc::c_void);
|
|
|
|
}
|
|
|
|
|
2012-08-07 20:10:06 -05:00
|
|
|
impl<T: send> packet<T>: has_buffer {
|
2012-07-23 15:50:12 -05:00
|
|
|
fn set_buffer(b: *libc::c_void) {
|
|
|
|
self.header.buffer = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-23 13:28:38 -05:00
|
|
|
fn mk_packet<T: send>() -> packet<T> {
|
|
|
|
{
|
|
|
|
header: packet_header(),
|
|
|
|
mut payload: none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-20 21:06:32 -05:00
|
|
|
fn unibuffer<T: send>() -> ~buffer<packet<T>> {
|
|
|
|
let b = ~{
|
|
|
|
header: buffer_header(),
|
|
|
|
data: {
|
|
|
|
header: packet_header(),
|
|
|
|
mut payload: none,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
b.data.header.buffer = reinterpret_cast(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
b
|
2012-07-11 14:45:54 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-20 21:06:32 -05:00
|
|
|
fn packet<T: send>() -> *packet<T> {
|
|
|
|
let b = unibuffer();
|
|
|
|
let p = ptr::addr_of(b.data);
|
|
|
|
// We'll take over memory management from here.
|
|
|
|
unsafe { forget(b) }
|
2012-06-29 20:15:28 -05:00
|
|
|
p
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-23 15:50:12 -05:00
|
|
|
fn entangle_buffer<T: send, Tstart: send>(
|
2012-08-14 18:39:57 -05:00
|
|
|
+buffer: ~buffer<T>,
|
2012-07-23 15:50:12 -05:00
|
|
|
init: fn(*libc::c_void, x: &T) -> *packet<Tstart>)
|
|
|
|
-> (send_packet_buffered<Tstart, T>, recv_packet_buffered<Tstart, T>)
|
|
|
|
{
|
|
|
|
let p = init(unsafe { reinterpret_cast(buffer) }, &buffer.data);
|
|
|
|
unsafe { forget(buffer) }
|
|
|
|
(send_packet_buffered(p), recv_packet_buffered(p))
|
|
|
|
}
|
|
|
|
|
2012-06-29 20:15:28 -05:00
|
|
|
#[abi = "rust-intrinsic"]
|
2012-08-06 12:33:31 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-03 19:33:20 -05:00
|
|
|
extern mod rusti {
|
2012-06-29 20:15:28 -05:00
|
|
|
fn atomic_xchng(&dst: int, src: int) -> int;
|
|
|
|
fn atomic_xchng_acq(&dst: int, src: int) -> int;
|
|
|
|
fn atomic_xchng_rel(&dst: int, src: int) -> int;
|
2012-07-23 13:28:38 -05:00
|
|
|
|
|
|
|
fn atomic_add_acq(&dst: int, src: int) -> int;
|
|
|
|
fn atomic_sub_rel(&dst: int, src: int) -> int;
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
|
2012-07-23 13:28:38 -05:00
|
|
|
// If I call the rusti versions directly from a polymorphic function,
|
|
|
|
// I get link errors. This is a bug that needs investigated more.
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-08-14 18:39:57 -05:00
|
|
|
fn atomic_xchng_rel(dst: &mut int, src: int) -> int {
|
|
|
|
rusti::atomic_xchng_rel(*dst, src)
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-08-14 18:39:57 -05:00
|
|
|
fn atomic_add_acq(dst: &mut int, src: int) -> int {
|
|
|
|
rusti::atomic_add_acq(*dst, src)
|
2012-07-23 13:28:38 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-08-14 18:39:57 -05:00
|
|
|
fn atomic_sub_rel(dst: &mut int, src: int) -> int {
|
|
|
|
rusti::atomic_sub_rel(*dst, src)
|
2012-07-23 13:28:38 -05:00
|
|
|
}
|
|
|
|
|
2012-08-03 20:57:43 -05:00
|
|
|
#[doc(hidden)]
|
2012-08-14 18:39:57 -05:00
|
|
|
fn swap_task(dst: &mut *rust_task, src: *rust_task) -> *rust_task {
|
2012-08-03 20:57:43 -05:00
|
|
|
// It might be worth making both acquire and release versions of
|
|
|
|
// this.
|
|
|
|
unsafe {
|
|
|
|
reinterpret_cast(rusti::atomic_xchng(
|
2012-08-14 18:39:57 -05:00
|
|
|
*(ptr::mut_addr_of(*dst) as *mut int),
|
2012-08-03 20:57:43 -05:00
|
|
|
src as int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-02 19:42:58 -05:00
|
|
|
type rust_task = libc::c_void;
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-03 19:33:20 -05:00
|
|
|
extern mod rustrt {
|
2012-07-02 19:42:58 -05:00
|
|
|
#[rust_stack]
|
|
|
|
fn rust_get_task() -> *rust_task;
|
2012-08-03 20:57:43 -05:00
|
|
|
#[rust_stack]
|
|
|
|
fn rust_task_ref(task: *rust_task);
|
|
|
|
fn rust_task_deref(task: *rust_task);
|
2012-07-02 19:42:58 -05:00
|
|
|
|
|
|
|
#[rust_stack]
|
|
|
|
fn task_clear_event_reject(task: *rust_task);
|
|
|
|
|
2012-07-24 17:22:44 -05:00
|
|
|
fn task_wait_event(this: *rust_task, killed: &mut *libc::c_void) -> bool;
|
2012-07-20 21:06:32 -05:00
|
|
|
pure fn task_signal_event(target: *rust_task, event: *libc::c_void);
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-10 12:58:44 -05:00
|
|
|
fn wait_event(this: *rust_task) -> *libc::c_void {
|
2012-07-24 17:22:44 -05:00
|
|
|
let mut event = ptr::null();
|
2012-07-10 12:58:44 -05:00
|
|
|
|
2012-07-24 17:22:44 -05:00
|
|
|
let killed = rustrt::task_wait_event(this, &mut event);
|
2012-07-10 12:58:44 -05:00
|
|
|
if killed && !task::failing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
fail ~"killed"
|
2012-07-10 12:58:44 -05:00
|
|
|
}
|
2012-07-24 17:22:44 -05:00
|
|
|
event
|
2012-07-10 12:58:44 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-08-14 18:39:57 -05:00
|
|
|
fn swap_state_acq(dst: &mut state, src: state) -> state {
|
2012-06-29 20:15:28 -05:00
|
|
|
unsafe {
|
|
|
|
reinterpret_cast(rusti::atomic_xchng_acq(
|
2012-08-14 18:39:57 -05:00
|
|
|
*(ptr::mut_addr_of(*dst) as *mut int),
|
2012-06-29 20:15:28 -05:00
|
|
|
src as int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-08-14 18:39:57 -05:00
|
|
|
fn swap_state_rel(dst: &mut state, src: state) -> state {
|
2012-06-29 20:15:28 -05:00
|
|
|
unsafe {
|
|
|
|
reinterpret_cast(rusti::atomic_xchng_rel(
|
2012-08-14 18:39:57 -05:00
|
|
|
*(ptr::mut_addr_of(*dst) as *mut int),
|
2012-06-29 20:15:28 -05:00
|
|
|
src as int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-20 21:06:32 -05:00
|
|
|
unsafe fn get_buffer<T: send>(p: *packet_header) -> ~buffer<T> {
|
|
|
|
transmute((*p).buf_header())
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
struct buffer_resource<T: send> {
|
2012-07-20 21:06:32 -05:00
|
|
|
let buffer: ~buffer<T>;
|
|
|
|
new(+b: ~buffer<T>) {
|
2012-07-24 11:35:44 -05:00
|
|
|
//let p = ptr::addr_of(*b);
|
2012-07-30 18:01:07 -05:00
|
|
|
//error!{"take %?", p};
|
2012-08-14 18:39:57 -05:00
|
|
|
atomic_add_acq(&mut b.header.ref_count, 1);
|
2012-07-20 21:06:32 -05:00
|
|
|
self.buffer = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
drop unsafe {
|
2012-07-31 19:31:24 -05:00
|
|
|
let b = move_it!{self.buffer};
|
2012-07-24 11:35:44 -05:00
|
|
|
//let p = ptr::addr_of(*b);
|
2012-07-30 18:01:07 -05:00
|
|
|
//error!{"drop %?", p};
|
2012-08-14 18:39:57 -05:00
|
|
|
let old_count = atomic_sub_rel(&mut b.header.ref_count, 1);
|
2012-07-23 13:28:38 -05:00
|
|
|
//let old_count = atomic_xchng_rel(b.header.ref_count, 0);
|
|
|
|
if old_count == 1 {
|
|
|
|
// The new count is 0.
|
|
|
|
|
2012-07-20 21:06:32 -05:00
|
|
|
// go go gadget drop glue
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
forget(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-08-14 18:39:57 -05:00
|
|
|
fn send<T: send, Tbuffer: send>(+p: send_packet_buffered<T, Tbuffer>,
|
|
|
|
+payload: T) -> bool {
|
2012-07-20 21:06:32 -05:00
|
|
|
let header = p.header();
|
2012-07-02 19:42:58 -05:00
|
|
|
let p_ = p.unwrap();
|
2012-07-20 21:06:32 -05:00
|
|
|
let p = unsafe { &*p_ };
|
|
|
|
assert ptr::addr_of(p.header) == header;
|
|
|
|
assert p.payload == none;
|
|
|
|
p.payload <- some(payload);
|
2012-08-14 18:39:57 -05:00
|
|
|
let old_state = swap_state_rel(&mut p.header.state, full);
|
2012-08-06 14:34:08 -05:00
|
|
|
match old_state {
|
2012-08-07 13:46:52 -05:00
|
|
|
empty => {
|
|
|
|
// Yay, fastpath.
|
2012-06-29 20:15:28 -05:00
|
|
|
|
2012-08-07 13:46:52 -05:00
|
|
|
// The receiver will eventually clean this up.
|
|
|
|
//unsafe { forget(p); }
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
full => fail ~"duplicate send",
|
|
|
|
blocked => {
|
|
|
|
debug!{"waking up task for %?", p_};
|
2012-08-14 18:39:57 -05:00
|
|
|
let old_task = swap_task(&mut p.header.blocked_task, ptr::null());
|
2012-08-07 13:46:52 -05:00
|
|
|
if !old_task.is_null() {
|
|
|
|
rustrt::task_signal_event(
|
|
|
|
old_task, ptr::addr_of(p.header) as *libc::c_void);
|
|
|
|
rustrt::rust_task_deref(old_task);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The receiver will eventually clean this up.
|
|
|
|
//unsafe { forget(p); }
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
terminated => {
|
|
|
|
// The receiver will never receive this. Rely on drop_glue
|
|
|
|
// to clean everything up.
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
/** Receives a message from a pipe.
|
|
|
|
|
|
|
|
Fails if the sender closes the connection.
|
|
|
|
|
|
|
|
*/
|
2012-08-14 18:39:57 -05:00
|
|
|
fn recv<T: send, Tbuffer: send>(+p: recv_packet_buffered<T, Tbuffer>) -> T {
|
2012-08-03 13:43:10 -05:00
|
|
|
option::unwrap_expect(try_recv(p), "connection closed")
|
2012-07-10 13:40:03 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
/** Attempts to receive a message from a pipe.
|
|
|
|
|
|
|
|
Returns `none` if the sender has closed the connection without sending
|
|
|
|
a message, or `some(T)` if a message was received.
|
|
|
|
|
|
|
|
*/
|
2012-08-14 18:39:57 -05:00
|
|
|
fn try_recv<T: send, Tbuffer: send>(+p: recv_packet_buffered<T, Tbuffer>)
|
2012-07-23 13:28:38 -05:00
|
|
|
-> option<T>
|
|
|
|
{
|
2012-07-02 19:42:58 -05:00
|
|
|
let p_ = p.unwrap();
|
2012-07-20 21:06:32 -05:00
|
|
|
let p = unsafe { &*p_ };
|
2012-08-07 18:54:00 -05:00
|
|
|
|
2012-08-10 18:17:53 -05:00
|
|
|
struct drop_state {
|
|
|
|
p: &packet_header;
|
|
|
|
|
|
|
|
drop {
|
|
|
|
if task::failing() {
|
|
|
|
self.p.state = terminated;
|
2012-08-14 18:39:57 -05:00
|
|
|
let old_task = swap_task(&mut self.p.blocked_task,
|
|
|
|
ptr::null());
|
2012-08-10 18:17:53 -05:00
|
|
|
if !old_task.is_null() {
|
|
|
|
rustrt::rust_task_deref(old_task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let _drop_state = drop_state { p: &p.header };
|
|
|
|
|
2012-08-07 18:54:00 -05:00
|
|
|
// optimistic path
|
|
|
|
match p.header.state {
|
|
|
|
full => {
|
|
|
|
let mut payload = none;
|
|
|
|
payload <-> p.payload;
|
|
|
|
p.header.state = empty;
|
|
|
|
return some(option::unwrap(payload))
|
|
|
|
},
|
|
|
|
terminated => return none,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// regular path
|
2012-07-02 19:42:58 -05:00
|
|
|
let this = rustrt::rust_get_task();
|
|
|
|
rustrt::task_clear_event_reject(this);
|
2012-08-03 20:57:43 -05:00
|
|
|
rustrt::rust_task_ref(this);
|
2012-08-14 18:39:57 -05:00
|
|
|
let old_task = swap_task(&mut p.header.blocked_task, this);
|
2012-08-03 20:57:43 -05:00
|
|
|
assert old_task.is_null();
|
2012-07-10 12:58:44 -05:00
|
|
|
let mut first = true;
|
2012-07-24 11:35:44 -05:00
|
|
|
let mut count = SPIN_COUNT;
|
2012-06-29 20:15:28 -05:00
|
|
|
loop {
|
2012-07-10 12:58:44 -05:00
|
|
|
rustrt::task_clear_event_reject(this);
|
2012-08-14 18:39:57 -05:00
|
|
|
let old_state = swap_state_acq(&mut p.header.state,
|
2012-06-29 20:15:28 -05:00
|
|
|
blocked);
|
2012-08-06 14:34:08 -05:00
|
|
|
match old_state {
|
2012-08-03 21:59:04 -05:00
|
|
|
empty => {
|
2012-07-30 18:01:07 -05:00
|
|
|
debug!{"no data available on %?, going to sleep.", p_};
|
2012-07-24 11:35:44 -05:00
|
|
|
if count == 0 {
|
|
|
|
wait_event(this);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
count -= 1;
|
|
|
|
// FIXME (#524): Putting the yield here destroys a lot
|
|
|
|
// of the benefit of spinning, since we still go into
|
|
|
|
// the scheduler at every iteration. However, without
|
|
|
|
// this everything spins too much because we end up
|
|
|
|
// sometimes blocking the thing we are waiting on.
|
|
|
|
task::yield();
|
|
|
|
}
|
2012-07-30 18:01:07 -05:00
|
|
|
debug!{"woke up, p.state = %?", copy p.header.state};
|
2012-07-10 12:58:44 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
blocked => if first {
|
|
|
|
fail ~"blocking on already blocked packet"
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2012-08-03 21:59:04 -05:00
|
|
|
full => {
|
2012-06-29 20:15:28 -05:00
|
|
|
let mut payload = none;
|
2012-07-20 21:06:32 -05:00
|
|
|
payload <-> p.payload;
|
2012-08-14 18:39:57 -05:00
|
|
|
let old_task = swap_task(&mut p.header.blocked_task, ptr::null());
|
2012-08-03 20:57:43 -05:00
|
|
|
if !old_task.is_null() {
|
|
|
|
rustrt::rust_task_deref(old_task);
|
|
|
|
}
|
2012-07-24 00:14:43 -05:00
|
|
|
p.header.state = empty;
|
2012-08-01 19:30:05 -05:00
|
|
|
return some(option::unwrap(payload))
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
terminated => {
|
2012-08-03 13:43:10 -05:00
|
|
|
// This assert detects when we've accidentally unsafely
|
|
|
|
// casted too big of a number to a state.
|
2012-06-29 20:15:28 -05:00
|
|
|
assert old_state == terminated;
|
2012-08-03 20:57:43 -05:00
|
|
|
|
2012-08-14 18:39:57 -05:00
|
|
|
let old_task = swap_task(&mut p.header.blocked_task, ptr::null());
|
2012-08-03 20:57:43 -05:00
|
|
|
if !old_task.is_null() {
|
|
|
|
rustrt::rust_task_deref(old_task);
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return none;
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-10 12:58:44 -05:00
|
|
|
first = false;
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-09 17:29:23 -05:00
|
|
|
/// Returns true if messages are available.
|
2012-08-14 18:39:57 -05:00
|
|
|
pure fn peek<T: send, Tb: send>(p: &recv_packet_buffered<T, Tb>) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match unsafe {(*p.header()).state} {
|
2012-08-03 21:59:04 -05:00
|
|
|
empty => false,
|
|
|
|
blocked => fail ~"peeking on blocked packet",
|
|
|
|
full | terminated => true
|
2012-07-09 17:29:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-07 20:10:06 -05:00
|
|
|
impl<T: send, Tb: send> recv_packet_buffered<T, Tb> {
|
2012-07-26 19:10:21 -05:00
|
|
|
pure fn peek() -> bool {
|
2012-08-14 18:39:57 -05:00
|
|
|
peek(&self)
|
2012-07-26 19:10:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-06-29 20:15:28 -05:00
|
|
|
fn sender_terminate<T: send>(p: *packet<T>) {
|
2012-07-20 21:06:32 -05:00
|
|
|
let p = unsafe { &*p };
|
2012-08-14 18:39:57 -05:00
|
|
|
match swap_state_rel(&mut p.header.state, terminated) {
|
2012-08-03 21:59:04 -05:00
|
|
|
empty => {
|
2012-07-09 18:57:47 -05:00
|
|
|
// The receiver will eventually clean up.
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
blocked => {
|
2012-07-09 18:57:47 -05:00
|
|
|
// wake up the target
|
2012-08-14 18:39:57 -05:00
|
|
|
let old_task = swap_task(&mut p.header.blocked_task, ptr::null());
|
2012-08-03 20:57:43 -05:00
|
|
|
if !old_task.is_null() {
|
2012-08-03 13:43:10 -05:00
|
|
|
rustrt::task_signal_event(
|
2012-08-03 20:57:43 -05:00
|
|
|
old_task,
|
|
|
|
ptr::addr_of(p.header) as *libc::c_void);
|
|
|
|
rustrt::rust_task_deref(old_task);
|
2012-08-03 13:43:10 -05:00
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
// The receiver will eventually clean up.
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
full => {
|
2012-06-29 20:15:28 -05:00
|
|
|
// This is impossible
|
2012-07-14 00:57:48 -05:00
|
|
|
fail ~"you dun goofed"
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
terminated => {
|
2012-08-03 20:57:43 -05:00
|
|
|
assert p.header.blocked_task.is_null();
|
2012-06-29 20:15:28 -05:00
|
|
|
// I have to clean up, use drop_glue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-06-29 20:15:28 -05:00
|
|
|
fn receiver_terminate<T: send>(p: *packet<T>) {
|
2012-07-20 21:06:32 -05:00
|
|
|
let p = unsafe { &*p };
|
2012-08-14 18:39:57 -05:00
|
|
|
match swap_state_rel(&mut p.header.state, terminated) {
|
2012-08-03 21:59:04 -05:00
|
|
|
empty => {
|
2012-08-10 19:15:31 -05:00
|
|
|
assert p.header.blocked_task.is_null();
|
2012-06-29 20:15:28 -05:00
|
|
|
// the sender will clean up
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
blocked => {
|
2012-08-14 18:39:57 -05:00
|
|
|
let old_task = swap_task(&mut p.header.blocked_task, ptr::null());
|
2012-08-10 19:15:31 -05:00
|
|
|
if !old_task.is_null() {
|
|
|
|
rustrt::rust_task_deref(old_task);
|
|
|
|
assert old_task == rustrt::rust_get_task();
|
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
terminated | full => {
|
2012-08-10 19:15:31 -05:00
|
|
|
assert p.header.blocked_task.is_null();
|
2012-06-29 20:15:28 -05:00
|
|
|
// I have to clean up, use drop_glue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
/** Returns when one of the packet headers reports data is available.
|
|
|
|
|
|
|
|
This function is primarily intended for building higher level waiting
|
|
|
|
functions, such as `select`, `select2`, etc.
|
|
|
|
|
|
|
|
It takes a vector slice of packet_headers and returns an index into
|
|
|
|
that vector. The index points to an endpoint that has either been
|
|
|
|
closed by the sender or has a message waiting to be received.
|
|
|
|
|
|
|
|
*/
|
2012-08-14 18:28:23 -05:00
|
|
|
fn wait_many<T: selectable>(pkts: &[T]) -> uint {
|
2012-07-03 19:33:20 -05:00
|
|
|
let this = rustrt::rust_get_task();
|
|
|
|
|
|
|
|
rustrt::task_clear_event_reject(this);
|
|
|
|
let mut data_avail = false;
|
|
|
|
let mut ready_packet = pkts.len();
|
2012-07-12 14:00:07 -05:00
|
|
|
for pkts.eachi |i, p| unsafe {
|
2012-08-14 18:28:23 -05:00
|
|
|
let p = unsafe { &*p.header() };
|
2012-07-03 19:33:20 -05:00
|
|
|
let old = p.mark_blocked(this);
|
2012-08-06 14:34:08 -05:00
|
|
|
match old {
|
2012-08-03 21:59:04 -05:00
|
|
|
full | terminated => {
|
2012-07-03 19:33:20 -05:00
|
|
|
data_avail = true;
|
|
|
|
ready_packet = i;
|
2012-07-12 14:00:07 -05:00
|
|
|
(*p).state = old;
|
2012-07-03 19:33:20 -05:00
|
|
|
break;
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
blocked => fail ~"blocking on blocked packet",
|
|
|
|
empty => ()
|
2012-07-03 19:33:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while !data_avail {
|
2012-07-30 18:01:07 -05:00
|
|
|
debug!{"sleeping on %? packets", pkts.len()};
|
2012-07-10 12:58:44 -05:00
|
|
|
let event = wait_event(this) as *packet_header;
|
2012-08-14 18:28:23 -05:00
|
|
|
let pos = vec::position(pkts, |p| p.header() == event);
|
2012-07-03 19:33:20 -05:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match pos {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(i) => {
|
2012-07-03 19:33:20 -05:00
|
|
|
ready_packet = i;
|
|
|
|
data_avail = true;
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
none => debug!{"ignoring spurious event, %?", event}
|
2012-07-03 19:33:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:01:07 -05:00
|
|
|
debug!{"%?", pkts[ready_packet]};
|
2012-07-03 19:33:20 -05:00
|
|
|
|
2012-08-14 18:28:23 -05:00
|
|
|
for pkts.each |p| { unsafe{ (*p.header()).unblock()} }
|
2012-07-03 19:33:20 -05:00
|
|
|
|
2012-08-03 13:43:10 -05:00
|
|
|
debug!("%?, %?", ready_packet, pkts[ready_packet]);
|
2012-07-03 19:33:20 -05:00
|
|
|
|
2012-07-12 14:00:07 -05:00
|
|
|
unsafe {
|
2012-08-14 18:28:23 -05:00
|
|
|
assert (*pkts[ready_packet].header()).state == full
|
|
|
|
|| (*pkts[ready_packet].header()).state == terminated;
|
2012-07-12 14:00:07 -05:00
|
|
|
}
|
2012-07-03 19:33:20 -05:00
|
|
|
|
|
|
|
ready_packet
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
/** Receives a message from one of two endpoints.
|
|
|
|
|
|
|
|
The return value is `left` if the first endpoint received something,
|
|
|
|
or `right` if the second endpoint receives something. In each case,
|
|
|
|
the result includes the other endpoint as well so it can be used
|
|
|
|
again. Below is an example of using `select2`.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
match select2(a, b) {
|
|
|
|
left((none, b)) {
|
|
|
|
// endpoint a was closed.
|
|
|
|
}
|
|
|
|
right((a, none)) {
|
|
|
|
// endpoint b was closed.
|
|
|
|
}
|
|
|
|
left((some(_), b)) {
|
|
|
|
// endpoint a received a message
|
|
|
|
}
|
|
|
|
right(a, some(_)) {
|
|
|
|
// endpoint b received a message.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Sometimes messages will be available on both endpoints at once. In
|
|
|
|
this case, `select2` may return either `left` or `right`.
|
|
|
|
|
|
|
|
*/
|
2012-07-23 20:50:53 -05:00
|
|
|
fn select2<A: send, Ab: send, B: send, Bb: send>(
|
|
|
|
+a: recv_packet_buffered<A, Ab>,
|
|
|
|
+b: recv_packet_buffered<B, Bb>)
|
2012-08-14 18:54:13 -05:00
|
|
|
-> Either<(option<A>, recv_packet_buffered<B, Bb>),
|
2012-07-23 20:50:53 -05:00
|
|
|
(recv_packet_buffered<A, Ab>, option<B>)>
|
2012-07-09 15:53:55 -05:00
|
|
|
{
|
2012-07-10 18:46:16 -05:00
|
|
|
let i = wait_many([a.header(), b.header()]/_);
|
2012-07-09 15:53:55 -05:00
|
|
|
|
|
|
|
unsafe {
|
2012-08-06 14:34:08 -05:00
|
|
|
match i {
|
2012-08-14 18:54:13 -05:00
|
|
|
0 => Left((try_recv(a), b)),
|
|
|
|
1 => Right((a, try_recv(b))),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail ~"select2 return an invalid packet"
|
2012-07-09 15:53:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-13 17:39:31 -05:00
|
|
|
trait selectable {
|
|
|
|
pure fn header() -> *packet_header;
|
|
|
|
}
|
|
|
|
|
2012-08-07 20:10:06 -05:00
|
|
|
impl *packet_header: selectable {
|
2012-08-06 20:29:05 -05:00
|
|
|
pure fn header() -> *packet_header { self }
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/// Returns the index of an endpoint that is ready to receive.
|
2012-07-13 17:39:31 -05:00
|
|
|
fn selecti<T: selectable>(endpoints: &[T]) -> uint {
|
2012-08-14 18:28:23 -05:00
|
|
|
wait_many(endpoints)
|
2012-07-10 18:46:16 -05:00
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/// Returns 0 or 1 depending on which endpoint is ready to receive
|
2012-08-14 18:54:13 -05:00
|
|
|
fn select2i<A: selectable, B: selectable>(a: &A, b: &B) -> Either<(), ()> {
|
2012-08-06 14:34:08 -05:00
|
|
|
match wait_many([a.header(), b.header()]/_) {
|
2012-08-14 18:54:13 -05:00
|
|
|
0 => Left(()),
|
|
|
|
1 => Right(()),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail ~"wait returned unexpected index"
|
2012-07-13 17:39:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/** Waits on a set of endpoints. Returns a message, its index, and a
|
|
|
|
list of the remaining endpoints.
|
|
|
|
|
|
|
|
*/
|
2012-07-23 20:50:53 -05:00
|
|
|
fn select<T: send, Tb: send>(+endpoints: ~[recv_packet_buffered<T, Tb>])
|
|
|
|
-> (uint, option<T>, ~[recv_packet_buffered<T, Tb>])
|
2012-07-03 19:33:20 -05:00
|
|
|
{
|
2012-07-10 18:46:16 -05:00
|
|
|
let ready = wait_many(endpoints.map(|p| p.header()));
|
2012-07-03 19:33:20 -05:00
|
|
|
let mut remaining = ~[];
|
|
|
|
let mut result = none;
|
|
|
|
do vec::consume(endpoints) |i, p| {
|
|
|
|
if i == ready {
|
2012-07-10 13:40:03 -05:00
|
|
|
result = try_recv(p);
|
2012-07-03 19:33:20 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
vec::push(remaining, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(ready, result, remaining)
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/** The sending end of a pipe. It can be used to send exactly one
|
|
|
|
message.
|
|
|
|
|
|
|
|
*/
|
2012-07-20 21:06:32 -05:00
|
|
|
type send_packet<T: send> = send_packet_buffered<T, packet<T>>;
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-20 21:06:32 -05:00
|
|
|
fn send_packet<T: send>(p: *packet<T>) -> send_packet<T> {
|
|
|
|
send_packet_buffered(p)
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
struct send_packet_buffered<T: send, Tbuffer: send> {
|
2012-06-29 20:15:28 -05:00
|
|
|
let mut p: option<*packet<T>>;
|
2012-07-20 21:06:32 -05:00
|
|
|
let mut buffer: option<buffer_resource<Tbuffer>>;
|
2012-06-29 20:15:28 -05:00
|
|
|
new(p: *packet<T>) {
|
2012-07-30 18:01:07 -05:00
|
|
|
//debug!{"take send %?", p};
|
2012-06-29 20:15:28 -05:00
|
|
|
self.p = some(p);
|
2012-07-20 21:06:32 -05:00
|
|
|
unsafe {
|
|
|
|
self.buffer = some(
|
|
|
|
buffer_resource(
|
|
|
|
get_buffer(ptr::addr_of((*p).header))));
|
|
|
|
};
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
drop {
|
|
|
|
//if self.p != none {
|
2012-07-30 18:01:07 -05:00
|
|
|
// debug!{"drop send %?", option::get(self.p)};
|
2012-06-29 20:15:28 -05:00
|
|
|
//}
|
|
|
|
if self.p != none {
|
|
|
|
let mut p = none;
|
|
|
|
p <-> self.p;
|
|
|
|
sender_terminate(option::unwrap(p))
|
|
|
|
}
|
2012-07-30 18:01:07 -05:00
|
|
|
//unsafe { error!{"send_drop: %?",
|
2012-07-24 00:14:43 -05:00
|
|
|
// if self.buffer == none {
|
|
|
|
// "none"
|
2012-07-30 18:01:07 -05:00
|
|
|
// } else { "some" }}; }
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
fn unwrap() -> *packet<T> {
|
|
|
|
let mut p = none;
|
|
|
|
p <-> self.p;
|
|
|
|
option::unwrap(p)
|
|
|
|
}
|
2012-07-20 21:06:32 -05:00
|
|
|
|
|
|
|
pure fn header() -> *packet_header {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.p {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(packet) => unsafe {
|
|
|
|
let packet = &*packet;
|
|
|
|
let header = ptr::addr_of(packet.header);
|
|
|
|
//forget(packet);
|
|
|
|
header
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2012-08-03 21:59:04 -05:00
|
|
|
none => fail ~"packet already consumed"
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-23 13:28:38 -05:00
|
|
|
|
|
|
|
fn reuse_buffer() -> buffer_resource<Tbuffer> {
|
2012-07-30 18:01:07 -05:00
|
|
|
//error!{"send reuse_buffer"};
|
2012-07-23 13:28:38 -05:00
|
|
|
let mut tmp = none;
|
|
|
|
tmp <-> self.buffer;
|
|
|
|
option::unwrap(tmp)
|
|
|
|
}
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
/// Represents the receive end of a pipe. It can receive exactly one
|
|
|
|
/// message.
|
2012-07-20 21:06:32 -05:00
|
|
|
type recv_packet<T: send> = recv_packet_buffered<T, packet<T>>;
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-20 21:06:32 -05:00
|
|
|
fn recv_packet<T: send>(p: *packet<T>) -> recv_packet<T> {
|
|
|
|
recv_packet_buffered(p)
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
struct recv_packet_buffered<T: send, Tbuffer: send> : selectable {
|
2012-06-29 20:15:28 -05:00
|
|
|
let mut p: option<*packet<T>>;
|
2012-07-20 21:06:32 -05:00
|
|
|
let mut buffer: option<buffer_resource<Tbuffer>>;
|
2012-06-29 20:15:28 -05:00
|
|
|
new(p: *packet<T>) {
|
2012-07-30 18:01:07 -05:00
|
|
|
//debug!{"take recv %?", p};
|
2012-06-29 20:15:28 -05:00
|
|
|
self.p = some(p);
|
2012-07-20 21:06:32 -05:00
|
|
|
unsafe {
|
|
|
|
self.buffer = some(
|
|
|
|
buffer_resource(
|
|
|
|
get_buffer(ptr::addr_of((*p).header))));
|
|
|
|
};
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
drop {
|
|
|
|
//if self.p != none {
|
2012-07-30 18:01:07 -05:00
|
|
|
// debug!{"drop recv %?", option::get(self.p)};
|
2012-06-29 20:15:28 -05:00
|
|
|
//}
|
|
|
|
if self.p != none {
|
|
|
|
let mut p = none;
|
|
|
|
p <-> self.p;
|
|
|
|
receiver_terminate(option::unwrap(p))
|
|
|
|
}
|
2012-07-30 18:01:07 -05:00
|
|
|
//unsafe { error!{"recv_drop: %?",
|
2012-07-24 00:14:43 -05:00
|
|
|
// if self.buffer == none {
|
|
|
|
// "none"
|
2012-07-30 18:01:07 -05:00
|
|
|
// } else { "some" }}; }
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
fn unwrap() -> *packet<T> {
|
|
|
|
let mut p = none;
|
|
|
|
p <-> self.p;
|
|
|
|
option::unwrap(p)
|
|
|
|
}
|
2012-07-09 17:29:23 -05:00
|
|
|
|
2012-07-12 14:00:07 -05:00
|
|
|
pure fn header() -> *packet_header {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.p {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(packet) => unsafe {
|
|
|
|
let packet = &*packet;
|
|
|
|
let header = ptr::addr_of(packet.header);
|
|
|
|
//forget(packet);
|
|
|
|
header
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2012-08-03 21:59:04 -05:00
|
|
|
none => fail ~"packet already consumed"
|
2012-07-09 17:29:23 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-23 13:28:38 -05:00
|
|
|
|
|
|
|
fn reuse_buffer() -> buffer_resource<Tbuffer> {
|
2012-07-30 18:01:07 -05:00
|
|
|
//error!{"recv reuse_buffer"};
|
2012-07-23 13:28:38 -05:00
|
|
|
let mut tmp = none;
|
|
|
|
tmp <-> self.buffer;
|
|
|
|
option::unwrap(tmp)
|
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-06-29 20:15:28 -05:00
|
|
|
fn entangle<T: send>() -> (send_packet<T>, recv_packet<T>) {
|
|
|
|
let p = packet();
|
|
|
|
(send_packet(p), recv_packet(p))
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/** Spawn a task to provide a service.
|
|
|
|
|
|
|
|
It takes an initialization function that produces a send and receive
|
|
|
|
endpoint. The send endpoint is returned to the caller and the receive
|
|
|
|
endpoint is passed to the new task.
|
|
|
|
|
|
|
|
*/
|
2012-07-23 20:50:53 -05:00
|
|
|
fn spawn_service<T: send, Tb: send>(
|
|
|
|
init: extern fn() -> (send_packet_buffered<T, Tb>,
|
|
|
|
recv_packet_buffered<T, Tb>),
|
|
|
|
+service: fn~(+recv_packet_buffered<T, Tb>))
|
|
|
|
-> send_packet_buffered<T, Tb>
|
2012-06-29 20:15:28 -05:00
|
|
|
{
|
|
|
|
let (client, server) = init();
|
|
|
|
|
|
|
|
// This is some nasty gymnastics required to safely move the pipe
|
|
|
|
// into a new task.
|
|
|
|
let server = ~mut some(server);
|
2012-07-02 13:38:45 -05:00
|
|
|
do task::spawn |move service| {
|
2012-06-29 20:15:28 -05:00
|
|
|
let mut server_ = none;
|
|
|
|
server_ <-> *server;
|
|
|
|
service(option::unwrap(server_))
|
|
|
|
}
|
|
|
|
|
|
|
|
client
|
|
|
|
}
|
2012-07-02 21:03:11 -05:00
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/** Like `spawn_service_recv`, but for protocols that start in the
|
|
|
|
receive state.
|
|
|
|
|
|
|
|
*/
|
2012-07-23 20:50:53 -05:00
|
|
|
fn spawn_service_recv<T: send, Tb: send>(
|
|
|
|
init: extern fn() -> (recv_packet_buffered<T, Tb>,
|
|
|
|
send_packet_buffered<T, Tb>),
|
|
|
|
+service: fn~(+send_packet_buffered<T, Tb>))
|
|
|
|
-> recv_packet_buffered<T, Tb>
|
2012-07-02 21:03:11 -05:00
|
|
|
{
|
|
|
|
let (client, server) = init();
|
|
|
|
|
|
|
|
// This is some nasty gymnastics required to safely move the pipe
|
|
|
|
// into a new task.
|
|
|
|
let server = ~mut some(server);
|
|
|
|
do task::spawn |move service| {
|
|
|
|
let mut server_ = none;
|
|
|
|
server_ <-> *server;
|
|
|
|
service(option::unwrap(server_))
|
|
|
|
}
|
|
|
|
|
|
|
|
client
|
|
|
|
}
|
2012-07-10 13:58:43 -05:00
|
|
|
|
|
|
|
// Streams - Make pipes a little easier in general.
|
|
|
|
|
|
|
|
proto! streamp {
|
|
|
|
open:send<T: send> {
|
|
|
|
data(T) -> open<T>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/// A trait for things that can send multiple messages.
|
2012-07-18 17:03:46 -05:00
|
|
|
trait channel<T: send> {
|
2012-08-06 12:33:31 -05:00
|
|
|
// It'd be nice to call this send, but it'd conflict with the
|
|
|
|
// built in send kind.
|
|
|
|
|
|
|
|
/// Sends a message.
|
2012-07-18 17:03:46 -05:00
|
|
|
fn send(+x: T);
|
2012-08-08 19:24:07 -05:00
|
|
|
|
|
|
|
/// Sends a message, or report if the receiver has closed the connection.
|
|
|
|
fn try_send(+x: T) -> bool;
|
2012-07-18 17:03:46 -05:00
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/// A trait for things that can receive multiple messages.
|
2012-07-18 17:03:46 -05:00
|
|
|
trait recv<T: send> {
|
2012-08-06 12:33:31 -05:00
|
|
|
/// Receives a message, or fails if the connection closes.
|
2012-07-18 17:03:46 -05:00
|
|
|
fn recv() -> T;
|
2012-08-06 12:33:31 -05:00
|
|
|
|
|
|
|
/** Receives a message if one is available, or returns `none` if
|
|
|
|
the connection is closed.
|
|
|
|
|
|
|
|
*/
|
2012-07-18 17:03:46 -05:00
|
|
|
fn try_recv() -> option<T>;
|
2012-08-06 12:33:31 -05:00
|
|
|
|
|
|
|
/** Returns true if a message is available or the connection is
|
|
|
|
closed.
|
|
|
|
|
|
|
|
*/
|
2012-07-18 17:03:46 -05:00
|
|
|
pure fn peek() -> bool;
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-11 14:45:54 -05:00
|
|
|
type chan_<T:send> = { mut endp: option<streamp::client::open<T>> };
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/// An endpoint that can send many messages.
|
2012-07-11 14:45:54 -05:00
|
|
|
enum chan<T:send> {
|
|
|
|
chan_(chan_<T>)
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-11 14:45:54 -05:00
|
|
|
type port_<T:send> = { mut endp: option<streamp::server::open<T>> };
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/// An endpoint that can receive many messages.
|
2012-07-11 14:45:54 -05:00
|
|
|
enum port<T:send> {
|
|
|
|
port_(port_<T>)
|
|
|
|
}
|
2012-07-10 13:58:43 -05:00
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/** Creates a `(chan, port)` pair.
|
|
|
|
|
|
|
|
These allow sending or receiving an unlimited number of messages.
|
|
|
|
|
|
|
|
*/
|
2012-07-10 13:58:43 -05:00
|
|
|
fn stream<T:send>() -> (chan<T>, port<T>) {
|
|
|
|
let (c, s) = streamp::init();
|
|
|
|
|
2012-07-11 14:45:54 -05:00
|
|
|
(chan_({ mut endp: some(c) }), port_({ mut endp: some(s) }))
|
2012-07-10 13:58:43 -05:00
|
|
|
}
|
|
|
|
|
2012-08-07 20:10:06 -05:00
|
|
|
impl<T: send> chan<T>: channel<T> {
|
2012-07-10 13:58:43 -05:00
|
|
|
fn send(+x: T) {
|
|
|
|
let mut endp = none;
|
|
|
|
endp <-> self.endp;
|
|
|
|
self.endp = some(
|
|
|
|
streamp::client::data(unwrap(endp), x))
|
|
|
|
}
|
2012-08-08 19:24:07 -05:00
|
|
|
|
|
|
|
fn try_send(+x: T) -> bool {
|
|
|
|
let mut endp = none;
|
|
|
|
endp <-> self.endp;
|
|
|
|
match move streamp::client::try_data(unwrap(endp), x) {
|
|
|
|
some(next) => {
|
|
|
|
self.endp = some(move_it!(next));
|
|
|
|
true
|
|
|
|
}
|
|
|
|
none => false
|
|
|
|
}
|
|
|
|
}
|
2012-07-10 13:58:43 -05:00
|
|
|
}
|
|
|
|
|
2012-08-07 20:10:06 -05:00
|
|
|
impl<T: send> port<T>: recv<T> {
|
2012-07-10 13:58:43 -05:00
|
|
|
fn recv() -> T {
|
|
|
|
let mut endp = none;
|
|
|
|
endp <-> self.endp;
|
|
|
|
let streamp::data(x, endp) = pipes::recv(unwrap(endp));
|
|
|
|
self.endp = some(endp);
|
|
|
|
x
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_recv() -> option<T> {
|
|
|
|
let mut endp = none;
|
|
|
|
endp <-> self.endp;
|
2012-08-06 14:34:08 -05:00
|
|
|
match move pipes::try_recv(unwrap(endp)) {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(streamp::data(x, endp)) => {
|
2012-07-31 19:31:24 -05:00
|
|
|
self.endp = some(move_it!{endp});
|
|
|
|
some(move_it!{x})
|
2012-07-10 13:58:43 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
none => none
|
2012-07-10 13:58:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pure fn peek() -> bool unchecked {
|
|
|
|
let mut endp = none;
|
|
|
|
endp <-> self.endp;
|
2012-08-06 14:34:08 -05:00
|
|
|
let peek = match endp {
|
2012-08-14 18:39:57 -05:00
|
|
|
some(endp) => pipes::peek(&endp),
|
2012-08-03 21:59:04 -05:00
|
|
|
none => fail ~"peeking empty stream"
|
2012-07-10 13:58:43 -05:00
|
|
|
};
|
|
|
|
self.endp <-> endp;
|
|
|
|
peek
|
|
|
|
}
|
|
|
|
}
|
2012-07-12 18:44:09 -05:00
|
|
|
|
|
|
|
// Treat a whole bunch of ports as one.
|
2012-08-01 13:56:46 -05:00
|
|
|
struct port_set<T: send> : recv<T> {
|
2012-07-12 18:44:09 -05:00
|
|
|
let mut ports: ~[pipes::port<T>];
|
|
|
|
|
|
|
|
new() { self.ports = ~[]; }
|
|
|
|
|
|
|
|
fn add(+port: pipes::port<T>) {
|
|
|
|
vec::push(self.ports, port)
|
|
|
|
}
|
|
|
|
|
2012-07-25 16:05:06 -05:00
|
|
|
fn chan() -> chan<T> {
|
|
|
|
let (ch, po) = stream();
|
|
|
|
self.add(po);
|
|
|
|
ch
|
|
|
|
}
|
|
|
|
|
2012-07-12 18:44:09 -05:00
|
|
|
fn try_recv() -> option<T> {
|
|
|
|
let mut result = none;
|
2012-08-02 20:55:31 -05:00
|
|
|
// we have to swap the ports array so we aren't borrowing
|
|
|
|
// aliasable mutable memory.
|
|
|
|
let mut ports = ~[];
|
|
|
|
ports <-> self.ports;
|
|
|
|
while result == none && ports.len() > 0 {
|
|
|
|
let i = wait_many(ports.map(|p| p.header()));
|
2012-08-06 14:34:08 -05:00
|
|
|
match move ports[i].try_recv() {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(copy m) => {
|
2012-08-02 20:55:31 -05:00
|
|
|
result = some(move m);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
none => {
|
2012-08-02 20:55:31 -05:00
|
|
|
// Remove this port.
|
|
|
|
let mut ports_ = ~[];
|
|
|
|
ports <-> ports_;
|
|
|
|
vec::consume(ports_,
|
|
|
|
|j, x| if i != j {
|
|
|
|
vec::push(ports, x)
|
|
|
|
});
|
|
|
|
}
|
2012-07-12 18:44:09 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-02 20:55:31 -05:00
|
|
|
ports <-> self.ports;
|
2012-07-12 18:44:09 -05:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn recv() -> T {
|
2012-08-02 17:40:36 -05:00
|
|
|
match move self.try_recv() {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(copy x) => move x,
|
|
|
|
none => fail ~"port_set: endpoints closed"
|
2012-08-02 17:40:36 -05:00
|
|
|
}
|
2012-07-12 18:44:09 -05:00
|
|
|
}
|
2012-07-18 17:03:46 -05:00
|
|
|
|
|
|
|
pure fn peek() -> bool {
|
|
|
|
// It'd be nice to use self.port.each, but that version isn't
|
|
|
|
// pure.
|
|
|
|
for vec::each(self.ports) |p| {
|
2012-08-01 19:30:05 -05:00
|
|
|
if p.peek() { return true }
|
2012-07-18 17:03:46 -05:00
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2012-07-12 18:44:09 -05:00
|
|
|
}
|
|
|
|
|
2012-08-07 20:10:06 -05:00
|
|
|
impl<T: send> port<T>: selectable {
|
2012-07-18 17:03:46 -05:00
|
|
|
pure fn header() -> *packet_header unchecked {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.endp {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(endp) => endp.header(),
|
|
|
|
none => fail ~"peeking empty stream"
|
2012-07-12 18:44:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/// A channel that can be shared between many senders.
|
2012-08-13 18:20:27 -05:00
|
|
|
type shared_chan<T: send> = unsafe::Exclusive<chan<T>>;
|
2012-07-11 17:00:40 -05:00
|
|
|
|
2012-08-07 20:10:06 -05:00
|
|
|
impl<T: send> shared_chan<T>: channel<T> {
|
2012-07-12 18:44:09 -05:00
|
|
|
fn send(+x: T) {
|
|
|
|
let mut xx = some(x);
|
2012-08-07 13:42:30 -05:00
|
|
|
do self.with |chan| {
|
2012-07-12 18:44:09 -05:00
|
|
|
let mut x = none;
|
|
|
|
x <-> xx;
|
|
|
|
chan.send(option::unwrap(x))
|
|
|
|
}
|
|
|
|
}
|
2012-08-08 19:24:07 -05:00
|
|
|
|
|
|
|
fn try_send(+x: T) -> bool {
|
|
|
|
let mut xx = some(x);
|
|
|
|
do self.with |chan| {
|
|
|
|
let mut x = none;
|
|
|
|
x <-> xx;
|
|
|
|
chan.try_send(option::unwrap(x))
|
|
|
|
}
|
|
|
|
}
|
2012-07-12 18:44:09 -05:00
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/// Converts a `chan` into a `shared_chan`.
|
2012-07-18 17:03:46 -05:00
|
|
|
fn shared_chan<T:send>(+c: chan<T>) -> shared_chan<T> {
|
2012-08-10 17:20:03 -05:00
|
|
|
unsafe::exclusive(c)
|
2012-07-12 18:44:09 -05:00
|
|
|
}
|
2012-07-25 16:46:15 -05:00
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/// Receive a message from one of two endpoints.
|
2012-07-25 16:46:15 -05:00
|
|
|
trait select2<T: send, U: send> {
|
2012-08-06 12:33:31 -05:00
|
|
|
/// Receive a message or return `none` if a connection closes.
|
2012-08-14 18:54:13 -05:00
|
|
|
fn try_select() -> Either<option<T>, option<U>>;
|
2012-08-06 12:33:31 -05:00
|
|
|
/// Receive a message or fail if a connection closes.
|
2012-08-14 18:54:13 -05:00
|
|
|
fn select() -> Either<T, U>;
|
2012-07-25 16:46:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: send, U: send, Left: selectable recv<T>, Right: selectable recv<U>>
|
2012-08-07 20:10:06 -05:00
|
|
|
(Left, Right): select2<T, U> {
|
2012-07-25 16:46:15 -05:00
|
|
|
|
2012-08-14 18:54:13 -05:00
|
|
|
fn select() -> Either<T, U> {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self {
|
2012-08-14 18:39:57 -05:00
|
|
|
(lp, rp) => match select2i(&lp, &rp) {
|
2012-08-14 18:54:13 -05:00
|
|
|
Left(()) => Left (lp.recv()),
|
|
|
|
Right(()) => Right(rp.recv())
|
2012-07-25 16:46:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 18:54:13 -05:00
|
|
|
fn try_select() -> Either<option<T>, option<U>> {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self {
|
2012-08-14 18:39:57 -05:00
|
|
|
(lp, rp) => match select2i(&lp, &rp) {
|
2012-08-14 18:54:13 -05:00
|
|
|
Left(()) => Left (lp.try_recv()),
|
|
|
|
Right(()) => Right(rp.try_recv())
|
2012-07-25 16:46:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-09 13:54:56 -05:00
|
|
|
proto! oneshot {
|
|
|
|
oneshot:send<T:send> {
|
|
|
|
send(T) -> !
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 19:48:37 -05:00
|
|
|
/// The send end of a oneshot pipe.
|
|
|
|
type chan_one<T: send> = oneshot::client::oneshot<T>;
|
|
|
|
/// The receive end of a oneshot pipe.
|
|
|
|
type port_one<T: send> = oneshot::server::oneshot<T>;
|
|
|
|
|
2012-08-13 16:57:00 -05:00
|
|
|
/// Initialiase a (send-endpoint, recv-endpoint) oneshot pipe pair.
|
2012-08-14 19:48:37 -05:00
|
|
|
fn oneshot<T: send>() -> (chan_one<T>, port_one<T>) {
|
2012-08-13 16:57:00 -05:00
|
|
|
oneshot::init()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive a message from a oneshot pipe, failing if the connection was
|
|
|
|
* closed.
|
|
|
|
*/
|
2012-08-14 19:48:37 -05:00
|
|
|
fn recv_one<T: send>(+port: port_one<T>) -> T {
|
2012-08-09 13:54:56 -05:00
|
|
|
let oneshot::send(message) = recv(port);
|
|
|
|
message
|
|
|
|
}
|
|
|
|
|
2012-08-13 16:57:00 -05:00
|
|
|
/// Receive a message from a oneshot pipe unless the connection was closed.
|
2012-08-14 19:48:37 -05:00
|
|
|
fn try_recv_one<T: send> (+port: port_one<T>) -> option<T> {
|
2012-08-09 13:54:56 -05:00
|
|
|
let message = try_recv(port);
|
|
|
|
|
|
|
|
if message == none { none }
|
|
|
|
else {
|
|
|
|
let oneshot::send(message) = option::unwrap(message);
|
|
|
|
some(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-13 16:57:00 -05:00
|
|
|
/// Send a message on a oneshot pipe, failing if the connection was closed.
|
2012-08-14 19:48:37 -05:00
|
|
|
fn send_one<T: send>(+chan: chan_one<T>, +data: T) {
|
2012-08-13 16:57:00 -05:00
|
|
|
oneshot::client::send(chan, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a message on a oneshot pipe, or return false if the connection was
|
|
|
|
* closed.
|
|
|
|
*/
|
2012-08-14 19:48:37 -05:00
|
|
|
fn try_send_one<T: send>(+chan: chan_one<T>, +data: T)
|
2012-08-13 16:57:00 -05:00
|
|
|
-> bool {
|
|
|
|
oneshot::client::try_send(chan, data).is_some()
|
|
|
|
}
|
|
|
|
|
2012-08-15 18:22:40 -05:00
|
|
|
mod rt {
|
|
|
|
// These are used to hide the option constructors from the
|
|
|
|
// compiler because their names are changing
|
|
|
|
fn make_some<T>(+val: T) -> option<T> { some(val) }
|
|
|
|
fn make_none<T>() -> option<T> { none }
|
|
|
|
}
|
|
|
|
|
2012-07-25 16:46:15 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
#[test]
|
|
|
|
fn test_select2() {
|
|
|
|
let (c1, p1) = pipes::stream();
|
|
|
|
let (c2, p2) = pipes::stream();
|
|
|
|
|
2012-08-03 14:04:08 -05:00
|
|
|
c1.send(~"abc");
|
2012-07-25 16:46:15 -05:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match (p1, p2).select() {
|
2012-08-14 18:54:13 -05:00
|
|
|
Right(_) => fail,
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2012-07-25 16:46:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
c2.send(123);
|
|
|
|
}
|
2012-08-09 13:54:56 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_oneshot() {
|
|
|
|
let (c, p) = oneshot::init();
|
|
|
|
|
|
|
|
oneshot::client::send(c, ());
|
|
|
|
|
|
|
|
recv_one(p)
|
|
|
|
}
|
2012-07-25 16:46:15 -05:00
|
|
|
}
|