ruby-changes:63412
From: Koichi <ko1@a...>
Date: Thu, 22 Oct 2020 00:44:22 +0900 (JST)
Subject: [ruby-changes:63412] 0c0d0752f1 (master): allow to access ivars of frozen shareable objects
https://git.ruby-lang.org/ruby.git/commit/?id=0c0d0752f1 From 0c0d0752f1fefd9b085f191b62946a811763ef1b Mon Sep 17 00:00:00 2001 From: Koichi Sasada <ko1@a...> Date: Thu, 22 Oct 2020 00:36:53 +0900 Subject: allow to access ivars of frozen shareable objects Accessing a shareable object is prohibitted because it can cause race condition, but if the shareable object is frozen, there is no problem to access ivars. diff --git a/bootstraptest/test_ractor.rb b/bootstraptest/test_ractor.rb index 6290b73..62b0b52 100644 --- a/bootstraptest/test_ractor.rb +++ b/bootstraptest/test_ractor.rb @@ -734,6 +734,18 @@ assert_equal 'can not access instance variables of shareable objects from non-ma https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_ractor.rb#L734 end } +# But a sharable object is frozen, it is allowed to access ivars from non-main Ractor +assert_equal '11', %q{ + [Object.new, [], ].map{|obj| + obj.instance_variable_set('@a', 1) + Ractor.make_shareable obj = obj.freeze + + Ractor.new obj do |obj| + obj.instance_variable_get('@a') + end.take.to_s + }.join +} + # cvar in sharable-objects are not allowed to access from non-main Ractor assert_equal 'can not access class variables from non-main Ractors', %q{ class C diff --git a/variable.c b/variable.c index 795ee3f..8ac40ef 100644 --- a/variable.c +++ b/variable.c @@ -922,7 +922,8 @@ generic_ivtbl(VALUE obj, ID id, bool force_check_ractor) https://github.com/ruby/ruby/blob/trunk/variable.c#L922 { ASSERT_vm_locking(); - if ((force_check_ractor || rb_is_instance_id(id)) && // not internal ID + if ((force_check_ractor || LIKELY(rb_is_instance_id(id)) /* not internal ID */ ) && + !RB_OBJ_FROZEN_RAW(obj) && UNLIKELY(!rb_ractor_main_p()) && UNLIKELY(rb_ractor_shareable_p(obj))) { rb_raise(rb_eRuntimeError, "can not access instance variables of shareable objects from non-main Ractors"); -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/