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