runtime/wasm: add timers and host event wakeups (based on #2198) - #2204
Draft
cpunion wants to merge 37 commits into
Draft
runtime/wasm: add timers and host event wakeups (based on #2198)#2204cpunion wants to merge 37 commits into
cpunion wants to merge 37 commits into
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
…/wasm-wasi-single-worker
…orker-asyncify-scheduler # Conflicts: # internal/build/source_patch_test.go
…/wasm-wasi-single-worker
…orker-asyncify-scheduler # Conflicts: # runtime/internal/runtime/z_default.go
…/wasm-wasi-single-worker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Depends on #2198.
Problem
The single-worker wasm runtime can park goroutines, but it has no host event source for time.
time.Sleep, timers, tickers, timeout selects, andAfterFunctherefore cannot suspend one G while allowing the worker to run another G and later resume the timer owner.Implementation
runtime/internal/wasmeventmin-heap with stable timer ordering, Stop/Reset semantics, periodic deadline catch-up, and a lazy dispatch layer. Packages that never use time retain only a small dormant dispatch path.emscripten_sleepfor J32/J64 host wakeups andpoll(NULL, 0, timeout)for default WASI Preview 1. Keep the existing explicitLLGO_WASI_THREADS=1timer backend unchanged.time.Now.//go:wasmimportlowering so WASI libc/runtime declarations retain their official host module/name.EM_INVOKERcontracts.The compiler, scheduler, timer queue, and host adapters remain separate. No new target or build tag is introduced, and native/embedded source selection is unchanged. Static wasm defer dispatch is supplied by the inherited #2208 dependency and is not part of this PR-specific diff.
Semantics covered
The shared J32, J64, and P1 acceptance fixture covers:
time.SleepTimerbehaviorTickerAfterFuncwaking a channel waiterselectValidation
Local macOS validation uses
GOMAXPROCS=2,GOMEMLIMIT=3GiB, and-p=1where applicable:cl/ssaimport, locality, and defer tests: passwasm timers okwasm-tools validate --features all: passEarlier broader validation also covered
-O0,-O3, ThinLTO, explicit P1 pthread compatibility, the full nested runtime module, and bounded Ubuntu tests.Coverage highlights:
runtime/internal/wasmevent: 93.9%wasmImportByDoc: 100%SetWasmImport: 100%Cost
On Apple M4 Max with
GOMAXPROCS=2, queue Reset+RunDue is 13.51-14.87 ns/op, 0 B/op, 0 allocs/op.Against #2198, a no-time J32 scheduler fixture grows from 416,551 to 418,082 bytes with
-pclntab=none: +1,531 bytes (+0.37%). With the default pclntab it grows from 568,087 to 571,522 bytes: +3,435 bytes (+0.60%, including metadata). Native output size remains unchanged apart from worktree-path metadata, and the representative bare-metal ELF is byte-identical.The PR-specific diff above #2198 is 24 files, 1,048 insertions, and 45 deletions.