Skip to content

Commit 1358f7f

Browse files
committed
Better synchronization for Atom.
1 parent b41c7e0 commit 1358f7f

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

lib/concurrent/atom.rb

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,27 @@ module Concurrent
2424
class Atom < Synchronization::Object
2525
include Dereferenceable
2626

27-
# @!macro [attach] atom_initialize
27+
# Create a new atom with the given initial value.
2828
#
29-
# Create a new atom with the given initial value.
29+
# @param [Object] value The initial value
30+
# @param [Hash] opts The options used to configure the atom
31+
# @option opts [Proc] :validator (nil) Optional proc used to validate new
32+
# values. It must accept one and only one argument which will be the
33+
# intended new value. The validator will return true if the new value
34+
# is acceptable else return false (preferrably) or raise an exception.
3035
#
31-
# @param [Object] value The initial value
32-
# @param [Hash] opts The options used to configure the atom
33-
# @option opts [Proc] :validator (nil) Optional proc used to validate new
34-
# values. It must accept one and only one argument which will be the
35-
# intended new value. The validator will return true if the new value
36-
# is acceptable else return false (preferrably) or raise an exception.
37-
#
38-
# @!macro deref_options
36+
# @!macro deref_options
3937
#
40-
# @raise [ArgumentError] if the validator is not a `Proc` (when given)
38+
# @raise [ArgumentError] if the validator is not a `Proc` (when given)
4139
def initialize(value, opts = {})
4240
super()
43-
synchronize{ ns_initialize(value, opts) }
41+
42+
@validator = opts.fetch(:validator, ->(v){ true })
43+
raise ArgumentError.new('validator must be a proc') unless @validator.is_a? Proc
44+
45+
@value = Concurrent::AtomicReference.new(value)
46+
ns_set_deref_options(opts)
47+
ensure_ivar_visibility!
4448
end
4549

4650
# The current value of the atom.
@@ -113,15 +117,6 @@ def compare_and_set(old_value, new_value)
113117

114118
private
115119

116-
# @!macro atom_initialize
117-
# @!visibility private
118-
def ns_initialize(value, opts)
119-
@validator = opts.fetch(:validator, ->(v){ true })
120-
raise ArgumentError.new('validator must be a proc') unless @validator.is_a? Proc
121-
@value = Concurrent::AtomicReference.new(value)
122-
ns_set_deref_options(opts)
123-
end
124-
125120
# @!macro atom_compare_and_set
126121
# @raise [Exception] if the validator proc raises an exception
127122
# @!visibility private

0 commit comments

Comments
 (0)