Commit 4b380748
4b380748cf20Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 6 files · +4 added · ~2 modified
Description
Run Mutex/Monitor#synchronize and Thread.new blocks, carry their value
In Spinel's single-threaded AOT model these concurrency primitives have
trivially-satisfied semantics, but they were lowered as unresolved calls
on the int-0 that Mutex.new/Monitor.new/Thread.new produce: the
block was silently dropped (synchronize) or the result was 0
(Thread#value), with no diagnostic.
Wire them through the existing value-returning-block machinery used by
then/StringIO.open:
mutex.synchronize { body }/monitor.synchronize { body }run the block and return its last-expression value (the lock is always held). The receiver is still evaluated for side effects. Works at top level and for ivar receivers inside instance methods (the bundler/rake idiom).Thread.new { body }runs the block eagerly and models the Thread value as the block's result;t.value(no args) reads it back as the identity on a scalar receiver, guarded so struct/hash/object.valueaccessors are never hijacked.
The compiler itself uses none of these primitives, so the change is purely additive and self-host-safe. Full suite: no new regressions.
Closes #1360
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 4b380748cf20
- Parent
- 0c21b9c76a0a
spinel_analyze.rbmodifiedView file
end return "int" end # `mutex.synchronize { body }`: single-threaded, so the result is the # block's last-expression value (the lock is trivially held). No block # param to bind -- synchronize yields nothing. if mname == "synchronize" blk = @nd_block[nid] if blk >= 0 bbody = @nd_body[blk] if bbody >= 0 bbs = get_stmts(bbody) if bbs.length > 0 return infer_type(bbs.last) end end return "int" end end # `Thread.new { body }`: single-threaded, so run the block eagerly; # the Thread value is modelled directly as the block's return value. # `t.value` / `t.join` then read that value back (identity below). if mname == "new" && constructor_class_name(recv) == "Thread" blk = @nd_block[nid] if blk >= 0 bbody = @nd_body[blk] if bbody >= 0 bbs = get_stmts(bbody) if bbs.length > 0 return infer_type(bbs.last) end end end return "int" end # `t.value` on a Thread (modelled as its eager block value): return the # receiver's type so the carried value reads back unchanged. In a # single-threaded model the Thread *is* its result value, so `value` is # the identity. A no-arg `value` is otherwise an unresolved call on the # carried scalar; treating it as identity is strictly better than the # int-0 fallback (and `value` on a real Integer is a NoMethodError in # CRuby anyway, so no valid program loses). Restricted to scalar # receivers so a struct/hash/array `value` access is never hijacked. if mname == "value" && recv >= 0 && @nd_arguments[nid] < 0 rtv = infer_type(recv) bt = base_type(rtv) if bt == "int" || bt == "string" || bt == "float" || bt == "bool" return rtv end end # `StringIO.open(str) { |io| ... }` returns the block's value (and # closes the io). Same value-returning-block shape as `then`, but the # block param is a freshly-constructed StringIO, not the receiver.spinel_codegen.rbmodifiedView file
Loading diff…
test/mutex_synchronize_block.rbaddedView file
Loading diff…
test/mutex_synchronize_block.rb.expectedaddedView file
Loading diff…
test/thread_new_value.rbaddedView file
Loading diff…
test/thread_new_value.rb.expectedaddedView file
Loading diff…