use crate::store::{Store, UpdateDeadline};
use crate::trap::Trap;
use crate::Result;
#[cfg(feature = "async")]
pub(super) async fn yield_now() {
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
struct YieldNow(bool);
impl Future for YieldNow {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
if self.0 {
Poll::Ready(())
} else {
self.0 = true;
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
YieldNow(false).await;
}
fn take_epoch_action<T>(store: &mut Store<T>) -> Result<UpdateDeadline> {
let mut cb = store.epoch_callback.take();
let action = match cb.as_mut() {
Some(f) => f(store.as_context_mut()),
None => Ok(UpdateDeadline::Interrupt),
};
store.epoch_callback = cb;
action
}
fn extend_epoch_deadline<T>(store: &mut Store<T>, delta: u64) {
let next = store.inner.engine().current_epoch().saturating_add(delta);
store.inner.set_epoch_deadline(next);
}
pub(super) fn apply_epoch_deadline<T>(store: &mut Store<T>) -> Result<()> {
match take_epoch_action(store)? {
UpdateDeadline::Interrupt => Err(Trap::Interrupt.into()),
UpdateDeadline::Continue(delta) => {
extend_epoch_deadline(store, delta);
Ok(())
}
#[cfg(feature = "async")]
UpdateDeadline::Yield(_) => Err(Trap::Interrupt.into()),
}
}
#[cfg(feature = "async")]
pub(super) async fn apply_epoch_deadline_async<T>(store: &mut Store<T>) -> Result<()> {
match take_epoch_action(store)? {
UpdateDeadline::Interrupt => Err(Trap::Interrupt.into()),
UpdateDeadline::Continue(delta) => {
extend_epoch_deadline(store, delta);
Ok(())
}
UpdateDeadline::Yield(delta) => {
yield_now().await;
extend_epoch_deadline(store, delta);
Ok(())
}
}
}