Skip to content

Commit dbb75a1

Browse files
committed
Consolidated attr_volatile from c-r and thread_safe.
1 parent 52e5f37 commit dbb75a1

File tree

13 files changed

+102
-138
lines changed

13 files changed

+102
-138
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ Derived from Ruby's [Struct](http://ruby-doc.org/core-2.2.0/Struct.html):
114114
* [Software transactional memory](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/TVar.html) (TVar)
115115
* [ReadWriteLock](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/ReadWriteLock.html)
116116
* [ReentrantReadWriteLock](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/ReentrantReadWriteLock.html)
117+
* [Volatile](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/Volatile.html)
118+
Provides `volatile` (in the JVM's sense) attribute accessors implemented atop of `Concurrent::AtomicReference`.
117119

118120
### Edge Features
119121

lib/concurrent.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
require 'concurrent/settable_struct'
2828
require 'concurrent/timer_task'
2929
require 'concurrent/tvar'
30+
require 'concurrent/volatile'
3031

3132
# @!macro [new] internal_implementation_note
3233
#

lib/concurrent/synchronization/abstract_object.rb

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

46
# @!macro synchronization_object
57
# @!visibility private
68
class AbstractObject
9+
extend Concurrent::Volatile
710

811
# @!macro [attach] synchronization_object_method_initialize
912
#
1013
# @abstract for helper ivar initialization if needed,
1114
# otherwise it can be left empty. It has to call ns_initialize.
1215
def initialize(*args, &block)
13-
raise NotImplementedError
16+
super(&nil)
1417
end
1518

1619
protected
@@ -138,26 +141,6 @@ def ns_broadcast
138141
def ensure_ivar_visibility!
139142
raise NotImplementedError
140143
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
161144
end
162145
end
163146
end

lib/concurrent/synchronization/java_object.rb

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,6 @@ 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-
3111
end
3212
end
3313
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(*names)
79+
# @!method self.attr_volatile(*attr_names)
8080
# @!macro synchronization_object_method_self_attr_volatile
8181
end
8282
end

lib/concurrent/synchronization/rbx_object.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,6 @@ 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
8567
end
8668
end
8769
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 Util::Volatile
237+
extend Concurrent::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 Util::Volatile
357+
extend Concurrent::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
@@ -10,11 +10,11 @@ module Util
1010
MAX_INT = (2 ** FIXNUM_BIT_SIZE) - 1
1111
CPU_COUNT = 16 # is there a way to determine this?
1212

13+
autoload :Volatile, 'concurrent/volatile'
1314
autoload :Adder, 'concurrent/thread_safe/util/adder'
1415
autoload :CheapLockable, 'concurrent/thread_safe/util/cheap_lockable'
1516
autoload :PowerOfTwoTuple, 'concurrent/thread_safe/util/power_of_two_tuple'
1617
autoload :Striped64, 'concurrent/thread_safe/util/striped64'
17-
autoload :Volatile, 'concurrent/thread_safe/util/volatile'
1818
autoload :VolatileTuple, 'concurrent/thread_safe/util/volatile_tuple'
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 Volatile
88+
extend Concurrent::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 Volatile
95+
extend Concurrent::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)