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

@@ -33,6 +33,8 @@
3333 void (*body)(sp_Ractor *);
3434 sp_RactorQueue inbox;
3535 sp_RactorQueue outbox;
36+ sp_RactorBlob *spawn_args; /* Ractor.new(args){...}: deep-copied block args */
37+ int spawn_argc;
3638 sp_Ractor *reg_next; /* process-lifetime registry link */
3739 };
3840
@@ -128,14 +130,25 @@
128130 sp_fiber_thread_init();
129131 r->body(r); /* body installs its own top-level rescue pad */
130132 sp_rq_close(&r->outbox);
133+ /* Free any spawn-arg blobs the body did not consume, plus the array. */
134+ if (r->spawn_args) {
135+ for (int i = 0; i < r->spawn_argc; i++) free(r->spawn_args[i].data);
136+ free(r->spawn_args); r->spawn_args = NULL;
137+ }
131138 sp_gc_thread_teardown();
132139 return NULL;
133140 }
134141
135-sp_Ractor *sp_Ractor_new(void (*body)(sp_Ractor *)) {
142+/* Ractor.new(args...) { |params...| }: the args are serialized on the creator
143+ side (reading the creator's heap) into the malloc'd `args` array, ownership
144+ of which transfers here; the body deserializes each into its own heap via
145+ sp_ractor_spawn_arg(). Pass args=NULL, argc=0 for a plain Ractor.new {}. */
146+sp_Ractor *sp_Ractor_new_args(void (*body)(sp_Ractor *), sp_RactorBlob *args, int argc) {
136147 sp_Ractor *r = (sp_Ractor *)calloc(1, sizeof(sp_Ractor));
137148 if (!r) sp_raise_cls("Ractor::Error", "failed to allocate Ractor");
138149 r->body = body;
150+ r->spawn_args = args;
151+ r->spawn_argc = argc;
139152 sp_rq_init(&r->inbox);
140153 sp_rq_init(&r->outbox);
141154 sp_ractor_register(r); /* retained for the process lifetime */
@@ -145,6 +158,22 @@
145158 return r;
146159 }
147160
161+sp_Ractor *sp_Ractor_new(void (*body)(sp_Ractor *)) {
162+ return sp_Ractor_new_args(body, NULL, 0);
163+}
164+
165+/* Deserialize the i-th spawn argument into this Ractor's heap (read once, per
166+ block parameter). Out-of-range yields nil, matching a block param with no
167+ corresponding argument. */
168+sp_RbVal sp_ractor_spawn_arg(int i) {
169+ sp_Ractor *r = sp_ractor_current;
170+ if (!r || i < 0 || i >= r->spawn_argc || !r->spawn_args[i].data)
171+ return (sp_RbVal){ .tag = SP_TAG_NIL, .cls_id = 0, .v = { .i = 0 } };
172+ sp_RactorBlob b = r->spawn_args[i];
173+ r->spawn_args[i].data = NULL; /* taken; trampoline won't double-free */
174+ return sp_ractor_from_blob(b);
175+}
176+
148177 void sp_Ractor_send(sp_Ractor *r, sp_RbVal v) {
149178 /* Serialize on the sender (reads the sender's heap) before crossing. */
150179 sp_rq_push(&r->inbox, sp_ractor_to_blob(v));