Skip to content

Commit a7525e4

Browse files
committed
Updated docs for AtomicFixnum.
1 parent f930550 commit a7525e4

File tree

1 file changed

+47
-29
lines changed

1 file changed

+47
-29
lines changed

lib/concurrent/atomic/atomic_fixnum.rb

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,12 @@ module Concurrent
66
# fixnum and thread-safe and guaranteed to succeed. Reads and writes may block
77
# briefly but no explicit locking is required.
88
#
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-
#
279
# @since 0.5.0
2810
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html java.util.concurrent.atomic.AtomicLong
2911
class MutexAtomicFixnum
3012

13+
# @!macro [attach] atomic_fixnum_method_initialize
14+
#
3115
# Creates a new `AtomicFixnum` with the given initial value.
3216
#
3317
# @param [Fixnum] init the initial value
@@ -38,38 +22,66 @@ def initialize(init = 0)
3822
@mutex = Mutex.new
3923
end
4024

41-
def allocate_storage(init)
42-
@value = init
43-
@mutex = Mutex.new
44-
end
45-
25+
# @!macro [attach] atomic_fixnum_method_value
26+
#
27+
# Retrieves the current `Fixnum` value.
28+
#
29+
# @return [Fixnum] the current value
4630
def value
4731
@mutex.synchronize do
4832
@value
4933
end
5034
end
5135

36+
# @!macro [attach] atomic_fixnum_method_value_eq
37+
#
38+
# Explicitly sets the value.
39+
#
40+
# @param [Fixnum] value the new value to be set
41+
#
42+
# @return [Fixnum] the current value
43+
#
44+
# @raise [ArgumentError] if the new value is not a `Fixnum`
5245
def value=(value)
5346
raise ArgumentError.new('value must be a Fixnum') unless value.is_a?(Fixnum)
5447
@mutex.synchronize do
5548
@value = value
5649
end
5750
end
5851

52+
# @!macro [attach] atomic_fixnum_method_increment
53+
#
54+
# Increases the current value by 1.
55+
#
56+
# @return [Fixnum] the current value after incrementation
5957
def increment
6058
@mutex.synchronize do
6159
@value += 1
6260
end
6361
end
6462
alias_method :up, :increment
6563

64+
# @!macro [attach] atomic_fixnum_method_decrement
65+
#
66+
# Decreases the current value by 1.
67+
#
68+
# @return [Fixnum] the current value after decrementation
6669
def decrement
6770
@mutex.synchronize do
6871
@value -= 1
6972
end
7073
end
7174
alias_method :down, :decrement
7275

76+
# @!macro [attach] atomic_fixnum_method_compare_and_set
77+
#
78+
# Atomically sets the value to the given updated value if the current
79+
# value == the expected value.
80+
#
81+
# @param [Fixnum] expect the expected value
82+
# @param [Fixnum] update the new value
83+
#
84+
# @return [Boolean] true if the value was updated else false
7385
def compare_and_set(expect, update)
7486
@mutex.synchronize do
7587
if @value == expect
@@ -84,39 +96,45 @@ def compare_and_set(expect, update)
8496

8597
if RUBY_PLATFORM == 'java'
8698

99+
# @!macro atomic_fixnum
87100
class JavaAtomicFixnum
88101

89-
# Creates a new `AtomicFixnum` with the given initial value.
102+
# @!macro atomic_fixnum_method_initialize
90103
#
91-
# @param [Fixnum] init the initial value
92-
# @raise [ArgumentError] if the initial value is not a `Fixnum`
93104
def initialize(init = 0)
94105
raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
95106
@atomic = java.util.concurrent.atomic.AtomicLong.new(init)
96107
end
97108

98-
def allocate_storage(init)
99-
end
100-
109+
# @!macro atomic_fixnum_method_value
110+
#
101111
def value
102112
@atomic.get
103113
end
104114

115+
# @!macro atomic_fixnum_method_value_eq
116+
#
105117
def value=(value)
106118
raise ArgumentError.new('value must be a Fixnum') unless value.is_a?(Fixnum)
107119
@atomic.set(value)
108120
end
109121

122+
# @!macro atomic_fixnum_method_increment
123+
#
110124
def increment
111125
@atomic.increment_and_get
112126
end
113127
alias_method :up, :increment
114128

129+
# @!macro atomic_fixnum_method_decrement
130+
#
115131
def decrement
116132
@atomic.decrement_and_get
117133
end
118134
alias_method :down, :decrement
119135

136+
# @!macro atomic_fixnum_method_compare_and_set
137+
#
120138
def compare_and_set(expect, update)
121139
@atomic.compare_and_set(expect, update)
122140
end

0 commit comments

Comments
 (0)