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
mrb_int n = sp_poly_length(v); return n > 0 ? sp_poly_arr_get(v, n - 1) : sp_box_nil();}/* Thread#value / #join through a poly slot. A Thread is modelled as a Fiber run to completion (single-threaded); when one is carried in a poly value -- e.g. an array of Threads, `(1..n).map { Thread.new { ... } }` -- #value/#join must dispatch at runtime on the boxed Fiber. value/resume return the block's result; join runs it and returns the thread (self). A non-Fiber poly returns nil, matching the NoMethodError gate for an unknown poly method. */static sp_RbVal sp_poly_fiber_value(sp_RbVal v) { if (v.tag == SP_TAG_OBJ && v.cls_id == SP_BUILTIN_FIBER) { sp_Fiber *f = (sp_Fiber *)v.v.p; if (f->state == 3) return f->yielded_value; /* already run: cached result, idempotent */ return sp_Fiber_resume(f, sp_box_nil()); } return sp_box_nil();}static sp_RbVal sp_poly_fiber_join(sp_RbVal v) { if (v.tag == SP_TAG_OBJ && v.cls_id == SP_BUILTIN_FIBER) { sp_Fiber *f = (sp_Fiber *)v.v.p; if (f->state != 3) sp_Fiber_resume(f, sp_box_nil()); } return v;}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;}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;}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;}src/codegen_call.cmodifiedView file
Loading diff…
test/thread_value_through_container.rbaddedView file
Loading diff…
test/thread_value_through_container.rb.expectedaddedView file
Loading diff…