Commit bd1ea603

bd1ea603e462Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

Dispatch Thread#value/#join on a Fiber carried through a poly slot

A Thread is modelled as a single-threaded Fiber run to completion. #value / #join were only handled for a statically-typed Fiber receiver, so a Thread collected through a container -- (1..3).map { Thread.new { ... } } then .map(&:value) -- became a poly element, #value fell through to the NoMethodError gate, and the result was [nil, nil, nil] instead of [10, 20, 30].

Add value/resume/join to the poly-receiver dispatch shortcut (the same place sum/min/max/first/last live): when no user class defines the name, emit a runtime helper that dispatches on the boxed Fiber. The helper is idempotent -- a finished thread returns its cached value rather than re-resuming a terminated fiber -- so reading #value more than once works too. A non-Fiber poly returns nil, matching the gate. If a user class defines the name, the call falls through to the normal poly dispatch instead.

Real parallelism is unchanged (still a single-threaded model). Refs #1261.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
bd1ea603e462
Parent
c01a389239c1

lib/sp_runtime.hmodifiedView file

@@ -4501,6 +4501,27 @@
45014501 mrb_int n = sp_poly_length(v);
45024502 return n > 0 ? sp_poly_arr_get(v, n - 1) : sp_box_nil();
45034503 }
4504+/* Thread#value / #join through a poly slot. A Thread is modelled as a Fiber run
4505+ to completion (single-threaded); when one is carried in a poly value -- e.g.
4506+ an array of Threads, `(1..n).map { Thread.new { ... } }` -- #value/#join must
4507+ dispatch at runtime on the boxed Fiber. value/resume return the block's
4508+ result; join runs it and returns the thread (self). A non-Fiber poly returns
4509+ nil, matching the NoMethodError gate for an unknown poly method. */
4510+static sp_RbVal sp_poly_fiber_value(sp_RbVal v) {
4511+ if (v.tag == SP_TAG_OBJ && v.cls_id == SP_BUILTIN_FIBER) {
4512+ sp_Fiber *f = (sp_Fiber *)v.v.p;
4513+ if (f->state == 3) return f->yielded_value; /* already run: cached result, idempotent */
4514+ return sp_Fiber_resume(f, sp_box_nil());
4515+ }
4516+ return sp_box_nil();
4517+}
4518+static sp_RbVal sp_poly_fiber_join(sp_RbVal v) {
4519+ if (v.tag == SP_TAG_OBJ && v.cls_id == SP_BUILTIN_FIBER) {
4520+ sp_Fiber *f = (sp_Fiber *)v.v.p;
4521+ if (f->state != 3) sp_Fiber_resume(f, sp_box_nil());
4522+ }
4523+ return v;
4524+}
45044525 static sp_PolyArray*sp_PolyPolyHash_keys(sp_PolyPolyHash*h){SP_GC_ROOT(h);sp_PolyArray*a=sp_PolyArray_new();SP_GC_ROOT(a);for(mrb_int i=0;i<h->len;i++)sp_PolyArray_push(a,h->keys[h->order[i]]);return a;}
45054526 static sp_PolyArray*sp_PolyPolyHash_values(sp_PolyPolyHash*h){SP_GC_ROOT(h);sp_PolyArray*a=sp_PolyArray_new();SP_GC_ROOT(a);for(mrb_int i=0;i<h->len;i++)sp_PolyArray_push(a,h->vals[h->order[i]]);return a;}
45064527 static sp_PolyPolyHash*sp_PolyPolyHash_dup(sp_PolyPolyHash*h){sp_PolyPolyHash*r=sp_PolyPolyHash_new();for(mrb_int i=0;i<h->len;i++)sp_PolyPolyHash_set(r,h->keys[h->order[i]],h->vals[h->order[i]]);return r;}