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