Skip to main content

WaitList

Struct WaitList 

Source
pub struct WaitList<I, O> { /* private fields */ }
Expand description

An intrusively linked list of futures waiting on an event.

This is the most fundamental primitive to many of the synchronization utilities provided by this crate.

§Examples

A simple unfair, unsynchronized async mutex.

use std::cell::Cell;
use std::cell::UnsafeCell;
use std::ops::Deref;
use std::ops::DerefMut;

use unsync::wait_list;
use unsync::wait_list::WaitList;

pub struct Mutex<T> {
    data: UnsafeCell<T>,
    locked: Cell<bool>,
    waiters: WaitList<(), ()>,
}

impl<T> Mutex<T> {
    pub const fn new(data: T) -> Self {
        Self {
            data: UnsafeCell::new(data),
            locked: Cell::new(false),
            waiters: WaitList::new(),
        }
    }

    pub async fn lock(&self) -> MutexGuard<'_, T> {
        while self.locked.replace(true) {
            self.waiters.wait(()).await;
        }
        MutexGuard { mutex: self }
    }
}

pub struct MutexGuard<'mutex, T> {
    mutex: &'mutex Mutex<T>,
}

impl<T> Deref for MutexGuard<'_, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        unsafe { &*self.mutex.data.get() }
    }
}

impl<T> DerefMut for MutexGuard<'_, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { &mut *self.mutex.data.get() }
    }
}

impl<T> Drop for MutexGuard<'_, T> {
    fn drop(&mut self) {
        self.mutex.locked.set(false);
        self.mutex.waiters.borrow().wake_one(());
    }
}

Implementations§

Source§

impl<I, O> WaitList<I, O>

Source

pub const fn new() -> Self

Construct a new empty WaitList.

§Examples
use unsync::wait_list::WaitList;

let list = WaitList::<(), ()>::new();
Source

pub fn try_borrow(&self) -> Option<Borrowed<'_, I, O>>

Attempt to borrow uniquely the contents of this list, returning None if it is already currently borrowed.

§Examples
use unsync::wait_list::WaitList;

let list = WaitList::<(), ()>::new();
let a = list.borrow();
assert!(list.try_borrow().is_none());
Source

pub fn borrow(&self) -> Borrowed<'_, I, O>

Borrow uniquely the contents of this list.

§Panics

Panics if the list is already borrowed. For a non-panicking variant, see WaitList::try_borrow.

use unsync::wait_list::WaitList;

let list = WaitList::<(), ()>::new();
let a = list.borrow();
let b = list.borrow(); // panics since `a` is live.
Source

pub async fn wait(&self, input: I) -> O

Wait on the wait list.

The returned future resolves once Borrowed::wake_one is called and it is at the front of the queue.

§Panics

Panics if the list is currently borrowed.

Trait Implementations§

Source§

impl<I, O> Debug for WaitList<I, O>

Source§

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

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

impl<I, O> Default for WaitList<I, O>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<I, O> !Freeze for WaitList<I, O>

§

impl<I, O> !RefUnwindSafe for WaitList<I, O>

§

impl<I, O> !Send for WaitList<I, O>

§

impl<I, O> !Sync for WaitList<I, O>

§

impl<I, O> !UnwindSafe for WaitList<I, O>

§

impl<I, O> Unpin for WaitList<I, O>

§

impl<I, O> UnsafeUnpin for WaitList<I, O>

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<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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.