7. The Rust core as built (phase 2b)
Crates:
crates/vortex-core:no_std, no allocator, nothread_local!, no I/O. Waiting goes through theFutextrait and time arrives through theClocktrait or asnow_usarguments (SBIO). Every capacity comes from an explicitConfig(PCND).Layoutis the single source of every offset (SSOT).crates/vortex-wasm: thecdylibforwasm32-unknown-unknownwith shared memory. It exposes a flatextern "C"API in which every export takes the region base, and it imports one function,vortex.vortex_now_us. It has no wasm-bindgen. Build withscripts/build-wasm.sh <max-memory-bytes>(pinned nightly,build-std=core).crates/vortex-core-tests: loom models, property tests and native stress tests.crates/vortex-wasm/tests-node/smoke.mjs: the built module in realworker_threads.
7.1 Protocols
| Protocol | Mechanism | Death of a worker at any instruction |
|---|---|---|
| Claim | CAS Queued(gen) → Running(gen, worker) on the slot word |
Task is either still queued or visibly owned; recover(worker) reports it |
| Queue progress | head advanced by CAS, by whoever looks next |
A claim that never advanced head is helped past |
| Completion | Slot word Running → Done, then bit OR in word, then summary |
Done without a bit is found by recover |
| Worker park | Set mask bit, fence(SeqCst), re-check work, wait on own futex word |
recover clears the dead worker's bit and hands queued work to another parked worker |
| Main wait | Arm flag, fence(SeqCst), re-check pending; JS Atomics.waitAsync(seq); worker: mark, fence(SeqCst), disarm and notify |
No lock is held, so there is nothing to recover |
Why fences, not just SeqCst accesses. Both handshakes are Dekker patterns: each side
stores and then loads a location the other side stores to. loom 0.7 treats SeqCst
loads and stores as AcqRel (its README lists this under unsupported features) and
models fence(SeqCst) precisely. With SeqCst accesses alone, loom reported deadlocks
in the park and main-wait models. Those reports were false: the algorithm is correct
under C11, and wasm atomics are sequentially consistent. With explicit fences the
models pass, and removing either fence makes them fail again (controls C1, C2).
7.2 Histograms
The histograms are log-linear. Values under 128 µs are exact. Above that, each power of
two is split into 64 sub-buckets (relative error at most 1/64), giving 1 728 buckets
of u64. Recording is four relaxed atomic operations, done by the worker. Mean,
variance and percentiles all derive from the buckets, using HdrHistogram's conventions
(percentiles return the highest equivalent value; mean and variance use the median
equivalent). min and max are exact. JS takes the square root of the variance.
7.3 Verification record
| Test | Kind | What it shows |
|---|---|---|
loom_models::concurrent_consumers_claim_each_task_at_most_once_and_lose_none |
loom, 2 consumers + producer | exactly-once claim |
loom_models::a_parked_worker_is_never_left_asleep_with_work_queued |
loom, blocking futex | no lost worker wake-up |
loom_models::the_main_thread_is_never_left_waiting_with_a_completion_pending |
loom, blocking futex | no lost main wake-up |
loom_models::a_consumer_that_dies_after_its_claim_does_not_block_the_queue |
loom | death between claim and head advance |
lifecycle (10 tests) |
unit | publish→release, FIFO, slot exhaustion, cancel and reuse, cancel-running names the worker, abortable exclusivity, recovery of lost and undelivered tasks, 100 000 ring wraps, helped head, config validation |
histogram (8 tests, 5 property-based) |
proptest | buckets tile u32 with no gaps, value-in-bucket, width ≤ 1/64, exact min/max/count, monotone percentiles, mean within bucket error |
stress (2 tests) |
3–4 native threads, 200 000 tasks each | every task delivered exactly once, with parking only and with spinning |
tests-node/smoke.mjs |
wasm in 2 and 4 worker_threads, 100 000 tasks |
shared memory, per-worker stacks, memory.atomic.wait32 in workers, wasm notify waking JS Atomics.waitAsync, results verified, no slot owned after terminate |
Negative controls. Each one breaks the code, the named test goes red for the stated
reason, the file is restored byte-identical (sha256 compared), touched, and the
tests are green again.
| # | Break | Red in | Reason observed |
|---|---|---|---|
| C1 | drop worker-side fence in park |
loom park model | deadlock |
| C2 | drop fence in MainWaiter::arm |
loom main-wait model | deadlock |
| C3 | claim by plain store instead of CAS | loom consumers model | duplicate claim (assertion) |
| C4 | consumer returns on a non-queued head instead of helping | lifecycle helped-head test |
second task never taken |
| C5 | drain reads word bits without clearing them | stress |
core guard "completion bit for a slot that is not done" (duplicate delivery caught) |
| C6 | bucket shift off by one | histogram |
value outside its bucket |
| C7 | cancel does not release the slot | lifecycle cancel test |
slot not reused |
| C8 | recovery ignores Done slots |
lifecycle recovery test |
undelivered task not reported |
| C9 | allocation does not advance the generation | lifecycle cancel test |
generations equal |
| S1 | worker never notifies the main waiter | smoke.mjs (60 s alarm) |
main thread hangs, alarm exit 142 |
7.4 Known gaps for phase 2c
Phase 2c resolved the first and last items below with push placement (chapter 8.1): the main thread never offers an abortable task to a busy worker, and run time is recorded on the main thread at piscina's points. The two middle items remain for phase 3 measurement.
- A busy worker that finds an abortable task at the head returns
Ineligible, even if a non-abortable task waits behind it. piscina'sskipQueuewould hand that later task to the busy worker. The shim must reproduce this, for example with a separate queue for abortable tasks. idle_waitreads the clock in every spin iteration, through a JS import. That is cheap compared with a futex sleep, but phase 3 should measure it.core_at(base)rebuilds the layout from the metadata on every FFI call. Cache it only if profiling shows the cost.runTimeis measured from claim to completion, on the worker's clock. piscina measures from dispatch to the main-thread callback. This divergence will be listed indocs/COMPAT.md.