1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 pub type Task = isize;
14 // tjc: I don't know why
16 use self::state::{empty, full, blocked, terminated};
18 use std::mem::{forget, transmute};
19 use std::mem::{replace, swap};
22 use std::marker::Send;
26 blocked_task: Option<Task>,
30 #[derive(PartialEq, Debug)]
39 pub struct packet<T> {
41 blocked_task: Option<Task>,
45 unsafe impl<T:Send> Send for packet<T> {}
47 pub fn packet<T:Send>() -> *const packet<T> {
49 let p: *const packet<T> = mem::transmute(Box::new(Stuff{
51 blocked_task: None::<Task>,
59 pub fn atomic_xchg(_dst: &mut isize, _src: isize) -> isize { panic!(); }
60 pub fn atomic_xchg_acq(_dst: &mut isize, _src: isize) -> isize { panic!(); }
61 pub fn atomic_xchg_rel(_dst: &mut isize, _src: isize) -> isize { panic!(); }
64 // We should consider moving this to ::std::unsafe, although I
65 // suspect graydon would want us to use void pointers instead.
66 pub unsafe fn uniquify<T>(x: *const T) -> Box<T> {
70 pub fn swap_state_acq(dst: &mut state, src: state) -> state {
72 transmute(rusti::atomic_xchg_acq(transmute(dst), src as isize))
76 pub fn swap_state_rel(dst: &mut state, src: state) -> state {
78 transmute(rusti::atomic_xchg_rel(transmute(dst), src as isize))
82 pub fn send<T:Send>(mut p: send_packet<T>, payload: T) {
84 let mut p = unsafe { uniquify(p) };
85 assert!((*p).payload.is_none());
86 (*p).payload = Some(payload);
87 let old_state = swap_state_rel(&mut (*p).state, full);
92 // The receiver will eventually clean this up.
95 full => { panic!("duplicate send") }
98 // The receiver will eventually clean this up.
102 // The receiver will never receive this. Rely on drop_glue
103 // to clean everything up.
108 pub fn recv<T:Send>(mut p: recv_packet<T>) -> Option<T> {
110 let mut p = unsafe { uniquify(p) };
112 let old_state = swap_state_acq(&mut (*p).state,
115 empty | blocked => { thread::yield_now(); }
117 let payload = replace(&mut p.payload, None);
118 return Some(payload.unwrap())
121 assert_eq!(old_state, terminated);
128 pub fn sender_terminate<T:Send>(p: *const packet<T>) {
129 let mut p = unsafe { uniquify(p) };
130 match swap_state_rel(&mut (*p).state, terminated) {
132 // The receiver will eventually clean up.
136 // This is impossible
137 panic!("you dun goofed")
140 // I have to clean up, use drop_glue
145 pub fn receiver_terminate<T:Send>(p: *const packet<T>) {
146 let mut p = unsafe { uniquify(p) };
147 match swap_state_rel(&mut (*p).state, terminated) {
149 // the sender will clean up
153 // this shouldn't happen.
154 panic!("terminating a blocked packet")
156 terminated | full => {
157 // I have to clean up, use drop_glue
162 pub struct send_packet<T:Send> {
163 p: Option<*const packet<T>>,
166 impl<T:Send> Drop for send_packet<T> {
170 let self_p: &mut Option<*const packet<T>> =
171 mem::transmute(&mut self.p);
172 let p = replace(self_p, None);
173 sender_terminate(p.unwrap())
179 impl<T:Send> send_packet<T> {
180 pub fn unwrap(&mut self) -> *const packet<T> {
181 replace(&mut self.p, None).unwrap()
185 pub fn send_packet<T:Send>(p: *const packet<T>) -> send_packet<T> {
191 pub struct recv_packet<T:Send> {
192 p: Option<*const packet<T>>,
195 impl<T:Send> Drop for recv_packet<T> {
199 let self_p: &mut Option<*const packet<T>> =
200 mem::transmute(&mut self.p);
201 let p = replace(self_p, None);
202 receiver_terminate(p.unwrap())
208 impl<T:Send> recv_packet<T> {
209 pub fn unwrap(&mut self) -> *const packet<T> {
210 replace(&mut self.p, None).unwrap()
214 pub fn recv_packet<T:Send>(p: *const packet<T>) -> recv_packet<T> {
220 pub fn entangle<T:Send>() -> (send_packet<T>, recv_packet<T>) {
222 (send_packet(p), recv_packet(p))
229 pub struct ping(::pipes::send_packet<pong>);
231 unsafe impl Send for ping {}
233 pub struct pong(::pipes::send_packet<ping>);
235 unsafe impl Send for pong {}
237 pub fn liberate_ping(p: ping) -> ::pipes::send_packet<pong> {
239 let _addr : *const ::pipes::send_packet<pong> = match &p {
240 &ping(ref x) => { mem::transmute(x) }
246 pub fn liberate_pong(p: pong) -> ::pipes::send_packet<ping> {
248 let _addr : *const ::pipes::send_packet<ping> = match &p {
249 &pong(ref x) => { mem::transmute(x) }
255 pub fn init() -> (client::ping, server::ping) {
262 pub type ping = ::pipes::send_packet<pingpong::ping>;
263 pub type pong = ::pipes::recv_packet<pingpong::pong>;
265 pub fn do_ping(c: ping) -> pong {
266 let (sp, rp) = ::pipes::entangle();
268 ::pipes::send(c, pingpong::ping(sp));
272 pub fn do_pong(c: pong) -> (ping, ()) {
273 let packet = ::pipes::recv(c);
274 if packet.is_none() {
275 panic!("sender closed the connection")
277 (pingpong::liberate_pong(packet.unwrap()), ())
284 pub type ping = ::pipes::recv_packet<pingpong::ping>;
285 pub type pong = ::pipes::send_packet<pingpong::pong>;
287 pub fn do_ping(c: ping) -> (pong, ()) {
288 let packet = ::pipes::recv(c);
289 if packet.is_none() {
290 panic!("sender closed the connection")
292 (pingpong::liberate_ping(packet.unwrap()), ())
295 pub fn do_pong(c: pong) -> ping {
296 let (sp, rp) = ::pipes::entangle();
297 ::pipes::send(c, pingpong::pong(sp));
303 fn client(chan: pingpong::client::ping) {
304 let chan = pingpong::client::do_ping(chan);
305 println!("Sent ping");
306 let (_chan, _data) = pingpong::client::do_pong(chan);
307 println!("Received pong");
310 fn server(chan: pingpong::server::ping) {
311 let (chan, _data) = pingpong::server::do_ping(chan);
312 println!("Received ping");
313 let _chan = pingpong::server::do_pong(chan);
314 println!("Sent pong");
319 // Commented out because of option::get error
321 let (client_, server_) = pingpong::init();
323 task::spawn {|client_|
324 let client__ = client_.take();
327 task::spawn {|server_|
328 let server__ = server_.take();