sync::atomic::{AtomicI32, Ordering},
};
-#[cfg(debug_assertions)]
-use crate::get_thread_id;
-
use crate::{waiter, PhantomUnsend};
+#[cfg(debug_assertions)]
+#[inline(always)]
+fn thread_id() -> std::thread::ThreadId {
+ std::thread::current().id()
+}
+
const UNLOCKED: i32 = 0;
const LOCKED: i32 = 1;
const LOCKED_WAIT: i32 = 2;
pub struct Mutex<T: ?Sized> {
- control: AtomicI32,
#[cfg(debug_assertions)]
- thread_id: AtomicI32,
+ thread_id: std::cell::Cell<Option<std::thread::ThreadId>>,
+ control: AtomicI32,
data: UnsafeCell<T>,
}
Self {
control: AtomicI32::new(UNLOCKED),
#[cfg(debug_assertions)]
- thread_id: AtomicI32::new(0),
+ thread_id: std::cell::Cell::new(None),
data: UnsafeCell::new(value),
}
}
unsafe fn raw_lock(&self) {
#[cfg(debug_assertions)]
- if self.thread_id.load(Ordering::Relaxed) == get_thread_id() {
+ if self.thread_id.get() == Some(std::thread::current().id()) {
panic!("recursion not supported")
}
) {
Ok(_) => {
#[cfg(debug_assertions)]
- self.thread_id.store(get_thread_id(), Ordering::Relaxed);
+ self.thread_id.set(Some(thread_id()));
return;
}
Err(x) => c = x,
) {
Ok(_) => {
#[cfg(debug_assertions)]
- self.thread_id.store(get_thread_id(), Ordering::Relaxed);
+ self.thread_id.set(Some(thread_id()));
return;
}
Err(x) => c = x,
unsafe fn raw_try_lock(&self) -> bool {
#[cfg(debug_assertions)]
- if self.thread_id.load(Ordering::Relaxed) == get_thread_id() {
+ if self.thread_id.get() == Some(thread_id()) {
panic!("recursion not supported")
}
.is_ok()
{
#[cfg(debug_assertions)]
- self.thread_id.store(get_thread_id(), Ordering::Relaxed);
+ self.thread_id.set(Some(thread_id()));
true
} else {
false
unsafe fn raw_unlock(&self) {
#[cfg(debug_assertions)]
- self.thread_id.store(0, Ordering::Relaxed);
+ self.thread_id.set(None);
if self.control.fetch_sub(1, Ordering::Release) != LOCKED {
self.control.store(UNLOCKED, Ordering::Release);