Commit 993f5140

993f5140279dMerged

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

Description

Thread.new { block }.value via single-threaded Fiber model

A Thread with a block has no real preemption in the AOT runtime, so model it as a Fiber: Thread.new returns TY_FIBER and #value resumes it to completion, yielding the block's last value. Fixes thread_new_value.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
993f5140279d
Parent
219cf9dd9018

src/analyze.cmodifiedView file

@@ -699,6 +699,8 @@
699699 if (cn && !strcmp(cn, "Regexp")) return TY_REGEX;
700700 /* Builtin object types */
701701 if (cn && !strcmp(cn, "Fiber")) return TY_FIBER;
702+ /* Thread.new { block }: modeled as a Fiber run to completion on #value. */
703+ if (cn && !strcmp(cn, "Thread") && nt_ref(nt, id, "block") >= 0) return TY_FIBER;
702704 if (cn && (!strcmp(cn, "Thread") || !strcmp(cn, "Mutex") || !strcmp(cn, "Monitor") ||
703705 !strcmp(cn, "Random") || !strcmp(cn, "IO") || !strcmp(cn, "File") ||
704706 !strcmp(cn, "GzipReader") || !strcmp(cn, "GzipWriter"))) return TY_POLY;
@@ -867,6 +869,11 @@
867869 if (rty && (!strcmp(rty, "ConstantReadNode") || !strcmp(rty, "ConstantPathNode"))) {
868870 const char *cn2 = nt_str(nt, recv, "name");
869871 if (cn2 && !strcmp(name, "new") && !strcmp(cn2, "Fiber")) return TY_FIBER;
872+ /* Thread.new { block } modeled as a Fiber (single-threaded); #value
873+ resumes it to completion. */
874+ if (cn2 && !strcmp(name, "new") && !strcmp(cn2, "Thread") &&
875+ nt_ref(nt, id, "block") >= 0)
876+ return TY_FIBER;
870877 if (cn2 && !strcmp(name, "new") &&
871878 (!strcmp(cn2, "Thread") || !strcmp(cn2, "Mutex") || !strcmp(cn2, "Random")))
872879 return TY_POLY;