158 lines
4.2 KiB
MoonBit
158 lines
4.2 KiB
MoonBit
// Coroutines on top of MoonBit's async intrinsics, scheduled by a
|
|
// `Dispatcher` instead of `moonbitlang/core/async`'s event loop.
|
|
//
|
|
// Why not `moonbitlang/core/async`: it owns the event loop (`async fn main`,
|
|
// `with_task_group`) and does not support the wasm-gc backend. GPUI's
|
|
// executors are driven by the platform (the browser's event loop, a native run
|
|
// loop, or a deterministic test scheduler), so we schedule coroutines
|
|
// ourselves with the same intrinsics `moonbitlang/core/async` is built on.
|
|
// They are experimental; see docs/PORTING.md.
|
|
|
|
///|
|
|
async fn[T] async_suspend(cb : ((T) -> Unit) -> Unit) -> T noraise + nocancel = "%async.suspend"
|
|
|
|
///|
|
|
fn run_async(f : async () -> Unit noraise + nocancel) = "%async.run"
|
|
|
|
///|
|
|
async fn[X] raise_cancellation_signal() -> X noraise = "%async.raise_cancellation_signal"
|
|
|
|
///|
|
|
async fn[X] capture_cancellation(
|
|
f : async () -> X,
|
|
handler~ : async () -> X nocancel,
|
|
) -> X nocancel = "%async.capture_cancellation"
|
|
|
|
///|
|
|
priv enum Wake {
|
|
Continue
|
|
Cancelled
|
|
}
|
|
|
|
///|
|
|
/// One running computation. While suspended, `continuation` resumes it.
|
|
priv struct Coroutine {
|
|
scheduler : Scheduler
|
|
mut continuation : ((Wake) -> Unit)?
|
|
mut cancelled : Bool
|
|
mut scheduled : Bool
|
|
mut finished : Bool
|
|
/// Woken while running: the next `suspend` returns immediately.
|
|
mut missed_wake : Bool
|
|
}
|
|
|
|
///|
|
|
/// Runs coroutines one step at a time through a dispatcher.
|
|
priv struct Scheduler {
|
|
dispatcher : &Dispatcher
|
|
}
|
|
|
|
///|
|
|
/// The coroutine running right now, across all schedulers (there is one
|
|
/// thread).
|
|
let current : Ref[Coroutine?] = { val: None, }
|
|
|
|
///|
|
|
/// Schedules `coroutine` to continue on the dispatcher, once.
|
|
fn Coroutine::wake(self : Coroutine) -> Unit {
|
|
if self.scheduled || self.finished {
|
|
return
|
|
}
|
|
self.scheduled = true
|
|
self.scheduler.dispatcher.dispatch(fn() { self.step() })
|
|
}
|
|
|
|
///|
|
|
fn Coroutine::step(self : Coroutine) -> Unit {
|
|
self.scheduled = false
|
|
guard self.continuation is Some(continuation) else {
|
|
// Woken while running (it has not suspended yet).
|
|
if !self.finished {
|
|
self.missed_wake = true
|
|
}
|
|
return
|
|
}
|
|
self.continuation = None
|
|
let previous = current.val
|
|
current.val = Some(self)
|
|
continuation(if self.cancelled { Cancelled } else { Continue })
|
|
current.val = previous
|
|
}
|
|
|
|
///|
|
|
/// The coroutine running right now; fails loudly outside of a task.
|
|
fn current_coroutine(operation : String) -> Coroutine {
|
|
match current.val {
|
|
Some(coroutine) => coroutine
|
|
None => abort("\{operation} must be called from inside a task")
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Suspends the current coroutine until it is woken. If the task is cancelled
|
|
/// (before or while suspended), raises the cancellation signal, which unwinds
|
|
/// the task and runs its `defer` blocks.
|
|
async fn suspend(coroutine : Coroutine) -> Unit noraise {
|
|
if coroutine.cancelled {
|
|
raise_cancellation_signal()
|
|
}
|
|
if coroutine.missed_wake {
|
|
coroutine.missed_wake = false
|
|
return
|
|
}
|
|
let wake : Wake = async_suspend(fn(k) { coroutine.continuation = Some(k) })
|
|
match wake {
|
|
Continue => ()
|
|
Cancelled => raise_cancellation_signal()
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Starts `body` as a new coroutine on the next dispatcher turn.
|
|
fn Scheduler::spawn(
|
|
self : Scheduler,
|
|
body : async () -> Unit,
|
|
on_cancelled : () -> Unit,
|
|
on_error : (Error) -> Unit,
|
|
on_finished : () -> Unit,
|
|
) -> Coroutine {
|
|
let coroutine : Coroutine = {
|
|
scheduler: self,
|
|
continuation: None,
|
|
cancelled: false,
|
|
scheduled: false,
|
|
finished: false,
|
|
missed_wake: false,
|
|
}
|
|
coroutine.continuation = Some(fn(wake) {
|
|
match wake {
|
|
Cancelled => {
|
|
// Cancelled before it ever ran: the body never starts.
|
|
coroutine.finished = true
|
|
on_cancelled()
|
|
on_finished()
|
|
}
|
|
Continue =>
|
|
run_async(() => {
|
|
capture_cancellation(body, handler=() => on_cancelled()) catch {
|
|
error => on_error(error)
|
|
}
|
|
coroutine.finished = true
|
|
on_finished()
|
|
})
|
|
}
|
|
})
|
|
coroutine.wake()
|
|
coroutine
|
|
}
|
|
|
|
///|
|
|
fn Coroutine::cancel(self : Coroutine) -> Unit {
|
|
if self.finished || self.cancelled {
|
|
return
|
|
}
|
|
self.cancelled = true
|
|
// Resume it so that it observes the cancellation (and runs its `defer`s).
|
|
self.wake()
|
|
}
|