@@ -6,28 +6,12 @@ module Concurrent
6
6
# fixnum and thread-safe and guaranteed to succeed. Reads and writes may block
7
7
# briefly but no explicit locking is required.
8
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
9
# @since 0.5.0
28
10
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html java.util.concurrent.atomic.AtomicLong
29
11
class MutexAtomicFixnum
30
12
13
+ # @!macro [attach] atomic_fixnum_method_initialize
14
+ #
31
15
# Creates a new `AtomicFixnum` with the given initial value.
32
16
#
33
17
# @param [Fixnum] init the initial value
@@ -38,38 +22,66 @@ def initialize(init = 0)
38
22
@mutex = Mutex . new
39
23
end
40
24
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
46
30
def value
47
31
@mutex . synchronize do
48
32
@value
49
33
end
50
34
end
51
35
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`
52
45
def value = ( value )
53
46
raise ArgumentError . new ( 'value must be a Fixnum' ) unless value . is_a? ( Fixnum )
54
47
@mutex . synchronize do
55
48
@value = value
56
49
end
57
50
end
58
51
52
+ # @!macro [attach] atomic_fixnum_method_increment
53
+ #
54
+ # Increases the current value by 1.
55
+ #
56
+ # @return [Fixnum] the current value after incrementation
59
57
def increment
60
58
@mutex . synchronize do
61
59
@value += 1
62
60
end
63
61
end
64
62
alias_method :up , :increment
65
63
64
+ # @!macro [attach] atomic_fixnum_method_decrement
65
+ #
66
+ # Decreases the current value by 1.
67
+ #
68
+ # @return [Fixnum] the current value after decrementation
66
69
def decrement
67
70
@mutex . synchronize do
68
71
@value -= 1
69
72
end
70
73
end
71
74
alias_method :down , :decrement
72
75
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
73
85
def compare_and_set ( expect , update )
74
86
@mutex . synchronize do
75
87
if @value == expect
@@ -84,39 +96,45 @@ def compare_and_set(expect, update)
84
96
85
97
if RUBY_PLATFORM == 'java'
86
98
99
+ # @!macro atomic_fixnum
87
100
class JavaAtomicFixnum
88
101
89
- # Creates a new `AtomicFixnum` with the given initial value.
102
+ # @!macro atomic_fixnum_method_initialize
90
103
#
91
- # @param [Fixnum] init the initial value
92
- # @raise [ArgumentError] if the initial value is not a `Fixnum`
93
104
def initialize ( init = 0 )
94
105
raise ArgumentError . new ( 'initial value must be a Fixnum' ) unless init . is_a? ( Fixnum )
95
106
@atomic = java . util . concurrent . atomic . AtomicLong . new ( init )
96
107
end
97
108
98
- def allocate_storage ( init )
99
- end
100
-
109
+ # @!macro atomic_fixnum_method_value
110
+ #
101
111
def value
102
112
@atomic . get
103
113
end
104
114
115
+ # @!macro atomic_fixnum_method_value_eq
116
+ #
105
117
def value = ( value )
106
118
raise ArgumentError . new ( 'value must be a Fixnum' ) unless value . is_a? ( Fixnum )
107
119
@atomic . set ( value )
108
120
end
109
121
122
+ # @!macro atomic_fixnum_method_increment
123
+ #
110
124
def increment
111
125
@atomic . increment_and_get
112
126
end
113
127
alias_method :up , :increment
114
128
129
+ # @!macro atomic_fixnum_method_decrement
130
+ #
115
131
def decrement
116
132
@atomic . decrement_and_get
117
133
end
118
134
alias_method :down , :decrement
119
135
136
+ # @!macro atomic_fixnum_method_compare_and_set
137
+ #
120
138
def compare_and_set ( expect , update )
121
139
@atomic . compare_and_set ( expect , update )
122
140
end
0 commit comments