1 use crate::sync::debug_sync::{RwLock, Mutex};
3 use super::{LockHeldState, LockTestExt};
10 #[cfg(not(feature = "backtrace"))]
11 fn recursive_lock_fail() {
12 let mutex = Mutex::new(());
13 let _a = mutex.lock().unwrap();
14 let _b = mutex.lock().unwrap();
19 let lock = RwLock::new(());
20 let _a = lock.read().unwrap();
21 let _b = lock.read().unwrap();
27 let a = Mutex::new(());
28 let b = Mutex::new(());
30 let _a = a.lock().unwrap();
31 let _b = b.lock().unwrap();
34 let _b = b.lock().unwrap();
35 let _a = a.lock().unwrap();
41 fn write_lockorder_fail() {
42 let a = RwLock::new(());
43 let b = RwLock::new(());
45 let _a = a.write().unwrap();
46 let _b = b.write().unwrap();
49 let _b = b.write().unwrap();
50 let _a = a.write().unwrap();
56 fn read_lockorder_fail() {
57 let a = RwLock::new(());
58 let b = RwLock::new(());
60 let _a = a.read().unwrap();
61 let _b = b.read().unwrap();
64 let _b = b.read().unwrap();
65 let _a = a.read().unwrap();
70 fn read_recursive_no_lockorder() {
71 // Like the above, but note that no lockorder is implied when we recursively read-lock a
72 // RwLock, causing this to pass just fine.
73 let a = RwLock::new(());
74 let b = RwLock::new(());
75 let _outer = a.read().unwrap();
77 let _a = a.read().unwrap();
78 let _b = b.read().unwrap();
81 let _b = b.read().unwrap();
82 let _a = a.read().unwrap();
88 fn read_write_lockorder_fail() {
89 let a = RwLock::new(());
90 let b = RwLock::new(());
92 let _a = a.write().unwrap();
93 let _b = b.read().unwrap();
96 let _b = b.read().unwrap();
97 let _a = a.write().unwrap();
102 fn test_thread_locked_state() {
103 let mtx = Arc::new(Mutex::new(()));
104 let mtx_ref = Arc::clone(&mtx);
105 assert_eq!(mtx.held_by_thread(), LockHeldState::NotHeldByThread);
107 let lck = mtx.lock().unwrap();
108 assert_eq!(mtx.held_by_thread(), LockHeldState::HeldByThread);
110 let thrd = std::thread::spawn(move || {
111 assert_eq!(mtx_ref.held_by_thread(), LockHeldState::NotHeldByThread);
113 thrd.join().unwrap();
114 assert_eq!(mtx.held_by_thread(), LockHeldState::HeldByThread);
117 assert_eq!(mtx.held_by_thread(), LockHeldState::NotHeldByThread);