]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issue-2718.rs
Rollup merge of #53110 - Xanewok:save-analysis-remap-path, r=nrc
[rust.git] / src / test / run-pass / issue-2718.rs
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.
4 //
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.
10 //
11
12 pub type Task = isize;
13
14 // tjc: I don't know why
15 pub mod pipes {
16     use self::state::{empty, full, blocked, terminated};
17     use super::Task;
18     use std::mem::{forget, transmute};
19     use std::mem::{replace, swap};
20     use std::mem;
21     use std::thread;
22     use std::marker::Send;
23
24     pub struct Stuff<T> {
25         state: state,
26         blocked_task: Option<Task>,
27         payload: Option<T>
28     }
29
30     #[derive(PartialEq, Debug)]
31     #[repr(isize)]
32     pub enum state {
33         empty,
34         full,
35         blocked,
36         terminated
37     }
38
39     pub struct packet<T> {
40         state: state,
41         blocked_task: Option<Task>,
42         payload: Option<T>
43     }
44
45     unsafe impl<T:Send> Send for packet<T> {}
46
47     pub fn packet<T:Send>() -> *const packet<T> {
48         unsafe {
49             let p: *const packet<T> = mem::transmute(Box::new(Stuff{
50                 state: empty,
51                 blocked_task: None::<Task>,
52                 payload: None::<T>
53             }));
54             p
55         }
56     }
57
58     mod rusti {
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!(); }
62     }
63
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> {
67         mem::transmute(x)
68     }
69
70     pub fn swap_state_acq(dst: &mut state, src: state) -> state {
71         unsafe {
72             transmute(rusti::atomic_xchg_acq(transmute(dst), src as isize))
73         }
74     }
75
76     pub fn swap_state_rel(dst: &mut state, src: state) -> state {
77         unsafe {
78             transmute(rusti::atomic_xchg_rel(transmute(dst), src as isize))
79         }
80     }
81
82     pub fn send<T:Send>(mut p: send_packet<T>, payload: T) {
83         let p = p.unwrap();
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);
88         match old_state {
89           empty => {
90             // Yay, fastpath.
91
92             // The receiver will eventually clean this up.
93             unsafe { forget(p); }
94           }
95           full => { panic!("duplicate send") }
96           blocked => {
97
98             // The receiver will eventually clean this up.
99             unsafe { forget(p); }
100           }
101           terminated => {
102             // The receiver will never receive this. Rely on drop_glue
103             // to clean everything up.
104           }
105         }
106     }
107
108     pub fn recv<T:Send>(mut p: recv_packet<T>) -> Option<T> {
109         let p = p.unwrap();
110         let mut p = unsafe { uniquify(p) };
111         loop {
112             let old_state = swap_state_acq(&mut (*p).state,
113                                            blocked);
114             match old_state {
115               empty | blocked => { thread::yield_now(); }
116               full => {
117                 let payload = replace(&mut p.payload, None);
118                 return Some(payload.unwrap())
119               }
120               terminated => {
121                 assert_eq!(old_state, terminated);
122                 return None;
123               }
124             }
125         }
126     }
127
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) {
131           empty | blocked => {
132             // The receiver will eventually clean up.
133             unsafe { forget(p) }
134           }
135           full => {
136             // This is impossible
137             panic!("you dun goofed")
138           }
139           terminated => {
140             // I have to clean up, use drop_glue
141           }
142         }
143     }
144
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) {
148           empty => {
149             // the sender will clean up
150             unsafe { forget(p) }
151           }
152           blocked => {
153             // this shouldn't happen.
154             panic!("terminating a blocked packet")
155           }
156           terminated | full => {
157             // I have to clean up, use drop_glue
158           }
159         }
160     }
161
162     pub struct send_packet<T:Send> {
163         p: Option<*const packet<T>>,
164     }
165
166     impl<T:Send> Drop for send_packet<T> {
167         fn drop(&mut self) {
168             unsafe {
169                 if self.p != None {
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())
174                 }
175             }
176         }
177     }
178
179     impl<T:Send> send_packet<T> {
180         pub fn unwrap(&mut self) -> *const packet<T> {
181             replace(&mut self.p, None).unwrap()
182         }
183     }
184
185     pub fn send_packet<T:Send>(p: *const packet<T>) -> send_packet<T> {
186         send_packet {
187             p: Some(p)
188         }
189     }
190
191     pub struct recv_packet<T:Send> {
192         p: Option<*const packet<T>>,
193     }
194
195     impl<T:Send> Drop for recv_packet<T> {
196         fn drop(&mut self) {
197             unsafe {
198                 if self.p != None {
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())
203                 }
204             }
205         }
206     }
207
208     impl<T:Send> recv_packet<T> {
209         pub fn unwrap(&mut self) -> *const packet<T> {
210             replace(&mut self.p, None).unwrap()
211         }
212     }
213
214     pub fn recv_packet<T:Send>(p: *const packet<T>) -> recv_packet<T> {
215         recv_packet {
216             p: Some(p)
217         }
218     }
219
220     pub fn entangle<T:Send>() -> (send_packet<T>, recv_packet<T>) {
221         let p = packet();
222         (send_packet(p), recv_packet(p))
223     }
224 }
225
226 pub mod pingpong {
227     use std::mem;
228
229     pub struct ping(::pipes::send_packet<pong>);
230
231     unsafe impl Send for ping {}
232
233     pub struct pong(::pipes::send_packet<ping>);
234
235     unsafe impl Send for pong {}
236
237     pub fn liberate_ping(p: ping) -> ::pipes::send_packet<pong> {
238         unsafe {
239             let _addr : *const ::pipes::send_packet<pong> = match &p {
240               &ping(ref x) => { mem::transmute(x) }
241             };
242             panic!()
243         }
244     }
245
246     pub fn liberate_pong(p: pong) -> ::pipes::send_packet<ping> {
247         unsafe {
248             let _addr : *const ::pipes::send_packet<ping> = match &p {
249               &pong(ref x) => { mem::transmute(x) }
250             };
251             panic!()
252         }
253     }
254
255     pub fn init() -> (client::ping, server::ping) {
256         ::pipes::entangle()
257     }
258
259     pub mod client {
260         use pingpong;
261
262         pub type ping = ::pipes::send_packet<pingpong::ping>;
263         pub type pong = ::pipes::recv_packet<pingpong::pong>;
264
265         pub fn do_ping(c: ping) -> pong {
266             let (sp, rp) = ::pipes::entangle();
267
268             ::pipes::send(c, pingpong::ping(sp));
269             rp
270         }
271
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")
276             }
277             (pingpong::liberate_pong(packet.unwrap()), ())
278         }
279     }
280
281     pub mod server {
282         use pingpong;
283
284         pub type ping = ::pipes::recv_packet<pingpong::ping>;
285         pub type pong = ::pipes::send_packet<pingpong::pong>;
286
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")
291             }
292             (pingpong::liberate_ping(packet.unwrap()), ())
293         }
294
295         pub fn do_pong(c: pong) -> ping {
296             let (sp, rp) = ::pipes::entangle();
297             ::pipes::send(c, pingpong::pong(sp));
298             rp
299         }
300     }
301 }
302
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");
308 }
309
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");
315 }
316
317 pub fn main() {
318   /*
319 //    Commented out because of option::get error
320
321     let (client_, server_) = pingpong::init();
322
323     task::spawn {|client_|
324         let client__ = client_.take();
325         client(client__);
326     };
327     task::spawn {|server_|
328         let server__ = server_.take();
329         server(server_ˊ);
330     };
331   */
332 }