Files
wasm2go/testdata/cg_threads_wake_dir.wat
9e1233c9df threads: atomics, wasi-threads, and a copy-on-write shared-memory image (#30)
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]>
2026-07-16 10:37:21 +09:00

38 lines
1.8 KiB
WebAssembly Text Format

;; main->agent futex wake direction. The agent parks in memory.atomic.wait32;
;; the MAIN thread (the primary Module, not a clone) stores + notifies. The
;; reverse direction (agent notifies, main waits) is what cg_threads.wat
;; already covers — this one is what a C mutex handoff does when the main
;; thread unlocks while an agent sleeps on the futex.
(module
(import "wasi" "thread-spawn" (func $spawn (param i32) (result i32)))
(memory 1 4 shared)
;; 16: futex word / 24: agent result flag
(func (export "wasi_thread_start") (param $tid i32) (param $arg i32)
;; park until the futex word becomes nonzero (wait while == 0)
(block $awake
(loop $again
(br_if $awake (i32.ne (i32.atomic.load (i32.const 16)) (i32.const 0)))
;; wait(addr=16, expected=0, timeout=-1) -> 0 ok / 1 not-equal
(drop (memory.atomic.wait32 (i32.const 16) (i32.const 0) (i64.const -1)))
(br $again)))
(i32.atomic.store (i32.const 24) (i32.const 7777)))
(func (export "run") (result i32)
(local $i i32)
(drop (call $spawn (i32.const 0)))
;; give the agent a moment to reach the wait (spin ~1M iterations)
(block $b (loop $l
(br_if $b (i32.ge_u (local.get $i) (i32.const 2000000)))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br $l)))
;; publish and wake
(i32.atomic.store (i32.const 16) (i32.const 1))
(drop (memory.atomic.notify (i32.const 16) (i32.const 1)))
;; wait for the agent's answer (bounded spin so a hang FAILS, not wedges)
(local.set $i (i32.const 0))
(block $got (loop $wait
(br_if $got (i32.ne (i32.atomic.load (i32.const 24)) (i32.const 0)))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br_if $got (i32.ge_u (local.get $i) (i32.const 500000000)))
(br $wait)))
(i32.atomic.load (i32.const 24))))