1 use crate::sys::locks as imp;
3 /// An OS-based mutual exclusion lock.
5 /// This mutex cleans up its resources in its `Drop` implementation, may safely
6 /// be moved (when not borrowed), and does not cause UB when used reentrantly.
8 /// This mutex does not implement poisoning.
10 /// This is either a wrapper around `LazyBox<imp::Mutex>` or `imp::Mutex`,
11 /// depending on the platform. It is boxed on platforms where `imp::Mutex` may
13 pub struct MovableMutex(imp::MovableMutex);
15 unsafe impl Sync for MovableMutex {}
18 /// Creates a new mutex.
20 #[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
21 pub const fn new() -> Self {
22 Self(imp::MovableMutex::new())
25 pub(super) fn raw(&self) -> &imp::Mutex {
29 /// Locks the mutex blocking the current thread until it is available.
31 pub fn raw_lock(&self) {
32 unsafe { self.0.lock() }
35 /// Attempts to lock the mutex without blocking, returning whether it was
36 /// successfully acquired or not.
38 pub fn try_lock(&self) -> bool {
39 unsafe { self.0.try_lock() }
42 /// Unlocks the mutex.
44 /// Behavior is undefined if the current thread does not actually hold the
47 pub unsafe fn raw_unlock(&self) {