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.
|
|
|
|
|
2013-05-27 08:49:54 -05:00
|
|
|
~~~ {.rust}
|
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
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
#[allow(missing_doc)];
|
|
|
|
|
2013-05-12 19:34:15 -05:00
|
|
|
use container::Container;
|
2013-04-22 19:34:42 -05:00
|
|
|
use cast::{forget, transmute, transmute_copy};
|
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;
|
2013-04-30 20:03:09 -05:00
|
|
|
use ops::Drop;
|
2013-03-16 14:49:12 -05:00
|
|
|
use option::{None, Option, Some};
|
2013-05-01 09:49:10 -05:00
|
|
|
use unstable::finally::Finally;
|
2013-02-27 21:53:31 -06:00
|
|
|
use unstable::intrinsics;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ptr;
|
2013-06-03 12:50:29 -05:00
|
|
|
use ptr::RawPtr;
|
2012-12-23 16:41:37 -06:00
|
|
|
use task;
|
|
|
|
use vec;
|
2013-05-12 19:34:15 -05:00
|
|
|
use vec::OwnedVector;
|
2013-05-05 23:42:54 -05:00
|
|
|
use util::replace;
|
2012-07-12 18:44:09 -05:00
|
|
|
|
2013-03-22 16:00:15 -05:00
|
|
|
static SPIN_COUNT: uint = 0;
|
2012-07-24 11:35:44 -05:00
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
macro_rules! move_it (
|
2013-04-22 16:27:30 -05:00
|
|
|
{ $x:expr } => ( unsafe { let y = *ptr::to_unsafe_ptr(&($x)); y } )
|
2012-08-22 19:24:52 -05:00
|
|
|
)
|
2012-07-12 18:44:09 -05:00
|
|
|
|
2013-03-22 15:09:20 -05:00
|
|
|
#[deriving(Eq)]
|
2012-08-28 13:11:15 -05:00
|
|
|
enum State {
|
|
|
|
Empty,
|
|
|
|
Full,
|
|
|
|
Blocked,
|
|
|
|
Terminated
|
2012-06-29 20:15:28 -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.
|
2013-05-03 16:20:34 -05:00
|
|
|
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.
|
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
|
|
|
|
2013-02-02 05:10:12 -06:00
|
|
|
pub struct PacketHeader {
|
2013-05-03 16:20:34 -05:00
|
|
|
state: State,
|
|
|
|
blocked_task: *rust_task,
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2013-04-22 19:34:42 -05:00
|
|
|
// This is a transmute_copy of a ~buffer, that can also be cast
|
2012-07-20 21:06:32 -05:00
|
|
|
// to a buffer_header if need be.
|
2013-05-03 16:20:34 -05:00
|
|
|
buffer: *libc::c_void,
|
2012-09-07 21:04:40 -05:00
|
|
|
}
|
|
|
|
|
2013-02-02 05:10:12 -06:00
|
|
|
pub fn PacketHeader() -> PacketHeader {
|
2012-09-07 21:04:40 -05:00
|
|
|
PacketHeader {
|
|
|
|
state: Empty,
|
|
|
|
blocked_task: ptr::null(),
|
|
|
|
buffer: ptr::null()
|
|
|
|
}
|
|
|
|
}
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl PacketHeader {
|
2012-07-20 21:06:32 -05:00
|
|
|
// Returns the old state.
|
2013-05-31 17:17:22 -05:00
|
|
|
pub unsafe fn mark_blocked(&mut self, 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);
|
2013-03-28 20:39:09 -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
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub unsafe fn unblock(&mut self) {
|
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() {
|
2013-04-23 18:33:33 -05:00
|
|
|
rustrt::rust_task_deref(old_task)
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
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
|
2013-05-03 16:20:34 -05:00
|
|
|
// thing. You'll probably want to forget them when you're done.
|
2013-05-31 17:17:22 -05:00
|
|
|
pub unsafe fn buf_header(&mut self) -> ~BufferHeader {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(self.buffer.is_not_null());
|
2013-04-22 19:34:42 -05:00
|
|
|
transmute_copy(&self.buffer)
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
2012-07-23 13:28:38 -05:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn set_buffer<T:Owned>(&mut self, b: ~Buffer<T>) {
|
2013-01-23 13:43:58 -06:00
|
|
|
unsafe {
|
2013-04-22 19:34:42 -05:00
|
|
|
self.buffer = transmute_copy(&b);
|
2013-01-23 13:43:58 -06:00
|
|
|
}
|
2012-07-23 13:28:38 -05:00
|
|
|
}
|
2012-07-11 14:45:54 -05:00
|
|
|
}
|
|
|
|
|
2013-01-28 12:46:43 -06:00
|
|
|
pub struct Packet<T> {
|
|
|
|
header: PacketHeader,
|
2013-05-03 16:20:34 -05:00
|
|
|
payload: Option<T>,
|
2013-01-28 12:46:43 -06:00
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
|
2012-09-26 18:26:19 -05:00
|
|
|
pub trait HasBuffer {
|
2013-05-03 16:20:34 -05:00
|
|
|
fn set_buffer(&mut self, b: *libc::c_void);
|
2012-08-28 13:11:15 -05:00
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<T:Owned> HasBuffer for Packet<T> {
|
2013-05-03 16:20:34 -05:00
|
|
|
fn set_buffer(&mut self, b: *libc::c_void) {
|
2012-08-28 13:11:15 -05:00
|
|
|
self.header.buffer = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -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
|
|
|
}
|
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
fn unibuffer<T>() -> ~Buffer<Packet<T>> {
|
2013-05-03 16:20:34 -05:00
|
|
|
let mut b = ~Buffer {
|
2013-01-24 13:37:36 -06:00
|
|
|
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 {
|
2013-04-22 19:34:42 -05:00
|
|
|
b.data.header.buffer = transmute_copy(&b);
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
2013-02-15 02:51:28 -06:00
|
|
|
b
|
2012-07-11 14:45:54 -05:00
|
|
|
}
|
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
pub fn packet<T>() -> *mut Packet<T> {
|
|
|
|
let mut b = unibuffer();
|
|
|
|
let p = ptr::to_mut_unsafe_ptr(&mut b.data);
|
2013-01-28 12:46:43 -06:00
|
|
|
// We'll take over memory management from here.
|
2013-05-03 16:20:34 -05:00
|
|
|
unsafe {
|
|
|
|
forget(b);
|
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
p
|
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
pub fn entangle_buffer<T:Owned,Tstart:Owned>(
|
2013-05-03 16:20:34 -05:00
|
|
|
mut buffer: ~Buffer<T>,
|
|
|
|
init: &fn(*libc::c_void, x: &mut T) -> *mut Packet<Tstart>)
|
2013-06-01 20:19:16 -05:00
|
|
|
-> (RecvPacketBuffered<Tstart, T>, SendPacketBuffered<Tstart, T>) {
|
2013-05-03 16:20:34 -05:00
|
|
|
unsafe {
|
|
|
|
let p = init(transmute_copy(&buffer), &mut buffer.data);
|
|
|
|
forget(buffer);
|
2013-06-01 20:19:16 -05:00
|
|
|
(RecvPacketBuffered(p), SendPacketBuffered(p))
|
2013-05-03 16:20:34 -05:00
|
|
|
}
|
2012-07-23 15:50:12 -05:00
|
|
|
}
|
|
|
|
|
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 {
|
2013-02-20 10:57:15 -06:00
|
|
|
transmute(intrinsics::atomic_xchg(transmute(dst), src as int))
|
2012-08-03 20:57:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 13:11:15 -05:00
|
|
|
#[allow(non_camel_case_types)]
|
2013-03-05 13:57:50 -06:00
|
|
|
pub type rust_task = libc::c_void;
|
2012-07-02 19:42:58 -05:00
|
|
|
|
2013-03-05 13:57:50 -06:00
|
|
|
pub mod rustrt {
|
|
|
|
use libc;
|
|
|
|
use super::rust_task;
|
|
|
|
|
|
|
|
pub extern {
|
|
|
|
#[rust_stack]
|
|
|
|
unsafe fn rust_get_task() -> *rust_task;
|
|
|
|
#[rust_stack]
|
|
|
|
unsafe fn rust_task_ref(task: *rust_task);
|
|
|
|
unsafe fn rust_task_deref(task: *rust_task);
|
|
|
|
|
|
|
|
#[rust_stack]
|
|
|
|
unsafe fn task_clear_event_reject(task: *rust_task);
|
|
|
|
|
|
|
|
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-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-05-05 17:18:51 -05:00
|
|
|
fail!("killed")
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
|
|
|
event
|
2012-07-10 12:58:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2013-02-20 10:57:15 -06:00
|
|
|
transmute(intrinsics::atomic_xchg_acq(transmute(dst), src as int))
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2013-02-20 10:57:15 -06:00
|
|
|
transmute(intrinsics::atomic_xchg_rel(transmute(dst), src as int))
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
pub unsafe fn get_buffer<T>(p: *mut PacketHeader) -> ~Buffer<T> {
|
2013-01-28 12:46:43 -06:00
|
|
|
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
|
|
|
struct BufferResource<T> {
|
|
|
|
buffer: ~Buffer<T>,
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2013-02-27 18:13:53 -06:00
|
|
|
}
|
|
|
|
|
2013-03-20 20:18:57 -05:00
|
|
|
#[unsafe_destructor]
|
2013-05-02 06:20:22 -05:00
|
|
|
impl<T> Drop for BufferResource<T> {
|
2013-02-27 18:13:53 -06:00
|
|
|
fn finalize(&self) {
|
2013-01-28 12:46:43 -06:00
|
|
|
unsafe {
|
2013-05-03 16:20:34 -05:00
|
|
|
let this: &mut BufferResource<T> = transmute(self);
|
|
|
|
|
|
|
|
let mut b = move_it!(this.buffer);
|
2013-04-22 16:27:30 -05:00
|
|
|
//let p = ptr::to_unsafe_ptr(*b);
|
2013-01-28 12:46:43 -06:00
|
|
|
//error!("drop %?", p);
|
2013-05-03 16:20:34 -05:00
|
|
|
let old_count = intrinsics::atomic_xsub_rel(
|
|
|
|
&mut b.header.ref_count,
|
|
|
|
1);
|
2013-01-28 12:46:43 -06:00
|
|
|
//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 {
|
2013-02-15 02:51:28 -06:00
|
|
|
forget(b)
|
2013-01-28 12:46:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
fn BufferResource<T>(mut b: ~Buffer<T>) -> BufferResource<T> {
|
2013-04-22 16:27:30 -05:00
|
|
|
//let p = ptr::to_unsafe_ptr(*b);
|
2013-01-28 12:46:43 -06:00
|
|
|
//error!("take %?", p);
|
2013-05-03 16:20:34 -05:00
|
|
|
unsafe {
|
|
|
|
intrinsics::atomic_xadd_acq(&mut b.header.ref_count, 1);
|
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
|
|
|
|
BufferResource {
|
|
|
|
// tjc: ????
|
2013-02-15 02:51:28 -06:00
|
|
|
buffer: b
|
2013-01-28 12:46:43 -06:00
|
|
|
}
|
|
|
|
}
|
2012-09-04 17:23:28 -05:00
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
pub fn send<T,Tbuffer>(mut p: SendPacketBuffered<T,Tbuffer>,
|
|
|
|
payload: T)
|
|
|
|
-> bool {
|
2013-01-28 12:46:43 -06:00
|
|
|
let header = p.header();
|
2013-05-09 15:27:24 -05:00
|
|
|
let p_ = p.unwrap();
|
2013-05-03 16:20:34 -05:00
|
|
|
let p = unsafe { &mut *p_ };
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(ptr::to_unsafe_ptr(&(p.header)), header);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(p.payload.is_none());
|
2013-02-15 02:51:28 -06:00
|
|
|
p.payload = Some(payload);
|
2013-01-28 12:46:43 -06:00
|
|
|
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;
|
|
|
|
}
|
2013-05-05 17:18:51 -05:00
|
|
|
Full => fail!("duplicate send"),
|
2013-01-28 12:46:43 -06:00
|
|
|
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,
|
2013-04-22 16:27:30 -05:00
|
|
|
ptr::to_unsafe_ptr(&(p.header)) as *libc::c_void);
|
2013-01-28 12:46:43 -06:00
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
2013-02-20 19:07:17 -06:00
|
|
|
pub fn recv<T:Owned,Tbuffer:Owned>(
|
2012-12-11 15:50:04 -06:00
|
|
|
p: RecvPacketBuffered<T, Tbuffer>) -> T {
|
2013-02-15 02:51:28 -06:00
|
|
|
try_recv(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
|
|
|
|
|
|
|
*/
|
2013-05-03 16:20:34 -05:00
|
|
|
pub fn try_recv<T:Owned,Tbuffer:Owned>(mut p: RecvPacketBuffered<T, Tbuffer>)
|
|
|
|
-> Option<T> {
|
2013-05-09 15:27:24 -05:00
|
|
|
let p_ = p.unwrap();
|
|
|
|
let p = unsafe { &mut *p_ };
|
2012-08-07 18:54:00 -05:00
|
|
|
|
2013-05-01 09:49:10 -05:00
|
|
|
do (|| {
|
|
|
|
try_recv_(p)
|
|
|
|
}).finally {
|
|
|
|
unsafe {
|
|
|
|
if task::failing() {
|
|
|
|
p.header.state = Terminated;
|
|
|
|
let old_task = swap_task(&mut p.header.blocked_task, ptr::null());
|
|
|
|
if !old_task.is_null() {
|
|
|
|
rustrt::rust_task_deref(old_task);
|
2012-08-10 18:17:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-30 20:03:09 -05:00
|
|
|
}
|
2013-05-01 09:49:10 -05:00
|
|
|
}
|
2012-08-10 18:17:53 -05:00
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
fn try_recv_<T:Owned>(p: &mut Packet<T>) -> Option<T> {
|
2012-08-07 18:54:00 -05:00
|
|
|
// optimistic path
|
|
|
|
match p.header.state {
|
2012-08-28 13:11:15 -05:00
|
|
|
Full => {
|
2013-05-05 23:42:54 -05:00
|
|
|
let payload = replace(&mut p.payload, None);
|
2012-08-28 13:11:15 -05:00
|
|
|
p.header.state = Empty;
|
2013-03-16 14:49:12 -05:00
|
|
|
return Some(payload.unwrap())
|
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);
|
2013-03-28 20:39:09 -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 => {
|
2013-05-01 09:49:10 -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-05-05 17:18:51 -05:00
|
|
|
fail!("blocking on already blocked packet")
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2012-08-28 13:11:15 -05:00
|
|
|
Full => {
|
2013-05-05 23:42:54 -05:00
|
|
|
let payload = replace(&mut p.payload, None);
|
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;
|
2013-03-16 14:49:12 -05:00
|
|
|
return Some(payload.unwrap())
|
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.
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(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.
|
2013-05-03 16:20:34 -05:00
|
|
|
pub fn peek<T:Owned,Tb:Owned>(p: &mut RecvPacketBuffered<T, Tb>) -> bool {
|
|
|
|
unsafe {
|
|
|
|
match (*p.header()).state {
|
|
|
|
Empty | Terminated => false,
|
2013-05-05 17:18:51 -05:00
|
|
|
Blocked => fail!("peeking on blocked packet"),
|
2013-05-03 16:20:34 -05:00
|
|
|
Full => true
|
|
|
|
}
|
2012-07-09 17:29:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
fn sender_terminate<T:Owned>(p: *mut Packet<T>) {
|
|
|
|
let p = unsafe {
|
|
|
|
&mut *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,
|
2013-04-22 16:27:30 -05:00
|
|
|
ptr::to_unsafe_ptr(&(p.header)) as *libc::c_void);
|
2013-01-10 23:23:07 -06:00
|
|
|
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-05-05 17:18:51 -05:00
|
|
|
fail!("you dun goofed")
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-08-28 13:11:15 -05:00
|
|
|
Terminated => {
|
2013-03-28 20:39:09 -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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
fn receiver_terminate<T:Owned>(p: *mut Packet<T>) {
|
|
|
|
let p = unsafe {
|
|
|
|
&mut *p
|
|
|
|
};
|
2012-08-28 13:11:15 -05:00
|
|
|
match swap_state_rel(&mut p.header.state, Terminated) {
|
|
|
|
Empty => {
|
2013-03-28 20:39:09 -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);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(old_task, rustrt::rust_get_task());
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
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 => {
|
2013-03-28 20:39:09 -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.
|
|
|
|
|
|
|
|
*/
|
2013-05-03 16:20:34 -05:00
|
|
|
pub fn wait_many<T: Selectable>(pkts: &mut [T]) -> uint {
|
|
|
|
let this = unsafe {
|
|
|
|
rustrt::rust_get_task()
|
|
|
|
};
|
2013-01-10 23:23:07 -06:00
|
|
|
|
|
|
|
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-05-03 16:20:34 -05:00
|
|
|
for vec::eachi_mut(pkts) |i, p| {
|
2013-01-23 13:43:58 -06:00
|
|
|
unsafe {
|
2013-05-03 16:20:34 -05:00
|
|
|
let p = &mut *p.header();
|
2013-01-23 13:43:58 -06:00
|
|
|
let old = p.mark_blocked(this);
|
|
|
|
match old {
|
2013-05-03 16:20:34 -05:00
|
|
|
Full | Terminated => {
|
|
|
|
data_avail = true;
|
|
|
|
ready_packet = i;
|
|
|
|
(*p).state = old;
|
|
|
|
break;
|
|
|
|
}
|
2013-05-05 17:18:51 -05:00
|
|
|
Blocked => fail!("blocking on blocked packet"),
|
2013-05-03 16:20:34 -05:00
|
|
|
Empty => ()
|
2013-01-23 13:43:58 -06:00
|
|
|
}
|
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;
|
2013-05-03 16:20:34 -05:00
|
|
|
|
|
|
|
let mut pos = None;
|
|
|
|
for vec::eachi_mut(pkts) |i, p| {
|
|
|
|
if p.header() == event {
|
|
|
|
pos = Some(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
debug!("%?", &mut pkts[ready_packet]);
|
2012-07-03 19:33:20 -05:00
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
for vec::each_mut(pkts) |p| {
|
|
|
|
unsafe {
|
|
|
|
(*p.header()).unblock()
|
|
|
|
}
|
|
|
|
}
|
2012-07-03 19:33:20 -05:00
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
debug!("%?, %?", ready_packet, &mut pkts[ready_packet]);
|
2012-07-03 19:33:20 -05:00
|
|
|
|
2012-07-12 14:00:07 -05:00
|
|
|
unsafe {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((*pkts[ready_packet].header()).state == Full
|
2013-03-06 15:58:02 -06:00
|
|
|
|| (*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-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
|
|
|
pub type SendPacket<T> = SendPacketBuffered<T, Packet<T>>;
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
pub fn SendPacket<T>(p: *mut Packet<T>) -> SendPacket<T> {
|
2013-01-28 12:46:43 -06:00
|
|
|
SendPacketBuffered(p)
|
|
|
|
}
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2013-01-28 12:46:43 -06:00
|
|
|
pub struct SendPacketBuffered<T, Tbuffer> {
|
2013-05-03 16:20:34 -05:00
|
|
|
p: Option<*mut Packet<T>>,
|
|
|
|
buffer: Option<BufferResource<Tbuffer>>,
|
2013-01-28 12:46:43 -06:00
|
|
|
}
|
2012-09-07 21:04:40 -05:00
|
|
|
|
2013-03-20 20:18:57 -05:00
|
|
|
#[unsafe_destructor]
|
2013-05-02 06:20:22 -05:00
|
|
|
impl<T:Owned,Tbuffer:Owned> Drop for SendPacketBuffered<T,Tbuffer> {
|
2013-01-28 12:46:43 -06:00
|
|
|
fn finalize(&self) {
|
2013-05-03 16:20:34 -05:00
|
|
|
unsafe {
|
|
|
|
let this: &mut SendPacketBuffered<T,Tbuffer> = transmute(self);
|
|
|
|
if this.p != None {
|
2013-05-05 23:42:54 -05:00
|
|
|
let p = replace(&mut this.p, None);
|
2013-05-03 16:20:34 -05:00
|
|
|
sender_terminate(p.unwrap())
|
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
pub fn SendPacketBuffered<T,Tbuffer>(p: *mut Packet<T>)
|
|
|
|
-> SendPacketBuffered<T,Tbuffer> {
|
2013-01-28 12:46:43 -06:00
|
|
|
SendPacketBuffered {
|
|
|
|
p: Some(p),
|
|
|
|
buffer: unsafe {
|
2013-05-03 16:20:34 -05:00
|
|
|
Some(BufferResource(get_buffer(&mut (*p).header)))
|
2013-01-28 12:46:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl<T,Tbuffer> SendPacketBuffered<T,Tbuffer> {
|
|
|
|
pub fn unwrap(&mut self) -> *mut Packet<T> {
|
2013-05-05 23:42:54 -05:00
|
|
|
replace(&mut self.p, None).unwrap()
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn header(&mut self) -> *mut PacketHeader {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.p {
|
2013-05-03 16:20:34 -05:00
|
|
|
Some(packet) => unsafe {
|
|
|
|
let packet = &mut *packet;
|
|
|
|
let header = ptr::to_mut_unsafe_ptr(&mut packet.header);
|
|
|
|
header
|
|
|
|
},
|
2013-05-05 17:18:51 -05:00
|
|
|
None => fail!("packet already consumed")
|
2012-07-20 21:06:32 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-23 13:28:38 -05:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn reuse_buffer(&mut self) -> BufferResource<Tbuffer> {
|
2012-08-22 19:24:52 -05:00
|
|
|
//error!("send reuse_buffer");
|
2013-05-05 23:42:54 -05:00
|
|
|
replace(&mut self.buffer, None).unwrap()
|
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
|
|
|
pub type RecvPacket<T> = RecvPacketBuffered<T, Packet<T>>;
|
2012-07-20 21:06:32 -05:00
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
pub fn RecvPacket<T>(p: *mut Packet<T>) -> RecvPacket<T> {
|
2013-01-28 12:46:43 -06:00
|
|
|
RecvPacketBuffered(p)
|
|
|
|
}
|
2013-05-03 16:20:34 -05:00
|
|
|
|
2013-01-28 12:46:43 -06:00
|
|
|
pub struct RecvPacketBuffered<T, Tbuffer> {
|
2013-05-03 16:20:34 -05:00
|
|
|
p: Option<*mut Packet<T>>,
|
|
|
|
buffer: Option<BufferResource<Tbuffer>>,
|
2013-01-28 12:46:43 -06:00
|
|
|
}
|
|
|
|
|
2013-03-20 20:18:57 -05:00
|
|
|
#[unsafe_destructor]
|
2013-05-02 06:20:22 -05:00
|
|
|
impl<T:Owned,Tbuffer:Owned> Drop for RecvPacketBuffered<T,Tbuffer> {
|
2013-01-28 12:46:43 -06:00
|
|
|
fn finalize(&self) {
|
2013-05-03 16:20:34 -05:00
|
|
|
unsafe {
|
|
|
|
let this: &mut RecvPacketBuffered<T,Tbuffer> = transmute(self);
|
|
|
|
if this.p != None {
|
2013-05-05 23:42:54 -05:00
|
|
|
let p = replace(&mut this.p, None);
|
2013-05-03 16:20:34 -05:00
|
|
|
receiver_terminate(p.unwrap())
|
|
|
|
}
|
2013-01-28 12:46:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-07 21:04:40 -05:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl<T:Owned,Tbuffer:Owned> RecvPacketBuffered<T, Tbuffer> {
|
|
|
|
pub fn unwrap(&mut self) -> *mut Packet<T> {
|
2013-05-05 23:42:54 -05:00
|
|
|
replace(&mut self.p, None).unwrap()
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
2012-07-09 17:29:23 -05:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn reuse_buffer(&mut self) -> BufferResource<Tbuffer> {
|
2013-05-05 23:42:54 -05:00
|
|
|
replace(&mut self.buffer, None).unwrap()
|
2012-11-29 20:37:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<T:Owned,Tbuffer:Owned> Selectable for RecvPacketBuffered<T, Tbuffer> {
|
2013-05-03 16:20:34 -05:00
|
|
|
fn header(&mut self) -> *mut PacketHeader {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.p {
|
2013-05-03 16:20:34 -05:00
|
|
|
Some(packet) => unsafe {
|
|
|
|
let packet = &mut *packet;
|
|
|
|
let header = ptr::to_mut_unsafe_ptr(&mut packet.header);
|
|
|
|
header
|
|
|
|
},
|
2013-05-05 17:18:51 -05:00
|
|
|
None => fail!("packet already consumed")
|
2012-07-09 17:29:23 -05:00
|
|
|
}
|
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
}
|
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
pub fn RecvPacketBuffered<T,Tbuffer>(p: *mut Packet<T>)
|
|
|
|
-> RecvPacketBuffered<T,Tbuffer> {
|
2013-01-28 12:46:43 -06:00
|
|
|
RecvPacketBuffered {
|
|
|
|
p: Some(p),
|
|
|
|
buffer: unsafe {
|
2013-05-03 16:20:34 -05:00
|
|
|
Some(BufferResource(get_buffer(&mut (*p).header)))
|
2013-01-28 12:46:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 17:23:28 -05:00
|
|
|
|
2013-06-01 20:19:16 -05:00
|
|
|
pub fn entangle<T>() -> (RecvPacket<T>, SendPacket<T>) {
|
2013-01-28 12:46:43 -06:00
|
|
|
let p = packet();
|
2013-06-01 20:19:16 -05:00
|
|
|
(RecvPacket(p), SendPacket(p))
|
2013-01-28 12:46:43 -06:00
|
|
|
}
|
2012-06-29 20:15:28 -05:00
|
|
|
|
2013-04-01 20:25:20 -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`.
|
|
|
|
|
2013-05-27 08:49:54 -05:00
|
|
|
~~~ {.rust}
|
2013-04-01 20:25:20 -05:00
|
|
|
match select2(a, b) {
|
2013-05-27 08:49:54 -05:00
|
|
|
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.
|
|
|
|
}
|
2013-04-01 20:25:20 -05:00
|
|
|
}
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Sometimes messages will be available on both endpoints at once. In
|
|
|
|
this case, `select2` may return either `left` or `right`.
|
|
|
|
|
|
|
|
*/
|
|
|
|
pub fn select2<A:Owned,Ab:Owned,B:Owned,Bb:Owned>(
|
2013-05-03 16:20:34 -05:00
|
|
|
mut a: RecvPacketBuffered<A, Ab>,
|
|
|
|
mut b: RecvPacketBuffered<B, Bb>)
|
2013-04-01 20:25:20 -05:00
|
|
|
-> Either<(Option<A>, RecvPacketBuffered<B, Bb>),
|
2013-05-03 16:20:34 -05:00
|
|
|
(RecvPacketBuffered<A, Ab>, Option<B>)> {
|
|
|
|
let mut endpoints = [ a.header(), b.header() ];
|
|
|
|
let i = wait_many(endpoints);
|
2013-04-01 20:25:20 -05:00
|
|
|
match i {
|
2013-05-03 16:20:34 -05:00
|
|
|
0 => Left((try_recv(a), b)),
|
|
|
|
1 => Right((a, try_recv(b))),
|
2013-05-05 17:18:51 -05:00
|
|
|
_ => fail!("select2 return an invalid packet")
|
2013-04-01 20:25:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Selectable {
|
2013-05-03 16:20:34 -05:00
|
|
|
fn header(&mut self) -> *mut PacketHeader;
|
2013-04-01 20:25:20 -05:00
|
|
|
}
|
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
impl Selectable for *mut PacketHeader {
|
|
|
|
fn header(&mut self) -> *mut PacketHeader { *self }
|
2013-04-01 20:25:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the index of an endpoint that is ready to receive.
|
2013-05-03 16:20:34 -05:00
|
|
|
pub fn selecti<T:Selectable>(endpoints: &mut [T]) -> uint {
|
2013-04-01 20:25:20 -05:00
|
|
|
wait_many(endpoints)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns 0 or 1 depending on which endpoint is ready to receive
|
2013-05-03 16:20:34 -05:00
|
|
|
pub fn select2i<A:Selectable,B:Selectable>(a: &mut A, b: &mut B)
|
|
|
|
-> Either<(), ()> {
|
|
|
|
let mut endpoints = [ a.header(), b.header() ];
|
|
|
|
match wait_many(endpoints) {
|
|
|
|
0 => Left(()),
|
|
|
|
1 => Right(()),
|
2013-05-05 17:18:51 -05:00
|
|
|
_ => fail!("wait returned unexpected index")
|
2013-04-01 20:25:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
/// Waits on a set of endpoints. Returns a message, its index, and a
|
|
|
|
/// list of the remaining endpoints.
|
|
|
|
pub fn select<T:Owned,Tb:Owned>(mut endpoints: ~[RecvPacketBuffered<T, Tb>])
|
|
|
|
-> (uint,
|
|
|
|
Option<T>,
|
|
|
|
~[RecvPacketBuffered<T, Tb>]) {
|
|
|
|
let mut endpoint_headers = ~[];
|
|
|
|
for vec::each_mut(endpoints) |endpoint| {
|
|
|
|
endpoint_headers.push(endpoint.header());
|
|
|
|
}
|
2013-04-01 20:25:20 -05:00
|
|
|
|
2013-05-03 16:20:34 -05:00
|
|
|
let ready = wait_many(endpoint_headers);
|
2013-04-01 20:25:20 -05:00
|
|
|
let mut remaining = endpoints;
|
|
|
|
let port = remaining.swap_remove(ready);
|
|
|
|
let result = try_recv(port);
|
|
|
|
(ready, result, remaining)
|
|
|
|
}
|
|
|
|
|
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
|
2013-02-15 02:51:28 -06:00
|
|
|
pub fn make_some<T>(val: T) -> Option<T> { Some(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)]
|
2013-04-15 10:08:52 -05:00
|
|
|
mod test {
|
2013-03-26 15:38:07 -05:00
|
|
|
use either::Right;
|
2013-02-02 05:10:12 -06:00
|
|
|
use comm::{Chan, Port, oneshot, recv_one, stream, Select2,
|
2013-03-26 15:38:07 -05:00
|
|
|
GenericChan, Peekable};
|
2012-12-27 19:53:04 -06:00
|
|
|
|
2012-07-25 16:46:15 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_select2() {
|
2013-02-02 05:10:12 -06:00
|
|
|
let (p1, c1) = stream();
|
|
|
|
let (p2, c2) = 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
|
|
|
|
2013-05-07 19:57:58 -05:00
|
|
|
let mut tuple = (p1, p2);
|
|
|
|
match tuple.select() {
|
|
|
|
Right(_) => fail!(),
|
|
|
|
_ => (),
|
2012-07-25 16:46:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
c2.send(123);
|
|
|
|
}
|
2012-08-09 13:54:56 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_oneshot() {
|
2013-04-28 00:05:39 -05:00
|
|
|
let (p, c) = oneshot();
|
2012-08-09 13:54:56 -05:00
|
|
|
|
2013-04-28 00:05:39 -05:00
|
|
|
c.send(());
|
2012-08-09 13:54:56 -05:00
|
|
|
|
2013-02-15 02:51:28 -06:00
|
|
|
recv_one(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
|
2013-02-15 02:51:28 -06:00
|
|
|
let _chan = chan;
|
2012-11-02 02:30:13 -05:00
|
|
|
}
|
|
|
|
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!port.peek());
|
2012-11-02 02:30:13 -05:00
|
|
|
}
|
2012-07-25 16:46:15 -05:00
|
|
|
}
|