#[inline] pubfn lock(&self) -> Pin<CMutexGuard<'_, T>> { letmut sguard = self.spin_lock.acquire(); ifself.locked.get() {
stack_pin_init!(let wait_entry = WaitEntry::insert_new(&self.wait_list)); // println!("wait list length: {}", self.wait_list.size()); whileself.locked.get() {
drop(sguard); #[cfg(feature = "std")]
thread::park();
sguard = self.spin_lock.acquire();
} // This does have an effect, as the ListHead inside wait_entry implements Drop! #[expect(clippy::drop_non_drop)]
drop(wait_entry);
} self.locked.set(true); unsafe {
Pin::new_unchecked(CMutexGuard {
mtx: self,
_pin: PhantomPinned,
})
}
}
#[allow(dead_code)] pubfn get_data_mut(self: Pin<&mutSelf>) -> &>mut T { // SAFETY: we have an exclusive reference and thus nobody has access to data. unsafe { &mut *self.data.get() }
}
}
unsafeimpl<T: Send> Send for CMutex<T> {} unsafeimpl<T: Send> Sync for CMutex<T> {}
#[cfg_attr(test, test)] #[allow(dead_code)] fn main() { #[cfg(feature = "std")]
{ let mtx: Pin<Arc<CMutex<usize>>> = Arc::pin_init(CMutex::new(0)).unwrap(); letmut handles = vec![]; let thread_count = 20; let workload = if cfg!(miri) { 100 } else { 1_000 }; for i in0..thread_count { let mtx = mtx.clone();
handles.push(
Builder::new()
.name(format!("worker #{i}"))
.spawn(move || { for _ in0..workload {
*mtx.lock() += 1;
}
println!("{i} halfway");
sleep(Duration::from_millis((i as u64) * 10)); for _ in0..workload {
*mtx.lock() += 1;
}
println!("{i} finished");
})
.expect("should not fail"),
);
} for h in handles {
h.join().expect("thread panicked");
}
println!("{:?}", &*mtx.lock());
assert_eq!(*mtx.lock(), workload * thread_count * 2);
}
}
Messung V0.5 in Prozent
¤ 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.0.10Bemerkung:
(vorverarbeitet am 2026-09-29)
¤
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.