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 crate::capability::HNil;
use crate::failure::Exit;
use crate::failure::FiberId;
use crate::kernel::{
AnyEnv, AnyExit, Effect, Instr, Runloop, cause_from_panic, from_async, to_exit, unerase_exit,
};
use crate::runtime::{Done, Executor, FiberContext};
fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
m.lock().unwrap_or_else(PoisonError::into_inner)
}
pub(crate) struct ExitCell<T> {
state: Mutex<CellState<T>>,
}
enum CellState<T> {
Waiting(Option<Waker>),
Ready(T),
Taken,
}
impl<T> ExitCell<T> {
pub(crate) fn new() -> Self {
Self {
state: Mutex::new(CellState::Waiting(None)),
}
}
pub(crate) fn complete(&self, v: T) -> Result<(), T> {
let mut s = lock(&self.state);
match &mut *s {
CellState::Waiting(w) => {
let w = w.take();
*s = CellState::Ready(v);
drop(s);
if let Some(w) = w {
w.wake();
}
Ok(())
}
CellState::Ready(_) | CellState::Taken => Err(v),
}
}
pub(crate) fn is_complete(&self) -> bool {
!matches!(*lock(&self.state), CellState::Waiting(_))
}
pub(crate) fn poll_take(&self, cx: &mut Context<'_>) -> Poll<T> {
let mut s = lock(&self.state);
match std::mem::replace(&mut *s, CellState::Taken) {
CellState::Ready(v) => Poll::Ready(v),
CellState::Waiting(old) => {
let w = match old {
Some(w) if w.will_wake(cx.waker()) => w,
_ => cx.waker().clone(),
};
*s = CellState::Waiting(Some(w));
Poll::Pending
}
CellState::Taken => panic!("id_effect: fiber exit taken twice (kernel bug)"),
}
}
}
pub(crate) type FiberCell = Arc<ExitCell<AnyExit>>;
struct CatchPanic<F>(F);
impl<F: Future + Unpin> Future for CatchPanic<F> {
type Output = Result<F::Output, Box<dyn Any + Send>>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let inner = &mut self.0;
match catch_unwind(AssertUnwindSafe(|| Pin::new(inner).poll(cx))) {
Ok(Poll::Pending) => Poll::Pending,
Ok(Poll::Ready(v)) => Poll::Ready(Ok(v)),
Err(payload) => Poll::Ready(Err(payload)),
}
}
}
pub(crate) async fn finish_children(ctx: &Arc<FiberContext>) {
loop {
let kids = ctx.drain_children();
if kids.is_empty() {
return;
}
for k in &kids {
k.request_interrupt();
}
for k in kids {
Done(k).await;
}
}
}
pub(crate) fn spawn_fiber(ctx: &Arc<FiberContext>, instr: Instr, env: AnyEnv) -> FiberCell {
if let Some(p) = ctx.parent() {
p.add_child(ctx);
}
let cell: FiberCell = Arc::new(ExitCell::new());
let task = {
let ctx = Arc::clone(ctx);
let cell = Arc::clone(&cell);
async move {
let exit = match CatchPanic(Runloop::new(instr, env, Arc::clone(&ctx))).await {
Ok(exit) => exit,
Err(payload) => Err(cause_from_panic(payload)),
};
finish_children(&ctx).await;
if let Some(p) = ctx.parent() {
p.remove_child(ctx.id());
}
ctx.mark_done();
let _ = cell.complete(exit);
}
};
ctx.executor().spawn(Box::pin(task));
cell
}
impl<A: Send + 'static, E: Send + 'static> Effect<A, E, HNil> {
pub fn run_fiber(self, fiber: Arc<FiberContext>) -> impl Future<Output = Exit<A, E>> + Send {
let run = self.run_on(Arc::clone(&fiber));
async move {
let exit = match CatchPanic(run).await {
Ok(exit) => exit,
Err(payload) => Exit::Failure(cause_from_panic(payload)),
};
finish_children(&fiber).await;
fiber.mark_done();
exit
}
}
pub fn fork_root(self, exec: Arc<dyn Executor>) -> FiberHandle<A, E> {
let ctx = FiberContext::new_root(exec);
let cell = spawn_fiber(&ctx, self.into_instr(), Arc::new(HNil) as AnyEnv);
FiberHandle::new(ctx, cell)
}
}
pub struct FiberHandle<A, E> {
ctx: Arc<FiberContext>,
cell: FiberCell,
_p: PhantomData<fn() -> (A, E)>,
}
impl<A, E> std::fmt::Debug for FiberHandle<A, E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("FiberHandle").field(&self.ctx.id()).finish()
}
}
impl<A: Send + 'static, E: Send + 'static> FiberHandle<A, E> {
pub(crate) fn new(ctx: Arc<FiberContext>, cell: FiberCell) -> Self {
Self {
ctx,
cell,
_p: PhantomData,
}
}
pub fn id(&self) -> FiberId {
self.ctx.id()
}
pub fn fiber(&self) -> &Arc<FiberContext> {
&self.ctx
}
pub fn is_done(&self) -> bool {
self.cell.is_complete()
}
pub fn join_future(self) -> JoinFuture<A, E> {
JoinFuture {
cell: self.cell,
_p: PhantomData,
}
}
pub fn join<R: Send + Sync + 'static>(self) -> Effect<A, E, R> {
Effect::from_instr(Instr::Future(Box::pin(CellFuture(self.cell))))
}
pub fn interrupt_now(&self) {
self.ctx.request_interrupt();
}
pub fn interrupt<E2: Send + 'static, R: Send + Sync + 'static>(
self,
) -> Effect<Exit<A, E>, E2, R> {
let FiberHandle { ctx, cell, .. } = self;
from_async(move || async move {
ctx.request_interrupt();
Ok(
JoinFuture::<A, E> {
cell,
_p: PhantomData,
}
.await,
)
})
}
}
#[must_use = "futures do nothing unless polled"]
pub struct JoinFuture<A, E> {
cell: FiberCell,
_p: PhantomData<fn() -> (A, E)>,
}
impl<A, E> std::fmt::Debug for JoinFuture<A, E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("JoinFuture")
}
}
impl<A: 'static, E: 'static> Future for JoinFuture<A, E> {
type Output = Exit<A, E>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Exit<A, E>> {
self
.cell
.poll_take(cx)
.map(|exit| to_exit(unerase_exit::<A, E>(exit)))
}
}
pub(crate) struct CellFuture(pub(crate) FiberCell);
impl Future for CellFuture {
type Output = AnyExit;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<AnyExit> {
self.0.poll_take(cx)
}
}
#[cfg(test)]
mod tests {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::task::Wake;
use super::*;
struct CountWake(AtomicUsize);
impl Wake for CountWake {
fn wake(self: Arc<Self>) {
self.0.fetch_add(1, Ordering::SeqCst);
}
}
#[test]
fn exit_cell_completes_once_and_is_taken_once() {
let counter = Arc::new(CountWake(AtomicUsize::new(0)));
let waker = Waker::from(Arc::clone(&counter));
let mut cx = Context::from_waker(&waker);
let cell = ExitCell::<u8>::new();
assert!(cell.poll_take(&mut cx).is_pending());
assert!(!cell.is_complete());
assert_eq!(cell.complete(1), Ok(()));
assert_eq!(counter.0.load(Ordering::SeqCst), 1, "joiner woken once");
assert_eq!(cell.complete(2), Err(2), "second completion refused");
assert_eq!(cell.poll_take(&mut cx), Poll::Ready(1));
assert!(cell.is_complete());
let again = catch_unwind(AssertUnwindSafe(|| cell.poll_take(&mut cx)));
assert!(again.is_err(), "second take is a kernel bug");
}
}