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