mirror of
https://github.com/hanzoai/wasm2go.git
synced 2026-10-30 15:06:48 +00:00
Adds the wasm threads proposal to the codegen and, on top of it, a
copy-on-write memory-sharing facility for embedders that stand up many
instances of the same module.
Threads
- Lower the 0xFE atomic family (load/store/rmw/cmpxchg across widths and
sub-word lanes, plus wait/notify) to sync/atomic-based module helpers.
- wasi_thread_spawn runs each guest thread on a goroutine. A shared memory
is allocated at its declared ceiling once and never relocates, so every
agent's cached base pointer stays valid; growth is a lone atomic store.
- Passive data segments, per-agent globals, coherent plain access for
shared memories, and multi-package output for threaded modules.
- A shared memory's ceiling is a runtime value the embedder chooses, not
the binary's declared maximum.
Copy-on-write shared memory
- NewWithMemory lets the caller supply the linear memory, and memory.init
compares before it writes — so a copy-on-write map of a pre-initialized
image survives the start section instead of being faulted private.
- NewSharedImage builds an initialized image once per process and maps it
copy-on-write into every instance: the data segments (identical in every
instance, read-mostly) then cost almost nothing per instance.
- NewSharedSnapshot / NewFromSnapshot share a WHOLE initialized instance —
memory and wasm globals both — so instances re-run nothing at all.
SaveGlobals/RestoreGlobals carry the initialized state that lives outside
linear memory (the stack pointer and other mutable globals).
Also: coverage for the threads/atomics helpers, a WASM2GO_SSA_PASSES_OFF
diagnostic knob, an AtomicWait timeout margin, and thread-trap visibility.
Co-authored-by: Claude Opus 4.8 <[email protected]>
29 lines
1.3 KiB
WebAssembly Text Format
29 lines
1.3 KiB
WebAssembly Text Format
;; Cross-thread visibility of PLAIN stores published via atomics: the main
|
|
;; thread writes a value with a normal store, then publishes with an atomic
|
|
;; store (release); the spawned thread spins on an atomic load (acquire) and
|
|
;; must then observe the plain store. This is the C/C++ mutex fast-path shape
|
|
;; (musl pthread_mutex uses a_cas/a_store around plain critical-section data).
|
|
(module
|
|
(import "wasi" "thread-spawn" (func $spawn (param i32) (result i32)))
|
|
(memory 1 4 shared)
|
|
;; addr 16: plain payload / addr 20: atomic flag / addr 24: agent's answer flag
|
|
(func (export "wasi_thread_start") (param $tid i32) (param $arg i32)
|
|
;; spin until the flag publishes
|
|
(block $done
|
|
(loop $spin
|
|
(br_if $done (i32.eq (i32.atomic.load (i32.const 20)) (i32.const 1)))
|
|
(br $spin)))
|
|
;; read the PLAIN store and publish the answer atomically
|
|
(i32.atomic.store (i32.const 24) (i32.load (i32.const 16))))
|
|
(func (export "run") (result i32)
|
|
(drop (call $spawn (i32.const 0)))
|
|
;; plain store, then release-publish
|
|
(i32.store (i32.const 16) (i32.const 4242))
|
|
(i32.atomic.store (i32.const 20) (i32.const 1))
|
|
;; wait for the agent's answer
|
|
(block $got
|
|
(loop $wait
|
|
(br_if $got (i32.ne (i32.atomic.load (i32.const 24)) (i32.const 0)))
|
|
(br $wait)))
|
|
(i32.atomic.load (i32.const 24))))
|