Skip to content

Commit 54c2b5a

Browse files
committed
AtomicBoolean and AtomicFixnum follow new subclass declaration convention.
1 parent ac4ac26 commit 54c2b5a

File tree

2 files changed

+70
-76
lines changed

2 files changed

+70
-76
lines changed

lib/concurrent/atomic/atomic_boolean.rb

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def true?
6262
synchronize { @value }
6363
end
6464

65-
# @!macro atomic_boolean_method_false_question
65+
# @!macro [attach] atomic_boolean_method_false_question
6666
#
6767
# Is the current value `false`
6868
#
@@ -91,57 +91,54 @@ def make_false
9191

9292
protected
9393

94+
# @!visibility private
9495
def ns_initialize(initial)
9596
@value = !!initial
9697
end
9798

99+
# @!visibility private
98100
def ns_make_value(value)
99101
old = @value
100102
@value = value
101103
old != @value
102104
end
103105
end
104106

105-
if Concurrent.on_jruby?
106-
107-
class AtomicBoolean < JavaAtomicBoolean
108-
end
109-
110-
elsif defined?(CAtomicBoolean)
111-
112-
# @!macro atomic_boolean
113-
class CAtomicBoolean
114-
115-
# @!method initialize
116-
# @!macro atomic_boolean_method_initialize
117-
118-
# @!method value
119-
# @!macro atomic_boolean_method_value_get
120-
121-
# @!method value=
122-
# @!macro atomic_boolean_method_value_set
123-
124-
# @!method true?
125-
# @!macro atomic_boolean_method_true_question
126-
127-
# @!method false?
128-
# @!macro atomic_boolean_method_false_question
129-
130-
# @!method make_true
131-
# @!macro atomic_boolean_method_make_true
132-
133-
# @!method make_false
134-
# @!macro atomic_boolean_method_make_false
135-
end
136-
137-
# @!macro atomic_boolean
138-
class AtomicBoolean < CAtomicBoolean
139-
end
140-
141-
else
142-
143-
# @!macro atomic_boolean
144-
class AtomicBoolean < MutexAtomicBoolean
145-
end
107+
AtomicBooleanImplementation = case
108+
when Concurrent.on_jruby?
109+
JavaAtomicBoolean
110+
when defined?(CAtomicBoolean)
111+
CAtomicBoolean
112+
else
113+
MutexAtomicBoolean
114+
end
115+
private_constant :AtomicBooleanImplementation
116+
117+
# @!macro atomic_boolean
118+
#
119+
# @see Concurrent::MutexAtomicBoolean
120+
class AtomicBoolean < AtomicBooleanImplementation
121+
122+
# @!method initialize(initial = false)
123+
# @!macro atomic_boolean_method_initialize
124+
125+
# @!method value
126+
# @!macro atomic_boolean_method_value_get
127+
128+
# @!method value=(value)
129+
# @!macro atomic_boolean_method_value_set
130+
131+
# @!method true?
132+
# @!macro atomic_boolean_method_true_question
133+
134+
# @!method false?
135+
# @!macro atomic_boolean_method_false_question
136+
137+
# @!method make_true
138+
# @!macro atomic_boolean_method_make_true
139+
140+
# @!method make_false
141+
# @!macro atomic_boolean_method_make_false
142+
146143
end
147144
end

lib/concurrent/atomic/atomic_fixnum.rb

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def decrement
9090
# @param [Fixnum] expect the expected value
9191
# @param [Fixnum] update the new value
9292
#
93-
# @return [Boolean] true if the value was updated else false
93+
# @return [Fixnum] true if the value was updated else false
9494
def compare_and_set(expect, update)
9595
synchronize do
9696
if @value == expect
@@ -104,17 +104,20 @@ def compare_and_set(expect, update)
104104

105105
protected
106106

107+
# @!visibility private
107108
def ns_initialize(initial)
108109
ns_set(initial)
109110
end
110111

111112
private
112113

114+
# @!visibility private
113115
def ns_set(value)
114116
range_check!(value)
115117
@value = value
116118
end
117119

120+
# @!visibility private
118121
def range_check!(value)
119122
if !value.is_a?(Fixnum)
120123
raise ArgumentError.new('value value must be a Fixnum')
@@ -128,44 +131,38 @@ def range_check!(value)
128131
end
129132
end
130133

131-
if Concurrent.on_jruby?
132-
133-
# @!macro atomic_fixnum
134-
class AtomicFixnum < JavaAtomicFixnum
135-
end
136-
137-
elsif defined?(CAtomicFixnum)
138-
139-
# @!macro atomic_fixnum
140-
class CAtomicFixnum
141-
142-
# @!method initialize
143-
# @!macro atomic_fixnum_method_initialize
144-
145-
# @!method value
146-
# @!macro atomic_fixnum_method_value_get
134+
AtomicFixnumImplementation = case
135+
when Concurrent.on_jruby?
136+
JavaAtomicFixnum
137+
when defined?(CAtomicFixnum)
138+
CAtomicFixnum
139+
else
140+
MutexAtomicFixnum
141+
end
142+
private_constant :AtomicFixnumImplementation
143+
144+
# @!macro atomic_fixnum
145+
#
146+
# @see Concurrent::MutexAtomicFixnum
147+
class AtomicFixnum < AtomicFixnumImplementation
148+
149+
# @!method initialize(initial = 0)
150+
# @!macro atomic_fixnum_method_initialize
147151

148-
# @!method value=
149-
# @!macro atomic_fixnum_method_value_set
152+
# @!method value
153+
# @!macro atomic_fixnum_method_value_get
150154

151-
# @!method increment
152-
# @!macro atomic_fixnum_method_increment
155+
# @!method value=(value)
156+
# @!macro atomic_fixnum_method_value_set
153157

154-
# @!method decrement
155-
# @!macro atomic_fixnum_method_decrement
158+
# @!method increment
159+
# @!macro atomic_fixnum_method_increment
156160

157-
# @!method compare_and_set
158-
# @!macro atomic_fixnum_method_compare_and_set
159-
end
161+
# @!method decrement
162+
# @!macro atomic_fixnum_method_decrement
160163

161-
# @!macro atomic_fixnum
162-
class AtomicFixnum < CAtomicFixnum
163-
end
164+
# @!method compare_and_set(expect, update)
165+
# @!macro atomic_fixnum_method_compare_and_set
164166

165-
else
166-
167-
# @!macro atomic_fixnum
168-
class AtomicFixnum < MutexAtomicFixnum
169-
end
170167
end
171168
end

0 commit comments

Comments
 (0)