rust/src/test/run-pass/issue-2718.rs

289 lines
7.7 KiB
Rust
Raw Normal View History

mod pipes {
2012-08-23 16:19:35 -05:00
import unsafe::{forget, transmute};
enum state {
empty,
full,
blocked,
terminated
}
2012-08-27 18:26:35 -05:00
impl state : cmp::Eq {
pure fn eq(&&other: state) -> bool {
(self as uint) == (other as uint)
}
pure fn ne(&&other: state) -> bool { !self.eq(other) }
2012-08-27 18:26:35 -05:00
}
type packet<T: send> = {
mut state: state,
2012-08-20 14:23:37 -05:00
mut blocked_task: Option<task::Task>,
mut payload: Option<T>
};
fn packet<T: send>() -> *packet<T> unsafe {
let p: *packet<T> = unsafe::transmute(~{
mut state: empty,
2012-08-20 14:23:37 -05:00
mut blocked_task: None::<task::Task>,
mut payload: None::<T>
});
p
}
#[abi = "rust-intrinsic"]
mod rusti {
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; }
}
// 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 {
unsafe { unsafe::transmute(x) }
}
2012-08-23 16:19:35 -05:00
fn swap_state_acq(+dst: &mut state, src: state) -> state {
unsafe {
2012-08-23 16:19:35 -05:00
transmute(rusti::atomic_xchg_acq(transmute(dst), src as int))
}
}
2012-08-23 16:19:35 -05:00
fn swap_state_rel(+dst: &mut state, src: state) -> state {
unsafe {
2012-08-23 16:19:35 -05:00
transmute(rusti::atomic_xchg_rel(transmute(dst), src as int))
}
}
fn send<T: send>(-p: send_packet<T>, -payload: T) {
let p = p.unwrap();
let p = unsafe { uniquify(p) };
2012-08-27 18:26:35 -05:00
assert (*p).payload.is_none();
2012-08-20 14:23:37 -05:00
(*p).payload <- Some(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 => {
// Yay, fastpath.
// The receiver will eventually clean this up.
unsafe { forget(p); }
}
2012-08-03 21:59:04 -05:00
full => { fail ~"duplicate send" }
blocked => {
// The receiver will eventually clean this up.
unsafe { forget(p); }
}
2012-08-03 21:59:04 -05:00
terminated => {
// The receiver will never receive this. Rely on drop_glue
// to clean everything up.
}
}
}
2012-08-20 14:23:37 -05:00
fn recv<T: send>(-p: recv_packet<T>) -> Option<T> {
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,
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;
payload <-> (*p).payload;
2012-08-20 14:23:37 -05:00
return Some(option::unwrap(payload))
}
2012-08-03 21:59:04 -05:00
terminated => {
assert old_state == terminated;
2012-08-20 14:23:37 -05:00
return None;
}
}
}
}
fn sender_terminate<T: send>(p: *packet<T>) {
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 => {
// The receiver will eventually clean up.
unsafe { forget(p) }
}
2012-08-03 21:59:04 -05:00
full => {
// This is impossible
fail ~"you dun goofed"
}
2012-08-03 21:59:04 -05:00
terminated => {
// I have to clean up, use drop_glue
}
}
}
fn receiver_terminate<T: send>(p: *packet<T>) {
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 => {
// the sender will clean up
unsafe { forget(p) }
}
2012-08-03 21:59:04 -05:00
blocked => {
// this shouldn't happen.
fail ~"terminating a blocked packet"
}
2012-08-03 21:59:04 -05:00
terminated | full => {
// I have to clean up, use drop_glue
}
}
}
2012-08-15 20:46:55 -05:00
struct send_packet<T: send> {
2012-09-06 21:40:15 -05:00
mut p: Option<*packet<T>>,
drop {
2012-08-20 14:23:37 -05:00
if self.p != None {
let mut p = None;
p <-> self.p;
sender_terminate(option::unwrap(p))
}
}
fn unwrap() -> *packet<T> {
2012-08-20 14:23:37 -05:00
let mut p = None;
p <-> self.p;
option::unwrap(p)
}
}
2012-09-05 17:58:43 -05:00
fn send_packet<T: send>(p: *packet<T>) -> send_packet<T> {
send_packet {
p: Some(p)
}
}
2012-08-15 20:46:55 -05:00
struct recv_packet<T: send> {
2012-09-06 21:40:15 -05:00
mut p: Option<*packet<T>>,
drop {
2012-08-20 14:23:37 -05:00
if self.p != None {
let mut p = None;
p <-> self.p;
receiver_terminate(option::unwrap(p))
}
}
fn unwrap() -> *packet<T> {
2012-08-20 14:23:37 -05:00
let mut p = None;
p <-> self.p;
option::unwrap(p)
}
}
2012-09-05 17:58:43 -05:00
fn recv_packet<T: send>(p: *packet<T>) -> recv_packet<T> {
recv_packet {
p: Some(p)
}
}
fn entangle<T: send>() -> (send_packet<T>, recv_packet<T>) {
let p = packet();
(send_packet(p), recv_packet(p))
}
}
mod pingpong {
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-08-23 16:19:35 -05:00
ping(x) => { unsafe::transmute(ptr::addr_of(x)) }
};
let liberated_value <- *addr;
unsafe::forget(p);
liberated_value
}
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-08-23 16:19:35 -05:00
pong(x) => { unsafe::transmute(ptr::addr_of(x)) }
};
let liberated_value <- *addr;
unsafe::forget(p);
liberated_value
}
fn init() -> (client::ping, server::ping) {
pipes::entangle()
}
mod client {
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();
pipes::send(c, ping(sp));
rp
}
fn do_pong(-c: pong) -> (ping, ()) {
let packet = pipes::recv(c);
2012-08-27 18:26:35 -05:00
if packet.is_none() {
fail ~"sender closed the connection"
}
(liberate_pong(option::unwrap(packet)), ())
}
}
mod server {
type ping = pipes::recv_packet<pingpong::ping>;
type pong = pipes::send_packet<pingpong::pong>;
fn do_ping(-c: ping) -> (pong, ()) {
let packet = pipes::recv(c);
2012-08-27 18:26:35 -05:00
if packet.is_none() {
fail ~"sender closed the connection"
}
(liberate_ping(option::unwrap(packet)), ())
}
fn do_pong(-c: pong) -> ping {
let (sp, rp) = pipes::entangle();
pipes::send(c, pong(sp));
rp
}
}
}
fn client(-chan: pingpong::client::ping) {
let chan = pingpong::client::do_ping(chan);
log(error, ~"Sent ping");
let (chan, _data) = pingpong::client::do_pong(chan);
log(error, ~"Received pong");
}
fn server(-chan: pingpong::server::ping) {
let (chan, _data) = pingpong::server::do_ping(chan);
log(error, ~"Received ping");
let chan = pingpong::server::do_pong(chan);
log(error, ~"Sent pong");
}
fn main() {
/*
// 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_ˊ));
};
*/
}