Skip to content

Commit 9f58449

Browse files
committed
Merge pull request #464 from ruby-concurrency/synchronization
Remove #ensure_ivar_visibility! in favor of .save_initialization!
2 parents de8b4d1 + 9ad8dd7 commit 9f58449

File tree

4 files changed

+8
-30
lines changed

4 files changed

+8
-30
lines changed

ext/com/concurrent_ruby/ext/SynchronizationLibrary.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static class JRubyAttrVolatile {
9393
// attempt to avoid code elimination.
9494
private static volatile ThreadContext threadContext = null;
9595

96-
@JRubyMethod(name = "full_memory_barrier", visibility = Visibility.PRIVATE)
96+
@JRubyMethod(name = "full_memory_barrier", visibility = Visibility.PUBLIC)
9797
public static IRubyObject fullMemoryBarrier(ThreadContext context, IRubyObject self) {
9898
// Prevent reordering of ivar writes with publication of this instance
9999
if (UnsafeHolder.U == null || !UnsafeHolder.SUPPORTS_FENCES) {
@@ -107,7 +107,7 @@ public static IRubyObject fullMemoryBarrier(ThreadContext context, IRubyObject s
107107
return context.nil;
108108
}
109109

110-
@JRubyMethod(name = "instance_variable_get_volatile", visibility = Visibility.PROTECTED)
110+
@JRubyMethod(name = "instance_variable_get_volatile", visibility = Visibility.PUBLIC)
111111
public static IRubyObject instanceVariableGetVolatile(ThreadContext context, IRubyObject self, IRubyObject name) {
112112
// Ensure we ses latest value with loadFence
113113
if (UnsafeHolder.U == null || !UnsafeHolder.SUPPORTS_FENCES) {
@@ -120,7 +120,7 @@ public static IRubyObject instanceVariableGetVolatile(ThreadContext context, IRu
120120
}
121121
}
122122

123-
@JRubyMethod(name = "instance_variable_set_volatile", visibility = Visibility.PROTECTED)
123+
@JRubyMethod(name = "instance_variable_set_volatile", visibility = Visibility.PUBLIC)
124124
public static IRubyObject InstanceVariableSetVolatile(ThreadContext context, IRubyObject self, IRubyObject name, IRubyObject value) {
125125
// Ensure we make last update visible
126126
if (UnsafeHolder.U == null || !UnsafeHolder.SUPPORTS_FENCES) {

lib/concurrent/synchronization/abstract_object.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,6 @@ def initialize
1010
raise NotImplementedError
1111
end
1212

13-
# @!macro [attach] synchronization_object_method_ensure_ivar_visibility
14-
#
15-
# Allows to construct immutable objects where all fields are visible after initialization, not requiring
16-
# further synchronization on access.
17-
# @example
18-
# class AClass
19-
# attr_reader :val
20-
# def initialize(val)
21-
# @val = val # final value, after assignment it's not changed (just convention, not enforced)
22-
# ensure_ivar_visibility!
23-
# # now it can be shared as Java's final field
24-
# end
25-
# end
26-
# @!visibility private
27-
def ensure_ivar_visibility!
28-
# We have to prevent ivar writes to reordered with storing of the final instance reference
29-
# Therefore wee need a fullFence to prevent reordering in both directions.
30-
full_memory_barrier
31-
end
32-
33-
protected
34-
3513
# @!visibility private
3614
# @abstract
3715
def full_memory_barrier

lib/concurrent/synchronization/object.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def self.safe_initialization!
5252
def self.new(*)
5353
object = super
5454
ensure
55-
object.ensure_ivar_visibility! if object
55+
object.full_memory_barrier if object
5656
end
5757

5858
@safe_initialization = true

spec/concurrent/synchronization_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ class ADClass < ACClass
4949
end
5050

5151
it 'does not ensure visibility when not needed' do
52-
expect_any_instance_of(AAClass).not_to receive(:ensure_ivar_visibility!)
52+
expect_any_instance_of(AAClass).not_to receive(:full_memory_barrier)
5353
AAClass.new
5454
end
5555

5656
it "does ensure visibility when specified" do
57-
expect_any_instance_of(ABClass).to receive(:ensure_ivar_visibility!)
57+
expect_any_instance_of(ABClass).to receive(:full_memory_barrier)
5858
ABClass.new
5959
end
6060

6161
it "does ensure visibility when specified in a parent" do
62-
expect_any_instance_of(ACClass).to receive(:ensure_ivar_visibility!)
62+
expect_any_instance_of(ACClass).to receive(:full_memory_barrier)
6363
ACClass.new
6464
end
6565

6666
it "does ensure visibility once when specified in child again" do
67-
expect_any_instance_of(ADClass).to receive(:ensure_ivar_visibility!)
67+
expect_any_instance_of(ADClass).to receive(:full_memory_barrier)
6868
ADClass.new
6969
end
7070

0 commit comments

Comments
 (0)