Struct Mutex

Source
pub struct Mutex<'a, T>
where T: Debug,
{ /* private fields */ }
Expand description

Represents a POSIX mutex which can be created by the MutexBuilder.

§Example

For a detailed builder example, see MutexBuilder.

use iceoryx2_bb_posix::mutex::*;
use core::time::Duration;

let handle = MutexHandle::<i32>::new();
let mutex = MutexBuilder::new().create(5, &handle)
    .expect("Failed to create mutex");

{
    let guard = mutex.lock().expect("failed to lock mutex");
    println!("current mutex value is: {}", *guard);
}

match mutex.try_lock().expect("failed to lock") {
    Some(mut guard) => *guard = 123, // set mutex value to 123
    None => println!("unable to acquire lock"),
};

match mutex.timed_lock(Duration::from_secs(1)).expect("failed to lock") {
    Some(guard) => println!("New mutex value is: {}", *guard),
    None => println!("Timeout occurred while trying to get lock.")
};

Implementations§

Source§

impl<T> Mutex<'_, T>
where T: Debug,

Source

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

Blocks until the ownership of the lock could be acquired. If it was successful it returns a MutexGuard to allow access to the underlying value. If the previously owning thread has died and MutexThreadTerminationBehavior::ReleaseWhenLocked was set it returns the error MutexLockError::LockAcquiredButOwnerDied which contains also the MutexGuard. The new owner now has the responsibility to either repair the underlying value of the mutex and call Mutex::make_consistent() when it is repaired or to undertake other measures when it is unrepairable.

Source

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

Tries to acquire the ownership of the lock. If it was successful it returns a MutexGuard packed inside an Option, if the lock is already owned by someone else it returns None. If the previously owning thread has died and MutexThreadTerminationBehavior::ReleaseWhenLocked was set it returns the error MutexLockError::LockAcquiredButOwnerDied which contains also the MutexGuard. The new owner now has the responsibility to either repair the underlying value of the mutex and call Mutex::make_consistent() when it is repaired or to undertake other measures when it is unrepairable.

Source

pub fn timed_lock( &self, duration: Duration, ) -> Result<Option<MutexGuard<'_, '_, T>>, MutexTimedLockError<'_, '_, T>>

Tries to acquire the ownership of the lock until the provided timeout has elapsed. If it was successful it returns a MutexGuard packed inside an Option, if the could not be acquired lock when the timeout passed it returns None. If the previously owning thread has died and MutexThreadTerminationBehavior::ReleaseWhenLocked was set it returns the error MutexTimedLockError::MutexLockError which contains also the MutexGuard. The new owner now has the responsibility to either repair the underlying value of the mutex and call Mutex::make_consistent() when it is repaired or to undertake other measures when it is unrepairable.

Source

pub fn make_consistent(&self)

If the previously owning thread has died and MutexThreadTerminationBehavior::ReleaseWhenLocked was set it returns the error MutexLockError::LockAcquiredButOwnerDied which contains also the MutexGuard. The new owner now has the responsibility to either repair the underlying value of the mutex and call Mutex::make_consistent() when it is repaired or to undertake other measures when it is unrepairable.

Trait Implementations§

Source§

impl<'a, T> Debug for Mutex<'a, T>
where T: Debug,

Source§

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

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

impl<'a, T> IpcCapable<'a, MutexHandle<T>> for Mutex<'a, T>
where T: Debug,

Source§

fn is_interprocess_capable(&self) -> bool

Returns true if the object is interprocess capable, otherwise false
Source§

unsafe fn from_ipc_handle(handle: &'a T) -> Self

Creates an IPC Capable object from its handle. Read more
Source§

impl<T> Send for Mutex<'_, T>
where T: Send + Debug,

Source§

impl<T> Sync for Mutex<'_, T>
where T: Send + Debug,

Auto Trait Implementations§

§

impl<'a, T> Freeze for Mutex<'a, T>

§

impl<'a, T> !RefUnwindSafe for Mutex<'a, T>

§

impl<'a, T> Unpin for Mutex<'a, T>

§

impl<'a, T> !UnwindSafe for Mutex<'a, 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> 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>,

Source§

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>,

Source§

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.