2023-01-29 04:01:49 +03:00
|
|
|
# \Ractor is an Actor-model abstraction for Ruby that provides thread-safe parallel execution.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Ractor.new makes a new \Ractor, which can run in parallel.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# # The simplest ractor
|
|
|
|
# r = Ractor.new {puts "I am in Ractor!"}
|
2021-01-10 23:39:37 +03:00
|
|
|
# r.take # wait for it to finish
|
2023-01-29 04:01:49 +03:00
|
|
|
# # Here, "I am in Ractor!" is printed
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Ractors do not share all objects with each other. There are two main benefits to this: across ractors, thread-safety
|
|
|
|
# concerns such as data-races and race-conditions are not possible. The other benefit is parallelism.
|
2020-12-21 19:05:52 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# To achieve this, object sharing is limited across ractors.
|
|
|
|
# For example, unlike in threads, ractors can't access all the objects available in other ractors. Even objects normally
|
|
|
|
# available through variables in the outer scope are prohibited from being used across ractors.
|
2020-12-21 19:05:52 +03:00
|
|
|
#
|
2020-12-19 21:04:40 +03:00
|
|
|
# a = 1
|
|
|
|
# r = Ractor.new {puts "I am in Ractor! a=#{a}"}
|
|
|
|
# # fails immediately with
|
|
|
|
# # ArgumentError (can not isolate a Proc because it accesses outer variables (a).)
|
|
|
|
#
|
2023-02-27 20:56:06 +03:00
|
|
|
# The object must be explicitly shared:
|
2023-01-29 04:01:49 +03:00
|
|
|
# a = 1
|
|
|
|
# r = Ractor.new(a) { |a1| puts "I am in Ractor! a=#{a1}"}
|
|
|
|
#
|
2020-12-19 21:04:40 +03:00
|
|
|
# On CRuby (the default implementation), Global Virtual Machine Lock (GVL) is held per ractor, so
|
2023-01-29 04:01:49 +03:00
|
|
|
# ractors can perform in parallel without locking each other. This is unlike the situation with threads
|
|
|
|
# on CRuby.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Instead of accessing shared state, objects should be passed to and from ractors by
|
|
|
|
# sending and receiving them as messages.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# a = 1
|
|
|
|
# r = Ractor.new do
|
2023-01-29 04:01:49 +03:00
|
|
|
# a_in_ractor = receive # receive blocks until somebody passes a message
|
2020-12-19 21:04:40 +03:00
|
|
|
# puts "I am in Ractor! a=#{a_in_ractor}"
|
|
|
|
# end
|
|
|
|
# r.send(a) # pass it
|
|
|
|
# r.take
|
2023-01-29 04:01:49 +03:00
|
|
|
# # Here, "I am in Ractor! a=1" is printed
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# There are two pairs of methods for sending/receiving messages:
|
|
|
|
#
|
|
|
|
# * Ractor#send and Ractor.receive for when the _sender_ knows the receiver (push);
|
|
|
|
# * Ractor.yield and Ractor#take for when the _receiver_ knows the sender (pull);
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# In addition to that, any arguments passed to Ractor.new are passed to the block and available there
|
|
|
|
# as if received by Ractor.receive, and the last block value is sent outside of the
|
2020-12-19 21:04:40 +03:00
|
|
|
# ractor as if sent by Ractor.yield.
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# A little demonstration of a classic ping-pong:
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# server = Ractor.new(name: "server") do
|
2020-12-19 21:04:40 +03:00
|
|
|
# puts "Server starts: #{self.inspect}"
|
|
|
|
# puts "Server sends: ping"
|
|
|
|
# Ractor.yield 'ping' # The server doesn't know the receiver and sends to whoever interested
|
|
|
|
# received = Ractor.receive # The server doesn't know the sender and receives from whoever sent
|
|
|
|
# puts "Server received: #{received}"
|
|
|
|
# end
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# client = Ractor.new(server) do |srv| # The server is sent to the client, and available as srv
|
2020-12-19 21:04:40 +03:00
|
|
|
# puts "Client starts: #{self.inspect}"
|
2023-01-29 04:01:49 +03:00
|
|
|
# received = srv.take # The client takes a message from the server
|
2020-12-19 21:04:40 +03:00
|
|
|
# puts "Client received from " \
|
|
|
|
# "#{srv.inspect}: #{received}"
|
|
|
|
# puts "Client sends to " \
|
|
|
|
# "#{srv.inspect}: pong"
|
2023-01-29 04:01:49 +03:00
|
|
|
# srv.send 'pong' # The client sends a message to the server
|
2020-12-19 21:04:40 +03:00
|
|
|
# end
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# [client, server].each(&:take) # Wait until they both finish
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# This will output something like:
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Server starts: #<Ractor:#2 server test.rb:1 running>
|
2020-12-19 21:04:40 +03:00
|
|
|
# Server sends: ping
|
|
|
|
# Client starts: #<Ractor:#3 test.rb:8 running>
|
2023-01-29 04:01:49 +03:00
|
|
|
# Client received from #<Ractor:#2 server test.rb:1 blocking>: ping
|
|
|
|
# Client sends to #<Ractor:#2 server test.rb:1 blocking>: pong
|
2020-12-19 21:04:40 +03:00
|
|
|
# Server received: pong
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Ractors receive their messages via the <em>incoming port</em>, and send them
|
2020-12-19 21:04:40 +03:00
|
|
|
# to the <em>outgoing port</em>. Either one can be disabled with Ractor#close_incoming and
|
2023-01-29 04:01:49 +03:00
|
|
|
# Ractor#close_outgoing, respectively. When a ractor terminates, its ports are closed
|
2020-12-21 19:05:52 +03:00
|
|
|
# automatically.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# == Shareable and unshareable objects
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# When an object is sent to and from a ractor, it's important to understand whether the
|
|
|
|
# object is shareable or unshareable. Most Ruby objects are unshareable objects. Even
|
|
|
|
# frozen objects can be unshareable if they contain (through their instance variables) unfrozen
|
|
|
|
# objects.
|
2020-12-21 19:05:52 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Shareable objects are those which can be used by several threads without compromising
|
|
|
|
# thread-safety, for example numbers, +true+ and +false+. Ractor.shareable? allows you to check this,
|
|
|
|
# and Ractor.make_shareable tries to make the object shareable if it's not already, and gives an error
|
|
|
|
# if it can't do it.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Ractor.shareable?(1) #=> true -- numbers and other immutable basic values are shareable
|
|
|
|
# Ractor.shareable?('foo') #=> false, unless the string is frozen due to # frozen_string_literal: true
|
2020-12-19 21:04:40 +03:00
|
|
|
# Ractor.shareable?('foo'.freeze) #=> true
|
2023-01-29 04:01:49 +03:00
|
|
|
# Ractor.shareable?([Object.new].freeze) #=> false, inner object is unfrozen
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# ary = ['hello', 'world']
|
|
|
|
# ary.frozen? #=> false
|
|
|
|
# ary[0].frozen? #=> false
|
|
|
|
# Ractor.make_shareable(ary)
|
|
|
|
# ary.frozen? #=> true
|
|
|
|
# ary[0].frozen? #=> true
|
|
|
|
# ary[1].frozen? #=> true
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# When a shareable object is sent (via #send or Ractor.yield), no additional processing occurs
|
|
|
|
# on it. It just becomes usable by both ractors. When an unshareable object is sent, it can be
|
|
|
|
# either _copied_ or _moved_. The first is the default, and it copies the object fully by
|
|
|
|
# deep cloning (Object#clone) the non-shareable parts of its structure.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# data = ['foo', 'bar'.freeze]
|
|
|
|
# r = Ractor.new do
|
|
|
|
# data2 = Ractor.receive
|
|
|
|
# puts "In ractor: #{data2.object_id}, #{data2[0].object_id}, #{data2[1].object_id}"
|
|
|
|
# end
|
|
|
|
# r.send(data)
|
|
|
|
# r.take
|
|
|
|
# puts "Outside : #{data.object_id}, #{data[0].object_id}, #{data[1].object_id}"
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# This will output something like:
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# In ractor: 340, 360, 320
|
|
|
|
# Outside : 380, 400, 320
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Note that the object ids of the array and the non-frozen string inside the array have changed in
|
|
|
|
# the ractor because they are different objects. The second array's element, which is a
|
|
|
|
# shareable frozen string, is the same object.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Deep cloning of objects may be slow, and sometimes impossible. Alternatively, <tt>move: true</tt> may
|
|
|
|
# be used during sending. This will <em>move</em> the unshareable object to the receiving ractor, making it
|
|
|
|
# inaccessible to the sending ractor.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# data = ['foo', 'bar']
|
|
|
|
# r = Ractor.new do
|
|
|
|
# data_in_ractor = Ractor.receive
|
|
|
|
# puts "In ractor: #{data_in_ractor.object_id}, #{data_in_ractor[0].object_id}"
|
|
|
|
# end
|
|
|
|
# r.send(data, move: true)
|
|
|
|
# r.take
|
|
|
|
# puts "Outside: moved? #{Ractor::MovedObject === data}"
|
|
|
|
# puts "Outside: #{data.inspect}"
|
|
|
|
#
|
|
|
|
# This will output:
|
|
|
|
#
|
|
|
|
# In ractor: 100, 120
|
|
|
|
# Outside: moved? true
|
|
|
|
# test.rb:9:in `method_missing': can not send any methods to a moved object (Ractor::MovedError)
|
|
|
|
#
|
|
|
|
# Notice that even +inspect+ (and more basic methods like <tt>__id__</tt>) is inaccessible
|
|
|
|
# on a moved object.
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Class and Module objects are shareable so the class/module definitions are shared between ractors.
|
|
|
|
# \Ractor objects are also shareable. All operations on shareable objects are thread-safe, so the thread-safety property
|
2020-12-24 10:10:10 +03:00
|
|
|
# will be kept. We can not define mutable shareable objects in Ruby, but C extensions can introduce them.
|
2020-12-21 19:05:52 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# It is prohibited to access (get) instance variables of shareable objects in other ractors if the values of the
|
|
|
|
# variables aren't shareable. This can occur because modules/classes are shareable, but they can have
|
|
|
|
# instance variables whose values are not. In non-main ractors, it's also prohibited to set instance
|
|
|
|
# variables on classes/modules (even if the value is shareable).
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# class C
|
|
|
|
# class << self
|
|
|
|
# attr_accessor :tricky
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# C.tricky = "unshareable".dup
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new(C) do |cls|
|
|
|
|
# puts "I see #{cls}"
|
|
|
|
# puts "I can't see #{cls.tricky}"
|
2023-01-29 04:01:49 +03:00
|
|
|
# cls.tricky = true # doesn't get here, but this would also raise an error
|
2020-12-19 21:04:40 +03:00
|
|
|
# end
|
|
|
|
# r.take
|
|
|
|
# # I see C
|
|
|
|
# # can not access instance variables of classes/modules from non-main Ractors (RuntimeError)
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Ractors can access constants if they are shareable. The main \Ractor is the only one that can
|
2020-12-19 21:04:40 +03:00
|
|
|
# access non-shareable constants.
|
|
|
|
#
|
|
|
|
# GOOD = 'good'.freeze
|
2023-01-29 04:01:49 +03:00
|
|
|
# BAD = 'bad'.dup
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new do
|
|
|
|
# puts "GOOD=#{GOOD}"
|
|
|
|
# puts "BAD=#{BAD}"
|
|
|
|
# end
|
|
|
|
# r.take
|
|
|
|
# # GOOD=good
|
|
|
|
# # can not access non-shareable objects in constant Object::BAD by non-main Ractor. (NameError)
|
|
|
|
#
|
|
|
|
# # Consider the same C class from above
|
|
|
|
#
|
|
|
|
# r = Ractor.new do
|
|
|
|
# puts "I see #{C}"
|
|
|
|
# puts "I can't see #{C.tricky}"
|
|
|
|
# end
|
|
|
|
# r.take
|
|
|
|
# # I see C
|
|
|
|
# # can not access instance variables of classes/modules from non-main Ractors (RuntimeError)
|
|
|
|
#
|
|
|
|
# See also the description of <tt># shareable_constant_value</tt> pragma in
|
2021-09-15 03:39:43 +03:00
|
|
|
# {Comments syntax}[rdoc-ref:syntax/comments.rdoc] explanation.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# == Ractors vs threads
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Each ractor has its own main Thread. New threads can be created from inside ractors
|
|
|
|
# (and, on CRuby, they share the GVL with other threads of this ractor).
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new do
|
|
|
|
# a = 1
|
|
|
|
# Thread.new {puts "Thread in ractor: a=#{a}"}.join
|
|
|
|
# end
|
|
|
|
# r.take
|
|
|
|
# # Here "Thread in ractor: a=1" will be printed
|
|
|
|
#
|
|
|
|
# == Note on code examples
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# In the examples below, sometimes we use the following method to wait for ractors that
|
|
|
|
# are not currently blocked to finish (or to make progress).
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# def wait
|
|
|
|
# sleep(0.1)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# It is **only for demonstration purposes** and shouldn't be used in a real code.
|
2023-01-29 04:01:49 +03:00
|
|
|
# Most of the time, #take is used to wait for ractors to finish.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
2020-12-21 19:05:52 +03:00
|
|
|
# == Reference
|
|
|
|
#
|
2021-09-15 03:39:43 +03:00
|
|
|
# See {Ractor design doc}[rdoc-ref:ractor.md] for more details.
|
2020-12-21 19:05:52 +03:00
|
|
|
#
|
2020-03-09 20:22:11 +03:00
|
|
|
class Ractor
|
2020-12-24 10:10:10 +03:00
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# Ractor.new(*args, name: nil) {|*args| block } -> ractor
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Create a new \Ractor with args and a block.
|
2020-03-09 20:22:11 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# The given block (Proc) will be isolated (can't access any outer variables). +self+
|
|
|
|
# inside the block will refer to the current \Ractor.
|
2020-03-09 20:22:11 +03:00
|
|
|
#
|
2020-12-24 10:10:10 +03:00
|
|
|
# r = Ractor.new { puts "Hi, I am #{self.inspect}" }
|
2020-12-19 21:04:40 +03:00
|
|
|
# r.take
|
|
|
|
# # Prints "Hi, I am #<Ractor:#2 test.rb:1 running>"
|
2020-03-09 20:22:11 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Any +args+ passed are propagated to the block arguments by the same rules as
|
|
|
|
# objects sent via #send/Ractor.receive. If an argument in +args+ is not shareable, it
|
2020-12-19 21:04:40 +03:00
|
|
|
# will be copied (via deep cloning, which might be inefficient).
|
2020-03-09 20:22:11 +03:00
|
|
|
#
|
2020-12-19 21:04:40 +03:00
|
|
|
# arg = [1, 2, 3]
|
|
|
|
# puts "Passing: #{arg} (##{arg.object_id})"
|
|
|
|
# r = Ractor.new(arg) {|received_arg|
|
2020-12-24 10:10:10 +03:00
|
|
|
# puts "Received: #{received_arg} (##{received_arg.object_id})"
|
2020-12-19 21:04:40 +03:00
|
|
|
# }
|
|
|
|
# r.take
|
|
|
|
# # Prints:
|
|
|
|
# # Passing: [1, 2, 3] (#280)
|
|
|
|
# # Received: [1, 2, 3] (#300)
|
|
|
|
#
|
|
|
|
# Ractor's +name+ can be set for debugging purposes:
|
2020-03-09 20:22:11 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# r = Ractor.new(name: 'my ractor') {}; r.take
|
2020-12-19 21:04:40 +03:00
|
|
|
# p r
|
|
|
|
# #=> #<Ractor:#3 my ractor test.rb:1 terminated>
|
2020-09-18 08:15:32 +03:00
|
|
|
#
|
2020-10-03 15:05:15 +03:00
|
|
|
def self.new(*args, name: nil, &block)
|
2020-03-09 20:22:11 +03:00
|
|
|
b = block # TODO: builtin bug
|
|
|
|
raise ArgumentError, "must be called with a block" unless block
|
2022-10-26 13:43:14 +03:00
|
|
|
if __builtin_cexpr!("RBOOL(ruby_single_main_ractor)")
|
|
|
|
warn("Ractor is experimental, and the behavior may change in future versions of Ruby! " \
|
|
|
|
"Also there are many implementation issues.", uplevel: 0, category: :experimental)
|
|
|
|
end
|
2020-03-09 20:22:11 +03:00
|
|
|
loc = caller_locations(1, 1).first
|
|
|
|
loc = "#{loc.path}:#{loc.lineno}"
|
|
|
|
__builtin_ractor_create(loc, name, args, b)
|
|
|
|
end
|
|
|
|
|
2020-12-19 21:04:40 +03:00
|
|
|
# Returns the currently executing Ractor.
|
|
|
|
#
|
|
|
|
# Ractor.current #=> #<Ractor:#1 running>
|
2020-03-09 20:22:11 +03:00
|
|
|
def self.current
|
|
|
|
__builtin_cexpr! %q{
|
2020-12-19 19:44:41 +03:00
|
|
|
rb_ractor_self(rb_ec_ractor_ptr(ec));
|
2020-12-20 09:13:07 +03:00
|
|
|
}
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
|
|
|
|
2023-01-29 04:01:49 +03:00
|
|
|
# Returns the number of Ractors currently running or blocking (waiting).
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# Ractor.count #=> 1
|
2020-12-21 19:05:52 +03:00
|
|
|
# r = Ractor.new(name: 'example') { Ractor.yield(1) }
|
2020-12-19 21:04:40 +03:00
|
|
|
# Ractor.count #=> 2 (main + example ractor)
|
2020-12-21 19:05:52 +03:00
|
|
|
# r.take # wait for Ractor.yield(1)
|
2023-01-29 04:01:49 +03:00
|
|
|
# r.take # wait until r will finish
|
2020-12-19 21:04:40 +03:00
|
|
|
# Ractor.count #=> 1
|
2020-03-09 20:22:11 +03:00
|
|
|
def self.count
|
|
|
|
__builtin_cexpr! %q{
|
|
|
|
ULONG2NUM(GET_VM()->ractor.cnt);
|
2020-12-20 09:13:07 +03:00
|
|
|
}
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
|
|
|
|
2020-12-24 10:10:10 +03:00
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# Ractor.select(*ractors, [yield_value:, move: false]) -> [ractor or symbol, obj]
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Wait for any ractor to have something in its outgoing port, read from this ractor, and
|
|
|
|
# then return that ractor and the object received.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r1 = Ractor.new {Ractor.yield 'from 1'}
|
|
|
|
# r2 = Ractor.new {Ractor.yield 'from 2'}
|
|
|
|
#
|
|
|
|
# r, obj = Ractor.select(r1, r2)
|
|
|
|
#
|
|
|
|
# puts "received #{obj.inspect} from #{r.inspect}"
|
|
|
|
# # Prints: received "from 1" from #<Ractor:#2 test.rb:1 running>
|
2023-01-29 04:01:49 +03:00
|
|
|
# # But could just as well print "from r2" here, either prints could be first.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# If one of the given ractors is the current ractor, and it is selected, +r+ will contain
|
|
|
|
# the +:receive+ symbol instead of the ractor object.
|
2020-03-09 20:22:11 +03:00
|
|
|
#
|
2020-12-19 21:04:40 +03:00
|
|
|
# r1 = Ractor.new(Ractor.current) do |main|
|
|
|
|
# main.send 'to main'
|
|
|
|
# Ractor.yield 'from 1'
|
|
|
|
# end
|
|
|
|
# r2 = Ractor.new do
|
|
|
|
# Ractor.yield 'from 2'
|
|
|
|
# end
|
2020-03-09 20:22:11 +03:00
|
|
|
#
|
2020-12-19 21:04:40 +03:00
|
|
|
# r, obj = Ractor.select(r1, r2, Ractor.current)
|
|
|
|
# puts "received #{obj.inspect} from #{r.inspect}"
|
2023-01-29 04:01:49 +03:00
|
|
|
# # Could print: received "to main" from :receive
|
2020-03-09 20:22:11 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# If +yield_value+ is provided, that value may be yielded if another ractor is calling #take.
|
|
|
|
# In this case, the pair <tt>[:yield, nil]</tt> is returned:
|
2020-03-09 20:22:11 +03:00
|
|
|
#
|
2020-12-19 21:04:40 +03:00
|
|
|
# r1 = Ractor.new(Ractor.current) do |main|
|
|
|
|
# puts "Received from main: #{main.take}"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# puts "Trying to select"
|
|
|
|
# r, obj = Ractor.select(r1, Ractor.current, yield_value: 123)
|
|
|
|
# wait
|
|
|
|
# puts "Received #{obj.inspect} from #{r.inspect}"
|
|
|
|
#
|
|
|
|
# This will print:
|
|
|
|
#
|
|
|
|
# Trying to select
|
|
|
|
# Received from main: 123
|
|
|
|
# Received nil from :yield
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# +move+ boolean flag defines whether yielded value will be copied (default) or moved.
|
2020-10-03 15:05:15 +03:00
|
|
|
def self.select(*ractors, yield_value: yield_unspecified = true, move: false)
|
2020-12-05 08:04:48 +03:00
|
|
|
raise ArgumentError, 'specify at least one ractor or `yield_value`' if yield_unspecified && ractors.empty?
|
|
|
|
|
2023-02-24 12:46:17 +03:00
|
|
|
begin
|
|
|
|
if ractors.delete Ractor.current
|
|
|
|
do_receive = true
|
|
|
|
else
|
|
|
|
do_receive = false
|
|
|
|
end
|
|
|
|
selector = Ractor::Selector.new(*ractors)
|
|
|
|
|
|
|
|
if yield_unspecified
|
|
|
|
selector.wait receive: do_receive
|
|
|
|
else
|
|
|
|
selector.wait receive: do_receive, yield_value: yield_value, move: move
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
selector.clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Ractor::Selector provides a functionality to wait multiple Ractor events.
|
|
|
|
# Ractor::Selector#wait is more lightweight than Ractor.select()
|
|
|
|
# because we don't have to specify all target ractors for each wait time.
|
|
|
|
#
|
|
|
|
# Ractor.select() uses Ractor::Selector internally to implement it.
|
|
|
|
#
|
|
|
|
class Selector
|
|
|
|
# call-seq:
|
|
|
|
# Ractor::Selector.new(*ractors)
|
|
|
|
#
|
|
|
|
# Creates a selector object.
|
|
|
|
#
|
|
|
|
# If a ractors parameter is given, it is same as the following code.
|
|
|
|
#
|
|
|
|
# selector = Ractor::Selector.new
|
|
|
|
# ractors.each{|r| selector.add r}
|
|
|
|
#
|
|
|
|
def self.new(*rs)
|
|
|
|
selector = __builtin_cexpr! %q{
|
|
|
|
ractor_selector_create(self);
|
|
|
|
}
|
|
|
|
rs.each{|r| selector.add(r) }
|
|
|
|
selector
|
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
|
|
|
# selector.add(ractor)
|
|
|
|
#
|
|
|
|
# Registers a ractor as a taking target by the selector.
|
|
|
|
#
|
|
|
|
def add r
|
|
|
|
__builtin_ractor_selector_add r
|
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
|
|
|
# selector.remove(ractor)
|
|
|
|
#
|
|
|
|
# Deregisters a ractor as a taking target by the selector.
|
|
|
|
#
|
|
|
|
def remove r
|
|
|
|
__builtin_ractor_selector_remove r
|
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
|
|
|
# selector.clear
|
|
|
|
#
|
|
|
|
# Deregisters all ractors.
|
|
|
|
def clear
|
|
|
|
__builtin_ractor_selector_clear
|
|
|
|
end
|
|
|
|
|
2023-03-02 12:27:44 +03:00
|
|
|
# call-seq:
|
|
|
|
# selector.empty?
|
|
|
|
#
|
|
|
|
# Returns true if the number of ractors in the waiting set at the current time is zero.
|
|
|
|
#
|
|
|
|
# Note that even if <tt>#empty?</tt> returns false, the subsequent <tt>#wait</tt>
|
|
|
|
# may raise an exception because other ractors may close the target ractors.
|
|
|
|
#
|
|
|
|
def empty?
|
|
|
|
__builtin_ractor_selector_empty_p
|
|
|
|
end
|
|
|
|
|
2023-02-24 12:46:17 +03:00
|
|
|
# call-seq:
|
|
|
|
# selector.wait(receive: false, yield_value: yield_value, move: false) -> [ractor or symbol, value]
|
|
|
|
#
|
|
|
|
# Waits Ractor events. It is lighter than Ractor.select() for many ractors.
|
|
|
|
#
|
|
|
|
# The simplest form is waiting for taking a value from one of
|
|
|
|
# registerred ractors like that.
|
|
|
|
#
|
|
|
|
# selector = Ractor::Selector.new(r1, r2, r3)
|
|
|
|
# r, v = selector.wait
|
|
|
|
#
|
|
|
|
# On this case, when r1, r2 or r3 is ready to take (yielding a value),
|
|
|
|
# this method takes the value from the ready (yielded) ractor
|
|
|
|
# and returns [the yielded ractor, the taking value].
|
|
|
|
#
|
|
|
|
# Note that if a take target ractor is closed, the ractor will be removed
|
|
|
|
# automatically.
|
|
|
|
#
|
|
|
|
# If you also want to wait with receiving an object from other ractors,
|
|
|
|
# you can specify receive: true keyword like:
|
|
|
|
#
|
|
|
|
# r, v = selector.wait receive: true
|
|
|
|
#
|
|
|
|
# On this case, wait for taking from r1, r2 or r3 and waiting for receving
|
|
|
|
# a value from other ractors.
|
|
|
|
# If it successes the receiving, it returns an array object [:receive, the received value].
|
|
|
|
#
|
|
|
|
# If you also want to wait with yielding a value, you can specify
|
|
|
|
# :yield_value like:
|
|
|
|
#
|
|
|
|
# r, v = selector.wait yield_value: obj
|
|
|
|
#
|
|
|
|
# On this case wait for taking from r1, r2, or r3 and waiting for taking
|
|
|
|
# yielding value (obj) by another ractor.
|
|
|
|
# If antoher ractor takes the value (obj), it returns an array object [:yield, nil].
|
|
|
|
#
|
|
|
|
# You can specify a keyword parameter <tt>move: true</tt> like Ractor.yield(obj, move: true)
|
|
|
|
#
|
|
|
|
def wait receive: false, yield_value: yield_unspecified = true, move: false
|
|
|
|
__builtin_ractor_selector_wait receive, !yield_unspecified, yield_value, move
|
|
|
|
end
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
|
|
|
|
2020-12-24 10:10:10 +03:00
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# Ractor.receive -> msg
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Receive a message from the incoming port of the current ractor (which was
|
|
|
|
# sent there by #send from another ractor).
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new do
|
|
|
|
# v1 = Ractor.receive
|
|
|
|
# puts "Received: #{v1}"
|
|
|
|
# end
|
|
|
|
# r.send('message1')
|
|
|
|
# r.take
|
|
|
|
# # Here will be printed: "Received: message1"
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Alternatively, the private instance method +receive+ may be used:
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new do
|
|
|
|
# v1 = receive
|
|
|
|
# puts "Received: #{v1}"
|
|
|
|
# end
|
|
|
|
# r.send('message1')
|
|
|
|
# r.take
|
2023-01-29 04:01:49 +03:00
|
|
|
# # This prints: "Received: message1"
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# The method blocks if the queue is empty.
|
|
|
|
#
|
|
|
|
# r = Ractor.new do
|
|
|
|
# puts "Before first receive"
|
|
|
|
# v1 = Ractor.receive
|
|
|
|
# puts "Received: #{v1}"
|
|
|
|
# v2 = Ractor.receive
|
|
|
|
# puts "Received: #{v2}"
|
|
|
|
# end
|
|
|
|
# wait
|
|
|
|
# puts "Still not received"
|
|
|
|
# r.send('message1')
|
|
|
|
# wait
|
|
|
|
# puts "Still received only one"
|
|
|
|
# r.send('message2')
|
|
|
|
# r.take
|
|
|
|
#
|
|
|
|
# Output:
|
|
|
|
#
|
|
|
|
# Before first receive
|
|
|
|
# Still not received
|
|
|
|
# Received: message1
|
|
|
|
# Still received only one
|
|
|
|
# Received: message2
|
|
|
|
#
|
2020-12-21 19:05:52 +03:00
|
|
|
# If close_incoming was called on the ractor, the method raises Ractor::ClosedError
|
2023-01-29 04:01:49 +03:00
|
|
|
# if there are no more messages in the incoming queue:
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# Ractor.new do
|
|
|
|
# close_incoming
|
|
|
|
# receive
|
|
|
|
# end
|
|
|
|
# wait
|
|
|
|
# # in `receive': The incoming port is already closed => #<Ractor:#2 test.rb:1 running> (Ractor::ClosedError)
|
|
|
|
#
|
2020-10-03 15:05:15 +03:00
|
|
|
def self.receive
|
2020-03-09 20:22:11 +03:00
|
|
|
__builtin_cexpr! %q{
|
2020-10-03 15:05:15 +03:00
|
|
|
ractor_receive(ec, rb_ec_ractor_ptr(ec))
|
2020-12-20 09:13:07 +03:00
|
|
|
}
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
|
|
|
|
2020-10-03 15:05:15 +03:00
|
|
|
class << self
|
|
|
|
alias recv receive
|
|
|
|
end
|
|
|
|
|
2020-12-16 11:18:07 +03:00
|
|
|
# same as Ractor.receive
|
2020-10-03 15:05:15 +03:00
|
|
|
private def receive
|
2020-03-09 20:22:11 +03:00
|
|
|
__builtin_cexpr! %q{
|
2020-12-16 11:18:07 +03:00
|
|
|
ractor_receive(ec, rb_ec_ractor_ptr(ec))
|
2020-12-20 09:13:07 +03:00
|
|
|
}
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
2020-10-03 15:05:15 +03:00
|
|
|
alias recv receive
|
2020-03-09 20:22:11 +03:00
|
|
|
|
2020-12-24 10:10:10 +03:00
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# Ractor.receive_if {|msg| block } -> msg
|
|
|
|
#
|
2020-12-08 08:04:18 +03:00
|
|
|
# Receive only a specific message.
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Instead of Ractor.receive, Ractor.receive_if can be given a pattern (or any
|
|
|
|
# filter) in a block and you can choose the messages to accept that are available in
|
|
|
|
# your ractor's incoming queue.
|
2020-12-08 08:04:18 +03:00
|
|
|
#
|
2020-12-19 21:04:40 +03:00
|
|
|
# r = Ractor.new do
|
|
|
|
# p Ractor.receive_if{|msg| msg.match?(/foo/)} #=> "foo3"
|
|
|
|
# p Ractor.receive_if{|msg| msg.match?(/bar/)} #=> "bar1"
|
|
|
|
# p Ractor.receive_if{|msg| msg.match?(/baz/)} #=> "baz2"
|
|
|
|
# end
|
|
|
|
# r << "bar1"
|
|
|
|
# r << "baz2"
|
|
|
|
# r << "foo3"
|
|
|
|
# r.take
|
|
|
|
#
|
|
|
|
# This will output:
|
|
|
|
#
|
|
|
|
# foo3
|
|
|
|
# bar1
|
|
|
|
# baz2
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# If the block returns a truthy value, the message is removed from the incoming queue
|
2020-12-24 10:10:10 +03:00
|
|
|
# and returned.
|
2023-01-29 04:01:49 +03:00
|
|
|
# Otherwise, the message remains in the incoming queue and the next messages are checked
|
|
|
|
# by the given block.
|
2020-12-24 10:10:10 +03:00
|
|
|
#
|
|
|
|
# If there are no messages left in the incoming queue, the method will
|
|
|
|
# block until new messages arrive.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
2020-12-24 10:10:10 +03:00
|
|
|
# If the block is escaped by break/return/exception/throw, the message is removed from
|
|
|
|
# the incoming queue as if a truthy value had been returned.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new do
|
|
|
|
# val = Ractor.receive_if{|msg| msg.is_a?(Array)}
|
|
|
|
# puts "Received successfully: #{val}"
|
|
|
|
# end
|
2020-12-08 08:04:18 +03:00
|
|
|
#
|
2020-12-19 21:04:40 +03:00
|
|
|
# r.send(1)
|
|
|
|
# r.send('test')
|
|
|
|
# wait
|
|
|
|
# puts "2 non-matching sent, nothing received"
|
|
|
|
# r.send([1, 2, 3])
|
|
|
|
# wait
|
|
|
|
#
|
|
|
|
# Prints:
|
|
|
|
#
|
|
|
|
# 2 non-matching sent, nothing received
|
|
|
|
# Received successfully: [1, 2, 3]
|
2020-12-08 08:04:18 +03:00
|
|
|
#
|
|
|
|
# Note that you can not call receive/receive_if in the given block recursively.
|
2023-01-29 04:01:49 +03:00
|
|
|
# You should not do any tasks in the block other than message filtration.
|
2020-12-08 08:04:18 +03:00
|
|
|
#
|
2020-12-19 21:04:40 +03:00
|
|
|
# Ractor.current << true
|
|
|
|
# Ractor.receive_if{|msg| Ractor.receive}
|
|
|
|
# #=> `receive': can not call receive/receive_if recursively (Ractor::Error)
|
2020-12-08 08:04:18 +03:00
|
|
|
#
|
|
|
|
def self.receive_if &b
|
|
|
|
Primitive.ractor_receive_if b
|
|
|
|
end
|
|
|
|
|
2023-01-29 04:01:49 +03:00
|
|
|
# same as Ractor.receive_if
|
2020-12-16 17:57:35 +03:00
|
|
|
private def receive_if &b
|
2020-12-08 08:04:18 +03:00
|
|
|
Primitive.ractor_receive_if b
|
|
|
|
end
|
|
|
|
|
2020-12-24 10:10:10 +03:00
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# ractor.send(msg, move: false) -> self
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Send a message to a Ractor's incoming queue to be accepted by Ractor.receive.
|
2020-03-09 20:22:11 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new do
|
2020-12-19 21:04:40 +03:00
|
|
|
# value = Ractor.receive
|
|
|
|
# puts "Received #{value}"
|
2020-03-09 20:22:11 +03:00
|
|
|
# end
|
2020-12-19 21:04:40 +03:00
|
|
|
# r.send 'message'
|
|
|
|
# # Prints: "Received: message"
|
|
|
|
#
|
|
|
|
# The method is non-blocking (will return immediately even if the ractor is not ready
|
|
|
|
# to receive anything):
|
|
|
|
#
|
|
|
|
# r = Ractor.new {sleep(5)}
|
|
|
|
# r.send('test')
|
|
|
|
# puts "Sent successfully"
|
|
|
|
# # Prints: "Sent successfully" immediately
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# An attempt to send to a ractor which already finished its execution will raise Ractor::ClosedError.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new {}
|
|
|
|
# r.take
|
|
|
|
# p r
|
|
|
|
# # "#<Ractor:#6 (irb):23 terminated>"
|
|
|
|
# r.send('test')
|
|
|
|
# # Ractor::ClosedError (The incoming-port is already closed)
|
|
|
|
#
|
|
|
|
# If close_incoming was called on the ractor, the method also raises Ractor::ClosedError.
|
|
|
|
#
|
|
|
|
# r = Ractor.new do
|
|
|
|
# sleep(500)
|
|
|
|
# receive
|
|
|
|
# end
|
|
|
|
# r.close_incoming
|
|
|
|
# r.send('test')
|
|
|
|
# # Ractor::ClosedError (The incoming-port is already closed)
|
2023-01-29 04:01:49 +03:00
|
|
|
# # The error is raised immediately, not when the ractor tries to receive
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# If the +obj+ is unshareable, by default it will be copied into the receiving ractor by deep cloning.
|
|
|
|
# If <tt>move: true</tt> is passed, the object is _moved_ into the receiving ractor and becomes
|
|
|
|
# inaccessible to the sender.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new {puts "Received: #{receive}"}
|
|
|
|
# msg = 'message'
|
|
|
|
# r.send(msg, move: true)
|
|
|
|
# r.take
|
|
|
|
# p msg
|
|
|
|
#
|
|
|
|
# This prints:
|
|
|
|
#
|
|
|
|
# Received: message
|
|
|
|
# in `p': undefined method `inspect' for #<Ractor::MovedObject:0x000055c99b9b69b8>
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# All references to the object and its parts will become invalid to the sender.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new {puts "Received: #{receive}"}
|
|
|
|
# s = 'message'
|
|
|
|
# ary = [s]
|
|
|
|
# copy = ary.dup
|
|
|
|
# r.send(ary, move: true)
|
|
|
|
#
|
|
|
|
# s.inspect
|
|
|
|
# # Ractor::MovedError (can not send any methods to a moved object)
|
|
|
|
# ary.class
|
|
|
|
# # Ractor::MovedError (can not send any methods to a moved object)
|
|
|
|
# copy.class
|
|
|
|
# # => Array, it is different object
|
|
|
|
# copy[0].inspect
|
|
|
|
# # Ractor::MovedError (can not send any methods to a moved object)
|
|
|
|
# # ...but its item was still a reference to `s`, which was moved
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# If the object is shareable, <tt>move: true</tt> has no effect on it:
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new {puts "Received: #{receive}"}
|
|
|
|
# s = 'message'.freeze
|
|
|
|
# r.send(s, move: true)
|
|
|
|
# s.inspect #=> "message", still available
|
|
|
|
#
|
2020-10-03 15:05:15 +03:00
|
|
|
def send(obj, move: false)
|
2020-03-09 20:22:11 +03:00
|
|
|
__builtin_cexpr! %q{
|
|
|
|
ractor_send(ec, RACTOR_PTR(self), obj, move)
|
2020-12-20 09:13:07 +03:00
|
|
|
}
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
2020-10-03 15:05:15 +03:00
|
|
|
alias << send
|
2020-03-09 20:22:11 +03:00
|
|
|
|
2020-12-24 10:10:10 +03:00
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# Ractor.yield(msg, move: false) -> nil
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Send a message to the current ractor's outgoing port to be accepted by #take.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new {Ractor.yield 'Hello from ractor'}
|
|
|
|
# puts r.take
|
|
|
|
# # Prints: "Hello from ractor"
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# This method is blocking, and will return only when somebody consumes the
|
2020-12-19 21:04:40 +03:00
|
|
|
# sent message.
|
|
|
|
#
|
|
|
|
# r = Ractor.new do
|
|
|
|
# Ractor.yield 'Hello from ractor'
|
|
|
|
# puts "Ractor: after yield"
|
|
|
|
# end
|
|
|
|
# wait
|
|
|
|
# puts "Still not taken"
|
|
|
|
# puts r.take
|
|
|
|
#
|
|
|
|
# This will print:
|
|
|
|
#
|
|
|
|
# Still not taken
|
|
|
|
# Hello from ractor
|
|
|
|
# Ractor: after yield
|
|
|
|
#
|
|
|
|
# If the outgoing port was closed with #close_outgoing, the method will raise:
|
|
|
|
#
|
|
|
|
# r = Ractor.new do
|
|
|
|
# close_outgoing
|
|
|
|
# Ractor.yield 'Hello from ractor'
|
|
|
|
# end
|
|
|
|
# wait
|
|
|
|
# # `yield': The outgoing-port is already closed (Ractor::ClosedError)
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# The meaning of the +move+ argument is the same as for #send.
|
2020-10-03 15:05:15 +03:00
|
|
|
def self.yield(obj, move: false)
|
2020-03-09 20:22:11 +03:00
|
|
|
__builtin_cexpr! %q{
|
|
|
|
ractor_yield(ec, rb_ec_ractor_ptr(ec), obj, move)
|
2020-12-20 09:13:07 +03:00
|
|
|
}
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
|
|
|
|
2020-12-24 10:10:10 +03:00
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# ractor.take -> msg
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Get a message from the ractor's outgoing port, which was put there by Ractor.yield or at ractor's
|
|
|
|
# termination.
|
2020-03-09 20:22:11 +03:00
|
|
|
#
|
2020-12-19 21:04:40 +03:00
|
|
|
# r = Ractor.new do
|
|
|
|
# Ractor.yield 'explicit yield'
|
|
|
|
# 'last value'
|
|
|
|
# end
|
|
|
|
# puts r.take #=> 'explicit yield'
|
|
|
|
# puts r.take #=> 'last value'
|
|
|
|
# puts r.take # Ractor::ClosedError (The outgoing-port is already closed)
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# The fact that the last value is also sent to the outgoing port means that +take+ can be used
|
|
|
|
# as an analog of Thread#join ("just wait until ractor finishes"). However, it will raise if
|
|
|
|
# somebody has already consumed that message.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# If the outgoing port was closed with #close_outgoing, the method will raise Ractor::ClosedError.
|
|
|
|
#
|
|
|
|
# r = Ractor.new do
|
|
|
|
# sleep(500)
|
|
|
|
# Ractor.yield 'Hello from ractor'
|
|
|
|
# end
|
|
|
|
# r.close_outgoing
|
|
|
|
# r.take
|
|
|
|
# # Ractor::ClosedError (The outgoing-port is already closed)
|
|
|
|
# # The error would be raised immediately, not when ractor will try to receive
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# If an uncaught exception is raised in the Ractor, it is propagated by take as a
|
2020-12-19 21:04:40 +03:00
|
|
|
# Ractor::RemoteError.
|
|
|
|
#
|
|
|
|
# r = Ractor.new {raise "Something weird happened"}
|
|
|
|
#
|
|
|
|
# begin
|
|
|
|
# r.take
|
|
|
|
# rescue => e
|
|
|
|
# p e # => #<Ractor::RemoteError: thrown by remote Ractor.>
|
|
|
|
# p e.ractor == r # => true
|
|
|
|
# p e.cause # => #<RuntimeError: Something weird happened>
|
|
|
|
# end
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Ractor::ClosedError is a descendant of StopIteration, so the termination of the ractor will break
|
|
|
|
# out of any loops that receive this message without propagating the error:
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new do
|
|
|
|
# 3.times {|i| Ractor.yield "message #{i}"}
|
|
|
|
# "finishing"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# loop {puts "Received: " + r.take}
|
|
|
|
# puts "Continue successfully"
|
|
|
|
#
|
|
|
|
# This will print:
|
|
|
|
#
|
|
|
|
# Received: message 0
|
|
|
|
# Received: message 1
|
|
|
|
# Received: message 2
|
|
|
|
# Received: finishing
|
|
|
|
# Continue successfully
|
2020-12-19 22:51:00 +03:00
|
|
|
def take
|
2020-03-09 20:22:11 +03:00
|
|
|
__builtin_cexpr! %q{
|
|
|
|
ractor_take(ec, RACTOR_PTR(self))
|
2020-12-20 09:13:07 +03:00
|
|
|
}
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
|
|
|
|
2020-12-19 19:44:41 +03:00
|
|
|
def inspect
|
|
|
|
loc = __builtin_cexpr! %q{ RACTOR_PTR(self)->loc }
|
|
|
|
name = __builtin_cexpr! %q{ RACTOR_PTR(self)->name }
|
2022-07-27 18:31:47 +03:00
|
|
|
id = __builtin_cexpr! %q{ UINT2NUM(rb_ractor_id(RACTOR_PTR(self))) }
|
2020-09-18 08:15:32 +03:00
|
|
|
status = __builtin_cexpr! %q{
|
|
|
|
rb_str_new2(ractor_status_str(RACTOR_PTR(self)->status_))
|
2020-12-20 09:13:07 +03:00
|
|
|
}
|
2020-09-18 08:15:32 +03:00
|
|
|
"#<Ractor:##{id}#{name ? ' '+name : ''}#{loc ? " " + loc : ''} #{status}>"
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
|
|
|
|
2020-12-21 21:30:40 +03:00
|
|
|
alias to_s inspect
|
|
|
|
|
2020-12-19 21:04:40 +03:00
|
|
|
# The name set in Ractor.new, or +nil+.
|
2020-03-09 20:22:11 +03:00
|
|
|
def name
|
2020-12-19 21:04:40 +03:00
|
|
|
__builtin_cexpr! %q{RACTOR_PTR(self)->name}
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
class RemoteError
|
|
|
|
attr_reader :ractor
|
|
|
|
end
|
|
|
|
|
2020-12-24 10:10:10 +03:00
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# ractor.close_incoming -> true | false
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Closes the incoming port and returns whether it was already closed. All further attempts
|
|
|
|
# to Ractor.receive in the ractor, and #send to the ractor will fail with Ractor::ClosedError.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new {sleep(500)}
|
|
|
|
# r.close_incoming #=> false
|
|
|
|
# r.close_incoming #=> true
|
|
|
|
# r.send('test')
|
|
|
|
# # Ractor::ClosedError (The incoming-port is already closed)
|
2020-03-09 20:22:11 +03:00
|
|
|
def close_incoming
|
|
|
|
__builtin_cexpr! %q{
|
|
|
|
ractor_close_incoming(ec, RACTOR_PTR(self));
|
2020-12-20 09:13:07 +03:00
|
|
|
}
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
|
|
|
|
2020-12-24 10:10:10 +03:00
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# ractor.close_outgoing -> true | false
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# Closes the outgoing port and returns whether it was already closed. All further attempts
|
|
|
|
# to Ractor.yield in the ractor, and #take from the ractor will fail with Ractor::ClosedError.
|
2020-12-19 21:04:40 +03:00
|
|
|
#
|
|
|
|
# r = Ractor.new {sleep(500)}
|
|
|
|
# r.close_outgoing #=> false
|
|
|
|
# r.close_outgoing #=> true
|
|
|
|
# r.take
|
|
|
|
# # Ractor::ClosedError (The outgoing-port is already closed)
|
2020-03-09 20:22:11 +03:00
|
|
|
def close_outgoing
|
|
|
|
__builtin_cexpr! %q{
|
|
|
|
ractor_close_outgoing(ec, RACTOR_PTR(self));
|
2020-12-20 09:13:07 +03:00
|
|
|
}
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
|
|
|
|
2020-12-24 10:10:10 +03:00
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# Ractor.shareable?(obj) -> true | false
|
|
|
|
#
|
2020-12-19 21:04:40 +03:00
|
|
|
# Checks if the object is shareable by ractors.
|
|
|
|
#
|
|
|
|
# Ractor.shareable?(1) #=> true -- numbers and other immutable basic values are frozen
|
2023-01-29 04:01:49 +03:00
|
|
|
# Ractor.shareable?('foo') #=> false, unless the string is frozen due to # frozen_string_literal: true
|
2020-12-19 21:04:40 +03:00
|
|
|
# Ractor.shareable?('foo'.freeze) #=> true
|
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# See also the "Shareable and unshareable objects" section in the \Ractor class docs.
|
2020-10-20 05:21:49 +03:00
|
|
|
def self.shareable? obj
|
|
|
|
__builtin_cexpr! %q{
|
2021-08-31 14:30:35 +03:00
|
|
|
RBOOL(rb_ractor_shareable_p(obj));
|
2020-12-20 09:13:07 +03:00
|
|
|
}
|
2020-10-20 05:21:49 +03:00
|
|
|
end
|
Ractor.make_shareable(obj)
Introduce new method Ractor.make_shareable(obj) which tries to make
obj shareable object. Protocol is here.
(1) If obj is shareable, it is shareable.
(2) If obj is not a shareable object and if obj can be shareable
object if it is frozen, then freeze obj. If obj has reachable
objects (rs), do rs.each{|o| Ractor.make_shareable(o)}
recursively (recursion is not Ruby-level, but C-level).
(3) Otherwise, raise Ractor::Error. Now T_DATA is not a shareable
object even if the object is frozen.
If the method finished without error, given obj is marked as
a sharable object.
To allow makng a shareable frozen T_DATA object, then set
`RUBY_TYPED_FROZEN_SHAREABLE` as type->flags. On default,
this flag is not set. It means user defined T_DATA objects are
not allowed to become shareable objects when it is frozen.
You can make any object shareable by setting FL_SHAREABLE flag,
so if you know that the T_DATA object is shareable (== thread-safe),
set this flag, at creation time for example. `Ractor` object is one
example, which is not a frozen, but a shareable object.
2020-10-20 18:54:03 +03:00
|
|
|
|
2020-12-18 23:52:18 +03:00
|
|
|
#
|
2020-12-24 10:10:10 +03:00
|
|
|
# call-seq:
|
|
|
|
# Ractor.make_shareable(obj, copy: false) -> shareable_obj
|
|
|
|
#
|
|
|
|
# Make +obj+ shareable between ractors.
|
2020-12-19 09:34:26 +03:00
|
|
|
#
|
2020-12-24 10:10:10 +03:00
|
|
|
# +obj+ and all the objects it refers to will be frozen, unless they are
|
|
|
|
# already shareable.
|
2020-12-18 23:52:18 +03:00
|
|
|
#
|
2023-01-29 04:01:49 +03:00
|
|
|
# If +copy+ keyword is +true+, it will copy objects before freezing them, and will not
|
|
|
|
# modify +obj+ or its internal objects.
|
2020-12-18 23:52:18 +03:00
|
|
|
#
|
|
|
|
# Note that the specification and implementation of this method are not
|
2020-12-24 10:10:10 +03:00
|
|
|
# mature and may be changed in the future.
|
2020-12-18 23:52:18 +03:00
|
|
|
#
|
2020-12-19 21:04:40 +03:00
|
|
|
# obj = ['test']
|
|
|
|
# Ractor.shareable?(obj) #=> false
|
|
|
|
# Ractor.make_shareable(obj) #=> ["test"]
|
|
|
|
# Ractor.shareable?(obj) #=> true
|
|
|
|
# obj.frozen? #=> true
|
|
|
|
# obj[0].frozen? #=> true
|
|
|
|
#
|
|
|
|
# # Copy vs non-copy versions:
|
|
|
|
# obj1 = ['test']
|
|
|
|
# obj1s = Ractor.make_shareable(obj1)
|
|
|
|
# obj1.frozen? #=> true
|
|
|
|
# obj1s.object_id == obj1.object_id #=> true
|
|
|
|
# obj2 = ['test']
|
|
|
|
# obj2s = Ractor.make_shareable(obj2, copy: true)
|
|
|
|
# obj2.frozen? #=> false
|
|
|
|
# obj2s.frozen? #=> true
|
2020-12-22 02:08:27 +03:00
|
|
|
# obj2s.object_id == obj2.object_id #=> false
|
2020-12-19 21:04:40 +03:00
|
|
|
# obj2s[0].object_id == obj2[0].object_id #=> false
|
|
|
|
#
|
|
|
|
# See also the "Shareable and unshareable objects" section in the Ractor class docs.
|
2020-12-18 23:52:18 +03:00
|
|
|
def self.make_shareable obj, copy: false
|
|
|
|
if copy
|
|
|
|
__builtin_cexpr! %q{
|
2020-12-20 20:20:57 +03:00
|
|
|
rb_ractor_make_shareable_copy(obj);
|
2020-12-18 23:52:18 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
__builtin_cexpr! %q{
|
|
|
|
rb_ractor_make_shareable(obj);
|
|
|
|
}
|
|
|
|
end
|
Ractor.make_shareable(obj)
Introduce new method Ractor.make_shareable(obj) which tries to make
obj shareable object. Protocol is here.
(1) If obj is shareable, it is shareable.
(2) If obj is not a shareable object and if obj can be shareable
object if it is frozen, then freeze obj. If obj has reachable
objects (rs), do rs.each{|o| Ractor.make_shareable(o)}
recursively (recursion is not Ruby-level, but C-level).
(3) Otherwise, raise Ractor::Error. Now T_DATA is not a shareable
object even if the object is frozen.
If the method finished without error, given obj is marked as
a sharable object.
To allow makng a shareable frozen T_DATA object, then set
`RUBY_TYPED_FROZEN_SHAREABLE` as type->flags. On default,
this flag is not set. It means user defined T_DATA objects are
not allowed to become shareable objects when it is frozen.
You can make any object shareable by setting FL_SHAREABLE flag,
so if you know that the T_DATA object is shareable (== thread-safe),
set this flag, at creation time for example. `Ractor` object is one
example, which is not a frozen, but a shareable object.
2020-10-20 18:54:03 +03:00
|
|
|
end
|
2020-12-21 19:55:15 +03:00
|
|
|
|
|
|
|
# get a value from ractor-local storage
|
|
|
|
def [](sym)
|
|
|
|
Primitive.ractor_local_value(sym)
|
|
|
|
end
|
|
|
|
|
|
|
|
# set a value in ractor-local storage
|
|
|
|
def []=(sym, val)
|
|
|
|
Primitive.ractor_local_value_set(sym, val)
|
|
|
|
end
|
2020-12-21 20:22:51 +03:00
|
|
|
|
|
|
|
# returns main ractor
|
|
|
|
def self.main
|
|
|
|
__builtin_cexpr! %q{
|
|
|
|
rb_ractor_self(GET_VM()->ractor.main_ractor);
|
|
|
|
}
|
|
|
|
end
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|