Struct flexible_locks::Mutex [] [src]

pub struct Mutex<T: MutexProtected + ?Sized> { /* fields omitted */ }

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].

Methods

impl<T: MutexProtected> Mutex<T> where
    T::DataType: Sized
[src]

[src]

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(),
    });
}

[src]

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);
}

impl<T: MutexProtected + ?Sized> Mutex<T>
[src]

[src]

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);
}

[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

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

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

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

impl<T: MutexProtected> From<T> for Mutex<T> where
    T::DataType: Sized
[src]

[src]

Performs the conversion.

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

[src]

Returns the "default value" for a type. Read more

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

[src]

Executes the destructor for this type. Read more