Struct axstd::sync::Mutex

source ·
pub struct Mutex<T: ?Sized> { /* private fields */ }
Available on crate feature multitask only.
Expand description

A mutual exclusion primitive useful for protecting shared data, similar to std::sync::Mutex.

When the mutex is locked, the current task will block and be put into the wait queue. When the mutex is unlocked, all tasks waiting on the queue will be woken up.

Implementations§

source§

impl<T> Mutex<T>

source

pub const fn new(data: T) -> Self

Creates a new Mutex wrapping the supplied data.

source

pub fn into_inner(self) -> T

Consumes this Mutex and unwraps the underlying data.

source§

impl<T: ?Sized> Mutex<T>

source

pub fn is_locked(&self) -> bool

Returns true if the lock is currently held.

Safety

This function provides no synchronization guarantees and so its result should be considered ‘out of date’ the instant it is called. Do not use it for synchronization purposes. However, it may be useful as a heuristic.

source

pub fn lock(&self) -> MutexGuard<'_, T>

Locks the Mutex and returns a guard that permits access to the inner data.

The returned value may be dereferenced for data access and the lock will be dropped when the guard falls out of scope.

source

pub fn try_lock(&self) -> Option<MutexGuard<'_, T>>

Try to lock this Mutex, returning a lock guard if successful.

source

pub unsafe fn force_unlock(&self)

Force unlock the Mutex.

Safety

This is extremely unsafe if the lock is not held by the current thread. However, this can be useful in some instances for exposing the lock to FFI that doesn’t know how to deal with RAII.

source

pub fn get_mut(&mut self) -> &mut T

Returns a mutable reference to the underlying data.

Since this call borrows the Mutex mutably, and a mutable reference is guaranteed to be exclusive in Rust, no actual locking needs to take place – the mutable borrow statically guarantees no locks exist. As such, this is a ‘zero-cost’ operation.

Trait Implementations§

source§

impl<T: ?Sized + Debug> Debug for Mutex<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: ?Sized + Default> Default for Mutex<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T: ?Sized + Send> Send for Mutex<T>

source§

impl<T: ?Sized + Send> Sync for Mutex<T>

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Mutex<T>

§

impl<T: ?Sized> Unpin for Mutex<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Mutex<T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> AsAny for T
where T: Any,

source§

fn as_any(&self) -> &(dyn Any + 'static)

把当前对象转化为 Any 类型,供后续 downcast 使用
source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

供 downcast_mut 使用
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.