2 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
3 // file at the top-level directory of this distribution and at
4 // http://rust-lang.org/COPYRIGHT.
6 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9 // option. This file may not be copied, modified, or distributed
10 // except according to those terms.
12 // ignore-lexer-test FIXME #15883
14 #![allow(unknown_features)]
15 #![feature(box_syntax)]
16 #![feature(unsafe_destructor)]
20 // tjc: I don't know why
22 use self::state::{empty, full, blocked, terminated};
24 use std::mem::{forget, transmute};
25 use std::mem::{replace, swap};
27 use std::thread::Thread;
28 use std::marker::Send;
32 blocked_task: Option<Task>,
36 #[derive(PartialEq, Show)]
45 pub struct packet<T> {
47 blocked_task: Option<Task>,
51 unsafe impl<T:Send> Send for packet<T> {}
53 pub fn packet<T:Send>() -> *const packet<T> {
55 let p: *const packet<T> = mem::transmute(box Stuff{
57 blocked_task: None::<Task>,
65 pub fn atomic_xchg(_dst: &mut int, _src: int) -> int { panic!(); }
66 pub fn atomic_xchg_acq(_dst: &mut int, _src: int) -> int { panic!(); }
67 pub fn atomic_xchg_rel(_dst: &mut int, _src: int) -> int { panic!(); }
70 // We should consider moving this to ::std::unsafe, although I
71 // suspect graydon would want us to use void pointers instead.
72 pub unsafe fn uniquify<T>(x: *const T) -> Box<T> {
76 pub fn swap_state_acq(dst: &mut state, src: state) -> state {
78 transmute(rusti::atomic_xchg_acq(transmute(dst), src as int))
82 pub fn swap_state_rel(dst: &mut state, src: state) -> state {
84 transmute(rusti::atomic_xchg_rel(transmute(dst), src as int))
88 pub fn send<T:Send>(mut p: send_packet<T>, payload: T) {
90 let mut p = unsafe { uniquify(p) };
91 assert!((*p).payload.is_none());
92 (*p).payload = Some(payload);
93 let old_state = swap_state_rel(&mut (*p).state, full);
98 // The receiver will eventually clean this up.
101 full => { panic!("duplicate send") }
104 // The receiver will eventually clean this up.
105 unsafe { forget(p); }
108 // The receiver will never receive this. Rely on drop_glue
109 // to clean everything up.
114 pub fn recv<T:Send>(mut p: recv_packet<T>) -> Option<T> {
116 let mut p = unsafe { uniquify(p) };
118 let old_state = swap_state_acq(&mut (*p).state,
121 empty | blocked => { Thread::yield_now(); }
123 let payload = replace(&mut p.payload, None);
124 return Some(payload.unwrap())
127 assert_eq!(old_state, terminated);
134 pub fn sender_terminate<T:Send>(p: *const packet<T>) {
135 let mut p = unsafe { uniquify(p) };
136 match swap_state_rel(&mut (*p).state, terminated) {
138 // The receiver will eventually clean up.
142 // This is impossible
143 panic!("you dun goofed")
146 // I have to clean up, use drop_glue
151 pub fn receiver_terminate<T:Send>(p: *const packet<T>) {
152 let mut p = unsafe { uniquify(p) };
153 match swap_state_rel(&mut (*p).state, terminated) {
155 // the sender will clean up
159 // this shouldn't happen.
160 panic!("terminating a blocked packet")
162 terminated | full => {
163 // I have to clean up, use drop_glue
168 pub struct send_packet<T> {
169 p: Option<*const packet<T>>,
173 impl<T:Send> Drop for send_packet<T> {
177 let self_p: &mut Option<*const packet<T>> =
178 mem::transmute(&self.p);
179 let p = replace(self_p, None);
180 sender_terminate(p.unwrap())
186 impl<T:Send> send_packet<T> {
187 pub fn unwrap(&mut self) -> *const packet<T> {
188 replace(&mut self.p, None).unwrap()
192 pub fn send_packet<T:Send>(p: *const packet<T>) -> send_packet<T> {
198 pub struct recv_packet<T> {
199 p: Option<*const packet<T>>,
203 impl<T:Send> Drop for recv_packet<T> {
207 let self_p: &mut Option<*const packet<T>> =
208 mem::transmute(&self.p);
209 let p = replace(self_p, None);
210 receiver_terminate(p.unwrap())
216 impl<T:Send> recv_packet<T> {
217 pub fn unwrap(&mut self) -> *const packet<T> {
218 replace(&mut self.p, None).unwrap()
222 pub fn recv_packet<T:Send>(p: *const packet<T>) -> recv_packet<T> {
228 pub fn entangle<T:Send>() -> (send_packet<T>, recv_packet<T>) {
230 (send_packet(p), recv_packet(p))
237 pub struct ping(::pipes::send_packet<pong>);
239 unsafe impl Send for ping {}
241 pub struct pong(::pipes::send_packet<ping>);
243 unsafe impl Send for pong {}
245 pub fn liberate_ping(p: ping) -> ::pipes::send_packet<pong> {
247 let _addr : *const ::pipes::send_packet<pong> = match &p {
248 &ping(ref x) => { mem::transmute(x) }
254 pub fn liberate_pong(p: pong) -> ::pipes::send_packet<ping> {
256 let _addr : *const ::pipes::send_packet<ping> = match &p {
257 &pong(ref x) => { mem::transmute(x) }
263 pub fn init() -> (client::ping, server::ping) {
270 pub type ping = ::pipes::send_packet<pingpong::ping>;
271 pub type pong = ::pipes::recv_packet<pingpong::pong>;
273 pub fn do_ping(c: ping) -> pong {
274 let (sp, rp) = ::pipes::entangle();
276 ::pipes::send(c, pingpong::ping(sp));
280 pub fn do_pong(c: pong) -> (ping, ()) {
281 let packet = ::pipes::recv(c);
282 if packet.is_none() {
283 panic!("sender closed the connection")
285 (pingpong::liberate_pong(packet.unwrap()), ())
292 pub type ping = ::pipes::recv_packet<pingpong::ping>;
293 pub type pong = ::pipes::send_packet<pingpong::pong>;
295 pub fn do_ping(c: ping) -> (pong, ()) {
296 let packet = ::pipes::recv(c);
297 if packet.is_none() {
298 panic!("sender closed the connection")
300 (pingpong::liberate_ping(packet.unwrap()), ())
303 pub fn do_pong(c: pong) -> ping {
304 let (sp, rp) = ::pipes::entangle();
305 ::pipes::send(c, pingpong::pong(sp));
311 fn client(chan: pingpong::client::ping) {
312 let chan = pingpong::client::do_ping(chan);
313 println!("Sent ping");
314 let (_chan, _data) = pingpong::client::do_pong(chan);
315 println!("Received pong");
318 fn server(chan: pingpong::server::ping) {
319 let (chan, _data) = pingpong::server::do_ping(chan);
320 println!("Received ping");
321 let _chan = pingpong::server::do_pong(chan);
322 println!("Sent pong");
327 // Commented out because of option::get error
329 let (client_, server_) = pingpong::init();
331 task::spawn {|client_|
332 let client__ = client_.take();
335 task::spawn {|server_|
336 let server__ = server_.take();