Skip to content

Support Ractor#value #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 3, 2025
Merged

Support Ractor#value #134

merged 3 commits into from
Jun 3, 2025

Conversation

hsbt
Copy link
Member

@hsbt hsbt commented Jun 3, 2025

from https://bugs.ruby-lang.org/issues/21262

We need to alias Ractor#value to Ractor#take for old versions of Ruby.

ko1 and others added 2 commits June 3, 2025 16:14
* 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
Co-authored-by: Sutou Kouhei <kou@clear-code.com>
kou pushed a commit to ruby/strscan that referenced this pull request Jun 3, 2025
This is same as ruby/stringio#134

---------

Co-authored-by: Koichi Sasada <ko1@atdot.net>
@kou kou merged commit 9954dab into master Jun 3, 2025
102 checks passed
@kou kou deleted the ractor-value branch June 3, 2025 07:44
matzbot pushed a commit to ruby/ruby that referenced this pull request Jun 3, 2025
(ruby/stringio#134)

from https://bugs.ruby-lang.org/issues/21262

We need to alias `Ractor#value` to `Ractor#take` for old versions of
Ruby.

---------

ruby/stringio@9954dabd80

Co-authored-by: Koichi Sasada <ko1@atdot.net>
Co-authored-by: Sutou Kouhei <kou@clear-code.com>
matzbot pushed a commit to ruby/ruby that referenced this pull request Jun 3, 2025
(ruby/strscan#157)

This is same as ruby/stringio#134

---------

ruby/strscan@141f9cf9b6

Co-authored-by: Koichi Sasada <ko1@atdot.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants