MutexWrap

Struct MutexWrap 

Source
pub struct MutexWrap<M: RawMutex, T: ?Sized> { /* private fields */ }
Expand description

A mutual exclusion primitive useful for protecting shared data

This mutex will block threads waiting for the lock to become available. The mutex can be statically initialized via the mutex_new macro, or created via a new constructor. Each mutex has a raw mutex and a type parameter which represents the data that it is protecting. The data can only be accessed through the RAII guards returned from lock, which guarantees that the data is only ever accessed when the mutex is locked.

§Differences from std::sync::Mutex

  • No poisoning.
  • No try_lock.
  • The underlying raw mutex primitive can be of any kind, within a Box or not, as long as the RawMutex trait is implemented. Choose carefully.
  • With care, this can allow to share data through FFI and contend on the same locks. See the ffi-example directory.

§Examples

extern crate flexible_locks;
#[macro_use]
extern crate flexible_locks_derive;
use flexible_locks::MutexWrap;

// Pick your choice of raw mutex;
#[cfg(windows)]
use flexible_locks::SRWLOCK as RawOsMutex;
#[cfg(unix)]
use flexible_locks::pthread_mutex_t as RawOsMutex;

use std::sync::Arc;
use std::thread;
use std::sync::mpsc::channel;

const N: usize = 10;

fn main() {
    // Spawn a few threads to increment a shared variable (non-atomically),
    // and let the main thread know once all increments are done.
    //
    // Here we're using an Arc to share memory among threads, and the data
    // inside the Arc is protected with a mutex.
    let data = Arc::new(MutexWrap::<RawOsMutex, _>::new(0));

    let (tx, rx) = channel();
    for _ in 0..N {
        let (data, tx) = (data.clone(), tx.clone());
        thread::spawn(move || {
            // The shared state can only be accessed once the lock is held.
            // Our non-atomic increment is safe because we're the only thread
            // which can access the shared state when the lock is held.
            let mut data = data.lock();
            *data += 1;
            if *data == N {
                tx.send(()).unwrap();
            }
            // the lock is unlocked here when `data` goes out of scope.
        });
    }
     
    rx.recv().unwrap();
}

Implementations§

Source§

impl<M: RawMutex + Default, T> MutexWrap<M, T>

Source

pub fn new(t: T) -> Self

Creates a new mutex in an unlocked state ready for use.

§Examples
#[macro_use]
extern crate flexible_locks;
#[macro_use]
extern crate flexible_locks_derive;
use flexible_locks::MutexWrap;

// Pick your choice of raw mutex;
#[cfg(windows)]
use flexible_locks::SRWLOCK as RawOsMutex;
#[cfg(unix)]
use flexible_locks::pthread_mutex_t as RawOsMutex;

fn main() {
    let mutex = MutexWrap::<RawOsMutex, _>::new(0);
}
Source

pub fn into_inner(self) -> T

Consumes this mutex, returning the underlying data.

§Examples
#[macro_use]
extern crate flexible_locks;
#[macro_use]
extern crate flexible_locks_derive;
use flexible_locks::MutexWrap;

// Pick your choice of raw mutex;
#[cfg(windows)]
use flexible_locks::SRWLOCK as RawOsMutex;
#[cfg(unix)]
use flexible_locks::pthread_mutex_t as RawOsMutex;

fn main() {
    let mutex = MutexWrap::<RawOsMutex, _>::new(0);
    assert_eq!(mutex.into_inner(), 0);
}
Source§

impl<M: RawMutex, T: ?Sized> MutexWrap<M, T>

Source

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

Acquires a mutex, blocking the current thread until it is able to do so.

This function will block the local 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.

The exact behavior on locking a mutex in the thread which already holds the lock depends on the underlying raw mutex implementation.

§Examples
extern crate flexible_locks;
#[macro_use]
extern crate flexible_locks_derive;
use flexible_locks::MutexWrap;

// Pick your choice of raw mutex;
#[cfg(windows)]
use flexible_locks::SRWLOCK as RawOsMutex;
#[cfg(unix)]
use flexible_locks::pthread_mutex_t as RawOsMutex;

use std::sync::Arc;
use std::thread;

fn main() {
    let mutex = Arc::new(MutexWrap::<RawOsMutex, _>::new(0));
    let c_mutex = mutex.clone();

    thread::spawn(move || {
        *c_mutex.lock() = 10;
    }).join().expect("thread::spawn failed");
    assert_eq!(*mutex.lock(), 10);
}
Source

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

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
extern crate flexible_locks;
#[macro_use]
extern crate flexible_locks_derive;
use flexible_locks::MutexWrap;

// Pick your choice of raw mutex;
#[cfg(windows)]
use flexible_locks::SRWLOCK as RawOsMutex;
#[cfg(unix)]
use flexible_locks::pthread_mutex_t as RawOsMutex;

fn main() {
    let mut mutex = MutexWrap::<RawOsMutex, _>::new(0);
    *mutex.get_mut() = 10;
    assert_eq!(*mutex.lock(), 10);
}

Trait Implementations§

Source§

impl<M: RawMutex + Default, T: Default> Default for MutexWrap<M, T>

Source§

fn default() -> Self

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

impl<M: RawMutex + Default, T> From<T> for MutexWrap<M, T>

Source§

fn from(t: T) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<M, T> !Freeze for MutexWrap<M, T>

§

impl<M, T> !RefUnwindSafe for MutexWrap<M, T>

§

impl<M, T> Send for MutexWrap<M, T>
where T: Send + ?Sized,

§

impl<M, T> Sync for MutexWrap<M, T>
where T: Send + ?Sized,

§

impl<M, T> Unpin for MutexWrap<M, T>
where M: Unpin, T: Unpin + ?Sized,

§

impl<M, T> UnwindSafe for MutexWrap<M, T>
where M: UnwindSafe, T: UnwindSafe + ?Sized,

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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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.