[−][src]Struct sunrise_kernel::sync::mutex::Mutex
A mutual exclusion primitive useful for protecting shared data
This mutex will block kernel threads waiting for the lock to become available. The
mutex can also be statically initialized or created via a new
constructor. Each mutex has a type parameter which represents the data that
it is protecting. The data can only be accessed through the RAII guards
returned from lock
and try_lock
, which guarantees that the data is only
ever accessed when the mutex is locked.
Fields
data: UnsafeCell<T>
The data that we're protecting.
Std Mutex boxes the data so it is Pin. We don't care for that in the kernel. However this adds a bound for T: Sized.
inner: MutexInner
The struct responsible for arbitrating accesses to .data
.
Implementations
impl<T> Mutex<T>
[src]
pub const fn new(t: T) -> Mutex<T>
[src]
Creates a new mutex in an unlocked state ready for use.
Examples
use crate::sync::Mutex; let mutex = Mutex::new(0);
pub fn into_inner(self) -> T
[src]
Consumes this mutex, returning the underlying data.
Examples
use std::sync::Mutex; let mutex = Mutex::new(0); assert_eq!(mutex.into_inner().unwrap(), 0);
pub fn lock(&self) -> MutexGuard<T>
[src]
Acquires a mutex, blocking the current kernel thread until it is able to do so.
This function will block the local kernel thread until it is available to acquire the mutex. Upon returning, the thread is the only thread with the lock held. An RAII guard is returned to allow scoped unlock of the lock. When the guard goes out of scope, the mutex will be unlocked.
Panics
This function panics when called if the lock is already held by the current thread.
pub fn try_lock(&self) -> TryLockResult<MutexGuard<T>>
[src]
Attempts to acquire this lock.
If the lock could not be acquired at this time, then Err
is returned.
Otherwise, an RAII guard is returned. The lock will be unlocked when the
guard is dropped.
This function does not preempt.
Note however that it still needs to lock the internal SpinLock
, and might temporarily
be blocking.
Double locking
Unlike lock
, this function will not panic if we already are the holder of this mutex,
and simply return Err
instead.
This makes it suitable for the kernel panic handler for example, where we want to acquire locks to resources possibly already held by the current thread, without panicking once more.
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 needs to
take place -- the mutable borrow statically guarantees no locks exist.
Examples
use std::sync::Mutex; let mut mutex = Mutex::new(0); *mutex.get_mut().unwrap() = 10; assert_eq!(*mutex.lock().unwrap(), 10);
Trait Implementations
impl<T: Debug> Debug for Mutex<T>
[src]
impl<T: Default> Default for Mutex<T>
[src]
impl<T> From<T> for Mutex<T>
[src]
fn from(t: T) -> Self
[src]
Creates a new mutex in an unlocked state ready for use.
This is equivalent to Mutex::new
.
impl<T: Send> Send for Mutex<T>
[src]
impl<T: Send> Sync for Mutex<T>
[src]
Auto Trait Implementations
impl<T> !RefUnwindSafe for Mutex<T>
impl<T> Unpin for Mutex<T> where
T: Unpin,
T: Unpin,
impl<T> !UnwindSafe for Mutex<T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<!> for T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,