1 use crate::sync::debug_sync::{RwLock, Mutex};
3 use super::{LockHeldState, LockTestExt};
9 #[cfg(not(feature = "backtrace"))]
10 fn recursive_lock_fail() {
11 let mutex = Mutex::new(());
12 let _a = mutex.lock().unwrap();
13 let _b = mutex.lock().unwrap();
18 #[cfg(not(feature = "backtrace"))]
20 let lock = RwLock::new(());
21 let _a = lock.read().unwrap();
22 let _b = lock.read().unwrap();
28 let a = Mutex::new(());
29 let b = Mutex::new(());
31 let _a = a.lock().unwrap();
32 let _b = b.lock().unwrap();
35 let _b = b.lock().unwrap();
36 let _a = a.lock().unwrap();
42 fn write_lockorder_fail() {
43 let a = RwLock::new(());
44 let b = RwLock::new(());
46 let _a = a.write().unwrap();
47 let _b = b.write().unwrap();
50 let _b = b.write().unwrap();
51 let _a = a.write().unwrap();
57 fn read_lockorder_fail() {
58 let a = RwLock::new(());
59 let b = RwLock::new(());
61 let _a = a.read().unwrap();
62 let _b = b.read().unwrap();
65 let _b = b.read().unwrap();
66 let _a = a.read().unwrap();
72 fn read_write_lockorder_fail() {
73 let a = RwLock::new(());
74 let b = RwLock::new(());
76 let _a = a.write().unwrap();
77 let _b = b.read().unwrap();
80 let _b = b.read().unwrap();
81 let _a = a.write().unwrap();
86 fn test_thread_locked_state() {
87 let mtx = Arc::new(Mutex::new(()));
88 let mtx_ref = Arc::clone(&mtx);
89 assert_eq!(mtx.held_by_thread(), LockHeldState::NotHeldByThread);
91 let lck = mtx.lock().unwrap();
92 assert_eq!(mtx.held_by_thread(), LockHeldState::HeldByThread);
94 let thrd = std::thread::spawn(move || {
95 assert_eq!(mtx_ref.held_by_thread(), LockHeldState::NotHeldByThread);
98 assert_eq!(mtx.held_by_thread(), LockHeldState::HeldByThread);
101 assert_eq!(mtx.held_by_thread(), LockHeldState::NotHeldByThread);