pub struct Qutex<T> { /* private fields */ }
Expand description
A lock-free-queue-backed exclusive data lock.
Implementations§
source§impl<T> Qutex<T>
impl<T> Qutex<T>
sourcepub fn new(val: T) -> Qutex<T>
pub fn new(val: T) -> Qutex<T>
Creates and returns a new Qutex
.
Examples found in repository?
examples/qutex.rs (line 14)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn main() {
let thread_count = 100;
let mut threads = Vec::with_capacity(thread_count);
let start_val = 0;
// Create a `Qutex` protecting a start value of zero.
let qutex = Qutex::new(start_val);
// Spawn several threads, each adding 1 to the protected value.
for _ in 0..thread_count {
// Obtain a 'guard' (akin to a `std::sync::MutexGuard`).
let future_val = qutex.clone().lock();
// Add 1 to the protected value. `future_val` is a `FutureGuard` which
// will resolve to a `Guard` providing mutable access to the protected
// value. The guard can be passed between futures combinators and will
// unlock the `Qutex` when dropped.
let future_add = future_val.map(|mut val| {
*val += 1;
});
// Spawn a thread which blocks upon completion of the above lock and
// add operations.
threads.push(thread::spawn(|| {
future_add.wait().unwrap();
}));
}
for thread in threads {
thread.join().unwrap();
}
let val = qutex.lock().wait().unwrap();
assert_eq!(*val, start_val + thread_count);
println!("Qutex final value: {}", *val);
}
sourcepub fn lock(self) -> FutureGuard<T>
pub fn lock(self) -> FutureGuard<T>
Returns a new FutureGuard
which can be used as a future and will
resolve into a Guard
.
Examples found in repository?
examples/qutex.rs (line 19)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn main() {
let thread_count = 100;
let mut threads = Vec::with_capacity(thread_count);
let start_val = 0;
// Create a `Qutex` protecting a start value of zero.
let qutex = Qutex::new(start_val);
// Spawn several threads, each adding 1 to the protected value.
for _ in 0..thread_count {
// Obtain a 'guard' (akin to a `std::sync::MutexGuard`).
let future_val = qutex.clone().lock();
// Add 1 to the protected value. `future_val` is a `FutureGuard` which
// will resolve to a `Guard` providing mutable access to the protected
// value. The guard can be passed between futures combinators and will
// unlock the `Qutex` when dropped.
let future_add = future_val.map(|mut val| {
*val += 1;
});
// Spawn a thread which blocks upon completion of the above lock and
// add operations.
threads.push(thread::spawn(|| {
future_add.wait().unwrap();
}));
}
for thread in threads {
thread.join().unwrap();
}
let val = qutex.lock().wait().unwrap();
assert_eq!(*val, start_val + thread_count);
println!("Qutex final value: {}", *val);
}
sourcepub unsafe fn push_request(&self, req: Request)
pub unsafe fn push_request(&self, req: Request)
Pushes a lock request onto the queue.
sourcepub fn get_mut(&mut self) -> Option<&mut T>
pub fn get_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the inner Vec
if there are currently
no other copies of this Qutex
.
Since this call borrows the inner lock mutably, no actual locking needs to take place—the mutable borrow statically guarantees no locks exist.
sourcepub fn as_mut_ptr(&self) -> *mut T
pub fn as_mut_ptr(&self) -> *mut T
Returns a mutable reference to the inner value.
sourcepub unsafe fn process_queue(&self)
pub unsafe fn process_queue(&self)
Pops the next lock request in the queue if this (the caller’s) lock is unlocked.
sourcepub unsafe fn direct_unlock(&self)
pub unsafe fn direct_unlock(&self)
Unlocks this (the caller’s) lock and wakes up the next task in the queue.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Qutex<T>
impl<T> !RefUnwindSafe for Qutex<T>
impl<T> Send for Qutex<T>where
T: Send,
impl<T> Sync for Qutex<T>where
T: Send,
impl<T> Unpin for Qutex<T>
impl<T> !UnwindSafe for Qutex<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more