use core::cell::UnsafeCell; use core::default::Default; use core::fmt; use core::hint; use core::marker::Sync; use core::ops::{Deref, DerefMut, Drop}; use core::option::Option::{self, None, Some}; use core::sync::atomic::{AtomicBool, Ordering};
/// This type provides MUTual EXclusion based on spinning. pub(crate) struct Mutex<T: ?Sized> {
lock: AtomicBool,
data: UnsafeCell<T>,
}
/// A guard to which the protected data can be accessed /// /// When the guard falls out of scope it will release the lock. #[derive(Debug)] pub(crate) struct MutexGuard<'a, T: ?Sized> {
lock: &'a AtomicBool,
data: &'a mut T,
}
// Same unsafe impls as `std::sync::Mutex` unsafeimpl<T: ?Sized + Send> Sync for Mutex<T> {} unsafeimpl<T: ?Sized + Send> Send for Mutex<T> {}
impl<T> Mutex<T> { /// Creates a new spinlock wrapping the supplied data. pub(crate) constfn new(user_data: T) -> Mutex<T> {
Mutex {
lock: AtomicBool::new(false),
data: UnsafeCell::new(user_data),
}
}
}
impl<T: ?Sized> Mutex<T> { fn obtain_lock(&self) { whileself
.lock
.compare_exchange_weak(false, true, Ordering::Acquire, Ordering::Relaxed)
.is_err()
{ // Wait until the lock looks unlocked before retrying whileself.lock.load(Ordering::Relaxed) {
hint::spin_loop();
}
}
}
/// Locks the spinlock and returns a guard. /// /// The returned value may be dereferenced for data access /// and the lock will be dropped when the guard falls out of scope. pub(crate) fn lock(&self) -> MutexGuard<'_, T> { self.obtain_lock();
MutexGuard {
lock: &self.lock,
data: unsafe { &mut *self.data.get() },
}
}
/// Tries to lock the mutex. If it is already locked, it will return None. Otherwise it returns /// a guard within Some. pub(crate) fn try_lock(&self) -> Option<MutexGuard<'_, T>> { ifself
.lock
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
.is_ok()
{
Some(MutexGuard {
lock: &self.lock,
data: unsafe { &mut *self.data.get() },
})
} else {
None
}
}
}
impl<'a, T: ?Sized> Deref for MutexGuard<'a, T> { type Target = T; fn deref<'b>(&'b self) -> &'b T {
&*self.data
}
}
impl<'a, T: ?Sized> DerefMut for MutexGuard<'a, T> { fn deref_mut<'b>(&'b mutself) -> &'b mut T {
&mut *self.data
}
}
impl<'a, T: ?Sized> Drop for MutexGuard<'a, T> { /// The dropping of the MutexGuard will release the lock it was created from. fn drop(&mutself) { self.lock.store(false, Ordering::Release);
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.0 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.