2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
~~~
|
2012-08-22 20:10:48 -05:00
|
|
|
proto! pingpong (
|
2012-08-01 13:56:46 -05:00
|
|
|
ping: send {
|
|
|
|
ping -> pong
|
|
|
|
}
|
|
|
|
pong: recv {
|
|
|
|
pong -> ping
|
|
|
|
}
|
2012-08-22 20:10:48 -05:00
|
|
|
)
|
2012-08-01 13:56:46 -05:00
|
|
|
~~~
|
|
|
|
|
|
|
|
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.
|
2012-10-03 16:52:09 -05:00
|
|
|
// tjc: allowing deprecated modes due to function issue,
|
|
|
|
// re-forbid after snapshot
|
2012-08-14 18:39:57 -05:00
|
|
|
#[forbid(deprecated_pattern)];
|
|
|
|
|
2013-01-26 01:02:27 -06:00
|
|
|
// Transitional -- needs snapshot
|
|
|
|
#[allow(structural_records)];
|
|
|
|
|
2012-09-04 13:12:17 -05:00
|
|
|
use cmp::Eq;
|
2012-09-18 19:34:08 -05:00
|
|
|
use cast::{forget, reinterpret_cast, transmute};
|
2012-09-04 13:12:17 -05:00
|
|
|
use either::{Either, Left, Right};
|
2013-01-08 21:37:25 -06:00
|
|
|
use kinds::Owned;
|
2012-12-23 16:41:37 -06:00
|
|
|
use libc;
|
|
|
|
use option;
|
2013-01-25 18:57:39 -06:00
|
|
|
use option::{None, Option, Some, unwrap};
|
2012-12-23 16:41:37 -06:00
|
|
|
use pipes;
|
|
|
|
use ptr;
|
|
|
|
use private;
|
|
|
|
use task;
|
|
|
|
use vec;
|
2012-07-12 18:44:09 -05:00
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-07-24 11:35:44 -05:00
|
|
|
const SPIN_COUNT: uint = 0;
|
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
macro_rules! move_it (
|
2012-11-19 22:53:52 -06:00
|
|
|
{ $x:expr } => ( unsafe { let y = move *ptr::addr_of(&($x)); move y } )
|
2012-08-22 19:24:52 -05:00
|
|
|
)
|
2012-07-12 18:44:09 -05:00
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-08-28 13:11:15 -05:00
|
|
|
enum State {
|
|
|
|
Empty,
|
|
|
|
Full,
|
|
|
|
Blocked,
|
|
|
|
Terminated
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
|
2012-09-19 20:00:26 -05:00
|
|
|
impl State : Eq {
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn eq(&self, other: &State) -> bool {
|
|
|
|
((*self) as uint) == ((*other) as uint)
|
|
|
|
}
|
|
|
|
pure fn ne(&self, other: &State) -> bool { !(*self).eq(other) }
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2012-09-26 18:26:19 -05:00
|
|
|
pub struct BufferHeader {
|
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.
|
2012-09-06 21:40:15 -05:00
|
|
|
mut ref_count: int,
|
2012-07-20 21:06:32 -05:00
|
|
|
|
|
|
|
// We may want a drop, and to be careful about stringing this
|
|
|
|
// thing along.
|
|
|
|
}
|
|
|
|
|
2013-01-24 13:37:44 -06:00
|
|
|
pub fn BufferHeader() -> BufferHeader {
|
2012-09-04 17:23:28 -05:00
|
|
|
BufferHeader {
|
|
|
|
ref_count: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-20 21:06:32 -05:00
|
|
|
// This is for protocols to associate extra data to thread around.
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2013-01-24 13:37:36 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
type Buffer<T: Owned> = {
|
2012-08-28 13:11:15 -05:00
|
|
|
header: BufferHeader,
|
2012-07-20 21:06:32 -05:00
|
|
|
data: T,
|
2012-07-03 19:33:20 -05:00
|
|
|
};
|
2013-01-24 13:37:36 -06:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
#[cfg(stage3)]
|
2013-01-28 12:46:43 -06:00
|
|
|
pub struct Buffer<T> {
|
2013-01-24 13:37:36 -06:00
|
|
|
header: BufferHeader,
|
|
|
|
data: T,
|
|
|
|
}
|
2012-07-03 19:33:20 -05:00
|
|
|
|
2012-08-28 13:11:15 -05:00
|
|
|
struct PacketHeader {
|
2012-09-06 21:40:15 -05:00
|
|
|
mut state: State,
|
|
|
|
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.
|
2012-09-06 21:40:15 -05:00
|
|
|
mut buffer: *libc::c_void,
|
2012-09-07 21:04:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn PacketHeader() -> PacketHeader {
|
|
|
|
PacketHeader {
|
|
|
|
state: Empty,
|
|
|
|
blocked_task: ptr::null(),
|
|
|
|
buffer: ptr::null()
|
|
|
|
}
|
|
|
|
}
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2012-09-07 21:04:40 -05:00
|
|
|
impl PacketHeader {
|
2012-07-20 21:06:32 -05:00
|
|
|
// Returns the old state.
|
2012-08-28 13:11:15 -05:00
|
|
|
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-28 13:11:15 -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());
|
2013-01-10 23:23:07 -06:00
|
|
|
if !old_task.is_null() {
|
|
|
|
unsafe {
|
|
|
|
rustrt::rust_task_deref(old_task)
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 13:11:15 -05:00
|
|
|
match swap_state_acq(&mut self.state, Empty) {
|
|
|
|
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.
|
2012-08-28 13:11:15 -05:00
|
|
|
unsafe fn buf_header() -> ~BufferHeader {
|
2012-07-20 21:06:32 -05:00
|
|
|
assert self.buffer.is_not_null();
|
2012-08-29 18:00:36 -05:00
|
|
|
reinterpret_cast(&self.buffer)
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
2012-07-23 13:28:38 -05:00
|
|
|
|
2013-01-23 13:43:58 -06:00
|
|
|
fn set_buffer<T: Owned>(b: ~Buffer<T>) {
|
|
|
|
unsafe {
|
|
|
|
self.buffer = reinterpret_cast(&b);
|
|
|
|
}
|
2012-07-23 13:28:38 -05:00
|
|
|
}
|
2012-07-11 14:45:54 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2013-01-24 12:43:33 -06:00
|
|
|
pub struct Packet<T: Owned> {
|
2012-08-28 13:11:15 -05:00
|
|
|
header: PacketHeader,
|
2012-08-20 14:23:37 -05:00
|
|
|
mut payload: Option<T>,
|
2013-01-24 12:43:33 -06:00
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub struct Packet<T> {
|
|
|
|
header: PacketHeader,
|
|
|
|
mut payload: Option<T>,
|
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
|
2012-08-28 13:11:15 -05:00
|
|
|
#[doc(hidden)]
|
2012-09-26 18:26:19 -05:00
|
|
|
pub trait HasBuffer {
|
2013-01-04 16:48:19 -06:00
|
|
|
fn set_buffer(b: *libc::c_void);
|
|
|
|
// FIXME #4421 remove after snapshot
|
2012-08-28 13:11:15 -05:00
|
|
|
fn set_buffer_(b: *libc::c_void);
|
|
|
|
}
|
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned> Packet<T>: HasBuffer {
|
2013-01-04 16:48:19 -06:00
|
|
|
fn set_buffer(b: *libc::c_void) {
|
2012-08-28 13:11:15 -05:00
|
|
|
self.header.buffer = b;
|
|
|
|
}
|
2013-01-04 16:48:19 -06:00
|
|
|
// FIXME #4421 remove after snapshot
|
|
|
|
fn set_buffer_(b: *libc::c_void) { self.set_buffer(b) }
|
2012-08-28 13:11:15 -05:00
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
#[doc(hidden)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn mk_packet<T: Owned>() -> Packet<T> {
|
2013-01-24 12:43:33 -06:00
|
|
|
Packet {
|
2012-08-28 13:11:15 -05:00
|
|
|
header: PacketHeader(),
|
2013-01-24 12:43:33 -06:00
|
|
|
payload: None,
|
2012-07-23 13:28:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
#[doc(hidden)]
|
2013-01-24 13:37:36 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
fn unibuffer<T: Owned>() -> ~Buffer<Packet<T>> {
|
2012-07-20 21:06:32 -05:00
|
|
|
let b = ~{
|
2012-08-28 13:11:15 -05:00
|
|
|
header: BufferHeader(),
|
2013-01-24 12:43:33 -06:00
|
|
|
data: Packet {
|
2012-08-28 13:11:15 -05:00
|
|
|
header: PacketHeader(),
|
2013-01-24 13:37:36 -06:00
|
|
|
payload: None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
b.data.header.buffer = reinterpret_cast(&b);
|
|
|
|
}
|
|
|
|
move b
|
|
|
|
}
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
#[cfg(stage3)]
|
2013-01-28 12:46:43 -06:00
|
|
|
fn unibuffer<T>() -> ~Buffer<Packet<T>> {
|
2013-01-24 13:37:36 -06:00
|
|
|
let b = ~Buffer {
|
|
|
|
header: BufferHeader(),
|
|
|
|
data: Packet {
|
|
|
|
header: PacketHeader(),
|
2013-01-24 12:43:33 -06:00
|
|
|
payload: None,
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
2012-08-29 18:00:36 -05:00
|
|
|
b.data.header.buffer = reinterpret_cast(&b);
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
2012-09-10 16:57:23 -05:00
|
|
|
move b
|
2012-07-11 14:45:54 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn packet<T: Owned>() -> *Packet<T> {
|
2012-07-20 21:06:32 -05:00
|
|
|
let b = unibuffer();
|
2012-10-01 14:47:02 -05:00
|
|
|
let p = ptr::addr_of(&(b.data));
|
2012-07-20 21:06:32 -05:00
|
|
|
// We'll take over memory management from here.
|
2012-09-11 19:17:54 -05:00
|
|
|
unsafe { forget(move b) }
|
2012-06-29 20:15:28 -05:00
|
|
|
p
|
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub fn packet<T>() -> *Packet<T> {
|
|
|
|
let b = unibuffer();
|
|
|
|
let p = ptr::addr_of(&(b.data));
|
|
|
|
// We'll take over memory management from here.
|
|
|
|
unsafe { forget(move b) }
|
|
|
|
p
|
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn entangle_buffer<T: Owned, Tstart: Owned>(
|
2012-10-02 13:37:37 -05:00
|
|
|
buffer: ~Buffer<T>,
|
2012-08-28 13:11:15 -05:00
|
|
|
init: fn(*libc::c_void, x: &T) -> *Packet<Tstart>)
|
|
|
|
-> (SendPacketBuffered<Tstart, T>, RecvPacketBuffered<Tstart, T>)
|
2012-07-23 15:50:12 -05:00
|
|
|
{
|
2012-08-29 18:00:36 -05:00
|
|
|
let p = init(unsafe { reinterpret_cast(&buffer) }, &buffer.data);
|
2012-09-11 19:17:54 -05:00
|
|
|
unsafe { forget(move buffer) }
|
2012-08-28 13:11:15 -05:00
|
|
|
(SendPacketBuffered(p), RecvPacketBuffered(p))
|
2012-07-23 15:50:12 -05:00
|
|
|
}
|
|
|
|
|
2012-09-20 17:59:09 -05:00
|
|
|
#[abi = "rust-intrinsic"]
|
|
|
|
#[doc(hidden)]
|
|
|
|
extern mod rusti {
|
2012-10-02 13:37:37 -05:00
|
|
|
fn atomic_xchg(dst: &mut int, src: int) -> int;
|
|
|
|
fn atomic_xchg_acq(dst: &mut int, src: int) -> int;
|
|
|
|
fn atomic_xchg_rel(dst: &mut int, src: int) -> int;
|
2012-09-20 17:59:09 -05:00
|
|
|
|
2012-10-02 13:37:37 -05:00
|
|
|
fn atomic_xadd_acq(dst: &mut int, src: int) -> int;
|
|
|
|
fn atomic_xsub_rel(dst: &mut 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-09-26 18:26:19 -05:00
|
|
|
pub fn atomic_xchng_rel(dst: &mut int, src: int) -> int {
|
2013-01-23 18:29:31 -06:00
|
|
|
unsafe {
|
|
|
|
rusti::atomic_xchg_rel(dst, src)
|
|
|
|
}
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-09-26 18:26:19 -05:00
|
|
|
pub fn atomic_add_acq(dst: &mut int, src: int) -> int {
|
2013-01-23 18:29:31 -06:00
|
|
|
unsafe {
|
|
|
|
rusti::atomic_xadd_acq(dst, src)
|
|
|
|
}
|
2012-07-23 13:28:38 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-09-26 18:26:19 -05:00
|
|
|
pub fn atomic_sub_rel(dst: &mut int, src: int) -> int {
|
2013-01-23 18:29:31 -06:00
|
|
|
unsafe {
|
|
|
|
rusti::atomic_xsub_rel(dst, src)
|
|
|
|
}
|
2012-07-23 13:28:38 -05:00
|
|
|
}
|
|
|
|
|
2012-08-03 20:57:43 -05:00
|
|
|
#[doc(hidden)]
|
2012-10-02 13:37:37 -05:00
|
|
|
pub 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 {
|
2012-09-11 19:17:54 -05:00
|
|
|
transmute(rusti::atomic_xchg(transmute(move dst), src as int))
|
2012-08-03 20:57:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-08-28 13:11:15 -05:00
|
|
|
#[allow(non_camel_case_types)]
|
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]
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe fn rust_get_task() -> *rust_task;
|
2012-08-03 20:57:43 -05:00
|
|
|
#[rust_stack]
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe fn rust_task_ref(task: *rust_task);
|
|
|
|
unsafe fn rust_task_deref(task: *rust_task);
|
2012-07-02 19:42:58 -05:00
|
|
|
|
|
|
|
#[rust_stack]
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe fn task_clear_event_reject(task: *rust_task);
|
2012-07-02 19:42:58 -05:00
|
|
|
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe fn task_wait_event(this: *rust_task, killed: &mut *libc::c_void)
|
|
|
|
-> bool;
|
|
|
|
unsafe 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 {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
let mut event = ptr::null();
|
2012-07-10 12:58:44 -05:00
|
|
|
|
2013-01-10 23:23:07 -06:00
|
|
|
let killed = rustrt::task_wait_event(this, &mut event);
|
|
|
|
if killed && !task::failing() {
|
2013-01-31 19:51:01 -06:00
|
|
|
die!(~"killed")
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
|
|
|
event
|
2012-07-10 12:58:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-10-02 13:37:37 -05:00
|
|
|
fn swap_state_acq(dst: &mut State, src: State) -> State {
|
2012-06-29 20:15:28 -05:00
|
|
|
unsafe {
|
2012-09-11 19:17:54 -05:00
|
|
|
transmute(rusti::atomic_xchg_acq(transmute(move dst), src as int))
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2012-10-02 13:37:37 -05:00
|
|
|
fn swap_state_rel(dst: &mut State, src: State) -> State {
|
2012-06-29 20:15:28 -05:00
|
|
|
unsafe {
|
2012-09-11 19:17:54 -05:00
|
|
|
transmute(rusti::atomic_xchg_rel(transmute(move dst), src as int))
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub unsafe fn get_buffer<T: Owned>(p: *PacketHeader) -> ~Buffer<T> {
|
2012-07-20 21:06:32 -05:00
|
|
|
transmute((*p).buf_header())
|
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub unsafe fn get_buffer<T>(p: *PacketHeader) -> ~Buffer<T> {
|
|
|
|
transmute((*p).buf_header())
|
|
|
|
}
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2012-08-24 18:26:41 -05:00
|
|
|
// This could probably be done with SharedMutableState to avoid move_it!().
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
struct BufferResource<T: Owned> {
|
2012-09-06 21:40:15 -05:00
|
|
|
buffer: ~Buffer<T>,
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2013-01-23 13:43:58 -06:00
|
|
|
drop {
|
|
|
|
unsafe {
|
|
|
|
let b = move_it!(self.buffer);
|
|
|
|
//let p = ptr::addr_of(*b);
|
|
|
|
//error!("drop %?", p);
|
|
|
|
let old_count = atomic_sub_rel(&mut b.header.ref_count, 1);
|
|
|
|
//let old_count = atomic_xchng_rel(b.header.ref_count, 0);
|
|
|
|
if old_count == 1 {
|
|
|
|
// The new count is 0.
|
|
|
|
|
|
|
|
// go go gadget drop glue
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
forget(move b)
|
|
|
|
}
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
struct BufferResource<T> {
|
|
|
|
buffer: ~Buffer<T>,
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2013-01-28 12:46:43 -06:00
|
|
|
drop {
|
|
|
|
unsafe {
|
|
|
|
let b = move_it!(self.buffer);
|
|
|
|
//let p = ptr::addr_of(*b);
|
|
|
|
//error!("drop %?", p);
|
|
|
|
let old_count = atomic_sub_rel(&mut b.header.ref_count, 1);
|
|
|
|
//let old_count = atomic_xchng_rel(b.header.ref_count, 0);
|
|
|
|
if old_count == 1 {
|
|
|
|
// The new count is 0.
|
|
|
|
|
|
|
|
// go go gadget drop glue
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
forget(move b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
fn BufferResource<T: Owned>(b: ~Buffer<T>) -> BufferResource<T> {
|
2012-09-04 17:23:28 -05:00
|
|
|
//let p = ptr::addr_of(*b);
|
|
|
|
//error!("take %?", p);
|
|
|
|
atomic_add_acq(&mut b.header.ref_count, 1);
|
|
|
|
|
|
|
|
BufferResource {
|
2012-09-19 00:35:28 -05:00
|
|
|
// tjc: ????
|
|
|
|
buffer: move b
|
2012-09-04 17:23:28 -05:00
|
|
|
}
|
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
fn BufferResource<T>(b: ~Buffer<T>) -> BufferResource<T> {
|
|
|
|
//let p = ptr::addr_of(*b);
|
|
|
|
//error!("take %?", p);
|
|
|
|
atomic_add_acq(&mut b.header.ref_count, 1);
|
|
|
|
|
|
|
|
BufferResource {
|
|
|
|
// tjc: ????
|
|
|
|
buffer: move b
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 17:23:28 -05:00
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn send<T: Owned, Tbuffer: Owned>(p: SendPacketBuffered<T, Tbuffer>,
|
2012-10-02 13:37:37 -05:00
|
|
|
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_ };
|
2012-10-01 14:47:02 -05:00
|
|
|
assert ptr::addr_of(&(p.header)) == header;
|
2012-08-27 19:43:15 -05:00
|
|
|
assert p.payload.is_none();
|
2012-10-23 13:11:23 -05:00
|
|
|
p.payload = move Some(move payload);
|
2012-08-28 13:11:15 -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-28 13:11:15 -05:00
|
|
|
Empty => {
|
2012-08-07 13:46:52 -05:00
|
|
|
// 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;
|
|
|
|
}
|
2013-01-31 19:51:01 -06:00
|
|
|
Full => die!(~"duplicate send"),
|
2012-08-28 13:11:15 -05:00
|
|
|
Blocked => {
|
2012-08-22 19:24:52 -05:00
|
|
|
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() {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
rustrt::task_signal_event(
|
|
|
|
old_task,
|
|
|
|
ptr::addr_of(&(p.header)) as *libc::c_void);
|
|
|
|
rustrt::rust_task_deref(old_task);
|
|
|
|
}
|
2012-08-07 13:46:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// The receiver will eventually clean this up.
|
|
|
|
//unsafe { forget(p); }
|
|
|
|
return true;
|
|
|
|
}
|
2012-08-28 13:11:15 -05:00
|
|
|
Terminated => {
|
2012-08-07 13:46:52 -05:00
|
|
|
// The receiver will never receive this. Rely on drop_glue
|
|
|
|
// to clean everything up.
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub fn send<T,Tbuffer>(p: SendPacketBuffered<T,Tbuffer>, payload: T) -> bool {
|
|
|
|
let header = p.header();
|
|
|
|
let p_ = p.unwrap();
|
|
|
|
let p = unsafe { &*p_ };
|
|
|
|
assert ptr::addr_of(&(p.header)) == header;
|
|
|
|
assert p.payload.is_none();
|
|
|
|
p.payload = move Some(move payload);
|
|
|
|
let old_state = swap_state_rel(&mut p.header.state, Full);
|
|
|
|
match old_state {
|
|
|
|
Empty => {
|
|
|
|
// Yay, fastpath.
|
|
|
|
|
|
|
|
// The receiver will eventually clean this up.
|
|
|
|
//unsafe { forget(p); }
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Full => fail ~"duplicate send",
|
|
|
|
Blocked => {
|
|
|
|
debug!("waking up task for %?", p_);
|
|
|
|
let old_task = swap_task(&mut p.header.blocked_task, ptr::null());
|
|
|
|
if !old_task.is_null() {
|
|
|
|
unsafe {
|
|
|
|
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-12-11 15:50:04 -06:00
|
|
|
pub fn recv<T: Owned, Tbuffer: Owned>(
|
|
|
|
p: RecvPacketBuffered<T, Tbuffer>) -> T {
|
2012-12-18 20:55:19 -06:00
|
|
|
try_recv(move p).expect("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.
|
|
|
|
|
2012-11-29 17:18:31 -06:00
|
|
|
Returns `None` if the sender has closed the connection without sending
|
2012-08-20 14:23:37 -05:00
|
|
|
a message, or `Some(T)` if a message was received.
|
2012-08-01 13:56:46 -05:00
|
|
|
|
|
|
|
*/
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn try_recv<T: Owned, Tbuffer: Owned>(p: RecvPacketBuffered<T, Tbuffer>)
|
2012-08-20 14:23:37 -05:00
|
|
|
-> Option<T>
|
2012-07-23 13:28:38 -05:00
|
|
|
{
|
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-28 13:11:15 -05:00
|
|
|
struct DropState {
|
2012-09-07 16:50:47 -05:00
|
|
|
p: &PacketHeader,
|
2012-08-10 18:17:53 -05:00
|
|
|
|
|
|
|
drop {
|
|
|
|
if task::failing() {
|
2012-08-28 13:11:15 -05:00
|
|
|
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() {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
rustrt::rust_task_deref(old_task);
|
|
|
|
}
|
2012-08-10 18:17:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-28 13:11:15 -05:00
|
|
|
let _drop_state = DropState { p: &p.header };
|
2012-08-10 18:17:53 -05:00
|
|
|
|
2012-08-07 18:54:00 -05:00
|
|
|
// optimistic path
|
|
|
|
match p.header.state {
|
2012-08-28 13:11:15 -05:00
|
|
|
Full => {
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut payload = None;
|
2012-08-07 18:54:00 -05:00
|
|
|
payload <-> p.payload;
|
2012-08-28 13:11:15 -05:00
|
|
|
p.header.state = Empty;
|
2012-09-11 19:17:54 -05:00
|
|
|
return Some(option::unwrap(move payload))
|
2012-08-07 18:54:00 -05:00
|
|
|
},
|
2012-08-28 13:11:15 -05:00
|
|
|
Terminated => return None,
|
2012-08-07 18:54:00 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// regular path
|
2013-01-10 23:23:07 -06:00
|
|
|
let this = unsafe { rustrt::rust_get_task() };
|
|
|
|
unsafe {
|
|
|
|
rustrt::task_clear_event_reject(this);
|
|
|
|
rustrt::rust_task_ref(this);
|
|
|
|
};
|
2012-09-19 00:35:28 -05:00
|
|
|
debug!("blocked = %x this = %x", p.header.blocked_task as uint,
|
|
|
|
this as uint);
|
2012-08-14 18:39:57 -05:00
|
|
|
let old_task = swap_task(&mut p.header.blocked_task, this);
|
2012-09-19 00:35:28 -05:00
|
|
|
debug!("blocked = %x this = %x old_task = %x",
|
|
|
|
p.header.blocked_task as uint,
|
|
|
|
this as uint, old_task as uint);
|
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 {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
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-08-28 13:11:15 -05:00
|
|
|
Blocked);
|
2012-08-06 14:34:08 -05:00
|
|
|
match old_state {
|
2012-08-28 13:11:15 -05:00
|
|
|
Empty => {
|
2012-08-22 19:24:52 -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-08-22 19:24:52 -05:00
|
|
|
debug!("woke up, p.state = %?", copy p.header.state);
|
2012-07-10 12:58:44 -05:00
|
|
|
}
|
2012-08-28 13:11:15 -05:00
|
|
|
Blocked => if first {
|
2013-01-31 19:51:01 -06:00
|
|
|
die!(~"blocking on already blocked packet")
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2012-08-28 13:11:15 -05:00
|
|
|
Full => {
|
2012-08-20 14:23:37 -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() {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
rustrt::rust_task_deref(old_task);
|
|
|
|
}
|
2012-08-03 20:57:43 -05:00
|
|
|
}
|
2012-08-28 13:11:15 -05:00
|
|
|
p.header.state = Empty;
|
2012-09-11 19:17:54 -05:00
|
|
|
return Some(option::unwrap(move payload))
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-08-28 13:11:15 -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-08-28 13:11:15 -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() {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
rustrt::rust_task_deref(old_task);
|
|
|
|
}
|
2012-08-03 20:57:43 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -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-12-11 15:50:04 -06:00
|
|
|
pub pure fn peek<T: Owned, Tb: Owned>(p: &RecvPacketBuffered<T, Tb>) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match unsafe {(*p.header()).state} {
|
2012-11-02 02:30:13 -05:00
|
|
|
Empty | Terminated => false,
|
2013-01-31 19:51:01 -06:00
|
|
|
Blocked => die!(~"peeking on blocked packet"),
|
2012-11-02 02:30:13 -05:00
|
|
|
Full => true
|
2012-07-09 17:29:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned, Tb: Owned> RecvPacketBuffered<T, Tb>: Peekable<T> {
|
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-12-11 15:50:04 -06:00
|
|
|
fn sender_terminate<T: Owned>(p: *Packet<T>) {
|
2012-07-20 21:06:32 -05:00
|
|
|
let p = unsafe { &*p };
|
2012-08-28 13:11:15 -05:00
|
|
|
match swap_state_rel(&mut p.header.state, Terminated) {
|
|
|
|
Empty => {
|
2012-07-09 18:57:47 -05:00
|
|
|
// The receiver will eventually clean up.
|
|
|
|
}
|
2012-08-28 13:11:15 -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() {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
rustrt::task_signal_event(
|
|
|
|
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-28 13:11:15 -05:00
|
|
|
Full => {
|
2012-06-29 20:15:28 -05:00
|
|
|
// This is impossible
|
2013-01-31 19:51:01 -06:00
|
|
|
die!(~"you dun goofed")
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-08-28 13:11:15 -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-12-11 15:50:04 -06:00
|
|
|
fn receiver_terminate<T: Owned>(p: *Packet<T>) {
|
2012-07-20 21:06:32 -05:00
|
|
|
let p = unsafe { &*p };
|
2012-08-28 13:11:15 -05:00
|
|
|
match swap_state_rel(&mut p.header.state, Terminated) {
|
|
|
|
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-28 13:11:15 -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() {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
rustrt::rust_task_deref(old_task);
|
|
|
|
assert old_task == rustrt::rust_get_task();
|
|
|
|
}
|
2012-08-10 19:15:31 -05:00
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-08-28 13:11:15 -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-28 13:11:15 -05:00
|
|
|
fn wait_many<T: Selectable>(pkts: &[T]) -> uint {
|
2013-01-10 23:23:07 -06:00
|
|
|
let this = unsafe { rustrt::rust_get_task() };
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
rustrt::task_clear_event_reject(this);
|
|
|
|
}
|
2012-07-03 19:33:20 -05:00
|
|
|
|
|
|
|
let mut data_avail = false;
|
|
|
|
let mut ready_packet = pkts.len();
|
2013-01-23 13:43:58 -06:00
|
|
|
for pkts.eachi |i, p| {
|
|
|
|
unsafe {
|
|
|
|
let p = &*p.header();
|
|
|
|
let old = p.mark_blocked(this);
|
|
|
|
match old {
|
|
|
|
Full | Terminated => {
|
|
|
|
data_avail = true;
|
|
|
|
ready_packet = i;
|
|
|
|
(*p).state = old;
|
|
|
|
break;
|
|
|
|
}
|
2013-01-31 19:51:01 -06:00
|
|
|
Blocked => die!(~"blocking on blocked packet"),
|
2013-01-23 13:43:58 -06:00
|
|
|
Empty => ()
|
|
|
|
}
|
2012-07-03 19:33:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while !data_avail {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("sleeping on %? packets", pkts.len());
|
2012-08-28 13:11:15 -05:00
|
|
|
let event = wait_event(this) as *PacketHeader;
|
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-20 14:23:37 -05:00
|
|
|
Some(i) => {
|
2012-07-03 19:33:20 -05:00
|
|
|
ready_packet = i;
|
|
|
|
data_avail = true;
|
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
None => debug!("ignoring spurious event, %?", event)
|
2012-07-03 19:33:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:24:52 -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-28 13:11:15 -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.
|
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
left((Some(_), b)) {
|
2012-08-01 13:56:46 -05:00
|
|
|
// endpoint a received a message
|
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
right(a, Some(_)) {
|
2012-08-01 13:56:46 -05:00
|
|
|
// 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-12-11 15:50:04 -06:00
|
|
|
pub fn select2<A: Owned, Ab: Owned, B: Owned, Bb: Owned>(
|
2012-10-02 13:37:37 -05:00
|
|
|
a: RecvPacketBuffered<A, Ab>,
|
|
|
|
b: RecvPacketBuffered<B, Bb>)
|
2012-08-28 13:11:15 -05:00
|
|
|
-> Either<(Option<A>, RecvPacketBuffered<B, Bb>),
|
|
|
|
(RecvPacketBuffered<A, Ab>, Option<B>)>
|
2012-07-09 15:53:55 -05:00
|
|
|
{
|
2012-10-09 23:28:04 -05:00
|
|
|
let i = wait_many([a.header(), b.header()]);
|
2012-07-09 15:53:55 -05:00
|
|
|
|
2012-08-22 21:20:08 -05:00
|
|
|
match i {
|
2012-09-10 16:57:23 -05:00
|
|
|
0 => Left((try_recv(move a), move b)),
|
|
|
|
1 => Right((move a, try_recv(move b))),
|
2013-01-31 19:51:01 -06:00
|
|
|
_ => die!(~"select2 return an invalid packet")
|
2012-07-09 15:53:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
#[doc(hidden)]
|
2012-08-28 13:11:15 -05:00
|
|
|
trait Selectable {
|
|
|
|
pure fn header() -> *PacketHeader;
|
2012-07-13 17:39:31 -05:00
|
|
|
}
|
|
|
|
|
2012-08-28 13:11:15 -05:00
|
|
|
impl *PacketHeader: Selectable {
|
|
|
|
pure fn header() -> *PacketHeader { self }
|
2012-08-06 20:29:05 -05:00
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/// Returns the index of an endpoint that is ready to receive.
|
2012-09-26 18:26:19 -05:00
|
|
|
pub 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-09-26 18:26:19 -05:00
|
|
|
pub fn select2i<A: Selectable, B: Selectable>(a: &A, b: &B) ->
|
|
|
|
Either<(), ()> {
|
2012-10-09 23:28:04 -05:00
|
|
|
match wait_many([a.header(), b.header()]) {
|
2012-08-14 18:54:13 -05:00
|
|
|
0 => Left(()),
|
|
|
|
1 => Right(()),
|
2013-01-31 19:51:01 -06:00
|
|
|
_ => die!(~"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-12-11 15:50:04 -06:00
|
|
|
pub fn select<T: Owned, Tb: Owned>(endpoints: ~[RecvPacketBuffered<T, Tb>])
|
2012-08-28 13:11:15 -05:00
|
|
|
-> (uint, Option<T>, ~[RecvPacketBuffered<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-10-23 13:11:23 -05:00
|
|
|
let mut remaining = move endpoints;
|
2012-09-28 00:20:47 -05:00
|
|
|
let port = remaining.swap_remove(ready);
|
2012-09-10 16:57:23 -05:00
|
|
|
let result = try_recv(move port);
|
|
|
|
(ready, move result, move remaining)
|
2012-07-03 19:33:20 -05:00
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/** The sending end of a pipe. It can be used to send exactly one
|
|
|
|
message.
|
|
|
|
|
|
|
|
*/
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub type SendPacket<T: Owned> = SendPacketBuffered<T, Packet<T>>;
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub type SendPacket<T> = SendPacketBuffered<T, Packet<T>>;
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn SendPacket<T: Owned>(p: *Packet<T>) -> SendPacket<T> {
|
2012-08-28 13:11:15 -05:00
|
|
|
SendPacketBuffered(p)
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub fn SendPacket<T>(p: *Packet<T>) -> SendPacket<T> {
|
|
|
|
SendPacketBuffered(p)
|
|
|
|
}
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub struct SendPacketBuffered<T: Owned, Tbuffer: Owned> {
|
2012-09-06 21:40:15 -05:00
|
|
|
mut p: Option<*Packet<T>>,
|
|
|
|
mut buffer: Option<BufferResource<Tbuffer>>,
|
2012-06-29 20:15:28 -05:00
|
|
|
drop {
|
|
|
|
//if self.p != none {
|
2012-08-22 19:24:52 -05:00
|
|
|
// debug!("drop send %?", option::get(self.p));
|
2012-06-29 20:15:28 -05:00
|
|
|
//}
|
2012-08-20 14:23:37 -05:00
|
|
|
if self.p != None {
|
|
|
|
let mut p = None;
|
2012-06-29 20:15:28 -05:00
|
|
|
p <-> self.p;
|
2012-09-11 19:17:54 -05:00
|
|
|
sender_terminate(option::unwrap(move p))
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-08-22 19:24:52 -05:00
|
|
|
//unsafe { error!("send_drop: %?",
|
2012-07-24 00:14:43 -05:00
|
|
|
// if self.buffer == none {
|
|
|
|
// "none"
|
2012-08-22 19:24:52 -05:00
|
|
|
// } else { "some" }); }
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-09-07 21:04:40 -05:00
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub struct SendPacketBuffered<T, Tbuffer> {
|
|
|
|
mut p: Option<*Packet<T>>,
|
|
|
|
mut buffer: Option<BufferResource<Tbuffer>>,
|
|
|
|
}
|
2012-09-07 21:04:40 -05:00
|
|
|
|
2013-01-28 12:46:43 -06:00
|
|
|
impl<T:Owned,Tbuffer:Owned> SendPacketBuffered<T,Tbuffer> : ::ops::Drop {
|
|
|
|
fn finalize(&self) {
|
|
|
|
//if self.p != none {
|
|
|
|
// debug!("drop send %?", option::get(self.p));
|
|
|
|
//}
|
|
|
|
if self.p != None {
|
|
|
|
let mut p = None;
|
|
|
|
p <-> self.p;
|
|
|
|
sender_terminate(option::unwrap(move p))
|
|
|
|
}
|
|
|
|
//unsafe { error!("send_drop: %?",
|
|
|
|
// if self.buffer == none {
|
|
|
|
// "none"
|
|
|
|
// } else { "some" }); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn SendPacketBuffered<T: Owned, Tbuffer: Owned>(p: *Packet<T>)
|
2012-09-07 21:04:40 -05:00
|
|
|
-> SendPacketBuffered<T, Tbuffer> {
|
|
|
|
//debug!("take send %?", p);
|
|
|
|
SendPacketBuffered {
|
|
|
|
p: Some(p),
|
|
|
|
buffer: unsafe {
|
|
|
|
Some(BufferResource(
|
2012-10-01 14:47:02 -05:00
|
|
|
get_buffer(ptr::addr_of(&((*p).header)))))
|
2012-09-07 21:04:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub fn SendPacketBuffered<T,Tbuffer>(p: *Packet<T>)
|
|
|
|
-> SendPacketBuffered<T, Tbuffer> {
|
|
|
|
//debug!("take send %?", p);
|
|
|
|
SendPacketBuffered {
|
|
|
|
p: Some(p),
|
|
|
|
buffer: unsafe {
|
|
|
|
Some(BufferResource(
|
|
|
|
get_buffer(ptr::addr_of(&((*p).header)))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(stage0)]
|
|
|
|
impl<T:Owned,Tbuffer:Owned> SendPacketBuffered<T,Tbuffer> {
|
|
|
|
fn unwrap() -> *Packet<T> {
|
|
|
|
let mut p = None;
|
|
|
|
p <-> self.p;
|
|
|
|
option::unwrap(move p)
|
|
|
|
}
|
|
|
|
|
|
|
|
pure fn header() -> *PacketHeader {
|
|
|
|
match self.p {
|
|
|
|
Some(packet) => unsafe {
|
|
|
|
let packet = &*packet;
|
|
|
|
let header = ptr::addr_of(&(packet.header));
|
|
|
|
//forget(packet);
|
|
|
|
header
|
|
|
|
},
|
2013-01-31 19:51:01 -06:00
|
|
|
None => die!(~"packet already consumed")
|
2013-01-28 12:46:43 -06:00
|
|
|
}
|
|
|
|
}
|
2012-09-07 21:04:40 -05:00
|
|
|
|
2013-01-28 12:46:43 -06:00
|
|
|
fn reuse_buffer() -> BufferResource<Tbuffer> {
|
|
|
|
//error!("send reuse_buffer");
|
|
|
|
let mut tmp = None;
|
|
|
|
tmp <-> self.buffer;
|
|
|
|
option::unwrap(move tmp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
impl<T,Tbuffer> SendPacketBuffered<T,Tbuffer> {
|
2012-08-28 13:11:15 -05:00
|
|
|
fn unwrap() -> *Packet<T> {
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut p = None;
|
2012-06-29 20:15:28 -05:00
|
|
|
p <-> self.p;
|
2012-09-11 19:17:54 -05:00
|
|
|
option::unwrap(move p)
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2012-08-28 13:11:15 -05:00
|
|
|
pure fn header() -> *PacketHeader {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.p {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(packet) => unsafe {
|
2012-08-03 21:59:04 -05:00
|
|
|
let packet = &*packet;
|
2012-10-01 14:47:02 -05:00
|
|
|
let header = ptr::addr_of(&(packet.header));
|
2012-08-03 21:59:04 -05:00
|
|
|
//forget(packet);
|
|
|
|
header
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2013-01-31 19:51:01 -06:00
|
|
|
None => die!(~"packet already consumed")
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-23 13:28:38 -05:00
|
|
|
|
2012-08-28 13:11:15 -05:00
|
|
|
fn reuse_buffer() -> BufferResource<Tbuffer> {
|
2012-08-22 19:24:52 -05:00
|
|
|
//error!("send reuse_buffer");
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut tmp = None;
|
2012-07-23 13:28:38 -05:00
|
|
|
tmp <-> self.buffer;
|
2012-09-11 19:17:54 -05:00
|
|
|
option::unwrap(move tmp)
|
2012-07-23 13:28:38 -05:00
|
|
|
}
|
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.
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub type RecvPacket<T: Owned> = RecvPacketBuffered<T, Packet<T>>;
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub type RecvPacket<T> = RecvPacketBuffered<T, Packet<T>>;
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn RecvPacket<T: Owned>(p: *Packet<T>) -> RecvPacket<T> {
|
2012-08-28 13:11:15 -05:00
|
|
|
RecvPacketBuffered(p)
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub fn RecvPacket<T>(p: *Packet<T>) -> RecvPacket<T> {
|
|
|
|
RecvPacketBuffered(p)
|
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub struct RecvPacketBuffered<T: Owned, Tbuffer: Owned> {
|
2012-09-06 21:40:15 -05:00
|
|
|
mut p: Option<*Packet<T>>,
|
|
|
|
mut buffer: Option<BufferResource<Tbuffer>>,
|
2012-06-29 20:15:28 -05:00
|
|
|
drop {
|
|
|
|
//if self.p != none {
|
2012-08-22 19:24:52 -05:00
|
|
|
// debug!("drop recv %?", option::get(self.p));
|
2012-06-29 20:15:28 -05:00
|
|
|
//}
|
2012-08-20 14:23:37 -05:00
|
|
|
if self.p != None {
|
|
|
|
let mut p = None;
|
2012-06-29 20:15:28 -05:00
|
|
|
p <-> self.p;
|
2012-09-11 19:17:54 -05:00
|
|
|
receiver_terminate(option::unwrap(move p))
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-08-22 19:24:52 -05:00
|
|
|
//unsafe { error!("recv_drop: %?",
|
2012-07-24 00:14:43 -05:00
|
|
|
// if self.buffer == none {
|
|
|
|
// "none"
|
2012-08-22 19:24:52 -05:00
|
|
|
// } else { "some" }); }
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-09-07 21:04:40 -05:00
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub struct RecvPacketBuffered<T, Tbuffer> {
|
|
|
|
mut p: Option<*Packet<T>>,
|
|
|
|
mut buffer: Option<BufferResource<Tbuffer>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T:Owned, Tbuffer:Owned> RecvPacketBuffered<T,Tbuffer> : ::ops::Drop {
|
|
|
|
fn finalize(&self) {
|
|
|
|
//if self.p != none {
|
|
|
|
// debug!("drop recv %?", option::get(self.p));
|
|
|
|
//}
|
|
|
|
if self.p != None {
|
|
|
|
let mut p = None;
|
|
|
|
p <-> self.p;
|
|
|
|
receiver_terminate(option::unwrap(move p))
|
|
|
|
}
|
|
|
|
//unsafe { error!("recv_drop: %?",
|
|
|
|
// if self.buffer == none {
|
|
|
|
// "none"
|
|
|
|
// } else { "some" }); }
|
|
|
|
}
|
|
|
|
}
|
2012-09-07 21:04:40 -05:00
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned, Tbuffer: Owned> RecvPacketBuffered<T, Tbuffer> {
|
2012-08-28 13:11:15 -05:00
|
|
|
fn unwrap() -> *Packet<T> {
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut p = None;
|
2012-06-29 20:15:28 -05:00
|
|
|
p <-> self.p;
|
2012-09-11 19:17:54 -05:00
|
|
|
option::unwrap(move p)
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-07-09 17:29:23 -05:00
|
|
|
|
2012-11-29 20:37:33 -06:00
|
|
|
fn reuse_buffer() -> BufferResource<Tbuffer> {
|
|
|
|
//error!("recv reuse_buffer");
|
|
|
|
let mut tmp = None;
|
|
|
|
tmp <-> self.buffer;
|
|
|
|
option::unwrap(move tmp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned, Tbuffer: Owned> RecvPacketBuffered<T, Tbuffer> : Selectable {
|
2012-08-28 13:11:15 -05:00
|
|
|
pure fn header() -> *PacketHeader {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.p {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(packet) => unsafe {
|
2012-08-03 21:59:04 -05:00
|
|
|
let packet = &*packet;
|
2012-10-01 14:47:02 -05:00
|
|
|
let header = ptr::addr_of(&(packet.header));
|
2012-08-03 21:59:04 -05:00
|
|
|
//forget(packet);
|
|
|
|
header
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2013-01-31 19:51:01 -06:00
|
|
|
None => die!(~"packet already consumed")
|
2012-07-09 17:29:23 -05:00
|
|
|
}
|
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn RecvPacketBuffered<T: Owned, Tbuffer: Owned>(p: *Packet<T>)
|
2012-09-04 17:23:28 -05:00
|
|
|
-> RecvPacketBuffered<T, Tbuffer> {
|
|
|
|
//debug!("take recv %?", p);
|
|
|
|
RecvPacketBuffered {
|
|
|
|
p: Some(p),
|
|
|
|
buffer: unsafe {
|
|
|
|
Some(BufferResource(
|
2012-10-01 14:47:02 -05:00
|
|
|
get_buffer(ptr::addr_of(&((*p).header)))))
|
2012-09-04 17:23:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub fn RecvPacketBuffered<T,Tbuffer>(p: *Packet<T>)
|
|
|
|
-> RecvPacketBuffered<T,Tbuffer> {
|
|
|
|
//debug!("take recv %?", p);
|
|
|
|
RecvPacketBuffered {
|
|
|
|
p: Some(p),
|
|
|
|
buffer: unsafe {
|
|
|
|
Some(BufferResource(
|
|
|
|
get_buffer(ptr::addr_of(&((*p).header)))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 17:23:28 -05:00
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn entangle<T: Owned>() -> (SendPacket<T>, RecvPacket<T>) {
|
2012-06-29 20:15:28 -05:00
|
|
|
let p = packet();
|
2012-08-28 13:11:15 -05:00
|
|
|
(SendPacket(p), RecvPacket(p))
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub fn entangle<T>() -> (SendPacket<T>, RecvPacket<T>) {
|
|
|
|
let p = packet();
|
|
|
|
(SendPacket(p), RecvPacket(p))
|
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
|
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-12-11 15:50:04 -06:00
|
|
|
pub fn spawn_service<T: Owned, Tb: Owned>(
|
2012-08-28 13:11:15 -05:00
|
|
|
init: extern fn() -> (SendPacketBuffered<T, Tb>,
|
|
|
|
RecvPacketBuffered<T, Tb>),
|
2012-10-04 21:58:31 -05:00
|
|
|
service: fn~(v: RecvPacketBuffered<T, Tb>))
|
2012-08-28 13:11:15 -05:00
|
|
|
-> SendPacketBuffered<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.
|
2012-09-10 16:57:23 -05:00
|
|
|
let server = ~mut Some(move server);
|
|
|
|
do task::spawn |move service, move server| {
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut server_ = None;
|
2012-06-29 20:15:28 -05:00
|
|
|
server_ <-> *server;
|
2012-09-11 19:17:54 -05:00
|
|
|
service(option::unwrap(move server_))
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
|
2012-09-10 16:57:23 -05:00
|
|
|
move client
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
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-12-11 15:50:04 -06:00
|
|
|
pub fn spawn_service_recv<T: Owned, Tb: Owned>(
|
2012-08-28 13:11:15 -05:00
|
|
|
init: extern fn() -> (RecvPacketBuffered<T, Tb>,
|
|
|
|
SendPacketBuffered<T, Tb>),
|
2012-10-04 21:58:31 -05:00
|
|
|
service: fn~(v: SendPacketBuffered<T, Tb>))
|
2012-08-28 13:11:15 -05:00
|
|
|
-> RecvPacketBuffered<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.
|
2012-09-10 16:57:23 -05:00
|
|
|
let server = ~mut Some(move server);
|
|
|
|
do task::spawn |move service, move server| {
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut server_ = None;
|
2012-07-02 21:03:11 -05:00
|
|
|
server_ <-> *server;
|
2012-09-11 19:17:54 -05:00
|
|
|
service(option::unwrap(move server_))
|
2012-07-02 21:03:11 -05:00
|
|
|
}
|
|
|
|
|
2012-09-10 16:57:23 -05:00
|
|
|
move client
|
2012-07-02 21:03:11 -05:00
|
|
|
}
|
2012-07-10 13:58:43 -05:00
|
|
|
|
|
|
|
// Streams - Make pipes a little easier in general.
|
|
|
|
|
2012-08-22 20:10:48 -05:00
|
|
|
proto! streamp (
|
2012-12-11 15:50:04 -06:00
|
|
|
Open:send<T: Owned> {
|
2012-08-28 13:11:15 -05:00
|
|
|
data(T) -> Open<T>
|
2012-07-10 13:58:43 -05:00
|
|
|
}
|
2012-08-22 20:10:48 -05:00
|
|
|
)
|
2012-07-10 13:58:43 -05:00
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/// A trait for things that can send multiple messages.
|
2012-11-25 16:09:53 -06:00
|
|
|
pub trait GenericChan<T> {
|
2012-08-06 12:33:31 -05:00
|
|
|
/// Sends a message.
|
2012-10-02 13:37:37 -05:00
|
|
|
fn send(x: T);
|
2012-11-25 16:09:53 -06:00
|
|
|
}
|
2012-08-08 19:24:07 -05:00
|
|
|
|
2012-11-25 16:09:53 -06:00
|
|
|
/// Things that can send multiple messages and can detect when the receiver
|
|
|
|
/// is closed
|
|
|
|
pub trait GenericSmartChan<T> {
|
2012-08-08 19:24:07 -05:00
|
|
|
/// Sends a message, or report if the receiver has closed the connection.
|
2012-10-02 13:37:37 -05:00
|
|
|
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-11-25 16:09:53 -06:00
|
|
|
pub trait GenericPort<T> {
|
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
|
|
|
|
2012-11-25 16:09:53 -06:00
|
|
|
/** Receives a message, or returns `none` if
|
|
|
|
the connection is closed or closes.
|
2012-08-06 12:33:31 -05:00
|
|
|
*/
|
2012-08-20 14:23:37 -05:00
|
|
|
fn try_recv() -> Option<T>;
|
2012-11-25 16:09:53 -06:00
|
|
|
}
|
2012-08-06 12:33:31 -05:00
|
|
|
|
2012-11-25 16:09:53 -06:00
|
|
|
/// Ports that can `peek`
|
|
|
|
pub trait Peekable<T> {
|
|
|
|
/// Returns true if a message is available
|
2012-07-18 17:03:46 -05:00
|
|
|
pure fn peek() -> bool;
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2013-01-24 12:43:33 -06:00
|
|
|
struct Chan_<T:Owned> {
|
2013-01-28 12:46:43 -06:00
|
|
|
mut endp: Option<streamp::client::Open<T>>
|
|
|
|
}
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
struct Chan_<T> {
|
|
|
|
mut endp: Option<streamp::client::Open<T>>
|
2013-01-24 12:43:33 -06:00
|
|
|
}
|
2012-07-11 14:45:54 -05:00
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/// An endpoint that can send many messages.
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub enum Chan<T:Owned> {
|
2012-08-28 13:11:15 -05:00
|
|
|
Chan_(Chan_<T>)
|
2012-07-11 14:45:54 -05:00
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub enum Chan<T> {
|
|
|
|
Chan_(Chan_<T>)
|
|
|
|
}
|
2012-07-11 14:45:54 -05:00
|
|
|
|
2012-08-01 13:56:46 -05:00
|
|
|
#[doc(hidden)]
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2013-01-24 12:43:33 -06:00
|
|
|
struct Port_<T:Owned> {
|
|
|
|
mut endp: Option<streamp::server::Open<T>>,
|
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
struct Port_<T> {
|
|
|
|
mut endp: Option<streamp::server::Open<T>>,
|
|
|
|
}
|
2012-07-11 14:45:54 -05:00
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/// An endpoint that can receive many messages.
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub enum Port<T:Owned> {
|
2012-08-28 13:11:15 -05:00
|
|
|
Port_(Port_<T>)
|
2012-07-11 14:45:54 -05:00
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub enum Port<T> {
|
|
|
|
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-12-11 15:50:04 -06:00
|
|
|
pub fn stream<T:Owned>() -> (Port<T>, Chan<T>) {
|
2012-07-10 13:58:43 -05:00
|
|
|
let (c, s) = streamp::init();
|
|
|
|
|
2013-01-24 12:43:33 -06:00
|
|
|
(Port_(Port_ { endp: Some(s) }), Chan_(Chan_{ endp: Some(c) }))
|
2012-07-10 13:58:43 -05:00
|
|
|
}
|
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned> Chan<T>: GenericChan<T> {
|
2012-10-02 13:37:37 -05:00
|
|
|
fn send(x: T) {
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut endp = None;
|
2012-07-10 13:58:43 -05:00
|
|
|
endp <-> self.endp;
|
2012-08-20 14:23:37 -05:00
|
|
|
self.endp = Some(
|
2012-09-11 19:17:54 -05:00
|
|
|
streamp::client::data(unwrap(move endp), move x))
|
2012-07-10 13:58:43 -05:00
|
|
|
}
|
2012-11-25 16:09:53 -06:00
|
|
|
}
|
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned> Chan<T>: GenericSmartChan<T> {
|
2012-08-08 19:24:07 -05:00
|
|
|
|
2012-10-02 13:37:37 -05:00
|
|
|
fn try_send(x: T) -> bool {
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut endp = None;
|
2012-08-08 19:24:07 -05:00
|
|
|
endp <-> self.endp;
|
2012-09-11 19:17:54 -05:00
|
|
|
match move streamp::client::try_data(unwrap(move endp), move x) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(move next) => {
|
2012-09-10 16:57:23 -05:00
|
|
|
self.endp = Some(move next);
|
2012-08-08 19:24:07 -05:00
|
|
|
true
|
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
None => false
|
2012-08-08 19:24:07 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-10 13:58:43 -05:00
|
|
|
}
|
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned> Port<T>: GenericPort<T> {
|
2012-07-10 13:58:43 -05:00
|
|
|
fn recv() -> T {
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut endp = None;
|
2012-07-10 13:58:43 -05:00
|
|
|
endp <-> self.endp;
|
2012-09-11 19:17:54 -05:00
|
|
|
let streamp::data(x, endp) = pipes::recv(unwrap(move endp));
|
2012-09-10 16:57:23 -05:00
|
|
|
self.endp = Some(move endp);
|
|
|
|
move x
|
2012-07-10 13:58:43 -05:00
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
fn try_recv() -> Option<T> {
|
|
|
|
let mut endp = None;
|
2012-07-10 13:58:43 -05:00
|
|
|
endp <-> self.endp;
|
2012-09-11 19:17:54 -05:00
|
|
|
match move pipes::try_recv(unwrap(move endp)) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(streamp::data(move x, move endp)) => {
|
2012-09-10 16:57:23 -05:00
|
|
|
self.endp = Some(move endp);
|
|
|
|
Some(move x)
|
2012-07-10 13:58:43 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
None => None
|
2012-07-10 13:58:43 -05:00
|
|
|
}
|
|
|
|
}
|
2012-11-25 16:09:53 -06:00
|
|
|
}
|
2012-07-10 13:58:43 -05:00
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned> Port<T>: Peekable<T> {
|
2013-01-23 13:43:58 -06:00
|
|
|
pure fn peek() -> bool {
|
|
|
|
unsafe {
|
|
|
|
let mut endp = None;
|
|
|
|
endp <-> self.endp;
|
|
|
|
let peek = match &endp {
|
|
|
|
&Some(ref endp) => pipes::peek(endp),
|
2013-01-31 19:51:01 -06:00
|
|
|
&None => die!(~"peeking empty stream")
|
2013-01-23 13:43:58 -06:00
|
|
|
};
|
|
|
|
self.endp <-> endp;
|
|
|
|
peek
|
|
|
|
}
|
2012-07-10 13:58:43 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-12 18:44:09 -05:00
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned> Port<T>: Selectable {
|
2013-01-23 13:43:58 -06:00
|
|
|
pure fn header() -> *PacketHeader {
|
|
|
|
unsafe {
|
|
|
|
match self.endp {
|
|
|
|
Some(ref endp) => endp.header(),
|
2013-01-31 19:51:01 -06:00
|
|
|
None => die!(~"peeking empty stream")
|
2013-01-23 13:43:58 -06:00
|
|
|
}
|
2012-09-07 21:04:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-16 19:50:21 -05:00
|
|
|
/// Treat many ports as one.
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub struct PortSet<T: Owned> {
|
2012-09-06 21:40:15 -05:00
|
|
|
mut ports: ~[pipes::Port<T>],
|
2012-09-07 21:04:40 -05:00
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub struct PortSet<T> {
|
|
|
|
mut ports: ~[pipes::Port<T>],
|
|
|
|
}
|
2012-09-07 21:04:40 -05:00
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn PortSet<T: Owned>() -> PortSet<T>{
|
2012-09-07 21:04:40 -05:00
|
|
|
PortSet {
|
|
|
|
ports: ~[]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned> PortSet<T> {
|
2012-07-12 18:44:09 -05:00
|
|
|
|
2012-10-02 13:37:37 -05:00
|
|
|
fn add(port: pipes::Port<T>) {
|
2012-09-26 19:33:34 -05:00
|
|
|
self.ports.push(move port)
|
2012-07-12 18:44:09 -05:00
|
|
|
}
|
|
|
|
|
2012-08-28 13:11:15 -05:00
|
|
|
fn chan() -> Chan<T> {
|
2012-12-11 14:26:41 -06:00
|
|
|
let (po, ch) = stream();
|
2012-09-10 16:57:23 -05:00
|
|
|
self.add(move po);
|
|
|
|
move ch
|
2012-07-25 16:05:06 -05:00
|
|
|
}
|
2012-11-29 20:37:33 -06:00
|
|
|
}
|
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned> PortSet<T> : GenericPort<T> {
|
2012-07-25 16:05:06 -05:00
|
|
|
|
2012-08-20 14:23:37 -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;
|
2012-08-27 19:49:35 -05:00
|
|
|
while result.is_none() && ports.len() > 0 {
|
2012-08-16 17:53:06 -05:00
|
|
|
let i = wait_many(ports);
|
2012-08-06 14:34:08 -05:00
|
|
|
match move ports[i].try_recv() {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(move m) => {
|
2012-09-10 16:57:23 -05:00
|
|
|
result = Some(move m);
|
2012-08-02 20:55:31 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {
|
2012-08-02 20:55:31 -05:00
|
|
|
// Remove this port.
|
2012-09-28 00:20:47 -05:00
|
|
|
let _ = ports.swap_remove(i);
|
2012-08-02 20:55:31 -05:00
|
|
|
}
|
2012-07-12 18:44:09 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-02 20:55:31 -05:00
|
|
|
ports <-> self.ports;
|
2012-09-10 16:57:23 -05:00
|
|
|
move result
|
2012-07-12 18:44:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn recv() -> T {
|
2012-12-18 20:55:19 -06:00
|
|
|
self.try_recv().expect("port_set: endpoints closed")
|
2012-07-12 18:44:09 -05:00
|
|
|
}
|
2012-07-18 17:03:46 -05:00
|
|
|
|
2012-11-25 16:09:53 -06:00
|
|
|
}
|
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned> PortSet<T> : Peekable<T> {
|
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.
|
2012-09-18 23:41:13 -05:00
|
|
|
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-06 12:33:31 -05:00
|
|
|
/// A channel that can be shared between many senders.
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub type SharedChan<T: Owned> = private::Exclusive<Chan<T>>;
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub type SharedChan<T> = private::Exclusive<Chan<T>>;
|
2012-07-11 17:00:40 -05:00
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned> SharedChan<T>: GenericChan<T> {
|
2012-10-02 13:37:37 -05:00
|
|
|
fn send(x: T) {
|
2012-09-10 16:57:23 -05:00
|
|
|
let mut xx = Some(move x);
|
2012-09-11 23:25:01 -05:00
|
|
|
do self.with_imm |chan| {
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut x = None;
|
2012-07-12 18:44:09 -05:00
|
|
|
x <-> xx;
|
2012-09-11 19:17:54 -05:00
|
|
|
chan.send(option::unwrap(move x))
|
2012-07-12 18:44:09 -05:00
|
|
|
}
|
|
|
|
}
|
2012-11-25 16:09:53 -06:00
|
|
|
}
|
2012-08-08 19:24:07 -05:00
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned> SharedChan<T>: GenericSmartChan<T> {
|
2012-10-02 13:37:37 -05:00
|
|
|
fn try_send(x: T) -> bool {
|
2012-09-10 16:57:23 -05:00
|
|
|
let mut xx = Some(move x);
|
2012-09-11 23:25:01 -05:00
|
|
|
do self.with_imm |chan| {
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut x = None;
|
2012-08-08 19:24:07 -05:00
|
|
|
x <-> xx;
|
2012-09-11 19:17:54 -05:00
|
|
|
chan.try_send(option::unwrap(move x))
|
2012-08-08 19:24:07 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-12 18:44:09 -05:00
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:31 -05:00
|
|
|
/// Converts a `chan` into a `shared_chan`.
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn SharedChan<T:Owned>(c: Chan<T>) -> SharedChan<T> {
|
2012-09-18 16:51:40 -05:00
|
|
|
private::exclusive(move 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-12-11 15:50:04 -06:00
|
|
|
pub trait Select2<T: Owned, U: Owned> {
|
2012-11-29 17:18:31 -06:00
|
|
|
/// Receive a message or return `None` if a connection closes.
|
2012-08-20 14:23:37 -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
|
|
|
}
|
|
|
|
|
2012-12-11 15:50:04 -06:00
|
|
|
impl<T: Owned, U: Owned,
|
2012-11-25 16:09:53 -06:00
|
|
|
Left: Selectable GenericPort<T>,
|
|
|
|
Right: Selectable GenericPort<U>>
|
2012-08-28 13:11:15 -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-09-28 15:00:07 -05:00
|
|
|
(ref lp, ref 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-20 14:23:37 -05:00
|
|
|
fn try_select() -> Either<Option<T>, Option<U>> {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self {
|
2012-09-28 15:00:07 -05:00
|
|
|
(ref lp, ref 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-22 20:10:48 -05:00
|
|
|
proto! oneshot (
|
2012-12-11 15:50:04 -06:00
|
|
|
Oneshot:send<T:Owned> {
|
2012-08-09 13:54:56 -05:00
|
|
|
send(T) -> !
|
|
|
|
}
|
2012-08-22 20:10:48 -05:00
|
|
|
)
|
2012-08-09 13:54:56 -05:00
|
|
|
|
2012-08-14 19:48:37 -05:00
|
|
|
/// The send end of a oneshot pipe.
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub type ChanOne<T: Owned> = oneshot::client::Oneshot<T>;
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub type ChanOne<T> = oneshot::client::Oneshot<T>;
|
2012-08-14 19:48:37 -05:00
|
|
|
/// The receive end of a oneshot pipe.
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage0)]
|
2012-12-11 15:50:04 -06:00
|
|
|
pub type PortOne<T: Owned> = oneshot::server::Oneshot<T>;
|
2013-01-28 12:46:43 -06:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
pub type PortOne<T> = oneshot::server::Oneshot<T>;
|
2012-08-14 19:48:37 -05:00
|
|
|
|
2012-08-13 16:57:00 -05:00
|
|
|
/// Initialiase a (send-endpoint, recv-endpoint) oneshot pipe pair.
|
2013-01-15 19:18:00 -06:00
|
|
|
pub fn oneshot<T: Owned>() -> (PortOne<T>, ChanOne<T>) {
|
|
|
|
let (chan, port) = oneshot::init();
|
|
|
|
(port, chan)
|
2012-08-13 16:57:00 -05:00
|
|
|
}
|
|
|
|
|
2013-01-15 21:53:35 -06:00
|
|
|
impl<T: Owned> PortOne<T> {
|
|
|
|
fn recv(self) -> T { recv_one(self) }
|
|
|
|
fn try_recv(self) -> Option<T> { try_recv_one(self) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Owned> ChanOne<T> {
|
|
|
|
fn send(self, data: T) { send_one(self, data) }
|
|
|
|
fn try_send(self, data: T) -> bool { try_send_one(self, data) }
|
|
|
|
}
|
|
|
|
|
2012-08-13 16:57:00 -05:00
|
|
|
/**
|
|
|
|
* Receive a message from a oneshot pipe, failing if the connection was
|
|
|
|
* closed.
|
|
|
|
*/
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn recv_one<T: Owned>(port: PortOne<T>) -> T {
|
2012-09-10 16:57:23 -05:00
|
|
|
let oneshot::send(message) = recv(move port);
|
|
|
|
move message
|
2012-08-09 13:54:56 -05:00
|
|
|
}
|
|
|
|
|
2012-08-13 16:57:00 -05:00
|
|
|
/// Receive a message from a oneshot pipe unless the connection was closed.
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn try_recv_one<T: Owned> (port: PortOne<T>) -> Option<T> {
|
2012-09-10 16:57:23 -05:00
|
|
|
let message = try_recv(move port);
|
2012-08-09 13:54:56 -05:00
|
|
|
|
2012-08-27 19:49:35 -05:00
|
|
|
if message.is_none() { None }
|
2012-08-09 13:54:56 -05:00
|
|
|
else {
|
2012-09-11 19:17:54 -05:00
|
|
|
let oneshot::send(message) = option::unwrap(move message);
|
2012-09-10 16:57:23 -05:00
|
|
|
Some(move message)
|
2012-08-09 13:54:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-13 16:57:00 -05:00
|
|
|
/// Send a message on a oneshot pipe, failing if the connection was closed.
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn send_one<T: Owned>(chan: ChanOne<T>, data: T) {
|
2012-09-10 16:57:23 -05:00
|
|
|
oneshot::client::send(move chan, move data);
|
2012-08-13 16:57:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a message on a oneshot pipe, or return false if the connection was
|
|
|
|
* closed.
|
|
|
|
*/
|
2012-12-11 15:50:04 -06:00
|
|
|
pub fn try_send_one<T: Owned>(chan: ChanOne<T>, data: T)
|
2012-08-13 16:57:00 -05:00
|
|
|
-> bool {
|
2012-09-10 16:57:23 -05:00
|
|
|
oneshot::client::try_send(move chan, move data).is_some()
|
2012-08-13 16:57:00 -05:00
|
|
|
}
|
|
|
|
|
2012-09-28 18:26:59 -05:00
|
|
|
pub mod rt {
|
2013-01-08 21:37:25 -06:00
|
|
|
use option::{None, Option, Some};
|
|
|
|
|
2012-08-15 18:22:40 -05:00
|
|
|
// These are used to hide the option constructors from the
|
|
|
|
// compiler because their names are changing
|
2012-10-02 13:37:37 -05:00
|
|
|
pub fn make_some<T>(val: T) -> Option<T> { Some(move val) }
|
2012-09-28 18:26:59 -05:00
|
|
|
pub fn make_none<T>() -> Option<T> { None }
|
2012-08-15 18:22:40 -05:00
|
|
|
}
|
|
|
|
|
2012-07-25 16:46:15 -05:00
|
|
|
#[cfg(test)]
|
2012-09-26 18:26:19 -05:00
|
|
|
pub mod test {
|
2013-01-08 21:37:25 -06:00
|
|
|
use either::{Either, Left, Right};
|
|
|
|
use pipes::{Chan, Port, oneshot, recv_one, stream};
|
2012-12-27 19:53:04 -06:00
|
|
|
use pipes;
|
|
|
|
|
2012-07-25 16:46:15 -05:00
|
|
|
#[test]
|
2012-09-26 18:26:19 -05:00
|
|
|
pub fn test_select2() {
|
2012-12-11 14:26:41 -06:00
|
|
|
let (p1, c1) = pipes::stream();
|
|
|
|
let (p2, c2) = pipes::stream();
|
2012-07-25 16:46:15 -05:00
|
|
|
|
2012-08-03 14:04:08 -05:00
|
|
|
c1.send(~"abc");
|
2012-07-25 16:46:15 -05:00
|
|
|
|
2012-09-19 00:35:28 -05:00
|
|
|
match (move p1, move p2).select() {
|
2013-01-31 19:51:01 -06:00
|
|
|
Right(_) => die!(),
|
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]
|
2012-09-26 18:26:19 -05:00
|
|
|
pub fn test_oneshot() {
|
2012-08-09 13:54:56 -05:00
|
|
|
let (c, p) = oneshot::init();
|
|
|
|
|
2012-09-19 00:35:28 -05:00
|
|
|
oneshot::client::send(move c, ());
|
2012-08-09 13:54:56 -05:00
|
|
|
|
2012-09-19 00:35:28 -05:00
|
|
|
recv_one(move p)
|
2012-08-09 13:54:56 -05:00
|
|
|
}
|
2012-11-02 02:30:13 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_peek_terminated() {
|
2012-12-11 14:26:41 -06:00
|
|
|
let (port, chan): (Port<int>, Chan<int>) = stream();
|
2012-11-02 02:30:13 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
// Destroy the channel
|
|
|
|
let _chan = move chan;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert !port.peek();
|
|
|
|
}
|
2012-07-25 16:46:15 -05:00
|
|
|
}
|