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

328 lines
8.8 KiB
Rust
Raw Normal View History

// xfail-fast
// 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.
2013-07-29 14:51:55 -05:00
pub type Task = int;
2012-09-19 00:45:24 -05:00
// tjc: I don't know why
pub mod pipes {
2013-07-29 14:51:55 -05:00
use super::Task;
use std::cast::{forget, transmute};
use std::cast;
use std::task;
use std::util;
pub struct Stuff<T> {
state: state,
2013-07-29 14:51:55 -05:00
blocked_task: Option<Task>,
payload: Option<T>
}
#[deriving(Eq)]
pub enum state {
empty,
full,
blocked,
terminated
}
2013-03-06 21:09:17 -06:00
pub struct packet<T> {
state: state,
2013-07-29 14:51:55 -05:00
blocked_task: Option<Task>,
payload: Option<T>
2013-03-06 21:09:17 -06:00
}
pub fn packet<T:Send>() -> *packet<T> {
unsafe {
let p: *packet<T> = cast::transmute(~Stuff{
state: empty,
2013-07-29 14:51:55 -05:00
blocked_task: None::<Task>,
payload: None::<T>
});
p
}
}
mod rusti {
pub fn atomic_xchg(_dst: &mut int, _src: int) -> int { fail!(); }
pub fn atomic_xchg_acq(_dst: &mut int, _src: int) -> int { fail!(); }
pub fn atomic_xchg_rel(_dst: &mut int, _src: int) -> int { fail!(); }
}
// We should consider moving this to ::std::unsafe, although I
// suspect graydon would want us to use void pointers instead.
2013-05-07 23:33:31 -05:00
pub unsafe fn uniquify<T>(x: *T) -> ~T {
2013-08-17 10:37:42 -05:00
cast::transmute(x)
}
2013-05-07 23:33:31 -05:00
pub fn swap_state_acq(dst: &mut state, src: state) -> state {
unsafe {
2013-02-15 04:44:18 -06:00
transmute(rusti::atomic_xchg_acq(transmute(dst), src as int))
}
}
2013-05-07 23:33:31 -05:00
pub fn swap_state_rel(dst: &mut state, src: state) -> state {
unsafe {
2013-02-15 04:44:18 -06:00
transmute(rusti::atomic_xchg_rel(transmute(dst), src as int))
}
}
pub fn send<T:Send>(mut p: send_packet<T>, payload: T) {
2013-08-17 10:37:42 -05:00
let p = p.unwrap();
let mut p = unsafe { uniquify(p) };
2013-03-28 20:39:09 -05:00
assert!((*p).payload.is_none());
2013-02-15 04:44:18 -06: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.
2013-02-15 04:44:18 -06:00
unsafe { forget(p); }
}
full => { fail!("duplicate send") }
2012-08-03 21:59:04 -05:00
blocked => {
// The receiver will eventually clean this up.
2013-02-15 04:44:18 -06:00
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.
}
}
}
pub fn recv<T:Send>(mut p: recv_packet<T>) -> Option<T> {
2013-08-17 10:37:42 -05:00
let p = p.unwrap();
let mut 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 {
empty | blocked => { task::deschedule(); }
2012-08-03 21:59:04 -05:00
full => {
2013-05-05 23:42:54 -05:00
let payload = util::replace(&mut p.payload, None);
return Some(payload.unwrap())
}
2012-08-03 21:59:04 -05:00
terminated => {
assert_eq!(old_state, terminated);
2012-08-20 14:23:37 -05:00
return None;
}
}
}
}
2013-08-17 10:37:42 -05:00
pub fn sender_terminate<T:Send>(p: *packet<T>) {
let mut 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.
2013-02-15 04:44:18 -06:00
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
}
}
}
2013-08-17 10:37:42 -05:00
pub fn receiver_terminate<T:Send>(p: *packet<T>) {
let mut 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
2013-02-15 04:44:18 -06:00
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
}
}
}
pub struct send_packet<T> {
p: Option<*packet<T>>,
}
#[unsafe_destructor]
impl<T:Send> Drop for send_packet<T> {
2013-09-16 20:18:07 -05:00
fn drop(&mut self) {
unsafe {
if self.p != None {
let self_p: &mut Option<*packet<T>> =
cast::transmute(&self.p);
2013-05-05 23:42:54 -05:00
let p = util::replace(self_p, None);
sender_terminate(p.unwrap())
}
}
}
}
impl<T:Send> send_packet<T> {
pub fn unwrap(&mut self) -> *packet<T> {
2013-05-05 23:42:54 -05:00
util::replace(&mut self.p, None).unwrap()
}
}
pub fn send_packet<T:Send>(p: *packet<T>) -> send_packet<T> {
2012-09-05 17:58:43 -05:00
send_packet {
p: Some(p)
}
}
pub struct recv_packet<T> {
p: Option<*packet<T>>,
}
#[unsafe_destructor]
impl<T:Send> Drop for recv_packet<T> {
2013-09-16 20:18:07 -05:00
fn drop(&mut self) {
unsafe {
if self.p != None {
let self_p: &mut Option<*packet<T>> =
cast::transmute(&self.p);
2013-05-05 23:42:54 -05:00
let p = util::replace(self_p, None);
receiver_terminate(p.unwrap())
}
}
}
}
impl<T:Send> recv_packet<T> {
pub fn unwrap(&mut self) -> *packet<T> {
2013-05-05 23:42:54 -05:00
util::replace(&mut self.p, None).unwrap()
}
}
pub fn recv_packet<T:Send>(p: *packet<T>) -> recv_packet<T> {
2012-09-05 17:58:43 -05:00
recv_packet {
p: Some(p)
}
}
pub fn entangle<T:Send>() -> (send_packet<T>, recv_packet<T>) {
let p = packet();
(send_packet(p), recv_packet(p))
}
}
pub mod pingpong {
use std::cast;
pub struct ping(::pipes::send_packet<pong>);
pub struct pong(::pipes::send_packet<ping>);
2013-05-07 23:33:31 -05:00
pub fn liberate_ping(p: ping) -> ::pipes::send_packet<pong> {
unsafe {
2013-08-17 10:37:42 -05:00
let _addr : *::pipes::send_packet<pong> = match &p {
2013-04-22 16:27:30 -05:00
&ping(ref x) => { cast::transmute(x) }
};
fail!()
}
}
2013-05-07 23:33:31 -05:00
pub fn liberate_pong(p: pong) -> ::pipes::send_packet<ping> {
unsafe {
2013-08-17 10:37:42 -05:00
let _addr : *::pipes::send_packet<ping> = match &p {
2013-04-22 16:27:30 -05:00
&pong(ref x) => { cast::transmute(x) }
};
fail!()
}
}
pub fn init() -> (client::ping, server::ping) {
::pipes::entangle()
}
pub mod client {
use pingpong;
pub type ping = ::pipes::send_packet<pingpong::ping>;
pub type pong = ::pipes::recv_packet<pingpong::pong>;
2013-05-07 23:33:31 -05:00
pub fn do_ping(c: ping) -> pong {
let (sp, rp) = ::pipes::entangle();
2013-02-15 04:44:18 -06:00
::pipes::send(c, pingpong::ping(sp));
rp
}
2013-05-07 23:33:31 -05:00
pub fn do_pong(c: pong) -> (ping, ()) {
2013-02-15 04:44:18 -06:00
let packet = ::pipes::recv(c);
2012-08-27 18:26:35 -05:00
if packet.is_none() {
fail!("sender closed the connection")
}
(pingpong::liberate_pong(packet.unwrap()), ())
}
}
pub mod server {
use pingpong;
pub type ping = ::pipes::recv_packet<pingpong::ping>;
pub type pong = ::pipes::send_packet<pingpong::pong>;
2013-05-07 23:33:31 -05:00
pub fn do_ping(c: ping) -> (pong, ()) {
2013-02-15 04:44:18 -06:00
let packet = ::pipes::recv(c);
2012-08-27 18:26:35 -05:00
if packet.is_none() {
fail!("sender closed the connection")
}
(pingpong::liberate_ping(packet.unwrap()), ())
}
2013-05-07 23:33:31 -05:00
pub fn do_pong(c: pong) -> ping {
let (sp, rp) = ::pipes::entangle();
2013-02-15 04:44:18 -06:00
::pipes::send(c, pingpong::pong(sp));
rp
}
}
}
2013-05-07 23:33:31 -05:00
fn client(chan: pingpong::client::ping) {
2013-02-15 04:44:18 -06:00
let chan = pingpong::client::do_ping(chan);
error!("Sent ping");
2013-02-15 04:44:18 -06:00
let (_chan, _data) = pingpong::client::do_pong(chan);
error!("Received pong");
}
2013-05-07 23:33:31 -05:00
fn server(chan: pingpong::server::ping) {
2013-02-15 04:44:18 -06:00
let (chan, _data) = pingpong::server::do_ping(chan);
error!("Received ping");
2013-02-15 04:44:18 -06:00
let _chan = pingpong::server::do_pong(chan);
error!("Sent pong");
}
pub fn main() {
/*
// Commented out because of option::get error
let (client_, server_) = pingpong::init();
let client_ = Cell::new(client_);
let server_ = Cell::new(server_);
2013-02-15 04:44:18 -06:00
task::spawn {|client_|
let client__ = client_.take();
client(client__);
};
2013-02-15 04:44:18 -06:00
task::spawn {|server_|
let server__ = server_.take();
server(server_ˊ);
};
*/
}