Skip to content

Commit 40e045c

Browse files
committed
Revert "Consolidated attr_volatile from c-r and thread_safe."
This reverts commit dbb75a1.
1 parent c0c82dd commit 40e045c

File tree

12 files changed

+138
-102
lines changed

12 files changed

+138
-102
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ Thread-safe variables:
9797
* [AtomicFixnum](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/AtomicFixnum.html) A numeric value that can be updated atomically.
9898
* [AtomicReference](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/MutexAtomic.html) An object reference that may be updated atomically.
9999
* [ThreadLocalVar](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/ThreadLocalVar.html) A variable where the value is different for each thread.
100-
* [Volatile](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/Volatile.html)
101-
Provides `volatile` (in the JVM's sense) attribute accessors implemented atop of `Concurrent::AtomicReference`.
102100

103101
#### Java-inspired ThreadPools and Other Executors
104102

lib/concurrent.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
require 'concurrent/timer_task'
2929
require 'concurrent/tuple'
3030
require 'concurrent/tvar'
31-
require 'concurrent/volatile'
3231

3332
# @!macro [new] internal_implementation_note
3433
#

lib/concurrent/synchronization/abstract_object.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
require 'concurrent/volatile'
2-
31
module Concurrent
42
module Synchronization
53

64
# @!macro synchronization_object
75
# @!visibility private
86
class AbstractObject
9-
extend Concurrent::Volatile
107

118
# @!macro [attach] synchronization_object_method_initialize
129
#
1310
# @abstract for helper ivar initialization if needed,
1411
# otherwise it can be left empty. It has to call ns_initialize.
1512
def initialize(*args, &block)
16-
super(&nil)
13+
raise NotImplementedError
1714
end
1815

1916
protected
@@ -141,6 +138,26 @@ def ns_broadcast
141138
def ensure_ivar_visibility!
142139
raise NotImplementedError
143140
end
141+
142+
# @!macro [attach] synchronization_object_method_self_attr_volatile
143+
#
144+
# creates methods for reading and writing to a instance variable with volatile (Java semantic) instance variable
145+
# return [Array<Symbol>] names of defined method names
146+
def self.attr_volatile(*names)
147+
names.each do |name|
148+
ivar = :"@volatile_#{name}"
149+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
150+
def #{name}
151+
#{ivar}
152+
end
153+
154+
def #{name}=(value)
155+
#{ivar} = value
156+
end
157+
RUBY
158+
end
159+
names.map { |n| [n, :"#{n}="] }.flatten
160+
end
144161
end
145162
end
146163
end

lib/concurrent/synchronization/java_object.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ module Synchronization
88
# @!visibility private
99
# @!macro internal_implementation_note
1010
class JavaObject < AbstractObject
11+
12+
def self.attr_volatile(*names)
13+
names.each do |name|
14+
15+
ivar = :"@volatile_#{name}"
16+
17+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
18+
def #{name}
19+
instance_variable_get_volatile(:#{ivar})
20+
end
21+
22+
def #{name}=(value)
23+
instance_variable_set_volatile(:#{ivar}, value)
24+
end
25+
RUBY
26+
27+
end
28+
names.map { |n| [n, :"#{n}="] }.flatten
29+
end
30+
1131
end
1232
end
1333
end

lib/concurrent/synchronization/object.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Object < Implementation
7676
# @!method ensure_ivar_visibility!
7777
# @!macro synchronization_object_method_ensure_ivar_visibility
7878

79-
# @!method self.attr_volatile(*attr_names)
79+
# @!method self.attr_volatile(*names)
8080
# @!macro synchronization_object_method_self_attr_volatile
8181
end
8282
end

lib/concurrent/synchronization/rbx_object.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ def ensure_ivar_visibility!
6464
# Rubinius instance variables are not volatile so we need to insert barrier
6565
Rubinius.memory_barrier
6666
end
67+
68+
def self.attr_volatile *names
69+
names.each do |name|
70+
ivar = :"@volatile_#{name}"
71+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
72+
def #{name}
73+
Rubinius.memory_barrier
74+
#{ivar}
75+
end
76+
77+
def #{name}=(value)
78+
#{ivar} = value
79+
Rubinius.memory_barrier
80+
end
81+
RUBY
82+
end
83+
names.map { |n| [n, :"#{n}="] }.flatten
84+
end
6785
end
6886
end
6987
end

lib/concurrent/thread_safe/atomic_reference_map_backend.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def delete_node_at(i, node, predecessor_node)
234234
#
235235
# @!visibility private
236236
class Node
237-
extend Concurrent::Volatile
237+
extend Util::Volatile
238238
attr_volatile :hash, :value, :next
239239

240240
include Util::CheapLockable
@@ -354,7 +354,7 @@ def locked_hash?(hash)
354354
# most locking stalls during resizes.
355355
TRANSFER_BUFFER_SIZE = 32
356356

357-
extend Concurrent::Volatile
357+
extend Util::Volatile
358358
attr_volatile :table, # The array of bins. Lazily initialized upon first insertion. Size is always a power of two.
359359

360360
# Table initialization and resizing control. When negative, the

lib/concurrent/thread_safe/util.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ module Util
1111
CPU_COUNT = 16 # is there a way to determine this?
1212

1313
autoload :Tuple, 'concurrent/tuple'
14-
autoload :Volatile, 'concurrent/volatile'
1514
autoload :Adder, 'concurrent/thread_safe/util/adder'
1615
autoload :CheapLockable, 'concurrent/thread_safe/util/cheap_lockable'
1716
autoload :PowerOfTwoTuple, 'concurrent/thread_safe/util/power_of_two_tuple'
17+
autoload :Volatile, 'concurrent/thread_safe/util/volatile'
1818
autoload :Striped64, 'concurrent/thread_safe/util/striped64'
1919
autoload :XorShiftRandom, 'concurrent/thread_safe/util/xor_shift_random'
2020
end

lib/concurrent/thread_safe/util/cheap_lockable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def cheap_broadcast
8585
else
8686
require 'thread'
8787

88-
extend Concurrent::Volatile
88+
extend Volatile
8989
attr_volatile :mutex
9090

9191
# Non-reentrant Mutex#syncrhonize

lib/concurrent/thread_safe/util/striped64.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def cas_computed
9292
end
9393
end
9494

95-
extend Concurrent::Volatile
95+
extend Volatile
9696
attr_volatile :cells, # Table of cells. When non-null, size is a power of 2.
9797
:base, # Base value, used mainly when there is no contention, but also as a fallback during table initialization races. Updated via CAS.
9898
:busy # Spinlock (locked via CAS) used when resizing and/or creating Cells.

0 commit comments

Comments
 (0)