Commit 5cca2421
5cca24211635Merged
Ori Pekelman <[email protected]> · 5 months ago · 21 files · +5 added · ~16 modified
Description
RFC: minimal pthread-backed Ractor (Milestone 1)
Add a working Ractor.new / Ractor.receive / Ractor.yield / r.send (r << v) / r.take running each Ractor on its own pthread, ported to the C compiler (src/) and the split runtime (lib/sp_gc.c, lib/sp_fiber.c, lib/sp_runtime.h).
The premise: Ractor is isolation + message passing, and Spinel's "all state is a file-scope static" shape maps onto that almost for free. Every per-execution-unit mutable global becomes __thread, so each Ractor pthread owns a private GC young/old heap, root stack, string heap, object pools, exception/catch landing pads, regex match state, and lambda arena -- and therefore collects independently with no global GC lock. The const class / dispatch tables stay shared and read-only. The only lock-guarded shared state is the new per-Ractor mailbox/outgoing queues.
Runtime (lib/):
- __thread the collector core (sp_gc.c/.h: heap, roots, bytes, cycle, mark stack, verify snapshot) and the fiber world (sp_fiber.c: root fiber, current pointer, suspended list), initialised per thread by a new sp_fiber_thread_init() (main thread via a constructor, Ractors from their entry trampoline).
- __thread the sp_runtime.h per-unit statics: GC threshold/buckets, string heap + literal cache, per-class object pools, exception + catch stacks, regex captures, at_exit hooks, lambda arena.
- New lib/sp_ractor.{c,h}: sp_Ractor struct, mutex+condvar FIFO mailbox / outgoing queues, pthread entry trampoline, and sp_gc_thread_teardown() (lib/sp_gc.c) freeing the Ractor's private heaps on exit so RSS stays bounded across many Ractors. Milestone-1 boundary codec copies only immediate values (Integer/Float/true/false/nil); heap-backed values raise Ractor::Error pending the serialization codec of a later milestone.
Compiler (src/):
- TY_RACTOR type; "Ractor" registered as a builtin constant; inference arms for Ractor.new -> ractor, .receive/.take -> poly, .yield -> nil, send/<< -> ractor.
- emit_ractor_new (codegen.c): lowers the block to a static _ractor_body_N(sp_Ractor*) wrapped in a top-level rescue landing pad (an uncaught exception terminates one Ractor, not the process). Enforces the isolation rule at compile time: a captured outer local or self raises Ractor::IsolationError.
- Dispatch arms for the class/instance methods; -pthread wired into the link (common.mk LDFLAGS, src/main.c cc driver, the sp_ractor.o build rule).
The __thread conversion is behaviour-neutral for single-threaded programs (full suite still 892 pass); with the milestone test it is 893 pass / 0 fail / 0 error. The demo prints 42/42, runs SPINEL_GC_VERIFY-clean, and is clean under both ThreadSanitizer (ASLR disabled) and AddressSanitizer. Four CPU-bound Ractors hit 366% CPU at the same wall-clock as one (true parallelism, no global GC lock); 5000 sequential Ractors hold peak RSS at ~5.5 MB (heaps reclaimed on exit). Captured-variable programs fail to compile with Ractor::IsolationError.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 5cca24211635
- Parent
- d40bc31b97a3
MakefilemodifiedView file
@mkdir -p build $(CC) -c -O2 -Wno-all $(SEC_FLAGS) -Ilib lib/sp_fiber.c -o build/sp_fiber.obuild/sp_ractor.o: lib/sp_ractor.c lib/sp_ractor.h lib/sp_gc.h lib/sp_types.h @mkdir -p build $(CC) -c -O2 -Wno-all -pthread $(SEC_FLAGS) -Ilib lib/sp_ractor.c -o build/sp_ractor.obuild/sp_net.o: lib/sp_net.c lib/sp_net.h @mkdir -p build $(CC) -c -O2 -Wno-all $(SEC_FLAGS) -Ilib lib/sp_net.c -o build/sp_net.oSP_RT_LIB = lib/libspinel_rt.a$(SP_RT_LIB): $(RE_OBJ) build/sp_bigint.o build/sp_crypto.o build/sp_pack.o build/sp_strscan.o build/sp_time.o build/sp_core.o build/sp_net.o build/sp_system.o build/sp_gc.o build/sp_fiber.o build/sp_io.o$(SP_RT_LIB): $(RE_OBJ) build/sp_bigint.o build/sp_crypto.o build/sp_pack.o build/sp_strscan.o build/sp_time.o build/sp_core.o build/sp_net.o build/sp_system.o build/sp_gc.o build/sp_fiber.o build/sp_ractor.o build/sp_io.o ar rcs $@ $^regexp: $(SP_RT_LIB) install -m 644 lib/sp_system.h $(SPNLDIR)/lib/ install -m 644 lib/sp_gc.h $(SPNLDIR)/lib/ install -m 644 lib/sp_fiber.h $(SPNLDIR)/lib/ install -m 644 lib/sp_ractor.h $(SPNLDIR)/lib/ install -m 644 lib/sp_io.h $(SPNLDIR)/lib/ install -m 644 lib/sp_time.h $(SPNLDIR)/lib/ install -m 644 lib/sp_net.h $(SPNLDIR)/lib/common.mkmodifiedView file
Loading diff…
docs/RACTOR-RFC.mdaddedView file
Loading diff…
lib/sp_fiber.cmodifiedView file
Loading diff…
lib/sp_fiber.hmodifiedView file
Loading diff…
lib/sp_gc.cmodifiedView file
Loading diff…
lib/sp_gc.hmodifiedView file
Loading diff…
lib/sp_ractor.caddedView file
Loading diff…
lib/sp_ractor.haddedView file
Loading diff…
lib/sp_runtime.hmodifiedView file
Loading diff…
src/analyze_infer.cmodifiedView file
Loading diff…
src/analyze_util.cmodifiedView file
Loading diff…
src/codegen.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
src/codegen_internal.hmodifiedView file
Loading diff…
src/codegen_util.cmodifiedView file
Loading diff…
src/main.cmodifiedView file
Loading diff…
src/types.cmodifiedView file
Loading diff…
src/types.hmodifiedView file
Loading diff…
test/ractor_basic.rbaddedView file
Loading diff…
test/ractor_basic.rb.expectedaddedView file
Loading diff…