2012-09-19 00:45:24 -05:00
|
|
|
|
// tjc: I don't know why
|
2012-06-26 00:11:19 -05:00
|
|
|
|
mod pipes {
|
2012-09-21 20:10:45 -05:00
|
|
|
|
#[legacy_exports];
|
2012-09-18 19:34:08 -05:00
|
|
|
|
use cast::{forget, transmute};
|
2012-06-26 00:11:19 -05:00
|
|
|
|
|
|
|
|
|
enum state {
|
|
|
|
|
empty,
|
|
|
|
|
full,
|
|
|
|
|
blocked,
|
|
|
|
|
terminated
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-27 18:26:35 -05:00
|
|
|
|
impl state : cmp::Eq {
|
2012-09-19 20:00:26 -05:00
|
|
|
|
pure fn eq(other: &state) -> bool {
|
|
|
|
|
(self as uint) == ((*other) as uint)
|
2012-08-27 18:26:35 -05:00
|
|
|
|
}
|
2012-09-19 20:00:26 -05:00
|
|
|
|
pure fn ne(other: &state) -> bool { !self.eq(other) }
|
2012-08-27 18:26:35 -05:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
|
type packet<T: Send> = {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
mut state: state,
|
2012-08-20 14:23:37 -05:00
|
|
|
|
mut blocked_task: Option<task::Task>,
|
|
|
|
|
mut payload: Option<T>
|
2012-06-26 00:11:19 -05:00
|
|
|
|
};
|
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
|
fn packet<T: Send>() -> *packet<T> unsafe {
|
2012-09-18 19:34:08 -05:00
|
|
|
|
let p: *packet<T> = cast::transmute(~{
|
2012-06-26 00:11:19 -05:00
|
|
|
|
mut state: empty,
|
2012-08-20 14:23:37 -05:00
|
|
|
|
mut blocked_task: None::<task::Task>,
|
|
|
|
|
mut payload: None::<T>
|
2012-06-26 00:11:19 -05:00
|
|
|
|
});
|
|
|
|
|
p
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[abi = "rust-intrinsic"]
|
|
|
|
|
mod rusti {
|
2012-09-21 20:10:45 -05:00
|
|
|
|
#[legacy_exports];
|
2012-08-23 16:19:35 -05:00
|
|
|
|
fn atomic_xchg(_dst: &mut int, _src: int) -> int { fail; }
|
|
|
|
|
fn atomic_xchg_acq(_dst: &mut int, _src: int) -> int { fail; }
|
|
|
|
|
fn atomic_xchg_rel(_dst: &mut int, _src: int) -> int { fail; }
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We should consider moving this to core::unsafe, although I
|
|
|
|
|
// suspect graydon would want us to use void pointers instead.
|
2012-08-23 16:19:35 -05:00
|
|
|
|
unsafe fn uniquify<T>(+x: *T) -> ~T {
|
2012-09-19 00:45:24 -05:00
|
|
|
|
unsafe { cast::transmute(move x) }
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-23 16:19:35 -05:00
|
|
|
|
fn swap_state_acq(+dst: &mut state, src: state) -> state {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
unsafe {
|
2012-09-19 00:45:24 -05:00
|
|
|
|
transmute(rusti::atomic_xchg_acq(transmute(move dst), src as int))
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-23 16:19:35 -05:00
|
|
|
|
fn swap_state_rel(+dst: &mut state, src: state) -> state {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
unsafe {
|
2012-09-19 00:45:24 -05:00
|
|
|
|
transmute(rusti::atomic_xchg_rel(transmute(move dst), src as int))
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
|
fn send<T: Send>(-p: send_packet<T>, -payload: T) {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
let p = p.unwrap();
|
|
|
|
|
let p = unsafe { uniquify(p) };
|
2012-08-27 18:26:35 -05:00
|
|
|
|
assert (*p).payload.is_none();
|
2012-10-23 13:11:23 -05:00
|
|
|
|
(*p).payload = move Some(move payload);
|
2012-08-23 16:19:35 -05:00
|
|
|
|
let old_state = swap_state_rel(&mut (*p).state, full);
|
2012-08-06 14:34:08 -05:00
|
|
|
|
match old_state {
|
2012-08-03 21:59:04 -05:00
|
|
|
|
empty => {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
// Yay, fastpath.
|
2012-06-25 14:21:01 -05:00
|
|
|
|
|
2012-06-26 00:11:19 -05:00
|
|
|
|
// The receiver will eventually clean this up.
|
2012-09-19 00:45:24 -05:00
|
|
|
|
unsafe { forget(move p); }
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
|
full => { fail ~"duplicate send" }
|
|
|
|
|
blocked => {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
|
|
|
|
|
// The receiver will eventually clean this up.
|
2012-09-19 00:45:24 -05:00
|
|
|
|
unsafe { forget(move p); }
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
|
terminated => {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
// The receiver will never receive this. Rely on drop_glue
|
|
|
|
|
// to clean everything up.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
|
fn recv<T: Send>(-p: recv_packet<T>) -> Option<T> {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
let p = p.unwrap();
|
|
|
|
|
let p = unsafe { uniquify(p) };
|
|
|
|
|
loop {
|
2012-08-23 16:19:35 -05:00
|
|
|
|
let old_state = swap_state_acq(&mut (*p).state,
|
2012-06-26 00:11:19 -05:00
|
|
|
|
blocked);
|
2012-08-06 14:34:08 -05:00
|
|
|
|
match old_state {
|
2012-08-03 21:59:04 -05:00
|
|
|
|
empty | blocked => { task::yield(); }
|
|
|
|
|
full => {
|
2012-08-20 14:23:37 -05:00
|
|
|
|
let mut payload = None;
|
2012-06-26 00:11:19 -05:00
|
|
|
|
payload <-> (*p).payload;
|
2012-09-19 00:45:24 -05:00
|
|
|
|
return Some(option::unwrap(move payload))
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
|
terminated => {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
assert old_state == terminated;
|
2012-08-20 14:23:37 -05:00
|
|
|
|
return None;
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
|
fn sender_terminate<T: Send>(p: *packet<T>) {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
let p = unsafe { uniquify(p) };
|
2012-08-23 16:19:35 -05:00
|
|
|
|
match swap_state_rel(&mut (*p).state, terminated) {
|
2012-08-03 21:59:04 -05:00
|
|
|
|
empty | blocked => {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
// The receiver will eventually clean up.
|
2012-09-19 00:45:24 -05:00
|
|
|
|
unsafe { forget(move p) }
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
|
full => {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
// This is impossible
|
2012-07-14 00:57:48 -05:00
|
|
|
|
fail ~"you dun goofed"
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
|
terminated => {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
// I have to clean up, use drop_glue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
|
fn receiver_terminate<T: Send>(p: *packet<T>) {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
let p = unsafe { uniquify(p) };
|
2012-08-23 16:19:35 -05:00
|
|
|
|
match swap_state_rel(&mut (*p).state, terminated) {
|
2012-08-03 21:59:04 -05:00
|
|
|
|
empty => {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
// the sender will clean up
|
2012-09-19 00:45:24 -05:00
|
|
|
|
unsafe { forget(move p) }
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
|
blocked => {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
// this shouldn't happen.
|
2012-07-14 00:57:48 -05:00
|
|
|
|
fail ~"terminating a blocked packet"
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
|
terminated | full => {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
// I have to clean up, use drop_glue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
|
struct send_packet<T: Send> {
|
2012-09-06 21:40:15 -05:00
|
|
|
|
mut p: Option<*packet<T>>,
|
2012-06-25 14:21:01 -05:00
|
|
|
|
drop {
|
2012-08-20 14:23:37 -05:00
|
|
|
|
if self.p != None {
|
|
|
|
|
let mut p = None;
|
2012-06-25 14:21:01 -05:00
|
|
|
|
p <-> self.p;
|
2012-09-19 00:45:24 -05:00
|
|
|
|
sender_terminate(option::unwrap(move p))
|
2012-06-25 14:21:01 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-07 21:04:40 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Send> send_packet<T> {
|
2012-06-25 14:21:01 -05:00
|
|
|
|
fn unwrap() -> *packet<T> {
|
2012-08-20 14:23:37 -05:00
|
|
|
|
let mut p = None;
|
2012-06-25 14:21:01 -05:00
|
|
|
|
p <-> self.p;
|
2012-09-19 00:45:24 -05:00
|
|
|
|
option::unwrap(move p)
|
2012-06-25 14:21:01 -05:00
|
|
|
|
}
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
|
fn send_packet<T: Send>(p: *packet<T>) -> send_packet<T> {
|
2012-09-05 17:58:43 -05:00
|
|
|
|
send_packet {
|
|
|
|
|
p: Some(p)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
|
struct recv_packet<T: Send> {
|
2012-09-06 21:40:15 -05:00
|
|
|
|
mut p: Option<*packet<T>>,
|
2012-06-26 00:11:19 -05:00
|
|
|
|
drop {
|
2012-08-20 14:23:37 -05:00
|
|
|
|
if self.p != None {
|
|
|
|
|
let mut p = None;
|
2012-06-26 00:11:19 -05:00
|
|
|
|
p <-> self.p;
|
2012-09-19 00:45:24 -05:00
|
|
|
|
receiver_terminate(option::unwrap(move p))
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-07 21:04:40 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Send> recv_packet<T> {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
fn unwrap() -> *packet<T> {
|
2012-08-20 14:23:37 -05:00
|
|
|
|
let mut p = None;
|
2012-06-26 00:11:19 -05:00
|
|
|
|
p <-> self.p;
|
2012-09-19 00:45:24 -05:00
|
|
|
|
option::unwrap(move p)
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
|
fn recv_packet<T: Send>(p: *packet<T>) -> recv_packet<T> {
|
2012-09-05 17:58:43 -05:00
|
|
|
|
recv_packet {
|
|
|
|
|
p: Some(p)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
|
fn entangle<T: Send>() -> (send_packet<T>, recv_packet<T>) {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
let p = packet();
|
|
|
|
|
(send_packet(p), recv_packet(p))
|
|
|
|
|
}
|
2012-06-25 14:21:01 -05:00
|
|
|
|
}
|
|
|
|
|
|
2012-06-26 00:11:19 -05:00
|
|
|
|
mod pingpong {
|
2012-09-21 20:10:45 -05:00
|
|
|
|
#[legacy_exports];
|
2012-06-26 00:11:19 -05:00
|
|
|
|
enum ping = pipes::send_packet<pong>;
|
|
|
|
|
enum pong = pipes::send_packet<ping>;
|
|
|
|
|
|
|
|
|
|
fn liberate_ping(-p: ping) -> pipes::send_packet<pong> unsafe {
|
2012-08-06 14:34:08 -05:00
|
|
|
|
let addr : *pipes::send_packet<pong> = match p {
|
2012-10-01 14:47:02 -05:00
|
|
|
|
ping(x) => { cast::transmute(ptr::addr_of(&x)) }
|
2012-06-26 00:11:19 -05:00
|
|
|
|
};
|
2012-10-23 13:11:23 -05:00
|
|
|
|
let liberated_value = move *addr;
|
2012-09-19 00:45:24 -05:00
|
|
|
|
cast::forget(move p);
|
|
|
|
|
move liberated_value
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn liberate_pong(-p: pong) -> pipes::send_packet<ping> unsafe {
|
2012-08-06 14:34:08 -05:00
|
|
|
|
let addr : *pipes::send_packet<ping> = match p {
|
2012-10-01 14:47:02 -05:00
|
|
|
|
pong(x) => { cast::transmute(ptr::addr_of(&x)) }
|
2012-06-26 00:11:19 -05:00
|
|
|
|
};
|
2012-10-23 13:11:23 -05:00
|
|
|
|
let liberated_value = move *addr;
|
2012-09-19 00:45:24 -05:00
|
|
|
|
cast::forget(move p);
|
|
|
|
|
move liberated_value
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn init() -> (client::ping, server::ping) {
|
|
|
|
|
pipes::entangle()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mod client {
|
2012-09-21 20:10:45 -05:00
|
|
|
|
#[legacy_exports];
|
2012-06-26 00:11:19 -05:00
|
|
|
|
type ping = pipes::send_packet<pingpong::ping>;
|
|
|
|
|
type pong = pipes::recv_packet<pingpong::pong>;
|
|
|
|
|
|
|
|
|
|
fn do_ping(-c: ping) -> pong {
|
|
|
|
|
let (sp, rp) = pipes::entangle();
|
|
|
|
|
|
2012-09-19 00:45:24 -05:00
|
|
|
|
pipes::send(move c, ping(move sp));
|
|
|
|
|
move rp
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn do_pong(-c: pong) -> (ping, ()) {
|
2012-09-19 00:45:24 -05:00
|
|
|
|
let packet = pipes::recv(move c);
|
2012-08-27 18:26:35 -05:00
|
|
|
|
if packet.is_none() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
fail ~"sender closed the connection"
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
2012-09-19 00:45:24 -05:00
|
|
|
|
(liberate_pong(option::unwrap(move packet)), ())
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mod server {
|
2012-09-21 20:10:45 -05:00
|
|
|
|
#[legacy_exports];
|
2012-06-26 00:11:19 -05:00
|
|
|
|
type ping = pipes::recv_packet<pingpong::ping>;
|
|
|
|
|
type pong = pipes::send_packet<pingpong::pong>;
|
|
|
|
|
|
|
|
|
|
fn do_ping(-c: ping) -> (pong, ()) {
|
2012-09-19 00:45:24 -05:00
|
|
|
|
let packet = pipes::recv(move c);
|
2012-08-27 18:26:35 -05:00
|
|
|
|
if packet.is_none() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
fail ~"sender closed the connection"
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
2012-09-19 00:45:24 -05:00
|
|
|
|
(liberate_ping(option::unwrap(move packet)), ())
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn do_pong(-c: pong) -> ping {
|
|
|
|
|
let (sp, rp) = pipes::entangle();
|
2012-09-19 00:45:24 -05:00
|
|
|
|
pipes::send(move c, pong(move sp));
|
|
|
|
|
move rp
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-06-25 14:21:01 -05:00
|
|
|
|
}
|
|
|
|
|
|
2012-06-26 00:11:19 -05:00
|
|
|
|
fn client(-chan: pingpong::client::ping) {
|
2012-09-19 00:45:24 -05:00
|
|
|
|
let chan = pingpong::client::do_ping(move chan);
|
2012-07-14 00:57:48 -05:00
|
|
|
|
log(error, ~"Sent ping");
|
2012-09-19 00:45:24 -05:00
|
|
|
|
let (_chan, _data) = pingpong::client::do_pong(move chan);
|
2012-07-14 00:57:48 -05:00
|
|
|
|
log(error, ~"Received pong");
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn server(-chan: pingpong::server::ping) {
|
2012-09-19 00:45:24 -05:00
|
|
|
|
let (chan, _data) = pingpong::server::do_ping(move chan);
|
2012-07-14 00:57:48 -05:00
|
|
|
|
log(error, ~"Received ping");
|
2012-09-19 00:45:24 -05:00
|
|
|
|
let _chan = pingpong::server::do_pong(move chan);
|
2012-07-14 00:57:48 -05:00
|
|
|
|
log(error, ~"Sent pong");
|
2012-06-26 00:11:19 -05:00
|
|
|
|
}
|
2012-06-25 14:21:01 -05:00
|
|
|
|
|
|
|
|
|
fn main() {
|
2012-06-26 00:11:19 -05:00
|
|
|
|
/*
|
|
|
|
|
// Commented out because of option::get error
|
|
|
|
|
|
|
|
|
|
let (client_, server_) = pingpong::init();
|
|
|
|
|
let client_ = ~mut some(client_);
|
|
|
|
|
let server_ = ~mut some(server_);
|
|
|
|
|
|
|
|
|
|
task::spawn {|move client_|
|
|
|
|
|
let mut client__ = none;
|
|
|
|
|
*client_ <-> client__;
|
|
|
|
|
client(option::unwrap(client__));
|
|
|
|
|
};
|
|
|
|
|
task::spawn {|move server_|
|
|
|
|
|
let mut server_ˊ = none;
|
|
|
|
|
*server_ <-> server_ˊ;
|
|
|
|
|
server(option::unwrap(server_ˊ));
|
|
|
|
|
};
|
|
|
|
|
*/
|
2012-06-25 14:21:01 -05:00
|
|
|
|
}
|