Commit 976fad66

976fad6689ceMerged

Ori Pekelman <[email protected]> · 5 months ago · 7 files · +2 added · ~5 modified

Description

Ractor Milestone 2: deep-copy message codec (String/Symbol/Array)

Replace the scalar-only boundary with a real deep-copy codec. Values cross through a heap-neutral malloc'd blob: the sender serializes from its private heap, the blob travels the mutex/condvar queue, the receiver rebuilds into its own heap, no shared pointers. The codec (sp_ractor_serialize / _deserialize) lives in sp_runtime.h -- it needs the heap allocators that are static to the generated TU -- and is installed into the library hooks by sp_re_init, the same pattern as the GC mark-globals hook.

Shareable now: Integer/Float/true/false/nil (by value), Symbol (by name, re-interned in the receiver's per-Ractor table), String (bytes), and Arrays -- poly + typed int/float/string, recursively. Hashes, plain objects, Procs, Fibers and IO raise Ractor::Error at the boundary (a clear message, not a dangling cross-heap pointer); extending to those is the next milestone.

The Ractor queues now carry the malloc'd blob instead of an sp_RbVal, so the codec is the single choke point and immediates no longer special-case.

Adds test/ractor_messages.rb (string/symbol/array round-trips via << and a variable send). Full suite 894 pass / 0 fail / 0 error; the codec test is clean under ThreadSanitizer + AddressSanitizer and SPINEL_GC_VERIFY.

Documents the Ractor#send-with-literal sharp edge: recv.send("m") is parsed as a reflective send (whole-program parser behaviour), so messages go via r << v or a local; r.send(42) / r.send([..]) are unaffected.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
976fad6689ce
Parent
5cca24211635

docs/RACTOR-RFC.mdmodifiedView file

@@ -1,6 +1,7 @@
11 # RFC: A Minimal Ractor for Spinel
22
3-Status: **experimental / Milestone 1**. This document is both the design
3+Status: **experimental / Milestone 2** (working; ported to the C compiler in
4+`src/`). This document is both the design
45 rationale and the record of what currently ships. It proposes whether to
56 graduate to a full implementation (see *Path forward*).
67
@@ -92,40 +93,50 @@
9293
9394 ## Implementation map
9495
95-- **`lib/sp_runtime.h`** — `__thread` on the bucket-(a) globals (both fiber
96- `#ifdef` branches); the lazy `__thread` root pointer; `sp_thread_init()` + a
97- main-thread constructor; `__thread` on the `SP_POOL_DEFINE` macro body; and
98- the new `sp_Ractor` / `sp_RactorCtrl` (reference-counted, malloc'd control
99- block with mutex+condvar mailbox/outgoing queues), the scalar `sp_deep_copy`
100- codec (`sp_ractor_serialize`/`_deserialize`/`_shareable`), the
101- `sp_ractor_new/_send/_receive/_yield/_take` helpers, and the per-thread
102- top-level `setjmp` landing pad that re-raises an uncaught Ractor exception in
103- the taker.
104-- **`spinel_analyze.rb`** — a `"ractor"` base type (added to
105- `type_is_pointer` / `is_nullable_pointer_type`); `Ractor.new → "ractor"` in
106- `infer_constructor_type`; and early inference arms for
107- `receive`/`recv`/`take`/`send`/`<<`/`yield` (placed before the generic
108- receiver-method dispatch so `take`/`send` aren't shadowed by `Array#take` /
109- the reflective `Object#send`).
110-- **`spinel_codegen.rb`** — `c_type "ractor" → "sp_Ractor *"` and the matching
111- pointer-type lists; `compile_ractor_new` + `_ractor_body_N` (a clone of the
112- Fiber body-emission shape); the dispatch arms (with the `send` arm placed
113- before the reflective `Object#send`, gated on the ractor receiver type); and
114- mirrored cache-miss inference arms.
115-- **`Makefile`** — `-pthread` in `CFLAGS`.
96+(The active compiler is the C reimplementation under `src/`; the runtime is
97+split across `lib/sp_gc.c`, `lib/sp_fiber.c`, and the header `lib/sp_runtime.h`.)
11698
99+- **`lib/sp_gc.{c,h}`, `lib/sp_fiber.{c,h}`, `lib/sp_runtime.h`** — `__thread` on
100+ every per-execution-unit mutable global (collector heap/roots/mark-stack/
101+ byte counters; fiber root/current/list; string heap + literal cache;
102+ `SP_POOL_DEFINE` pools; exception + catch stacks; regex match state; at_exit;
103+ lambda arena), plus `sp_fiber_thread_init()` (a main-thread constructor wires
104+ the main thread), `sp_gc_thread_teardown()` (frees a Ractor's heaps on exit),
105+ and the Milestone-2 message codec `sp_ractor_serialize`/`_deserialize`
106+ (installed into the runtime hooks by `sp_re_init`).
107+- **`lib/sp_ractor.{c,h}`** — `sp_Ractor` + mutex/condvar FIFO mailbox/outgoing
108+ queues, the pthread entry trampoline, and the serialize/deserialize hook
109+ plumbing. The generated body installs a top-level `setjmp` rescue pad so an
110+ uncaught Ractor exception terminates one Ractor rather than the process.
111+- **`src/analyze*.c`** — `TY_RACTOR`; `"Ractor"` as a builtin constant; inference
112+ arms (`Ractor.new → ractor`, `receive`/`take → poly`, `yield → nil`,
113+ `send`/`<< → ractor`).
114+- **`src/codegen*.c`** — `c_type TY_RACTOR → "sp_Ractor *"` and the scalar-return
115+ list; `emit_ractor_new` (Fiber-body-shaped, rejects captures/self with a
116+ compile-time `Ractor::IsolationError`); the class/instance dispatch arms.
117+- **`common.mk` / `src/main.c` / `Makefile`** — `-pthread` in `LDFLAGS`, in the
118+ in-process `cc` link driver, and on the new `sp_ractor.o` build rule.
119+
117120 ## Deliberate divergences (where "minimal" buys simplicity)
118121
119-1. **Value codec carries scalars only** — Integer / Float / true / false / nil.
120- Strings, Symbols, and containers (Array/Hash/objects) raise `Ractor::Error`
121- at the boundary. Extending the codec is the main follow-up.
122-2. **Single-slot mailbox with backpressure**, not CRuby's unbounded mailbox.
123- `send`/`yield` block while a value is still pending.
124-3. **`@@cvars` / `$globals` stay `static` (shared)**, not yet `__thread`. CRuby
125- isolates them per-Ractor and raises on cross-Ractor mutation; we have not
126- thread-local-ized them because class-body cvar initializers would need a
127- trampoline replay. Programs that touch cvars/gvars across Ractors are out of
128- scope for this milestone.
122+1. **Value codec covers immediates, Symbol, String, and Arrays** (poly + typed
123+ int/float/string), deep-copied through a heap-neutral malloc'd blob
124+ (`sp_ractor_serialize`/`_deserialize`, installed via a hook because the
125+ rebuild allocators are static to the generated TU). Hashes, plain objects,
126+ Procs, Fibers, and IO raise `Ractor::Error` at the boundary — extending the
127+ codec to those (and to nested objects) is the main follow-up. Symbols travel
128+ by name and are re-interned in the receiver's per-Ractor table.
129+2. **FIFO mailbox / outgoing queue** (mutex + condvar, grows on demand), so a
130+ Ractor may `receive` several messages and a sender need not block per slot.
131+3. **`@@cvars` / `$globals` are `__thread`** → isolated per Ractor, rather than
132+ CRuby's "shared, raise on cross-Ractor mutation". Simpler and safe, but a
133+ real semantic difference: a class-body cvar initializer has not run in a
134+ child Ractor unless re-executed there. Programs that rely on cross-Ractor
135+ cvar/gvar sharing are out of scope for this milestone.
136+4. **`Ractor#send` with a bare String/Symbol *literal*** is parsed as a
137+ reflective send (`recv.send("m")` → `recv.m`, a whole-program parser
138+ behaviour), so send messages via `r << v` or a local (`r.send(v)`); literals
139+ like `r.send(42)` / `r.send([..])` are unaffected.
129140 4. **No spawn args / block params** and **no shareable-by-value capture** yet:
130141 any captured outer variable or `self` is a compile-time
131142 `Ractor::IsolationError`. The block's return value is not delivered to