]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/issues/issue-33770.rs
New upstream version 1.31.0~beta.4+dfsg1
[rustc.git] / src / test / run-pass / issues / issue-33770.rs
1 // Copyright 2016 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 // run-pass
12 // ignore-cloudabi no processes
13 // ignore-emscripten no processes
14
15 use std::process::{Command, Stdio};
16 use std::env;
17 use std::sync::{Mutex, RwLock};
18 use std::time::Duration;
19 use std::thread;
20
21 fn test_mutex() {
22 let m = Mutex::new(0);
23 let _g = m.lock().unwrap();
24 let _g2 = m.lock().unwrap();
25 }
26
27 fn test_try_mutex() {
28 let m = Mutex::new(0);
29 let _g = m.lock().unwrap();
30 let _g2 = m.try_lock().unwrap();
31 }
32
33 fn test_rwlock_ww() {
34 let m = RwLock::new(0);
35 let _g = m.write().unwrap();
36 let _g2 = m.write().unwrap();
37 }
38
39 fn test_try_rwlock_ww() {
40 let m = RwLock::new(0);
41 let _g = m.write().unwrap();
42 let _g2 = m.try_write().unwrap();
43 }
44
45 fn test_rwlock_rw() {
46 let m = RwLock::new(0);
47 let _g = m.read().unwrap();
48 let _g2 = m.write().unwrap();
49 }
50
51 fn test_try_rwlock_rw() {
52 let m = RwLock::new(0);
53 let _g = m.read().unwrap();
54 let _g2 = m.try_write().unwrap();
55 }
56
57 fn test_rwlock_wr() {
58 let m = RwLock::new(0);
59 let _g = m.write().unwrap();
60 let _g2 = m.read().unwrap();
61 }
62
63 fn test_try_rwlock_wr() {
64 let m = RwLock::new(0);
65 let _g = m.write().unwrap();
66 let _g2 = m.try_read().unwrap();
67 }
68
69 fn main() {
70 let args: Vec<String> = env::args().collect();
71 if args.len() > 1 {
72 match &*args[1] {
73 "mutex" => test_mutex(),
74 "try_mutex" => test_try_mutex(),
75 "rwlock_ww" => test_rwlock_ww(),
76 "try_rwlock_ww" => test_try_rwlock_ww(),
77 "rwlock_rw" => test_rwlock_rw(),
78 "try_rwlock_rw" => test_try_rwlock_rw(),
79 "rwlock_wr" => test_rwlock_wr(),
80 "try_rwlock_wr" => test_try_rwlock_wr(),
81 _ => unreachable!(),
82 }
83 // If we reach this point then the test failed
84 println!("TEST FAILED: {}", args[1]);
85 } else {
86 let mut v = vec![];
87 v.push(Command::new(&args[0]).arg("mutex").stderr(Stdio::null()).spawn().unwrap());
88 v.push(Command::new(&args[0]).arg("try_mutex").stderr(Stdio::null()).spawn().unwrap());
89 v.push(Command::new(&args[0]).arg("rwlock_ww").stderr(Stdio::null()).spawn().unwrap());
90 v.push(Command::new(&args[0]).arg("try_rwlock_ww").stderr(Stdio::null()).spawn().unwrap());
91 v.push(Command::new(&args[0]).arg("rwlock_rw").stderr(Stdio::null()).spawn().unwrap());
92 v.push(Command::new(&args[0]).arg("try_rwlock_rw").stderr(Stdio::null()).spawn().unwrap());
93 v.push(Command::new(&args[0]).arg("rwlock_wr").stderr(Stdio::null()).spawn().unwrap());
94 v.push(Command::new(&args[0]).arg("try_rwlock_wr").stderr(Stdio::null()).spawn().unwrap());
95
96 thread::sleep(Duration::new(1, 0));
97
98 // Make sure all subprocesses either panicked or were killed because they deadlocked
99 for mut c in v {
100 c.kill().ok();
101 assert!(!c.wait().unwrap().success());
102 }
103 }
104 }