diff --git a/bootstraptest/test_ractor.rb b/bootstraptest/test_ractor.rb index 5bf697a5d0..d9fd7cd731 100644 --- a/bootstraptest/test_ractor.rb +++ b/bootstraptest/test_ractor.rb @@ -1564,6 +1564,24 @@ assert_equal '1', %q{ }.take } +# Ractor-local storage +assert_equal '2', %q{ + Ractor.new { + fails = 0 + begin + Ractor.main[:key] # cannot get ractor local storage from non-main ractor + rescue => e + fails += 1 if e.message =~ /Cannot get ractor local/ + end + begin + Ractor.main[:key] = 'val' + rescue => e + fails += 1 if e.message =~ /Cannot set ractor local/ + end + fails + }.take +} + ### ### Synchronization tests ### diff --git a/ractor.rb b/ractor.rb index b22cb68939..3b649f042f 100644 --- a/ractor.rb +++ b/ractor.rb @@ -835,15 +835,21 @@ class Ractor end end - # get a value from ractor-local storage of current Ractor + # get a value from ractor-local storage for current Ractor # Obsolete and use Ractor.[] instead. def [](sym) + if (self != Ractor.current) + raise RuntimeError, "Cannot get ractor local storage for non-current ractor" + end Primitive.ractor_local_value(sym) end - # set a value in ractor-local storage of current Ractor + # set a value in ractor-local storage for current Ractor # Obsolete and use Ractor.[]= instead. def []=(sym, val) + if (self != Ractor.current) + raise RuntimeError, "Cannot set ractor local storage for non-current ractor" + end Primitive.ractor_local_value_set(sym, val) end