Commit 8ba58bf8
8ba58bf842a5Merged
Ori Pekelman <[email protected]> · 5 months ago · 6 files · +2 added · ~4 modified
Description
Ractor Milestone 3a: spawn arguments (Ractor.new(a,b){|x,y|...})
A Ractor block may now take parameters bound to deep-copied spawn arguments: Ractor.new(a, b) { |x, y| ... } serializes a/b on the creator side (reading the creator's heap) into a malloc'd blob array whose ownership transfers to the new Ractor; the body deserializes each into its own heap via sp_ractor_spawn_arg(i). Reuses the M2 message codec, so any shareable value (immediate/Symbol/String/Array) can seed a Ractor.
- lib/sp_ractor.{c,h}: sp_Ractor carries spawn_args; sp_Ractor_new_args() + sp_ractor_spawn_arg(); the trampoline frees unconsumed arg blobs on exit.
- src/codegen.c (emit_ractor_new): collect block params + call args; declare each param from sp_ractor_spawn_arg; serialize args at the creation site into sp_Ractor_new_args. Block params are no longer rejected, and are excluded from the capture check (they are params, not captures).
- src/analyze_pass.c: Ractor block params typed poly (same pass as Fiber).
The isolation rule still rejects genuine captured outer locals / self (now with a message pointing at spawn args / send). Full suite 929 pass / 0 fail / 0 error; spawn demo GC-verify- and valgrind-clean. Adds test/ractor_spawn.rb.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 8ba58bf842a5
- Parent
- 533e8aa3a861
lib/sp_ractor.cmodifiedView file
void (*body)(sp_Ractor *); sp_RactorQueue inbox; sp_RactorQueue outbox; sp_RactorBlob *spawn_args; /* Ractor.new(args){...}: deep-copied block args */ int spawn_argc; sp_Ractor *reg_next; /* process-lifetime registry link */}; sp_fiber_thread_init(); r->body(r); /* body installs its own top-level rescue pad */ sp_rq_close(&r->outbox); /* Free any spawn-arg blobs the body did not consume, plus the array. */ if (r->spawn_args) { for (int i = 0; i < r->spawn_argc; i++) free(r->spawn_args[i].data); free(r->spawn_args); r->spawn_args = NULL; } sp_gc_thread_teardown(); return NULL;}sp_Ractor *sp_Ractor_new(void (*body)(sp_Ractor *)) {/* Ractor.new(args...) { |params...| }: the args are serialized on the creator side (reading the creator's heap) into the malloc'd `args` array, ownership of which transfers here; the body deserializes each into its own heap via sp_ractor_spawn_arg(). Pass args=NULL, argc=0 for a plain Ractor.new {}. */sp_Ractor *sp_Ractor_new_args(void (*body)(sp_Ractor *), sp_RactorBlob *args, int argc) { sp_Ractor *r = (sp_Ractor *)calloc(1, sizeof(sp_Ractor)); if (!r) sp_raise_cls("Ractor::Error", "failed to allocate Ractor"); r->body = body; r->spawn_args = args; r->spawn_argc = argc; sp_rq_init(&r->inbox); sp_rq_init(&r->outbox); sp_ractor_register(r); /* retained for the process lifetime */ return r;}sp_Ractor *sp_Ractor_new(void (*body)(sp_Ractor *)) { return sp_Ractor_new_args(body, NULL, 0);}/* Deserialize the i-th spawn argument into this Ractor's heap (read once, per block parameter). Out-of-range yields nil, matching a block param with no corresponding argument. */sp_RbVal sp_ractor_spawn_arg(int i) { sp_Ractor *r = sp_ractor_current; if (!r || i < 0 || i >= r->spawn_argc || !r->spawn_args[i].data) return (sp_RbVal){ .tag = SP_TAG_NIL, .cls_id = 0, .v = { .i = 0 } }; sp_RactorBlob b = r->spawn_args[i]; r->spawn_args[i].data = NULL; /* taken; trampoline won't double-free */ return sp_ractor_from_blob(b);}void sp_Ractor_send(sp_Ractor *r, sp_RbVal v) { /* Serialize on the sender (reads the sender's heap) before crossing. */ sp_rq_push(&r->inbox, sp_ractor_to_blob(v));lib/sp_ractor.hmodifiedView file
Loading diff…
src/analyze_pass.cmodifiedView file
Loading diff…
src/codegen.cmodifiedView file
Loading diff…
test/ractor_spawn.rbaddedView file
Loading diff…
test/ractor_spawn.rb.expectedaddedView file
Loading diff…