Mutex

Struct Mutex 

Source
pub struct Mutex<T: MutexProtected + ?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 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.
  • The raw mutex primitive can be embedded anywhere in the data type. See the MutexWrap type for a variant that looks more like std::sync::Mutex but still allows to use a specific raw mutex primitive.
  • 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::{Mutex, RawMutex};

// 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;

#[derive(MutexProtected, Default)]
struct Data {
    a: usize,
    b: usize,
    #[mutex]
    mutex: RawOsMutex,
}

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(Mutex::new(Data::default()));

    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.a += 1;
            if data.a == N {
                tx.send(()).unwrap();
            }
            // the lock is unlocked here when `data` goes out of scope.
        });
    }
     
    rx.recv().unwrap();
}

Please note that #[derive(MutexProtected)] treats structs containing only two fields including the raw mutex differently, such that the data handed by Mutex::lock is the non-mutex field, rather than the whole data. In that case, it is preferable to use MutexWrap instead. See MutexProtected::DataType.

Implementations§

Source§

impl<T: MutexProtected> Mutex<T>
where T::DataType: Sized,

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::{Mutex, RawMutex};

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

#[derive(MutexProtected)]
struct Data {
    a: usize,
    b: usize,
    #[mutex]
    mutex: RawOsMutex,
}

fn main() {
    let mutex = Mutex::new(Data {
        a: 2,
        b: 1,
        mutex: Default::default(),
    });
}
Source

pub fn into_inner(self) -> T::DataType

Consumes this mutex, returning the underlying data.

When the data type contains the raw mutex, which happens with #[derive(MutexProtected)], the returned data obviously still contains it. It is however in a destroyed state and may not be reused.

§Examples
#[macro_use]
extern crate flexible_locks;
#[macro_use]
extern crate flexible_locks_derive;
use flexible_locks::{Mutex, RawMutex};

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

#[derive(MutexProtected)]
struct Data {
    a: usize,
    b: usize,
    #[mutex]
    mutex: RawOsMutex,
}

fn main() {
    let mutex = Mutex::new(Data {
        a: 2,
        b: 1,
        mutex: Default::default(),
    });
    let data = mutex.into_inner();
    assert_eq!(data.a, 2);
    assert_eq!(data.b, 1);
}
Source§

impl<T: MutexProtected + ?Sized> Mutex<T>

Source

pub fn lock(&self) -> MutexGuard<'_, 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::{Mutex, RawMutex};

// 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;

#[derive(MutexProtected, Default)]
struct Data {
    a: usize,
    b: usize,
    #[mutex]
    mutex: RawOsMutex,
}

fn main() {
    let mutex = Arc::new(Mutex::new(Data::default()));
    let c_mutex = mutex.clone();

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

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

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::{Mutex, RawMutex};

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

#[derive(MutexProtected, Default)]
struct Data {
    a: usize,
    b: usize,
    #[mutex]
    mutex: RawOsMutex,
}

fn main() {
    let mut mutex = Mutex::new(Data::default());
    mutex.get_mut().a = 10;
    assert_eq!(mutex.lock().a, 10);
}

Trait Implementations§

Source§

impl<T: MutexProtected<DataType = T> + Sized + Default> Default for Mutex<T>

Source§

fn default() -> Self

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

impl<T: MutexProtected + ?Sized> Drop for Mutex<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: MutexProtected> From<T> for Mutex<T>
where T::DataType: Sized,

Source§

fn from(t: T) -> Self

Converts to this type from the input type.
Source§

impl<T: MutexProtected + ?Sized + Send> Send for Mutex<T>

Source§

impl<T: MutexProtected + ?Sized + Send> Sync for Mutex<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Mutex<T>

§

impl<T> !RefUnwindSafe for Mutex<T>

§

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

§

impl<T> UnwindSafe for Mutex<T>
where 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<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.