From 5e8e785129b1bee36cd4e298962693af9238c226 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_time.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_time.rb b/test/test_time.rb index 23e8e10..55964d0 100644 --- a/test/test_time.rb +++ b/test/test_time.rb @@ -74,7 +74,7 @@ def test_rfc2822_nonlinear if defined?(Ractor) def test_rfc2822_ractor assert_ractor(<<~RUBY, require: 'time') - actual = Ractor.new { Time.rfc2822("Fri, 21 Nov 1997 09:55:06 -0600") }.take + actual = Ractor.new { Time.rfc2822("Fri, 21 Nov 1997 09:55:06 -0600") }.value assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600, actual) RUBY end From 2a1827b0ce396d96e600405cbb6c1fb0395b4293 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Tue, 3 Jun 2025 16:41:19 +0900 Subject: [PATCH 2/2] Alias value or join to take in old Ruby --- test/test_time.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/test_time.rb b/test/test_time.rb index 55964d0..06db77b 100644 --- a/test/test_time.rb +++ b/test/test_time.rb @@ -74,6 +74,9 @@ def test_rfc2822_nonlinear if defined?(Ractor) def test_rfc2822_ractor assert_ractor(<<~RUBY, require: 'time') + class Ractor + alias value take unless method_defined? :value # compat with Ruby 3.4 and olders + end actual = Ractor.new { Time.rfc2822("Fri, 21 Nov 1997 09:55:06 -0600") }.value assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600, actual) RUBY