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 #![feature(unsafe_destructor)]
18 // tjc: I don't know why
20 use self::state::{empty, full, blocked, terminated};
22 use std::mem::{forget, transmute};
23 use std::mem::{replace, swap};
25 use std::thread::Thread;
30 blocked_task: Option<Task>,
34 #[derive(PartialEq, Show)]
43 pub struct packet<T> {
45 blocked_task: Option<Task>,
49 unsafe impl<T:Send> Send for packet<T> {}
51 pub fn packet<T:Send>() -> *const packet<T> {
53 let p: *const packet<T> = mem::transmute(box Stuff{
55 blocked_task: None::<Task>,
63 pub fn atomic_xchg(_dst: &mut int, _src: int) -> int { panic!(); }
64 pub fn atomic_xchg_acq(_dst: &mut int, _src: int) -> int { panic!(); }
65 pub fn atomic_xchg_rel(_dst: &mut int, _src: int) -> int { panic!(); }
68 // We should consider moving this to ::std::unsafe, although I
69 // suspect graydon would want us to use void pointers instead.
70 pub unsafe fn uniquify<T>(x: *const T) -> Box<T> {
74 pub fn swap_state_acq(dst: &mut state, src: state) -> state {
76 transmute(rusti::atomic_xchg_acq(transmute(dst), src as int))
80 pub fn swap_state_rel(dst: &mut state, src: state) -> state {
82 transmute(rusti::atomic_xchg_rel(transmute(dst), src as int))
86 pub fn send<T:Send>(mut p: send_packet<T>, payload: T) {
88 let mut p = unsafe { uniquify(p) };
89 assert!((*p).payload.is_none());
90 (*p).payload = Some(payload);
91 let old_state = swap_state_rel(&mut (*p).state, full);
96 // The receiver will eventually clean this up.
99 full => { panic!("duplicate send") }
102 // The receiver will eventually clean this up.
103 unsafe { forget(p); }
106 // The receiver will never receive this. Rely on drop_glue
107 // to clean everything up.
112 pub fn recv<T:Send>(mut p: recv_packet<T>) -> Option<T> {
114 let mut p = unsafe { uniquify(p) };
116 let old_state = swap_state_acq(&mut (*p).state,
119 empty | blocked => { Thread::yield_now(); }
121 let payload = replace(&mut p.payload, None);
122 return Some(payload.unwrap())
125 assert_eq!(old_state, terminated);
132 pub fn sender_terminate<T:Send>(p: *const packet<T>) {
133 let mut p = unsafe { uniquify(p) };
134 match swap_state_rel(&mut (*p).state, terminated) {
136 // The receiver will eventually clean up.
140 // This is impossible
141 panic!("you dun goofed")
144 // I have to clean up, use drop_glue
149 pub fn receiver_terminate<T:Send>(p: *const packet<T>) {
150 let mut p = unsafe { uniquify(p) };
151 match swap_state_rel(&mut (*p).state, terminated) {
153 // the sender will clean up
157 // this shouldn't happen.
158 panic!("terminating a blocked packet")
160 terminated | full => {
161 // I have to clean up, use drop_glue
166 pub struct send_packet<T> {
167 p: Option<*const packet<T>>,
171 impl<T:Send> Drop for send_packet<T> {
175 let self_p: &mut Option<*const packet<T>> =
176 mem::transmute(&self.p);
177 let p = replace(self_p, None);
178 sender_terminate(p.unwrap())
184 impl<T:Send> send_packet<T> {
185 pub fn unwrap(&mut self) -> *const packet<T> {
186 replace(&mut self.p, None).unwrap()
190 pub fn send_packet<T:Send>(p: *const packet<T>) -> send_packet<T> {
196 pub struct recv_packet<T> {
197 p: Option<*const packet<T>>,
201 impl<T:Send> Drop for recv_packet<T> {
205 let self_p: &mut Option<*const packet<T>> =
206 mem::transmute(&self.p);
207 let p = replace(self_p, None);
208 receiver_terminate(p.unwrap())
214 impl<T:Send> recv_packet<T> {
215 pub fn unwrap(&mut self) -> *const packet<T> {
216 replace(&mut self.p, None).unwrap()
220 pub fn recv_packet<T:Send>(p: *const packet<T>) -> recv_packet<T> {
226 pub fn entangle<T:Send>() -> (send_packet<T>, recv_packet<T>) {
228 (send_packet(p), recv_packet(p))
235 pub struct ping(::pipes::send_packet<pong>);
237 unsafe impl Send for ping {}
239 pub struct pong(::pipes::send_packet<ping>);
241 unsafe impl Send for pong {}
243 pub fn liberate_ping(p: ping) -> ::pipes::send_packet<pong> {
245 let _addr : *const ::pipes::send_packet<pong> = match &p {
246 &ping(ref x) => { mem::transmute(x) }
252 pub fn liberate_pong(p: pong) -> ::pipes::send_packet<ping> {
254 let _addr : *const ::pipes::send_packet<ping> = match &p {
255 &pong(ref x) => { mem::transmute(x) }
261 pub fn init() -> (client::ping, server::ping) {
268 pub type ping = ::pipes::send_packet<pingpong::ping>;
269 pub type pong = ::pipes::recv_packet<pingpong::pong>;
271 pub fn do_ping(c: ping) -> pong {
272 let (sp, rp) = ::pipes::entangle();
274 ::pipes::send(c, pingpong::ping(sp));
278 pub fn do_pong(c: pong) -> (ping, ()) {
279 let packet = ::pipes::recv(c);
280 if packet.is_none() {
281 panic!("sender closed the connection")
283 (pingpong::liberate_pong(packet.unwrap()), ())
290 pub type ping = ::pipes::recv_packet<pingpong::ping>;
291 pub type pong = ::pipes::send_packet<pingpong::pong>;
293 pub fn do_ping(c: ping) -> (pong, ()) {
294 let packet = ::pipes::recv(c);
295 if packet.is_none() {
296 panic!("sender closed the connection")
298 (pingpong::liberate_ping(packet.unwrap()), ())
301 pub fn do_pong(c: pong) -> ping {
302 let (sp, rp) = ::pipes::entangle();
303 ::pipes::send(c, pingpong::pong(sp));
309 fn client(chan: pingpong::client::ping) {
310 let chan = pingpong::client::do_ping(chan);
311 println!("Sent ping");
312 let (_chan, _data) = pingpong::client::do_pong(chan);
313 println!("Received pong");
316 fn server(chan: pingpong::server::ping) {
317 let (chan, _data) = pingpong::server::do_ping(chan);
318 println!("Received ping");
319 let _chan = pingpong::server::do_pong(chan);
320 println!("Sent pong");
325 // Commented out because of option::get error
327 let (client_, server_) = pingpong::init();
329 task::spawn {|client_|
330 let client__ = client_.take();
333 task::spawn {|server_|
334 let server__ = server_.take();