diff options
author | Koichi Sasada <ko1@atdot.net> | 2020-10-30 00:32:53 +0900 |
---|---|---|
committer | Koichi Sasada <ko1@atdot.net> | 2020-10-30 03:12:09 +0900 |
commit | 5d97bdc2dcb835c877010daa033cc2b1dfeb86d6 (patch) | |
tree | 147bbd2e687a9db2bbd5fede670690eae644ebe1 /bootstraptest | |
parent | 502d6d845946f367ffb6e972e9c4ac401da94e99 (diff) |
Ractor.make_shareable(a_proc)
Ractor.make_shareable() supports Proc object if
(1) a Proc only read outer local variables (no assignments)
(2) read outer local variables are shareable.
Read local variables are stored in a snapshot, so after making
shareable Proc, any assignments are not affeect like that:
```ruby
a = 1
pr = Ractor.make_shareable(Proc.new{p a})
pr.call #=> 1
a = 2
pr.call #=> 1 # `a = 2` doesn't affect
```
[Feature #17284]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3722
Diffstat (limited to 'bootstraptest')
-rw-r--r-- | bootstraptest/test_ractor.rb | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/bootstraptest/test_ractor.rb b/bootstraptest/test_ractor.rb index 6e006b246c..a61e741b54 100644 --- a/bootstraptest/test_ractor.rb +++ b/bootstraptest/test_ractor.rb @@ -922,6 +922,33 @@ assert_equal 'true', %q{ [a.frozen?, a[0].frozen?] == [true, false] } +# Ractor.make_shareable(a_proc) makes a proc shareable. +assert_equal 'true', %q{ + a = [1, [2, 3], {a: "4"}] + pr = Proc.new do + a + end + Ractor.make_shareable(a) # referred value should be shareable + Ractor.make_shareable(pr) + Ractor.shareable?(pr) +} + +# Ractor.make_shareable(a_proc) makes a proc shareable. +assert_equal 'can not make a Proc shareable because it accesses outer variables (a).', %q{ + a = b = nil + pr = Proc.new do + c = b # assign to a is okay because c is block local variable + # reading b is okay + a = b # assign to a is not allowed #=> Ractor::Error + end + + begin + Ractor.make_shareable(pr) + rescue => e + e.message + end +} + ### ### Synchronization tests ### |