Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/integration.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,27 @@ pub fn run_async_main(main : async () -> Unit) -> Unit {
let _ = @coroutine.spawn(main)
@event_loop.reschedule()
}

///|
/// Scheduler-only entry for wasm / wasm-gc. The async library does not
/// provide IO on these targets (`sleep`, `Timer`, sockets, fs, etc. all
/// `abort` via the existing `unimplemented.mbt` stubs), but pure
/// coroutine code (`pause`, `with_task_group`, `aqueue`, `semaphore`,
/// `cond_var`) does not depend on the event loop and works fine — this
/// shim just drains the ready queue.
#coverage.skip
#cfg(target="wasm")
#doc(hidden)
pub fn run_async_main(main : async () -> Unit) -> Unit {
let _ = @coroutine.spawn(main)
@event_loop.reschedule()
}

///|
#coverage.skip
#cfg(target="wasm-gc")
#doc(hidden)
pub fn run_async_main(main : async () -> Unit) -> Unit {
let _ = @coroutine.spawn(main)
@event_loop.reschedule()
}
15 changes: 15 additions & 0 deletions src/internal/event_loop/unimplemented.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ pub fn Timer::cancel(timer : Timer) -> Unit {
ignore(timer)
abort("`Timer::cancel()` is unimplemented on WASM backend")
}

///|
/// WASM stub for the scheduler-only `reschedule` entry point. Drains
/// immediately-ready coroutines (including any wake-ups produced by
/// running coroutines themselves) and then returns. Gating on
/// `has_immediately_ready_task` rather than `no_more_work` ensures
/// blocked coroutines (`blocking > 0`, e.g. suspended on an external
/// host callback) do NOT cause this function to tight-spin: when there
/// is nothing ready to run we exit and yield back to the host, which
/// can re-enter via `run_async_main` once new work is available.
pub fn reschedule() -> Unit {
while @coroutine.has_immediately_ready_task() {
@coroutine.reschedule()
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline this loop into call sites

Loading