use std::any::Any;
use std::future::Future;
use std::marker::PhantomData;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::pin::Pin;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::task::{Context, Poll, Waker};
use std::time::Duration;
use super::anybox::{AnyEnv, downcast_env, downcast_env_arc, erase_exit, unerase_exit};
use super::bind::BindHold;
use super::cause::{
cause_from_panic, from_exit, map_fail_mut, map_fail_once, split_failure, to_exit,
};
use super::ctx::Ctx;
use super::effect::{Effect, Node};
use super::runloop::{Instr, Runloop};
use crate::capability::{HNil, Plucker};
use crate::failure::Never;
use crate::failure::{Cause, Defect, Exit};
use crate::observability::{Span, SpanHook};
use crate::runtime::{BlockOnInAsync, Executor, FiberContext, LocalBoxFuture, NotLocal, Runtime};
use crate::runtime::{finish_children, in_async_context, park_on};
type LBox = Box<dyn Any>;
type LExit = Result<LBox, Cause<LBox>>;
type LCont = Box<dyn FnOnce(LBox) -> LInstr>;
type LHandler = Box<dyn FnOnce(LExit) -> LInstr>;
type LStepFn = Box<dyn FnOnce(&AnyEnv, &Arc<FiberContext>) -> LInstr>;
type LAsyncFn = Box<dyn FnOnce(&AnyEnv, &Arc<FiberContext>) -> LocalBoxFuture<'static, LExit>>;
type LProvideFn = Box<dyn FnOnce(&AnyEnv) -> AnyEnv>;
fn lerase<T: 'static>(t: T) -> LBox {
Box::new(t)
}
fn lunbox<T: 'static>(b: LBox) -> T {
match b.downcast::<T>() {
Ok(t) => *t,
Err(_) => panic!("id_effect: local erased value type mismatch (kernel bug)"),
}
}
fn lerase_exit<A: 'static, E: 'static>(r: Result<A, Cause<E>>) -> LExit {
match r {
Ok(a) => Ok(lerase(a)),
Err(c) => Err(map_fail_mut(c, &mut lerase)),
}
}
fn lunerase_exit<A: 'static, E: 'static>(r: LExit) -> Result<A, Cause<E>> {
match r {
Ok(a) => Ok(lunbox(a)),
Err(c) => Err(map_fail_mut(c, &mut lunbox)),
}
}
enum LInstr {
Pure(LExit),
Step(LStepFn),
Async(LAsyncFn),
Seq(Box<(LInstr, Vec<LCont>)>),
Fold(Box<LInstr>, LHandler),
Provide(Box<LInstr>, LProvideFn),
Mask(Box<LInstr>, Option<u32>),
}
enum LFrame {
Cont(LCont),
Fold(LHandler),
Env(AnyEnv),
Mask(u32),
}
const BUDGET: u32 = 2048;
struct LocalRunloop {
cur: Option<LInstr>,
fut: Option<LocalBoxFuture<'static, LExit>>,
stack: Vec<LFrame>,
env: AnyEnv,
fiber: Arc<FiberContext>,
}
impl LocalRunloop {
#[inline(always)]
fn unwind(&mut self, mut exit: LExit) -> LInstr {
while let Some(frame) = self.stack.pop() {
match frame {
LFrame::Cont(k) => match exit {
Ok(v) => return k(v),
Err(c) => exit = Err(c),
},
LFrame::Fold(h) => return h(exit),
LFrame::Env(e) => self.env = e,
LFrame::Mask(m) => {
self.fiber.set_mask(m);
if exit.is_ok() && self.fiber.should_interrupt() {
exit = Err(Cause::Interrupt(self.fiber.id()));
}
}
}
}
LInstr::Pure(exit)
}
}
impl Future for LocalRunloop {
type Output = LExit;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<LExit> {
let this = &mut *self;
let mut budget = BUDGET;
loop {
match catch_unwind(AssertUnwindSafe(|| this.drive(cx, &mut budget))) {
Ok(p) => return p,
Err(payload) => {
this.fut = None;
this.cur = Some(LInstr::Pure(Err(cause_from_panic(payload))));
}
}
}
}
}
impl LocalRunloop {
fn drive(&mut self, cx: &mut Context<'_>, budget: &mut u32) -> Poll<LExit> {
let mut instr = match self.fut.as_mut() {
Some(fut) => {
let exit = if self.fiber.should_interrupt() {
Err(Cause::Interrupt(self.fiber.id()))
} else {
match fut.as_mut().poll(cx) {
Poll::Ready(exit) => exit,
Poll::Pending => match self.suspend(cx) {
Some(i) => {
self.cur = Some(i);
return self.drive(cx, budget);
}
None => return Poll::Pending,
},
}
};
self.fut = None;
LInstr::Pure(exit)
}
None => match self.cur.take() {
Some(i) => i,
None => unreachable!("id_effect: local runloop polled after completion"),
},
};
loop {
if !matches!(instr, LInstr::Pure(Err(_))) && self.fiber.should_interrupt() {
instr = LInstr::Pure(Err(Cause::Interrupt(self.fiber.id())));
}
instr = match instr {
LInstr::Pure(exit) => {
if self.stack.is_empty() {
return Poll::Ready(exit);
}
match self.feed(exit, cx, budget) {
Some(next) => next,
None => return Poll::Pending,
}
}
LInstr::Step(f) => f(&self.env, &self.fiber),
LInstr::Async(f) => {
let fut = f(&self.env, &self.fiber);
match self.start(fut, cx) {
Some(next) => next,
None => return Poll::Pending,
}
}
LInstr::Seq(body) => {
let (inner, conts) = *body;
self.stack.extend(conts.into_iter().rev().map(LFrame::Cont));
inner
}
LInstr::Fold(inner, h) => {
self.stack.push(LFrame::Fold(h));
*inner
}
LInstr::Provide(inner, f) => {
let new_env = f(&self.env);
let old = std::mem::replace(&mut self.env, new_env);
self.stack.push(LFrame::Env(old));
*inner
}
LInstr::Mask(inner, set) => {
let old = self.fiber.mask();
self.stack.push(LFrame::Mask(old));
self.fiber.set_mask(set.unwrap_or(old + 1));
*inner
}
};
*budget -= 1;
if *budget == 0 {
self.cur = Some(instr);
cx.waker().wake_by_ref();
return Poll::Pending;
}
}
}
#[inline(always)]
fn feed(&mut self, mut exit: LExit, cx: &mut Context<'_>, budget: &mut u32) -> Option<LInstr> {
loop {
let v = match (exit, self.stack.last()) {
(Ok(v), Some(LFrame::Cont(_))) => v,
(exit, _) => return Some(self.unwind(exit)),
};
let Some(LFrame::Cont(k)) = self.stack.pop() else {
unreachable!("id_effect: checked Cont frame (kernel bug)")
};
exit = match k(v) {
LInstr::Pure(e) => e,
LInstr::Async(f) => {
if self.fiber.should_interrupt() {
Err(Cause::Interrupt(self.fiber.id()))
} else {
let mut fut = f(&self.env, &self.fiber);
match fut.as_mut().poll(cx) {
Poll::Ready(e) => e,
Poll::Pending => {
self.fut = Some(fut);
return self.suspend(cx);
}
}
}
}
other => return Some(other),
};
if exit.is_ok() && self.fiber.should_interrupt() {
exit = Err(Cause::Interrupt(self.fiber.id()));
}
*budget -= 1;
if *budget == 0 {
self.cur = Some(LInstr::Pure(exit));
cx.waker().wake_by_ref();
return None;
}
}
}
#[inline]
fn start(
&mut self,
mut fut: LocalBoxFuture<'static, LExit>,
cx: &mut Context<'_>,
) -> Option<LInstr> {
match fut.as_mut().poll(cx) {
Poll::Ready(exit) => Some(LInstr::Pure(exit)),
Poll::Pending => {
self.fut = Some(fut);
self.suspend(cx)
}
}
}
fn suspend(&mut self, cx: &mut Context<'_>) -> Option<LInstr> {
self.fiber.register_waker(cx.waker());
if self.fiber.should_interrupt() {
self.fut = None;
Some(LInstr::Pure(Err(Cause::Interrupt(self.fiber.id()))))
} else {
None
}
}
}
#[must_use = "effects are lazy; run them with `block_on`, `run_on` or `run_on_local`"]
pub struct LocalEffect<A, E = Never, R = HNil> {
node: LNode<A, E, R>,
}
enum LNode<A, E, R> {
Ready(Result<A, Cause<E>>),
Op(LInstr, PhantomData<fn() -> R>),
}
impl<A, E, R> std::fmt::Debug for LocalEffect<A, E, R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self.node {
LNode::Ready(_) => "LocalEffect::Ready",
LNode::Op(..) => "LocalEffect::Op",
})
}
}
impl<A, E, R> LocalEffect<A, E, R> {
const fn ready(r: Result<A, Cause<E>>) -> Self {
Self {
node: LNode::Ready(r),
}
}
fn from_instr(i: LInstr) -> Self {
Self {
node: LNode::Op(i, PhantomData),
}
}
pub const fn succeed(a: A) -> Self {
Self::ready(Ok(a))
}
pub const fn fail(e: E) -> Self {
Self::ready(Err(Cause::Fail(e)))
}
pub fn die(defect: impl Into<Defect>) -> Self {
Self::ready(Err(Cause::Die(defect.into())))
}
}
impl<A, E, R> LocalEffect<A, E, R>
where
A: 'static,
E: 'static,
R: Send + Sync + 'static,
{
fn into_instr(self) -> LInstr {
match self.node {
LNode::Ready(r) => LInstr::Pure(lerase_exit(r)),
LNode::Op(i, _) => i,
}
}
fn step(f: impl FnOnce(&AnyEnv, &Arc<FiberContext>) -> LInstr + 'static) -> Self {
Self::from_instr(LInstr::Step(Box::new(f)))
}
pub fn sync(f: impl FnOnce() -> A + 'static) -> Self {
Self::step(move |_, _| LInstr::Pure(Ok(lerase(f()))))
}
pub fn suspend(f: impl FnOnce() -> Self + 'static) -> Self {
Self::step(move |_, _| f().into_instr())
}
pub fn from_future<F, Fut>(f: F) -> Self
where
F: FnOnce() -> Fut + 'static,
Fut: Future<Output = Result<A, E>> + 'static,
{
Self::from_instr(LInstr::Async(Box::new(move |_, _| {
let fut = f();
Box::pin(async move { lerase_exit(fut.await.map_err(Cause::Fail)) })
})))
}
pub fn from_ctx<F, Fut>(f: F) -> Self
where
F: FnOnce(Ctx<R>) -> Fut + 'static,
Fut: Future<Output = Result<A, E>> + 'static,
{
Self::from_instr(LInstr::Async(Box::new(move |env, fiber| {
let fut = f(Ctx::new(downcast_env_arc::<R>(env), Arc::clone(fiber)));
Box::pin(async move { lerase_exit(fut.await.map_err(Cause::Fail)) })
})))
}
fn seq(self, k: LCont) -> LInstr {
match self.node {
LNode::Op(LInstr::Seq(mut body), _) => {
body.1.push(k);
LInstr::Seq(body)
}
node => LInstr::Seq(Box::new((Self { node }.into_instr(), vec![k]))),
}
}
pub fn map<B: 'static>(self, f: impl FnOnce(A) -> B + 'static) -> LocalEffect<B, E, R> {
match self.node {
LNode::Ready(r) => LocalEffect::step(move |_, _| LInstr::Pure(lerase_exit(r.map(f)))),
node => LocalEffect::from_instr(Self { node }.seq(Box::new(move |v| {
LInstr::Pure(Ok(lerase(f(lunbox::<A>(v)))))
}))),
}
}
pub fn flat_map<B: 'static>(
self,
f: impl FnOnce(A) -> LocalEffect<B, E, R> + 'static,
) -> LocalEffect<B, E, R> {
match self.node {
LNode::Ready(Ok(a)) => LocalEffect::step(move |_, _| f(a).into_instr()),
LNode::Ready(Err(c)) => LocalEffect::ready(Err(c)),
node => LocalEffect::from_instr(
Self { node }.seq(Box::new(move |v| f(lunbox::<A>(v)).into_instr())),
),
}
}
pub fn then<B: 'static>(self, next: LocalEffect<B, E, R>) -> LocalEffect<B, E, R> {
self.flat_map(move |_| next)
}
pub fn zip<B: 'static>(self, other: LocalEffect<B, E, R>) -> LocalEffect<(A, B), E, R> {
self.flat_map(move |a| other.map(move |b| (a, b)))
}
pub fn map_err<E2: 'static>(self, f: impl FnOnce(E) -> E2 + 'static) -> LocalEffect<A, E2, R> {
match self.node {
LNode::Ready(r) => {
LocalEffect::step(move |_, _| LInstr::Pure(lerase_exit(r.map_err(|c| map_fail_once(c, f)))))
}
node => LocalEffect::from_instr(LInstr::Fold(
Box::new(Self { node }.into_instr()),
Box::new(move |exit| match exit {
Ok(v) => LInstr::Pure(Ok(v)),
Err(c) => LInstr::Pure(Err(map_fail_once(c, |e| lerase(f(lunbox::<E>(e)))))),
}),
)),
}
}
pub fn catch<E2: 'static>(
self,
f: impl FnOnce(E) -> LocalEffect<A, E2, R> + 'static,
) -> LocalEffect<A, E2, R> {
match self.node {
LNode::Ready(Ok(a)) => LocalEffect::ready(Ok(a)),
LNode::Ready(Err(c)) => match split_failure::<E, E2>(c) {
Ok(e) => LocalEffect::step(move |_, _| f(e).into_instr()),
Err(c) => LocalEffect::ready(Err(c)),
},
node => LocalEffect::from_instr(LInstr::Fold(
Box::new(Self { node }.into_instr()),
Box::new(move |exit| match exit {
Ok(v) => LInstr::Pure(Ok(v)),
Err(c) => match split_failure::<LBox, LBox>(c) {
Ok(e) => f(lunbox::<E>(e)).into_instr(),
Err(c) => LInstr::Pure(Err(c)),
},
}),
)),
}
}
pub fn exit<E2: 'static>(self) -> LocalEffect<Exit<A, E>, E2, R> {
match self.node {
LNode::Ready(r) => LocalEffect::ready(Ok(to_exit(r))),
node => LocalEffect::from_instr(LInstr::Fold(
Box::new(Self { node }.into_instr()),
Box::new(|exit| LInstr::Pure(Ok(lerase(to_exit(lunerase_exit::<A, E>(exit)))))),
)),
}
}
pub fn ensuring(self, finalizer: LocalEffect<(), Never, R>) -> Self {
Self::step(move |_, fiber| {
let restore = fiber.mask();
let handler: LHandler = Box::new(move |exit| {
LInstr::Fold(
Box::new(finalizer.into_instr()),
Box::new(move |fin| match (exit, fin) {
(exit, Ok(_)) => LInstr::Pure(exit),
(Ok(_), Err(fc)) => LInstr::Pure(Err(fc)),
(Err(c), Err(fc)) => LInstr::Pure(Err(Cause::Then(Box::new(c), Box::new(fc)))),
}),
)
});
LInstr::Mask(
Box::new(LInstr::Fold(
Box::new(LInstr::Mask(Box::new(self.into_instr()), Some(restore))),
handler,
)),
None,
)
})
}
pub fn uninterruptible(self) -> Self {
LocalEffect::from_instr(LInstr::Mask(Box::new(self.into_instr()), None))
}
pub fn with_span_hook(self, hook: Arc<dyn SpanHook>) -> Self {
Self::step(move |_, fiber| {
let slot = Span::hook_ref();
let prev = slot.get_in(fiber);
slot.set_in(fiber, Some(hook));
let restore = LocalEffect::<(), Never, R>::step(move |_, fiber| {
Span::hook_ref().set_in(fiber, prev);
LInstr::Pure(Ok(lerase(())))
});
self.ensuring(restore).into_instr()
})
}
pub fn provide<T, I>(self, t: T) -> LocalEffect<A, E, R::Rest>
where
T: Send + Sync + 'static,
R: Plucker<T, I>,
{
LocalEffect::from_instr(LInstr::Provide(
Box::new(self.into_instr()),
Box::new(move |env| {
let rest = downcast_env::<R::Rest>(env).clone();
Arc::new(R::plant(Arc::new(t), rest)) as AnyEnv
}),
))
}
pub fn provide_env(self, env: R) -> LocalEffect<A, E, HNil> {
LocalEffect::from_instr(LInstr::Provide(
Box::new(self.into_instr()),
Box::new(move |_| Arc::new(env) as AnyEnv),
))
}
pub(crate) fn run_in(self, ctx: Ctx<R>) -> LocalRunFuture<A, E> {
let state = match self.node {
LNode::Ready(r) => LRunState::Ready(Some(r)),
LNode::Op(instr, _) => LRunState::Running(LocalRunloop {
cur: Some(instr),
fut: None,
stack: Vec::new(),
env: ctx.env as AnyEnv,
fiber: ctx.fiber,
}),
};
LocalRunFuture {
state,
_out: PhantomData,
}
}
}
impl<A: 'static, R: Send + Sync + 'static> LocalEffect<A, Never, R> {
pub fn widen<E: 'static>(self) -> LocalEffect<A, E, R> {
self.map_err(|n| match n {})
}
}
impl<E: 'static, R: Send + Sync + 'static> LocalEffect<(), E, R> {
pub fn sleep(d: Duration) -> Self {
Self::from_instr(LInstr::Async(Box::new(move |_, fiber| {
let s = fiber.executor().sleep(d);
Box::pin(async move {
s.await;
Ok(lerase(()))
})
})))
}
pub fn yield_fiber() -> Self {
Self::from_instr(LInstr::Async(Box::new(|_, _| {
let mut yielded = false;
Box::pin(std::future::poll_fn(move |cx| {
if yielded {
Poll::Ready(Ok(lerase(())))
} else {
yielded = true;
cx.waker().wake_by_ref();
Poll::Pending
}
}))
})))
}
}
#[must_use = "futures do nothing unless polled"]
pub struct LocalRunFuture<A, E> {
state: LRunState<A, E>,
_out: PhantomData<fn() -> (A, E)>,
}
enum LRunState<A, E> {
Ready(Option<Result<A, Cause<E>>>),
Running(LocalRunloop),
}
impl<A, E> Unpin for LocalRunFuture<A, E> {}
impl<A, E> std::fmt::Debug for LocalRunFuture<A, E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self.state {
LRunState::Ready(_) => "LocalRunFuture::Ready",
LRunState::Running(_) => "LocalRunFuture::Running",
})
}
}
impl<A: 'static, E: 'static> Future for LocalRunFuture<A, E> {
type Output = Exit<A, E>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Exit<A, E>> {
match &mut self.get_mut().state {
LRunState::Ready(r) => Poll::Ready(to_exit(
r.take()
.expect("id_effect: LocalRunFuture polled after completion"),
)),
LRunState::Running(rl) => Pin::new(rl)
.poll(cx)
.map(|exit| to_exit(lunerase_exit::<A, E>(exit))),
}
}
}
impl<A: 'static, E: 'static> LocalEffect<A, E, HNil> {
pub fn run_on(self, fiber: Arc<FiberContext>) -> LocalRunFuture<A, E> {
self.run_in(Ctx::new(Arc::new(HNil), fiber))
}
pub fn run_fiber(self, fiber: Arc<FiberContext>) -> impl Future<Output = Exit<A, E>> {
let run = self.run_on(Arc::clone(&fiber));
async move {
let exit = run.await;
finish_children(&fiber).await;
fiber.mark_done();
exit
}
}
pub fn try_block_on(self) -> Result<Exit<A, E>, BlockOnInAsync> {
if in_async_context() {
return Err(BlockOnInAsync);
}
Ok(match self.node {
LNode::Ready(r) => to_exit(r),
node => {
let exec = Arc::clone(Runtime::global().executor());
park_on(Self { node }.run_fiber(FiberContext::new_root(exec)))
}
})
}
#[allow(
clippy::disallowed_methods,
reason = "G-nb: the convenience blocking edge itself, delegating to `try_block_on`"
)]
pub fn block_on(self) -> Exit<A, E> {
self
.try_block_on()
.unwrap_or_else(|e| Exit::Failure(Cause::Die(Defect::new(e.to_string()))))
}
}
fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
m.lock().unwrap_or_else(PoisonError::into_inner)
}
struct LocalSlot<A, E> {
exit: Option<Exit<A, E>>,
waker: Option<Waker>,
}
struct JoinLocal<A, E> {
slot: Arc<Mutex<LocalSlot<A, E>>>,
fiber: Arc<FiberContext>,
done: bool,
}
impl<A, E> Future for JoinLocal<A, E> {
type Output = Exit<A, E>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Exit<A, E>> {
let mut slot = lock(&self.slot);
match slot.exit.take() {
Some(exit) => {
drop(slot);
self.done = true;
Poll::Ready(exit)
}
None => {
slot.waker = Some(cx.waker().clone());
Poll::Pending
}
}
}
}
impl<A, E> Drop for JoinLocal<A, E> {
fn drop(&mut self) {
if !self.done {
self.fiber.request_interrupt();
}
}
}
impl<A: Send + 'static, E: Send + 'static> LocalEffect<A, E, HNil> {
pub fn run_on_local(self, exec: &Arc<dyn Executor>) -> Result<Effect<A, E, HNil>, NotLocal> {
let slot = Arc::new(Mutex::new(LocalSlot {
exit: None,
waker: None,
}));
let fiber = FiberContext::new_root(Arc::clone(exec));
let run = self.run_fiber(Arc::clone(&fiber));
let done = Arc::clone(&slot);
exec.spawn_local(Box::pin(async move {
let exit = run.await;
let waker = {
let mut s = lock(&done);
s.exit = Some(exit);
s.waker.take()
};
if let Some(w) = waker {
w.wake();
}
}))?;
let join = JoinLocal {
slot,
fiber,
done: false,
};
Ok(Effect::from_instr(Instr::Future(Box::pin(async move {
erase_exit(from_exit(join.await))
}))))
}
}
pub(crate) struct HoldMask<F> {
fut: F,
fiber: Arc<FiberContext>,
hold: BindHold,
}
impl<F> HoldMask<F> {
pub(crate) fn new(fut: F, fiber: Arc<FiberContext>) -> Self {
Self {
fut,
fiber,
hold: BindHold::default(),
}
}
}
impl<F: Future + Unpin> Future for HoldMask<F> {
type Output = F::Output;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<F::Output> {
let this = &mut *self;
let outer = this.hold.lift(&this.fiber);
let polled = Pin::new(&mut this.fut).poll(cx);
this.hold.settle(&this.fiber, outer, polled.is_pending());
polled
}
}
impl<F> Drop for HoldMask<F> {
fn drop(&mut self) {
self.hold.release(&self.fiber);
}
}
pub trait IntoLocal<A, E, R> {
fn into_local(self) -> LocalEffect<A, E, R>;
}
impl<A, E, R> IntoLocal<A, E, R> for LocalEffect<A, E, R> {
fn into_local(self) -> Self {
self
}
}
impl<A, E, R> IntoLocal<A, E, R> for Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
fn into_local(self) -> LocalEffect<A, E, R> {
self.local()
}
}
impl<A, E, R> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
pub fn local(self) -> LocalEffect<A, E, R> {
match self.node {
Node::Pure(r) => LocalEffect::ready(r),
node => {
let instr = Effect { node }.into_instr();
LocalEffect::from_instr(LInstr::Async(Box::new(move |env, fiber| {
let rl = Runloop::new(instr, Arc::clone(env), Arc::clone(fiber));
let rl = HoldMask::new(rl, Arc::clone(fiber));
Box::pin(async move { lerase_exit(unerase_exit::<A, E>(rl.await)) })
})))
}
}
}
}