Skip to content

Commit ddaecea

Browse files
committed
Refactored AtomicFixnum as subclass/superclass rather than module include.
1 parent c2e4f6b commit ddaecea

File tree

2 files changed

+210
-216
lines changed

2 files changed

+210
-216
lines changed
Lines changed: 76 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
11
module Concurrent
22

3-
# @!visibility private
4-
module MutexAtomicFixnum # :nodoc:
3+
# @!macro [attach] atomic_fixnum
4+
#
5+
# A numeric value that can be updated atomically. Reads and writes to an atomic
6+
# fixnum and thread-safe and guaranteed to succeed. Reads and writes may block
7+
# briefly but no explicit locking is required.
8+
#
9+
# @!method value()
10+
# Retrieves the current `Fixnum` value
11+
# @return [Fixnum] the current value
12+
#
13+
# @!method value=(value)
14+
# Explicitly sets the value
15+
# @param [Fixnum] value the new value to be set
16+
# @return [Fixnum] the current value
17+
# @raise [ArgumentError] if the new value is not a `Fixnum`
18+
#
19+
# @!method increment()
20+
# Increases the current value by 1
21+
# @return [Fixnum] the current value after incrementation
22+
#
23+
# @!method decrement()
24+
# Decreases the current value by 1
25+
# @return [Fixnum] the current value after decrementation
26+
#
27+
# @since 0.5.0
28+
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html java.util.concurrent.atomic.AtomicLong
29+
class MutexAtomicFixnum
30+
31+
# Creates a new `AtomicFixnum` with the given initial value.
32+
#
33+
# @param [Fixnum] init the initial value
34+
# @raise [ArgumentError] if the initial value is not a `Fixnum`
35+
def initialize(init = 0)
36+
raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
37+
@value = init
38+
@mutex = Mutex.new
39+
end
540

641
def allocate_storage(init)
742
@value = init
@@ -26,12 +61,14 @@ def increment
2661
@value += 1
2762
end
2863
end
64+
alias_method :up, :increment
2965

3066
def decrement
3167
@mutex.synchronize do
3268
@value -= 1
3369
end
3470
end
71+
alias_method :down, :decrement
3572

3673
def compare_and_set(expect, update)
3774
@mutex.synchronize do
@@ -45,77 +82,54 @@ def compare_and_set(expect, update)
4582
end
4683
end
4784

48-
# @!visibility private
49-
module JavaAtomicFixnum # :nodoc:
85+
if RUBY_PLATFORM == 'java'
5086

51-
def allocate_storage(init)
52-
@atomic = java.util.concurrent.atomic.AtomicLong.new(init)
53-
end
87+
class JavaAtomicFixnum
5488

55-
def value
56-
@atomic.get
57-
end
89+
# Creates a new `AtomicFixnum` with the given initial value.
90+
#
91+
# @param [Fixnum] init the initial value
92+
# @raise [ArgumentError] if the initial value is not a `Fixnum`
93+
def initialize(init = 0)
94+
raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
95+
@atomic = java.util.concurrent.atomic.AtomicLong.new(init)
96+
end
5897

59-
def value=(value)
60-
raise ArgumentError.new('value must be a Fixnum') unless value.is_a?(Fixnum)
61-
@atomic.set(value)
62-
end
98+
def allocate_storage(init)
99+
end
63100

64-
def increment
65-
@atomic.increment_and_get
66-
end
101+
def value
102+
@atomic.get
103+
end
67104

68-
def decrement
69-
@atomic.decrement_and_get
70-
end
105+
def value=(value)
106+
raise ArgumentError.new('value must be a Fixnum') unless value.is_a?(Fixnum)
107+
@atomic.set(value)
108+
end
71109

72-
def compare_and_set(expect, update)
73-
@atomic.compare_and_set(expect, update)
74-
end
75-
end
110+
def increment
111+
@atomic.increment_and_get
112+
end
113+
alias_method :up, :increment
76114

77-
# A numeric value that can be updated atomically. Reads and writes to an atomic
78-
# fixnum and thread-safe and guaranteed to succeed. Reads and writes may block
79-
# briefly but no explicit locking is required.
80-
#
81-
# @!method value()
82-
# Retrieves the current `Fixnum` value
83-
# @return [Fixnum] the current value
84-
#
85-
# @!method value=(value)
86-
# Explicitly sets the value
87-
# @param [Fixnum] value the new value to be set
88-
# @return [Fixnum] the current value
89-
# @raise [ArgumentError] if the new value is not a `Fixnum`
90-
#
91-
# @!method increment()
92-
# Increases the current value by 1
93-
# @return [Fixnum] the current value after incrementation
94-
#
95-
# @!method decrement()
96-
# Decreases the current value by 1
97-
# @return [Fixnum] the current value after decrementation
98-
#
99-
# @since 0.5.0
100-
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html java.util.concurrent.atomic.AtomicLong
101-
class AtomicFixnum
115+
def decrement
116+
@atomic.decrement_and_get
117+
end
118+
alias_method :down, :decrement
102119

103-
# Creates a new `AtomicFixnum` with the given initial value.
104-
#
105-
# @param [Fixnum] init the initial value
106-
# @raise [ArgumentError] if the initial value is not a `Fixnum`
107-
def initialize(init = 0)
108-
raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
109-
allocate_storage(init)
120+
def compare_and_set(expect, update)
121+
@atomic.compare_and_set(expect, update)
122+
end
110123
end
111124

112-
if RUBY_PLATFORM == 'java'
113-
include JavaAtomicFixnum
114-
else
115-
include MutexAtomicFixnum
125+
# @!macro atomic_fixnum
126+
class AtomicFixnum < JavaAtomicFixnum
116127
end
117128

118-
alias_method :up, :increment
119-
alias_method :down, :decrement
129+
else
130+
131+
# @!macro atomic_fixnum
132+
class AtomicFixnum < MutexAtomicFixnum
133+
end
120134
end
121135
end

0 commit comments

Comments
 (0)