hirofa_utils/
debug_mutex.rs

1use parking_lot::Mutex;
2use parking_lot::MutexGuard;
3
4pub struct DebugMutex<T> {
5    name: &'static str,
6    mtx: Mutex<T>,
7}
8
9impl<T> DebugMutex<T> {
10    pub fn new(inner: T, name: &'static str) -> Self {
11        DebugMutex {
12            mtx: Mutex::new(inner),
13            name,
14        }
15    }
16    pub fn lock(&self, reason: &'static str) -> anyhow::Result<MutexGuard<T>> {
17        log::trace!(
18            "lock mutex:{} for: {} from thread: {}",
19            self.name,
20            reason,
21            thread_id::get()
22        );
23
24        let ret = self.mtx.lock();
25
26        log::trace!(
27            "locked mutex:{} for: {} from thread: {}",
28            self.name,
29            reason,
30            thread_id::get()
31        );
32
33        Ok(ret)
34
35        // first try_lock
36
37        // if locked by other thread
38
39        // trace!("lock mutex:{} for: {} from thread: {} -IS LOCKED- wait for thread: {}, locked_reason: {}", self.name, reason);
40
41        // if locked by same thread
42
43        // panic!("LOCKED BY SELF: lock mutex:{} for: {} from thread: {} -IS LOCKED- wait for thread: {}, locked_reason: {}", self.name, reason);
44    }
45}