Skip to content

Commit 1bd4a48

Browse files
committed
Simplify dependencies in concurrent/synchronization/volatile
1 parent 6ab093c commit 1bd4a48

File tree

8 files changed

+80
-121
lines changed

8 files changed

+80
-121
lines changed

ext/concurrent-ruby/com/concurrent_ruby/ext/SynchronizationLibrary.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ public static IRubyObject fullMemoryBarrier(ThreadContext context, IRubyObject m
149149
return context.nil;
150150
}
151151

152-
@JRubyMethod(name = "instance_variable_get_volatile", visibility = Visibility.PUBLIC)
152+
@JRubyMethod(name = "instance_variable_get_volatile", visibility = Visibility.PUBLIC, module = true)
153153
public static IRubyObject instanceVariableGetVolatile(
154154
ThreadContext context,
155+
IRubyObject module,
155156
IRubyObject self,
156157
IRubyObject name) {
157158
// Ensure we ses latest value with loadFence
@@ -165,9 +166,10 @@ public static IRubyObject instanceVariableGetVolatile(
165166
}
166167
}
167168

168-
@JRubyMethod(name = "instance_variable_set_volatile", visibility = Visibility.PUBLIC)
169+
@JRubyMethod(name = "instance_variable_set_volatile", visibility = Visibility.PUBLIC, module = true)
169170
public static IRubyObject InstanceVariableSetVolatile(
170171
ThreadContext context,
172+
IRubyObject module,
171173
IRubyObject self,
172174
IRubyObject name,
173175
IRubyObject value) {

lib/concurrent-ruby/concurrent/synchronization.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
require 'concurrent/utility/native_extension_loader' # load native parts first
2-
31
require 'concurrent/utility/engine'
42

5-
require 'concurrent/synchronization/abstract_object'
6-
require 'concurrent/synchronization/mri_object'
7-
require 'concurrent/synchronization/jruby_object'
8-
require 'concurrent/synchronization/truffleruby_object'
93
require 'concurrent/synchronization/object'
104
require 'concurrent/synchronization/volatile'
115

lib/concurrent-ruby/concurrent/synchronization/full_memory_barrier.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'concurrent/utility/native_extension_loader' # load native parts first
2+
13
module Concurrent
24
module Synchronization
35
case

lib/concurrent-ruby/concurrent/synchronization/jruby_object.rb

Lines changed: 0 additions & 35 deletions
This file was deleted.

lib/concurrent-ruby/concurrent/synchronization/mri_object.rb

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/concurrent-ruby/concurrent/synchronization/object.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'concurrent/utility/native_extension_loader' # load native parts first
2+
13
require 'concurrent/synchronization/safe_initialization'
24
require 'concurrent/synchronization/volatile'
35
require 'concurrent/atomic/atomic_reference'

lib/concurrent-ruby/concurrent/synchronization/truffleruby_object.rb

Lines changed: 0 additions & 34 deletions
This file was deleted.

lib/concurrent-ruby/concurrent/synchronization/volatile.rb

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require 'concurrent/utility/native_extension_loader' # load native parts first
2+
require 'concurrent/synchronization/full_memory_barrier'
3+
14
module Concurrent
25
module Synchronization
36

@@ -20,22 +23,77 @@ module Synchronization
2023
# foo.bar = 2
2124
# => 2
2225

23-
Volatile = case
24-
when Concurrent.on_cruby?
25-
MriAttrVolatile
26-
when Concurrent.on_jruby?
27-
JRubyAttrVolatile
28-
when Concurrent.on_truffleruby?
29-
TruffleRubyAttrVolatile
30-
else
31-
warn 'Possibly unsupported Ruby implementation'
32-
MriAttrVolatile
33-
end
34-
35-
Volatile.class_exec do
26+
module Volatile
27+
def self.included(base)
28+
base.extend(ClassMethods)
29+
end
30+
3631
def full_memory_barrier
37-
Concurrent::Synchronization.full_memory_barrier
32+
Synchronization.full_memory_barrier
33+
end
34+
35+
module ClassMethods
36+
if Concurrent.on_cruby?
37+
def attr_volatile(*names)
38+
names.each do |name|
39+
ivar = :"@volatile_#{name}"
40+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
41+
def #{name}
42+
#{ivar}
43+
end
44+
45+
def #{name}=(value)
46+
#{ivar} = value
47+
end
48+
RUBY
49+
end
50+
names.map { |n| [n, :"#{n}="] }.flatten
51+
end
52+
53+
elsif Concurrent.on_jruby?
54+
def attr_volatile(*names)
55+
names.each do |name|
56+
ivar = :"@volatile_#{name}"
57+
58+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
59+
def #{name}
60+
::Concurrent::Synchronization::JRubyAttrVolatile.instance_variable_get_volatile(self, :#{ivar})
61+
end
62+
63+
def #{name}=(value)
64+
::Concurrent::Synchronization::JRubyAttrVolatile.instance_variable_set_volatile(self, :#{ivar}, value)
65+
end
66+
RUBY
67+
68+
end
69+
names.map { |n| [n, :"#{n}="] }.flatten
70+
end
71+
72+
else
73+
warn 'Possibly unsupported Ruby implementation' unless Concurrent.on_truffleruby?
74+
75+
def attr_volatile(*names)
76+
names.each do |name|
77+
ivar = :"@volatile_#{name}"
78+
79+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
80+
def #{name}
81+
::Concurrent::Synchronization.full_memory_barrier
82+
#{ivar}
83+
end
84+
85+
def #{name}=(value)
86+
#{ivar} = value
87+
::Concurrent::Synchronization.full_memory_barrier
88+
end
89+
RUBY
90+
end
91+
92+
names.map { |n| [n, :"#{n}="] }.flatten
93+
end
94+
end
3895
end
96+
3997
end
4098
end
4199
end

0 commit comments

Comments
 (0)