3 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
4 // file at the top-level directory of this distribution and at
5 // http://rust-lang.org/COPYRIGHT.
7 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 // option. This file may not be copied, modified, or distributed
11 // except according to those terms.
15 // tjc: I don't know why
18 use std::cast::{forget, transmute};
25 blocked_task: Option<Task>,
37 pub struct packet<T> {
39 blocked_task: Option<Task>,
43 pub fn packet<T:Send>() -> *packet<T> {
45 let p: *packet<T> = cast::transmute(~Stuff{
47 blocked_task: None::<Task>,
55 pub fn atomic_xchg(_dst: &mut int, _src: int) -> int { fail!(); }
56 pub fn atomic_xchg_acq(_dst: &mut int, _src: int) -> int { fail!(); }
57 pub fn atomic_xchg_rel(_dst: &mut int, _src: int) -> int { fail!(); }
60 // We should consider moving this to ::std::unsafe, although I
61 // suspect graydon would want us to use void pointers instead.
62 pub unsafe fn uniquify<T>(x: *T) -> ~T {
66 pub fn swap_state_acq(dst: &mut state, src: state) -> state {
68 transmute(rusti::atomic_xchg_acq(transmute(dst), src as int))
72 pub fn swap_state_rel(dst: &mut state, src: state) -> state {
74 transmute(rusti::atomic_xchg_rel(transmute(dst), src as int))
78 pub fn send<T:Send>(mut p: send_packet<T>, payload: T) {
80 let mut p = unsafe { uniquify(p) };
81 assert!((*p).payload.is_none());
82 (*p).payload = Some(payload);
83 let old_state = swap_state_rel(&mut (*p).state, full);
88 // The receiver will eventually clean this up.
91 full => { fail!("duplicate send") }
94 // The receiver will eventually clean this up.
98 // The receiver will never receive this. Rely on drop_glue
99 // to clean everything up.
104 pub fn recv<T:Send>(mut p: recv_packet<T>) -> Option<T> {
106 let mut p = unsafe { uniquify(p) };
108 let old_state = swap_state_acq(&mut (*p).state,
111 empty | blocked => { task::deschedule(); }
113 let payload = util::replace(&mut p.payload, None);
114 return Some(payload.unwrap())
117 assert_eq!(old_state, terminated);
124 pub fn sender_terminate<T:Send>(p: *packet<T>) {
125 let mut p = unsafe { uniquify(p) };
126 match swap_state_rel(&mut (*p).state, terminated) {
128 // The receiver will eventually clean up.
132 // This is impossible
133 fail!("you dun goofed")
136 // I have to clean up, use drop_glue
141 pub fn receiver_terminate<T:Send>(p: *packet<T>) {
142 let mut p = unsafe { uniquify(p) };
143 match swap_state_rel(&mut (*p).state, terminated) {
145 // the sender will clean up
149 // this shouldn't happen.
150 fail!("terminating a blocked packet")
152 terminated | full => {
153 // I have to clean up, use drop_glue
158 pub struct send_packet<T> {
159 p: Option<*packet<T>>,
163 impl<T:Send> Drop for send_packet<T> {
167 let self_p: &mut Option<*packet<T>> =
168 cast::transmute(&self.p);
169 let p = util::replace(self_p, None);
170 sender_terminate(p.unwrap())
176 impl<T:Send> send_packet<T> {
177 pub fn unwrap(&mut self) -> *packet<T> {
178 util::replace(&mut self.p, None).unwrap()
182 pub fn send_packet<T:Send>(p: *packet<T>) -> send_packet<T> {
188 pub struct recv_packet<T> {
189 p: Option<*packet<T>>,
193 impl<T:Send> Drop for recv_packet<T> {
197 let self_p: &mut Option<*packet<T>> =
198 cast::transmute(&self.p);
199 let p = util::replace(self_p, None);
200 receiver_terminate(p.unwrap())
206 impl<T:Send> recv_packet<T> {
207 pub fn unwrap(&mut self) -> *packet<T> {
208 util::replace(&mut self.p, None).unwrap()
212 pub fn recv_packet<T:Send>(p: *packet<T>) -> recv_packet<T> {
218 pub fn entangle<T:Send>() -> (send_packet<T>, recv_packet<T>) {
220 (send_packet(p), recv_packet(p))
227 pub struct ping(::pipes::send_packet<pong>);
228 pub struct pong(::pipes::send_packet<ping>);
230 pub fn liberate_ping(p: ping) -> ::pipes::send_packet<pong> {
232 let _addr : *::pipes::send_packet<pong> = match &p {
233 &ping(ref x) => { cast::transmute(x) }
239 pub fn liberate_pong(p: pong) -> ::pipes::send_packet<ping> {
241 let _addr : *::pipes::send_packet<ping> = match &p {
242 &pong(ref x) => { cast::transmute(x) }
248 pub fn init() -> (client::ping, server::ping) {
255 pub type ping = ::pipes::send_packet<pingpong::ping>;
256 pub type pong = ::pipes::recv_packet<pingpong::pong>;
258 pub fn do_ping(c: ping) -> pong {
259 let (sp, rp) = ::pipes::entangle();
261 ::pipes::send(c, pingpong::ping(sp));
265 pub fn do_pong(c: pong) -> (ping, ()) {
266 let packet = ::pipes::recv(c);
267 if packet.is_none() {
268 fail!("sender closed the connection")
270 (pingpong::liberate_pong(packet.unwrap()), ())
277 pub type ping = ::pipes::recv_packet<pingpong::ping>;
278 pub type pong = ::pipes::send_packet<pingpong::pong>;
280 pub fn do_ping(c: ping) -> (pong, ()) {
281 let packet = ::pipes::recv(c);
282 if packet.is_none() {
283 fail!("sender closed the connection")
285 (pingpong::liberate_ping(packet.unwrap()), ())
288 pub fn do_pong(c: pong) -> ping {
289 let (sp, rp) = ::pipes::entangle();
290 ::pipes::send(c, pingpong::pong(sp));
296 fn client(chan: pingpong::client::ping) {
297 let chan = pingpong::client::do_ping(chan);
298 error!(~"Sent ping");
299 let (_chan, _data) = pingpong::client::do_pong(chan);
300 error!(~"Received pong");
303 fn server(chan: pingpong::server::ping) {
304 let (chan, _data) = pingpong::server::do_ping(chan);
305 error!(~"Received ping");
306 let _chan = pingpong::server::do_pong(chan);
307 error!(~"Sent pong");
312 // Commented out because of option::get error
314 let (client_, server_) = pingpong::init();
315 let client_ = Cell::new(client_);
316 let server_ = Cell::new(server_);
318 task::spawn {|client_|
319 let client__ = client_.take();
322 task::spawn {|server_|
323 let server__ = server_.take();