Skip to main content

RawMutex

Trait RawMutex 

Source
pub unsafe trait RawMutex {
    type Guard;

    const INIT: Self;

    // Required methods
    fn lock(&self) -> Self::Guard;
    fn try_lock(&self) -> Option<Self::Guard>;
    unsafe fn unlock(&self, guard: Self::Guard);
    fn is_locked(&self) -> bool;
}
Expand description

Basic operations for a mutex.

Types implementing this trait can be used by Mutex to form a safe and fully-functioning mutex type.

§Safety

Implementations must provide mutual exclusion, with acquire semantics on successful locking and release semantics on unlocking. These requirements also apply to subtraits.

Each successful lock operation returns guard state representing ownership of the mutex. Operations on a locked mutex must receive the state for that acquisition. Unlocking consumes the state.

Guard state may unlock the mutex when dropped, but forgetting it must be safe. A caller with exclusive ownership of the mutex object may move or drop it even while it is locked; this must also be safe.

If a lock operation unwinds, it must not leave the caller holding a new acquisition. If an operation consuming guard state unwinds, it must release the acquisition represented by that state, even if it changed or re-acquired the lock before panicking. Neither case may release an acquisition held independently of the call.

Required Associated Constants§

Source

const INIT: Self

Initial value for an unlocked mutex.

Required Associated Types§

Source

type Guard

Per-acquisition state stored in the lock guard.

This type also determines whether guards are Send and Sync. Stateless implementations can use GuardSend or GuardNoSend.

Required Methods§

Source

fn lock(&self) -> Self::Guard

Acquires this mutex, blocking the current thread until it is able to do so.

Source

fn try_lock(&self) -> Option<Self::Guard>

Attempts to acquire this mutex without blocking. Returns the guard state if the lock was successfully acquired and None otherwise.

Source

unsafe fn unlock(&self, guard: Self::Guard)

Unlocks this mutex.

§Safety

This method may only be called if the mutex is held in the current context, i.e. it must be given the state returned by a successful call to lock, try_lock, try_lock_for or try_lock_until.

Source

fn is_locked(&self) -> bool

Checks whether the mutex is currently locked.

The result is a momentary snapshot and may be stale by the time it is returned.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§