Struct ksched::sync::Mutex[][src]

pub struct Mutex<T: ?Sized> { /* fields omitted */ }
Expand description

An async mutex.

The locking mechanism uses a FIFO wait queue to avoid starvation.

Examples

use ksched::sync::Mutex;

let m: Mutex<usize> = Mutex::new(1);

let mut guard = m.lock().await;
*guard = 2;
drop(guard);

let guard = m.lock().await;
assert_eq!(*guard, 2);

Implementations

impl<T> Mutex<T>[src]

pub const fn new(data: T) -> Mutex<T>[src]

Creates a new async mutex.

Examples

use ksched::sync::Mutex;

let mutex: Mutex<usize> = Mutex::new(0);

pub fn into_inner(self) -> T[src]

Consumes the mutex, returning the underlying data.

Examples

use ksched::sync::Mutex;

let mutex: Mutex<usize> = Mutex::new(10);
assert_eq!(mutex.into_inner(), 10);

impl<T: ?Sized> Mutex<T>[src]

pub async fn acquire(&self)[src]

Acquire the mutex, which must be release manually by Self::release

pub unsafe fn release(&self)[src]

Unlock manually.

pub async fn lock(&self) -> MutexGuard<'_, T>[src]

Acquires the mutex.

Since inserting current task to the wait queue requires memory allocation, this function may return [AllocError] on oom. Otherwise, returns a guard that releases the mutex when dropped.

Examples

use ksched::sync::Mutex;

let mutex: Mutex<usize> = Mutex::new(10);
let guard = mutex.lock().await;
assert_eq!(*guard, 10);

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

Attempts to acquire the mutex.

If the mutex could not be acquired at this time, then None is returned. Otherwise, a guard is returned that releases the mutex when dropped.

Examples

use ksched::sync::Mutex;

let mutex = Mutex::new(10);
if let Some(guard) = mutex.try_lock() {
    assert_eq!(*guard, 10);
}

pub fn get_mut(&mut self) -> &mut T[src]

Returns a mutable reference to the underlying data.

Since this call borrows the mutex mutably, no actual locking takes place – the mutable borrow statically guarantees the mutex is not already acquired.

Examples

use ksched::sync::Mutex;

let mut mutex: Mutex<usize> = Mutex::new(0);
*mutex.get_mut() = 10;
assert_eq!(*mutex.lock().await, 10);

Trait Implementations

impl<T: Debug + ?Sized> Debug for Mutex<T>[src]

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

Formats the value using the given formatter. Read more

impl<T: Default + ?Sized> Default for Mutex<T>[src]

fn default() -> Mutex<T>[src]

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

impl<T> From<T> for Mutex<T>[src]

fn from(val: T) -> Mutex<T>[src]

Performs the conversion.

impl<T: Send + ?Sized> Send for Mutex<T>[src]

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

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<!> for T[src]

pub fn from(t: !) -> T[src]

Performs the conversion.

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.