pub struct Mutex<T: ?Sized> { /* private fields */ }Expand description
A mutual-exclusion lock whose wait is a future.
Uncontended, try_lock and a guard’s drop are one
atomic read-modify-write each and allocate nothing. A caller takes the
lock whenever it is free, even while others wait; under contention
lock spins briefly within the poll (see the
module docs), then queues and
returns Pending.
An unlock wakes the oldest waiter to try again; a waiter that loses
MAX_BYPASS times is handed the lock directly,
and try_lock fails until it has had it.
It does not poison: a panic in a holder unlocks it.
use nuclei::sync::Mutex;
let counter = Mutex::new(0u32);
*counter.try_lock().expect("uncontended") += 1;
assert_eq!(counter.into_inner(), 1);Implementations§
Source§impl<T: ?Sized> Mutex<T>
impl<T: ?Sized> Mutex<T>
Sourcepub fn try_lock(&self) -> Result<MutexGuard<'_, T>, TryLockError>
pub fn try_lock(&self) -> Result<MutexGuard<'_, T>, TryLockError>
Locks the mutex if it is free and no handoff is owed.
§Errors
TryLockError when the mutex is locked or owed to a waiter.
Sourcepub fn try_lock_owned(
self: Arc<Self>,
) -> Result<OwnedMutexGuard<T>, TryLockError>
pub fn try_lock_owned( self: Arc<Self>, ) -> Result<OwnedMutexGuard<T>, TryLockError>
try_lock through an Arc, so the guard can
outlive the borrow.
§Errors
TryLockError when the mutex is locked or owed to a waiter.
Sourcepub fn lock(&self) -> Lock<'_, T> ⓘ
pub fn lock(&self) -> Lock<'_, T> ⓘ
Waits for the lock. Uncontended, the first poll is one load and one
atomic read-modify-write. Contended, a poll may spin for up to the
lock’s spin budget before it queues, and again after a retry it lost,
before it returns Pending; it is lock-free, not wait-free: see
Progress.
Dropping the future withdraws the wait, and passes on a lock handed to it meanwhile.
Sourcepub fn lock_owned(self: Arc<Self>) -> LockOwned<T> ⓘ
pub fn lock_owned(self: Arc<Self>) -> LockOwned<T> ⓘ
lock through an Arc, so the guard can outlive the
borrow.
Sourcepub fn blocking_lock(&self) -> MutexGuard<'_, T>
pub fn blocking_lock(&self) -> MutexGuard<'_, T>
lock for a thread that may block: it parks the
calling thread until the lock is free. On a nuclei shard (inside one of its tasks) it does not park the thread: it runs a nested block_on, so the shard keeps running its other tasks meanwhile.
Inside another runtime’s task it blocks that task’s executor thread,
so do not: the task that would unlock may be the one that thread was
running.
§Panics
On wasm32, where no other thread can unlock, when the lock is held:
the call could never end.