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>
impl<I, O> WaitList<I, O>
Sourcepub fn try_borrow(&self) -> Option<Borrowed<'_, I, O>>
pub fn try_borrow(&self) -> Option<Borrowed<'_, I, O>>
Sourcepub fn borrow(&self) -> Borrowed<'_, I, O>
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.Sourcepub async fn wait(&self, input: I) -> O
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§
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> 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