11require 'concurrent/atomic/atomic_reference'
22require 'concurrent/collection/copy_on_notify_observer_set'
33require 'concurrent/concern/observable'
4- require 'concurrent/synchronization'
54
65# @!macro [new] thread_safe_variable_comparison
76#
@@ -91,13 +90,9 @@ module Concurrent
9190 #
9291 # @see http://clojure.org/atoms Clojure Atoms
9392 # @see http://clojure.org/state Values and Change - Clojure's approach to Identity and State
94- class Atom < Synchronization :: Object
93+ class Atom
9594 include Concern ::Observable
9695
97- safe_initialization!
98- private ( *attr_volatile_with_cas ( :value ) )
99- public :value
100-
10196 # Create a new atom with the given initial value.
10297 #
10398 # @param [Object] value The initial value
@@ -111,15 +106,18 @@ class Atom < Synchronization::Object
111106 #
112107 # @raise [ArgumentError] if the validator is not a `Proc` (when given)
113108 def initialize ( value , opts = { } )
114- @Validator = opts . fetch ( :validator , -> v { true } )
109+ @validator = opts . fetch ( :validator , -> v { true } )
115110 self . observers = Collection ::CopyOnNotifyObserverSet . new
116- super ( value )
111+ @state = AtomicReference . new ( value )
117112 end
118113
119114 # @!method value
120115 # The current value of the atom.
121116 #
122117 # @return [Object] The current value.
118+ def value
119+ @state . get
120+ end
123121
124122 alias_method :deref , :value
125123
@@ -160,7 +158,7 @@ def swap(*args)
160158 begin
161159 new_value = yield ( old_value , *args )
162160 break old_value unless valid? ( new_value )
163- break new_value if compare_and_set ( old_value , new_value )
161+ break new_value if @state . compare_and_set ( old_value , new_value )
164162 rescue
165163 break old_value
166164 end
@@ -177,7 +175,7 @@ def swap(*args)
177175 #
178176 # @return [Boolean] True if the value is changed else false.
179177 def compare_and_set ( old_value , new_value )
180- if valid? ( new_value ) && compare_and_set_value ( old_value , new_value )
178+ if valid? ( new_value ) && @state . compare_and_set ( old_value , new_value )
181179 observers . notify_observers ( Time . now , old_value , new_value )
182180 true
183181 else
@@ -196,7 +194,7 @@ def compare_and_set(old_value, new_value)
196194 def reset ( new_value )
197195 old_value = value
198196 if valid? ( new_value )
199- self . value = new_value
197+ @state . set ( new_value )
200198 observers . notify_observers ( Time . now , old_value , new_value )
201199 new_value
202200 else
@@ -212,7 +210,7 @@ def reset(new_value)
212210 # @return [Boolean] false if the validator function returns false or raises
213211 # an exception else true
214212 def valid? ( new_value )
215- @Validator . call ( new_value )
213+ @validator . call ( new_value )
216214 rescue
217215 false
218216 end
0 commit comments