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

@@ -224,6 +224,10 @@
224224 @mkdir -p build
225225 $(CC) -c -O2 -Wno-all $(SEC_FLAGS) -Ilib lib/sp_fiber.c -o build/sp_fiber.o
226226
227+build/sp_ractor.o: lib/sp_ractor.c lib/sp_ractor.h lib/sp_gc.h lib/sp_types.h
228+ @mkdir -p build
229+ $(CC) -c -O2 -Wno-all -pthread $(SEC_FLAGS) -Ilib lib/sp_ractor.c -o build/sp_ractor.o
230+
227231 build/sp_net.o: lib/sp_net.c lib/sp_net.h
228232 @mkdir -p build
229233 $(CC) -c -O2 -Wno-all $(SEC_FLAGS) -Ilib lib/sp_net.c -o build/sp_net.o
@@ -234,7 +238,7 @@
234238
235239 SP_RT_LIB = lib/libspinel_rt.a
236240
237-$(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
241+$(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
238242 ar rcs $@ $^
239243
240244 regexp: $(SP_RT_LIB)
@@ -547,6 +551,7 @@
547551 install -m 644 lib/sp_system.h $(SPNLDIR)/lib/
548552 install -m 644 lib/sp_gc.h $(SPNLDIR)/lib/
549553 install -m 644 lib/sp_fiber.h $(SPNLDIR)/lib/
554+ install -m 644 lib/sp_ractor.h $(SPNLDIR)/lib/
550555 install -m 644 lib/sp_io.h $(SPNLDIR)/lib/
551556 install -m 644 lib/sp_time.h $(SPNLDIR)/lib/
552557 install -m 644 lib/sp_net.h $(SPNLDIR)/lib/