Skip to content

Commit cfef2d4

Browse files
committed
ensure_ivar_visibility! improvements
1 parent 2cb263d commit cfef2d4

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

ext/com/concurrent_ruby/ext/SynchronizationLibrary.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,18 @@ public IRubyObject nsBroadcast(ThreadContext context) {
111111

112112
@JRubyMethod(name = "ensure_ivar_visibility!", visibility = Visibility.PROTECTED)
113113
public IRubyObject ensureIvarVisibilityBang(ThreadContext context) {
114-
if (UnsafeHolder.SUPPORTS_FENCES)
115-
UnsafeHolder.storeFence();
116-
else
117-
anVolatileField = 1;
114+
if (UnsafeHolder.U == null) {
115+
// We are screwed
116+
throw new UnsupportedOperationException();
117+
} else if (UnsafeHolder.SUPPORTS_FENCES)
118+
// We have to prevent ivar writes to reordered with storing of the final instance reference
119+
// Therefore wee need a fullFence to prevent reordering in both directions.
120+
UnsafeHolder.fullFence();
121+
else {
122+
// Assumption that this is not eliminated, if false it will break non x86 platforms.
123+
UnsafeHolder.U.putIntVolatile(this, AN_VOLATILE_FIELD_OFFSET, 1);
124+
UnsafeHolder.U.getIntVolatile(this, AN_VOLATILE_FIELD_OFFSET);
125+
}
118126
return context.nil;
119127
}
120128

0 commit comments

Comments
 (0)