Skip to main content

BorrowedMutex

Struct BorrowedMutex 

Source
pub struct BorrowedMutex<'a, R: RobustnessMarker> { /* private fields */ }
Expand description

A mutex that borrows the memory for its underlying mutex. The underlying mutex will not be destroyed when BorrowedMutex is dropped. It is the caller’s responsibility to call destroy when the mutex is no longer needed.

§Safety

The methods of BorrowedMutex, unlike those of OwnedMutex, are unsafe. This is because the underlying mutex may be in a shared memory mapping, where it may be modifiable by other processes, and calling these methods can lead to undefined behaviour if certain invariants are not upheld. Here is a non-exhaustive list of some of the situations that will lead to undefined behaviour:

  • Another process destroys the mutex before your process calls destroy. Calling any method on the mutex (even destroy itself) is now UB.
  • Another process unlocks the mutex without holding a lock.
  • A process owns a lock and tries to lock the mutex again (unless the mutex is error-checked).

For a more thorough description of what situations can result in UB, read some of the pthread_mutex_* pages in the POSIX standard.

Implementations§

Source§

impl<R: RobustnessMarker> BorrowedMutex<'_, R>

Source

pub unsafe fn from_raw<T>( raw: *mut RawMutexAlloc, _memory: &T, ) -> BorrowedMutex<'_, R>

Constructs a BorrowedMutex from an existing, initialised underlying mutex at a given location in memory.

  • raw: A pointer to an existing raw mutex. Note that, unlike MutexBuilder::build_borrowed, this method expects that the pointee is initialised.

  • _memory: A reference to a RAII object whose lifetime determines the validity of raw (e.g. a struct that manages a memory map).

§Safety

The caller must guarantee that raw points to a valid, initialised RawMutexAlloc (a.k.a. pthread_mutex_t) whose robustness attribute matches R. Getting the robustness wrong does not corrupt anything by itself, but it does mean the guard type is lying about which outcomes are possible.

Also, read the type-level safety section.

Source

pub unsafe fn destroy(self)

Calls pthread_mutex_destroy on the underlying mutex. It is safe to re-initialise another mutex at this address afterwards.

Failure to call this function before destroying the mutex can result in resource leaks, but will not lead to undefined behaviour.

§Safety

The caller must ensure that there is no way for the mutex to be locked by any thread (from any process) while the call is taking place. Notably, this means the mutex can not be in the middle of a wait call to a condvar.

Moreover, the caller must ensure that destroy is only called once.

Attempting to use the mutex after it has been destroyed or destroying the same mutex twice is undefined behaviour. You can, however, safely create a new mutex at the same address after destroying the old one.

Also, read the type-level safety section.

Source

pub unsafe fn try_lock(&self) -> Result<Option<R::Guard<'_>>, MutexLockError>

Attempts to lock the mutex in a non-blocking manner. If a lock was obtained, the Some variant is returned. If a lock could not be obtained but otherwise no error occurred, the None variant is returned. Otherwise, an error is returned.

§Safety

This method is unsafe because the backing mutex may be in shared memory and modifiable by many processes, where it can potentially be left in an invalid state. As long as processes only modify the mutex through libc’s pthread* functions and don’t cause any undefined behaviour on their own such as double unlocking or double locking (unless the mutex is recursive), it should generally be safe to call this function.

Also, read the type-level safety section.

§Errors

See MutexLockError

Source

pub unsafe fn lock(&self) -> Result<R::Guard<'_>, MutexLockError>

Attempts to lock the mutex, blocking until a lock is obtained.

§Safety

This method is unsafe because the backing mutex may be in shared memory and modifiable by many processes, where it can potentially be left in an invalid state. As long as processes only modify the mutex through libc’s pthread* functions and don’t cause any undefined behaviour on their own such as double unlocking or double locking (unless the mutex is recursive), it should generally be safe to call this function.

Also, read the type-level safety section.

§Errors

See MutexLockError

Source

pub unsafe fn lock_for( &self, timeout: Duration, ) -> Result<Option<R::Guard<'_>>, MutexLockError>

Available on non-Apple only.

Attempts to lock the mutex, blocking until a lock is obtained or timeout elapses. A lock that could not be obtained in time is reported as Ok(None) rather than as an error.

The timeout is resolved against CLOCK_REALTIME, which is the clock pthread_mutex_timedlock is defined in terms of. Stepping the system clock therefore moves the deadline.

Not available on Apple platforms, which do not implement pthread_mutex_timedlock.

§Safety

The same conditions as for lock apply.

§Errors

See MutexLockError

Source

pub fn as_raw_mutex(&self) -> *mut pthread_mutex_t

Returns a pointer to the underlying pthread_mutex_t.

Trait Implementations§

Source§

impl<R: RobustnessMarker> Clone for BorrowedMutex<'_, R>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<R: RobustnessMarker> Copy for BorrowedMutex<'_, R>

Source§

impl<R: RobustnessMarker> Debug for BorrowedMutex<'_, R>

Source§

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

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

impl<R: RobustnessMarker> Send for BorrowedMutex<'_, R>

Source§

impl<R: RobustnessMarker> Sync for BorrowedMutex<'_, R>

Source§

impl<R: RobustnessMarker> Unpin for BorrowedMutex<'_, R>

Auto Trait Implementations§

§

impl<'a, R> !RefUnwindSafe for BorrowedMutex<'a, R>

§

impl<'a, R> !UnwindSafe for BorrowedMutex<'a, R>

§

impl<'a, R> Freeze for BorrowedMutex<'a, R>

§

impl<'a, R> UnsafeUnpin for BorrowedMutex<'a, R>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.