Crates.io | flexible-locks |
lib.rs | flexible-locks |
version | 0.1.0 |
source | src |
created_at | 2018-04-28 00:58:13.631823 |
updated_at | 2018-04-28 00:58:13.631823 |
description | Flexible Locks |
homepage | |
repository | https://github.com/glandium/flexible-locks |
max_upload_size | |
id | 62689 |
size | 60,755 |
This crate aims at providing generic, flexible implementations of locking
primitives. For now, it only provides Mutex
types (i.e. no RwLock
, etc.),
without poisoning, and without try_lock
. Support for those can be
added in the future if there is interest (patches welcome). Poisoning is not
necessary with panic=abort.
The provided types allow flexibility in layout and locking implementation.
std::sync::Mutex
try_lock
.Box
or
not, as long as the RawMutex
trait is implemented. Choose carefully.MutexWrap
type for a variant that looks more like std::sync::Mutex
,
but still allows to use a specific raw mutex primitive.ffi-example
directory.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();
}
The parking_lot
feature can be enabled, providing a RawMutex
implementation for parking_log::Mutex<()>
.
License: Apache-2.0/MIT