10. Core specification: one layout, two cores
The shim talks to a pool's shared region only through CoreApi (js/src/core/api.ts).
Two cores implement it over the layout and protocol below:
| Core | Option | Code |
|---|---|---|
| JS (default) | core: 'js' |
js/src/core/js/ |
| Rust, compiled to wasm | core: 'rust' |
crates/vortex-core, crates/vortex-wasm |
This chapter is the single source of truth. js/test/core-conformance.test.mjs holds
both cores to it:
- every config is sized and rejected identically;
- every address the shim uses is at the same offset from the base;
- 40 random protocol-following sequences of 400 operations each give identical results at every step and byte-identical regions (Meta through the out buffer) after each one.
Negative controls C26–C29 (8.8 table) show the test catches a wrong completion bit, a wrong inbox size, a staged claim while busy, and a wrong histogram bucket.
1. Configuration and limits
Every field is required; neither core has a default.
| Field | Range |
|---|---|
slots |
1..=0xFFF0 (tickets stay below the sentinels) |
workers |
1..=4095 (12-bit worker field) |
workerHistograms |
boolean |
spinCapUs |
u32; 0 disables the spin |
slotBytes |
> 0, multiple of 8 |
stackBytes |
> 0, multiple of 16 (wasm shadow stacks; reserved by the JS core too, so the layouts match) |
Derived sizes:
ringCapacity = nextPow2(2 * slots);inboxCapacity = nextPow2(slots);doneWords = ceil(slots / 32).
2. Region and Meta
A region starts at base (the JS core uses base 0 in its own memory; the Rust core grows
its memory by whole pages and uses the first new page). Meta is 13 u32 at the base, in
this order:
magic = 0x56545831, slots, workers, workerHistograms, spinCapUs, slotBytes,
stackBytes, w32Off, w64Off, outOff, arenaOff, stacksOff, total.
Byte offsets from the base:
w32Off = align(52, 8);w64Off = align(w32Off + 4 * w32Len, 8);outOff = w64Off + 8 * w64Len(2 * slotsu32 of main-thread scratch);arenaOff = align(outOff + 8 * slots, 16)(slots * slotBytes);stacksOff = align(arenaOff + slots * slotBytes, 16);total = stacksOff + workers * stackBytes, which must fit in u32.
A worker attaches by reading Meta at the base and recomputing the layout.
3. Word arrays
w32 (u32), in order, each starting where the previous ends:
| Span | Words |
|---|---|
| header: HEAD, TAIL, DONE_SEQ, MAIN_WAITING, FREE_TOP | 5 |
| states | slots |
| flags | slots |
| ring (pull mode; unused by the shim, kept for layout parity) | ringCapacity |
| done words | doneWords |
| done summary | ceil(doneWords / 32) |
| park mask | ceil(workers / 32) |
| park words (futex words) | workers |
| spin budget (µs) | workers |
| free items | slots |
| inbox heads, inbox tails | workers each |
| inbox cells | workers * inboxCapacity |
w64 (u64):
- created µs:
slotswords; - then the histograms:
(2 + (workerHistograms ? workers : 0)) * 1731words.
4. Slot word and ticket
- Slot word:
[generation:16][worker:12][state:4]. - States: Free 0, Queued 1, Running 2, Done 3, Cancelled 4, Staged 5.
- Ticket:
[generation:16][slot:16]. - Allocation advances the generation (wrapping). Release writes
(generation, 0, Free).
Every claim is one compare-and-swap that also records the owner:
- the worker claims Staged → Running;
- the main thread withdraws Staged → Cancelled;
- the worker completes Running → Done.
A failed completion means the task was written off and its result is dropped.
5. Free list (main thread only)
A stack:
initstores itemsslots-1 .. 0, then setsFREE_TOP = slots;- allocate pops it (the slot must be Free, else fatal);
- release pushes it (pushing onto a full stack is fatal: a double release).
6. Inboxes
One single-producer, single-consumer ring per worker:
- the producer writes the cell, then the tail;
- the consumer peeks the head cell and advances the head;
- a push onto a full ring is fatal.
recover sets head = tail.
next(worker, busy) walks the inbox:
- a current Staged entry for this worker is claimed only when
busy == 0(otherwise INELIGIBLE); - a current Running entry for this worker is returned;
- anything else (stale generation, withdrawn) is popped and skipped;
- an empty inbox gives EMPTY.
7. Completion set
A worker that completes a task:
- sets the slot's bit in its done word;
- sets the word's bit in the summary;
- notifies the main waiter (section 8), which is the only place DONE_SEQ advances.
The drain swaps each summary word to 0, then each announced word to 0. It reports the tickets in ascending slot order and writes them to the out buffer.
release_drained(n) releases the first n tickets in the out buffer (n at most
slots), so the shim pays one call per drain rather than one release per task: it
reads every drained result, releases them all, then delivers.
8. Parking and the main waiter
Parking (worker):
- read the park word;
- set the mask bit;
- re-check for work;
- wait on the word;
- clear the bit.
Futex words (DONE_SEQ and the park words) advance by WAKE_STEP = 2, so they are always
even and the i32 result -1 never names a value.
Waking (producer): if the worker's bit is set and this call clears it, advance the
word and notify one waiter. prepare/withdraw are the non-blocking form (the value
for Atomics.waitAsync, or -1 if work is present).
Main waiter:
armreads DONE_SEQ, sets MAIN_WAITING, re-checks the summary, and returns the value to wait on, or -1 if completions are pending.- A completing worker that swaps MAIN_WAITING from 1 to 0 then advances DONE_SEQ and
notifies it. It reports 2 from
finish_assigned, against 1 for a plain delivery and 0 for a write-off. - DONE_SEQ advances after the swap, never with the completion: otherwise a completion
the main thread drained before arming could advance DONE_SEQ before
armreads it and swap MAIN_WAITING after, spending the wake on nothing and leaving the main thread waiting with the next completion stranded (loom_main_wait.rs).
The idle spin (sync atomics) reads no clock:
- it spins
budget * spinsPerUsiterations of the work check, wherespinsPerUsis calibrated once per worker by timingspin_probe; work found doubles the budget (up to the cap); - otherwise it parks, with the first wait bounded by the cap: woken within the cap, the budget becomes the cap (a spin would have caught it); timed out, it halves and the wait continues without a bound.
The JS core uses only atomics as strong as or stronger than the Rust core's: JS
Atomics are sequentially consistent.
9. Histograms
Each histogram is count, min (u64 max when empty), max, then 1728 buckets:
- values below 128 µs are exact;
- each higher power of two is split into 64 sub-buckets.
Recording truncates to an integer µs, with a minimum of 1. Ids: 0 run time, 1 wait time, 2 + w worker w.
Readings:
- percentiles report a bucket's highest value;
- mean and variance use its median value;
- min and max are exact.
10. Call signatures
Every hot-path operation takes and returns only i32 values, so V8 can use its fast
JS-to-wasm wrappers: a wait value comes back as an i32 (-1 means "do not wait"), and a
µs count crosses as two u32 halves (lo, hi; exact below 2^53). The histogram
readers (mean, variance, percentile, min, max) return f64: they read u64 values, run
only when a summary is requested, and an i64 would cross as a BigInt.
11. What differs between the cores
Only the cost of an operation differs. The Rust core:
- pays a JS-to-wasm crossing per call (it imports nothing but its memory);
- traps on a broken invariant.
The JS core throws an Error instead. Chapter 9 measures the cost per core.