use core::{future::Future, time::Duration};
use crate::{
Either2, EventLoopError, NotEnoughResources,
claimed::Claimed,
select2,
selector::{EVENTS_PER_CALL, Selector, SelectorEvent},
storage::LoopStorage,
tasks::{SlotWaiterTakenError, TaskReservation},
};
#[must_use]
pub struct Context<
'sel,
E: 'static,
const EVENTS: usize,
const TASKS: usize,
const TASK_SIZE: usize,
const ROOT_TASK_SIZE: usize,
const TIMERS: usize,
const FDS: usize,
> {
pub(crate) storage:
&'static LoopStorage<E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
pub(crate) selector: &'sel Selector,
pub(crate) claimed: Claimed<'sel>,
}
impl<
'sel,
E: 'static,
const EVENTS: usize,
const TASKS: usize,
const TASK_SIZE: usize,
const ROOT_TASK_SIZE: usize,
const TIMERS: usize,
const FDS: usize,
> Context<'sel, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>
{
pub fn dispatch(&self, event: E) -> Result<(), E> {
self.storage.events.push(event)
}
pub fn spawn<F, Fut>(
&self,
f: F,
) -> Result<
impl Future<Output = ()> + use<E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS, F, Fut>,
NotEnoughResources,
>
where
F: FnOnce(Self) -> Fut,
Fut: Future<Output = ()> + 'sel,
{
self.storage.tasks.spawn(self.claimed, f(*self))
}
pub fn reserve_task(
&self,
) -> impl Future<
Output = Result<
TaskReservation<'sel, TASKS, TASK_SIZE, ROOT_TASK_SIZE>,
SlotWaiterTakenError,
>,
> + use<'sel, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS> {
self.storage.tasks.reserve(self.claimed)
}
pub fn sleep(
&self,
duration: Duration,
) -> impl Future<Output = Result<(), NotEnoughResources>>
+ use<'sel, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS> {
let timers = &self.storage.timers;
timers.sleep_until(timers.now().saturating_add(duration))
}
pub fn timeout<F: Future>(
&self,
duration: Duration,
future: F,
) -> impl Future<Output = Result<MaybeTimedOut<F::Output>, NotEnoughResources>> {
let sleep = self.sleep(duration);
async move {
match select2(future, sleep).await {
Either2::Left(output) => Ok(MaybeTimedOut::Completed(output)),
Either2::Right(Ok(())) => Ok(MaybeTimedOut::Timeout),
Either2::Right(Err(err)) => Err(err),
}
}
}
pub fn notified(
&self,
) -> impl Future<Output = ()> + use<'sel, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>
{
self.storage.notification.notified()
}
pub(crate) fn clear(&self) {
self.storage.tasks.drop_all();
self.storage.timers.drop_wakers();
self.storage.io_waiters.drop_wakers();
self.storage.notification.clear();
self.storage.events.clear();
}
pub(crate) fn drain(&self) -> Result<(), EventLoopError> {
if !self.storage.tasks.has_root() {
return Err(EventLoopError::RootFinished);
}
loop {
self.process_selector_events()?;
if !self.storage.tasks.has_woken() {
break;
}
if self.storage.tasks.run_ready().is_ready() {
self.storage.tasks.drop_all();
self.flush()?;
return Err(EventLoopError::RootFinished);
}
}
self.flush()?;
Ok(())
}
fn process_selector_events(&self) -> rustix::io::Result<()> {
let mut events = [None; EVENTS_PER_CALL];
loop {
let full = self.selector.poll_nonblocking(&mut events)?;
for event in events.iter().map_while(|event| *event) {
match event {
SelectorEvent::Io { fd, ready } => {
self.storage.io_waiters.wake_ready(fd, ready);
}
SelectorEvent::Notify => self.storage.notification.fire(),
SelectorEvent::Timer => {
let now = self.storage.timers.now();
while let Some(waker) = self.storage.timers.take_expired(now) {
waker.wake();
}
}
}
}
if !full {
return Ok(());
}
}
}
pub(crate) fn flush(&self) -> rustix::io::Result<()> {
self.selector.flush(
self.storage.timers.next_deadline(),
self.storage.timers.now(),
)
}
}
impl<
E: 'static,
const EVENTS: usize,
const TASKS: usize,
const TASK_SIZE: usize,
const ROOT_TASK_SIZE: usize,
const TIMERS: usize,
const FDS: usize,
> Clone for Context<'_, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>
{
fn clone(&self) -> Self {
*self
}
}
impl<
E: 'static,
const EVENTS: usize,
const TASKS: usize,
const TASK_SIZE: usize,
const ROOT_TASK_SIZE: usize,
const TIMERS: usize,
const FDS: usize,
> Copy for Context<'_, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>
{
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MaybeTimedOut<T> {
Timeout,
Completed(T),
}
#[cfg(test)]
mod tests {
use core::{
future::{Future, pending},
pin::pin,
sync::atomic::{AtomicBool, AtomicU64, Ordering::Relaxed},
task::{Context, Poll, Waker},
time::Duration,
};
use super::MaybeTimedOut;
use crate::{
LoopStorage, Selector,
testing::{FlagOnDrop, assert_yields, fake_instant, wake_expired_timers},
time::Instant,
};
type Storage = LoopStorage<u32, 3, 3, 128, 512, 3, 1>;
#[test]
fn future_finishing_first_wins() {
static STORAGE: Storage = Storage::new();
let selector = Selector::new().unwrap();
let (ctx, root) = STORAGE.claim(&selector).unwrap();
ctx.storage.tasks.spawn_root(root, ctx.claimed, async move {
let result = ctx
.timeout(Duration::from_secs(60), ctx.notified())
.await
.unwrap();
let event = match result {
MaybeTimedOut::Timeout => 0,
MaybeTimedOut::Completed(()) => 1,
};
ctx.dispatch(event).unwrap();
pending::<()>().await;
});
assert_eq!(ctx.storage.tasks.run_ready(), Poll::Pending);
assert!(ctx.storage.timers.next_deadline().is_some());
ctx.storage.notification.fire();
assert_eq!(ctx.storage.tasks.run_ready(), Poll::Pending);
assert_yields!(ctx.storage.events.iter_removing(), [1]);
assert_eq!(ctx.storage.timers.next_deadline(), None);
ctx.clear();
}
#[test]
fn elapsing_drops_the_future() {
static DROPPED: AtomicBool = AtomicBool::new(false);
static NOW: AtomicU64 = AtomicU64::new(0);
fn now() -> Instant {
fake_instant(&NOW)
}
static STORAGE: Storage = Storage::with_clock(now);
let selector = Selector::new().unwrap();
let (ctx, root) = STORAGE.claim(&selector).unwrap();
ctx.storage.tasks.spawn_root(root, ctx.claimed, async move {
let result = ctx
.timeout(Duration::from_millis(10), async {
let _flag = FlagOnDrop::new(&DROPPED);
pending::<()>().await;
})
.await
.unwrap();
let event = match result {
MaybeTimedOut::Timeout => 0,
MaybeTimedOut::Completed(()) => 1,
};
ctx.dispatch(event).unwrap();
pending::<()>().await;
});
assert_eq!(ctx.storage.tasks.run_ready(), Poll::Pending);
assert!(!DROPPED.load(Relaxed));
NOW.store(10, Relaxed);
wake_expired_timers(&ctx.storage.timers);
assert_eq!(ctx.storage.tasks.run_ready(), Poll::Pending);
assert_yields!(ctx.storage.events.iter_removing(), [0]);
assert!(DROPPED.load(Relaxed));
assert_eq!(ctx.storage.timers.next_deadline(), None);
ctx.clear();
}
#[test]
fn ready_future_beats_a_zero_timeout() {
static STORAGE: Storage = Storage::new();
let selector = Selector::new().unwrap();
let ctx = STORAGE.claim(&selector).unwrap().0;
let mut cx = Context::from_waker(Waker::noop());
let mut timeout = pin!(ctx.timeout(Duration::ZERO, async {
let value = 1;
let borrowed = &value;
core::future::ready(()).await;
*borrowed
}));
assert_eq!(
timeout.as_mut().poll(&mut cx),
Poll::Ready(Ok(MaybeTimedOut::Completed(1)))
);
assert_eq!(ctx.storage.timers.next_deadline(), None);
}
#[test]
fn pending_pinned_future_keeps_its_timer_until_dropped() {
static STORAGE: Storage = Storage::new();
let selector = Selector::new().unwrap();
let ctx = STORAGE.claim(&selector).unwrap().0;
let mut cx = Context::from_waker(Waker::noop());
{
let mut timeout = pin!(ctx.timeout(Duration::from_secs(60), async {
let value = 1;
let borrowed = &value;
pending::<()>().await;
*borrowed
}));
assert_eq!(timeout.as_mut().poll(&mut cx), Poll::Pending);
assert_eq!(timeout.as_mut().poll(&mut cx), Poll::Pending);
assert!(ctx.storage.timers.next_deadline().is_some());
}
assert_eq!(ctx.storage.timers.next_deadline(), None);
}
#[test]
fn sleeps_complete_in_deadline_order() {
static NOW: AtomicU64 = AtomicU64::new(0);
fn now() -> Instant {
fake_instant(&NOW)
}
static STORAGE: Storage = Storage::with_clock(now);
let selector = Selector::new().unwrap();
let (ctx, root) = STORAGE.claim(&selector).unwrap();
ctx.storage.tasks.spawn_root(root, ctx.claimed, async move {
let handles = [300, 100, 200].map(|ms| {
ctx.spawn(async move |ctx| {
ctx.sleep(Duration::from_millis(ms)).await.unwrap();
ctx.dispatch(u32::try_from(ms).unwrap()).unwrap();
})
.unwrap()
});
for handle in handles {
handle.await;
}
});
assert_eq!(ctx.storage.tasks.run_ready(), Poll::Pending);
NOW.store(300, Relaxed);
wake_expired_timers(&ctx.storage.timers);
assert_eq!(ctx.storage.tasks.run_ready(), Poll::Ready(()));
assert_yields!(ctx.storage.events.iter_removing(), [100, 200, 300]);
ctx.clear();
}
}