Struct flexible_locks::MutexWrap
[−]
[src]
pub struct MutexWrap<M: RawMutex, T: ?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 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(); }
Methods
impl<M: RawMutex + Default, T> MutexWrap<M, T>
[src]
pub fn new(t: T) -> Self
[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::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); }
pub fn into_inner(self) -> T
[src]
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); }
impl<M: RawMutex, T: ?Sized> MutexWrap<M, T>
[src]
pub fn lock(&self) -> MutexGuard<MutexWrapper<M, T>>
[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::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); }
ⓘImportant traits for &'a mut Ipub fn get_mut(&mut self) -> &mut T
[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::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); }