From d0e4ccbefcdd6032d0ae70bc54c9a4fb55d92576 Mon Sep 17 00:00:00 2001 From: Koichi Sasada Date: Tue, 22 Dec 2020 02:22:51 +0900 Subject: [PATCH] add Ractor.main It returns main Ractor, like Thread.main. [Feature #17418] --- bootstraptest/test_ractor.rb | 7 +++++++ ractor.rb | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/bootstraptest/test_ractor.rb b/bootstraptest/test_ractor.rb index 06bd739931..843714a7bc 100644 --- a/bootstraptest/test_ractor.rb +++ b/bootstraptest/test_ractor.rb @@ -396,6 +396,13 @@ assert_equal 'ok', %q{ end } +# Ractor.main returns main ractor +assert_equal 'true', %q{ + Ractor.new{ + Ractor.main + }.take == Ractor.current +} + # a ractor with closed outgoing port should terminate assert_equal 'ok', %q{ Ractor.new do diff --git a/ractor.rb b/ractor.rb index d72e02b60d..0a152425f0 100644 --- a/ractor.rb +++ b/ractor.rb @@ -768,4 +768,11 @@ class Ractor def []=(sym, val) Primitive.ractor_local_value_set(sym, val) end + + # returns main ractor + def self.main + __builtin_cexpr! %q{ + rb_ractor_self(GET_VM()->ractor.main_ractor); + } + end end