Struct qutex::Qutex

source ·
pub struct Qutex<T> { /* private fields */ }
Expand description

A lock-free-queue-backed exclusive data lock.

Implementations§

source§

impl<T> Qutex<T>

source

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

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

pub unsafe fn push_request(&self, req: Request)

Pushes a lock request onto the queue.

source

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.

source

pub fn as_ptr(&self) -> *const T

Returns a reference to the inner value.

source

pub fn as_mut_ptr(&self) -> *mut T

Returns a mutable reference to the inner value.

source

pub unsafe fn process_queue(&self)

Pops the next lock request in the queue if this (the caller’s) lock is unlocked.

source

pub unsafe fn direct_unlock(&self)

Unlocks this (the caller’s) lock and wakes up the next task in the queue.

Trait Implementations§

source§

impl<T> Clone for Qutex<T>

source§

fn clone(&self) -> Qutex<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for Qutex<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Default> Default for Qutex<T>

source§

fn default() -> Self

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

impl<T> From<T> for Qutex<T>

source§

fn from(val: T) -> Qutex<T>

Converts to this type from the input type.

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> 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<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
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> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.