From a621bfb12268dc71cc4b756bd746a27c544bd181 Mon Sep 17 00:00:00 2001 From: Koichi Sasada Date: Tue, 27 May 2025 03:58:04 +0900 Subject: [PATCH 1/2] `Ractor::Port` * Added `Ractor::Port` * `Ractor::Port#receive` (support multi-threads) * `Rcator::Port#close` * `Ractor::Port#closed?` * Added some methods * `Ractor#join` * `Ractor#value` * `Ractor#monitor` * `Ractor#unmonitor` * Removed some methods * `Ractor#take` * `Ractor.yield` * Change the spec * `Racotr.select` You can wait for multiple sequences of messages with `Ractor::Port`. ```ruby ports = 3.times.map{ Ractor::Port.new } ports.map.with_index do |port, ri| Ractor.new port,ri do |port, ri| 3.times{|i| port << "r#{ri}-#{i}"} end end p ports.each{|port| pp 3.times.map{port.receive}} ``` In this example, we use 3 ports, and 3 Ractors send messages to them respectively. We can receive a series of messages from each port. You can use `Ractor#value` to get the last value of a Ractor's block: ```ruby result = Ractor.new do heavy_task() end.value ``` You can wait for the termination of a Ractor with `Ractor#join` like this: ```ruby Ractor.new do some_task() end.join ``` `#value` and `#join` are similar to `Thread#value` and `Thread#join`. To implement `#join`, `Ractor#monitor` (and `Ractor#unmonitor`) is introduced. This commit changes `Ractor.select()` method. It now only accepts ports or Ractors, and returns when a port receives a message or a Ractor terminates. We removes `Ractor.yield` and `Ractor#take` because: * `Ractor::Port` supports most of similar use cases in a simpler manner. * Removing them significantly simplifies the code. We also change the internal thread scheduler code (thread_pthread.c): * During barrier synchronization, we keep the `ractor_sched` lock to avoid deadlocks. This lock is released by `rb_ractor_sched_barrier_end()` which is called at the end of operations that require the barrier. * fix potential deadlock issues by checking interrupts just before setting UBF. https://bugs.ruby-lang.org/issues/21262 --- test/test_tmpdir.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/test_tmpdir.rb b/test/test_tmpdir.rb index adc2918..789f433 100644 --- a/test/test_tmpdir.rb +++ b/test/test_tmpdir.rb @@ -134,16 +134,17 @@ def assert_mktmpdir_traversal def test_ractor assert_ractor(<<~'end;', require: "tmpdir") - r = Ractor.new do + port = Ractor::Port.new + r = Ractor.new port do |port| Dir.mktmpdir() do |d| - Ractor.yield d + port << d Ractor.receive end end - dir = r.take + dir = port.receive assert_file.directory? dir r.send true - r.take + r.join assert_file.not_exist? dir end; end From f12c766996eb55651a94de617f8814e0640b877b Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Tue, 3 Jun 2025 16:49:10 +0900 Subject: [PATCH 2/2] Restore Ractor.yield style test for old version of Ruby --- test/test_tmpdir.rb | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/test/test_tmpdir.rb b/test/test_tmpdir.rb index 789f433..c91fc33 100644 --- a/test/test_tmpdir.rb +++ b/test/test_tmpdir.rb @@ -134,18 +134,32 @@ def assert_mktmpdir_traversal def test_ractor assert_ractor(<<~'end;', require: "tmpdir") - port = Ractor::Port.new - r = Ractor.new port do |port| - Dir.mktmpdir() do |d| - port << d - Ractor.receive + if defined?(Ractor::Port) + port = Ractor::Port.new + r = Ractor.new port do |port| + Dir.mktmpdir() do |d| + port << d + Ractor.receive + end + end + dir = port.receive + assert_file.directory? dir + r.send true + r.join + assert_file.not_exist? dir + else + r = Ractor.new do + Dir.mktmpdir() do |d| + Ractor.yield d + Ractor.receive + end end + dir = r.take + assert_file.directory? dir + r.send true + r.take + assert_file.not_exist? dir end - dir = port.receive - assert_file.directory? dir - r.send true - r.join - assert_file.not_exist? dir end; end end