66import org .jruby .RubyClass ;
77import org .jruby .RubyModule ;
88import org .jruby .RubyObject ;
9+ import org .jruby .RubyBasicObject ;
910import org .jruby .anno .JRubyClass ;
1011import org .jruby .anno .JRubyMethod ;
1112import org .jruby .runtime .ObjectAllocator ;
@@ -47,6 +48,9 @@ public void load(Ruby runtime, boolean wrap) throws IOException {
4748 defineModule ("Concurrent" ).
4849 defineModuleUnder ("Synchronization" );
4950
51+ RubyModule jrubyAttrVolatileModule = synchronizationModule .defineModuleUnder ("JRubyAttrVolatile" );
52+ jrubyAttrVolatileModule .defineAnnotatedMethods (JRubyAttrVolatile .class );
53+
5054 defineClass (runtime , synchronizationModule , "AbstractObject" , "JRubyObject" ,
5155 JRubyObject .class , JRUBY_OBJECT_ALLOCATOR );
5256
@@ -81,21 +85,12 @@ private RubyClass defineClass(Ruby runtime, RubyModule namespace, String parentN
8185 // SynchronizedVariableAccessor wraps with synchronized block, StampedVariableAccessor uses fullFence or
8286 // volatilePut
8387
84- @ JRubyClass ( name = "JRubyObject" , parent = "AbstractObject" )
85- public static class JRubyObject extends RubyObject {
88+ // module JRubyAttrVolatile
89+ public static class JRubyAttrVolatile {
8690 private static volatile ThreadContext threadContext = null ;
8791
88- public JRubyObject (Ruby runtime , RubyClass metaClass ) {
89- super (runtime , metaClass );
90- }
91-
92- @ JRubyMethod
93- public IRubyObject initialize (ThreadContext context ) {
94- return this ;
95- }
96-
9792 @ JRubyMethod (name = "full_memory_barrier" , visibility = Visibility .PRIVATE )
98- public IRubyObject fullMemoryBarrier (ThreadContext context ) {
93+ public static IRubyObject fullMemoryBarrier (ThreadContext context , IRubyObject self ) {
9994 // Prevent reordering of ivar writes with publication of this instance
10095 if (UnsafeHolder .U == null || !UnsafeHolder .SUPPORTS_FENCES ) {
10196 // Assuming that following volatile read and write is not eliminated it simulates fullFence.
@@ -109,35 +104,43 @@ public IRubyObject fullMemoryBarrier(ThreadContext context) {
109104 }
110105
111106 @ JRubyMethod (name = "instance_variable_get_volatile" , visibility = Visibility .PROTECTED )
112- public IRubyObject instanceVariableGetVolatile (ThreadContext context , IRubyObject name ) {
107+ public static IRubyObject instanceVariableGetVolatile (ThreadContext context , IRubyObject self , IRubyObject name ) {
113108 // Ensure we ses latest value with loadFence
114109 if (UnsafeHolder .U == null || !UnsafeHolder .SUPPORTS_FENCES ) {
115110 // piggybacking on volatile read, simulating loadFence
116111 final ThreadContext oldContext = threadContext ;
117- return instance_variable_get (context , name );
112+ return (( RubyBasicObject ) self ). instance_variable_get (context , name );
118113 } else {
119114 UnsafeHolder .loadFence ();
120- return instance_variable_get (context , name );
115+ return (( RubyBasicObject ) self ). instance_variable_get (context , name );
121116 }
122117 }
123118
124119 @ JRubyMethod (name = "instance_variable_set_volatile" , visibility = Visibility .PROTECTED )
125- public IRubyObject InstanceVariableSetVolatile (ThreadContext context , IRubyObject name , IRubyObject value ) {
120+ public static IRubyObject InstanceVariableSetVolatile (ThreadContext context , IRubyObject self , IRubyObject name , IRubyObject value ) {
126121 // Ensure we make last update visible
127122 if (UnsafeHolder .U == null || !UnsafeHolder .SUPPORTS_FENCES ) {
128123 // piggybacking on volatile write, simulating storeFence
129- final IRubyObject result = instance_variable_set (name , value );
124+ final IRubyObject result = (( RubyBasicObject ) self ). instance_variable_set (name , value );
130125 threadContext = context ;
131126 return result ;
132127 } else {
133128 // JRuby uses StampedVariableAccessor which calls fullFence
134129 // so no additional steps needed.
135130 // See https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/runtime/ivars/StampedVariableAccessor.java#L151-L159
136- return instance_variable_set (name , value );
131+ return (( RubyBasicObject ) self ). instance_variable_set (name , value );
137132 }
138133 }
139134 }
140135
136+ @ JRubyClass (name = "JRubyObject" , parent = "AbstractObject" )
137+ public static class JRubyObject extends RubyObject {
138+
139+ public JRubyObject (Ruby runtime , RubyClass metaClass ) {
140+ super (runtime , metaClass );
141+ }
142+ }
143+
141144 @ JRubyClass (name = "Object" , parent = "JRubyObject" )
142145 public static class Object extends JRubyObject {
143146
0 commit comments