1
1
module Concurrent
2
2
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
+ # @since 0.5.0
10
+ # @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html java.util.concurrent.atomic.AtomicLong
11
+ class MutexAtomicFixnum
5
12
6
- def allocate_storage ( init )
13
+ # @!macro [attach] atomic_fixnum_method_initialize
14
+ #
15
+ # Creates a new `AtomicFixnum` with the given initial value.
16
+ #
17
+ # @param [Fixnum] init the initial value
18
+ # @raise [ArgumentError] if the initial value is not a `Fixnum`
19
+ def initialize ( init = 0 )
20
+ raise ArgumentError . new ( 'initial value must be a Fixnum' ) unless init . is_a? ( Fixnum )
7
21
@value = init
8
22
@mutex = Mutex . new
9
23
end
10
24
25
+ # @!macro [attach] atomic_fixnum_method_value
26
+ #
27
+ # Retrieves the current `Fixnum` value.
28
+ #
29
+ # @return [Fixnum] the current value
11
30
def value
12
31
@mutex . synchronize do
13
32
@value
14
33
end
15
34
end
16
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`
17
45
def value = ( value )
18
46
raise ArgumentError . new ( 'value must be a Fixnum' ) unless value . is_a? ( Fixnum )
19
47
@mutex . synchronize do
20
48
@value = value
21
49
end
22
50
end
23
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
24
57
def increment
25
58
@mutex . synchronize do
26
59
@value += 1
27
60
end
28
61
end
62
+ alias_method :up , :increment
29
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
30
69
def decrement
31
70
@mutex . synchronize do
32
71
@value -= 1
33
72
end
34
73
end
74
+ alias_method :down , :decrement
35
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
36
85
def compare_and_set ( expect , update )
37
86
@mutex . synchronize do
38
87
if @value == expect
@@ -45,77 +94,60 @@ def compare_and_set(expect, update)
45
94
end
46
95
end
47
96
48
- # @!visibility private
49
- module JavaAtomicFixnum # :nodoc:
97
+ if RUBY_PLATFORM == 'java'
50
98
51
- def allocate_storage ( init )
52
- @atomic = java . util . concurrent . atomic . AtomicLong . new ( init )
53
- end
99
+ # @!macro atomic_fixnum
100
+ class JavaAtomicFixnum
54
101
55
- def value
56
- @atomic . get
57
- end
58
-
59
- def value = ( value )
60
- raise ArgumentError . new ( 'value must be a Fixnum' ) unless value . is_a? ( Fixnum )
61
- @atomic . set ( value )
62
- end
102
+ # @!macro atomic_fixnum_method_initialize
103
+ #
104
+ def initialize ( init = 0 )
105
+ raise ArgumentError . new ( 'initial value must be a Fixnum' ) unless init . is_a? ( Fixnum )
106
+ @atomic = java . util . concurrent . atomic . AtomicLong . new ( init )
107
+ end
63
108
64
- def increment
65
- @atomic . increment_and_get
66
- end
109
+ # @!macro atomic_fixnum_method_value
110
+ #
111
+ def value
112
+ @atomic . get
113
+ end
67
114
68
- def decrement
69
- @atomic . decrement_and_get
70
- end
115
+ # @!macro atomic_fixnum_method_value_eq
116
+ #
117
+ def value = ( value )
118
+ raise ArgumentError . new ( 'value must be a Fixnum' ) unless value . is_a? ( Fixnum )
119
+ @atomic . set ( value )
120
+ end
71
121
72
- def compare_and_set ( expect , update )
73
- @atomic . compare_and_set ( expect , update )
74
- end
75
- end
122
+ # @!macro atomic_fixnum_method_increment
123
+ #
124
+ def increment
125
+ @atomic . increment_and_get
126
+ end
127
+ alias_method :up , :increment
76
128
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
129
+ # @!macro atomic_fixnum_method_decrement
130
+ #
131
+ def decrement
132
+ @atomic . decrement_and_get
133
+ end
134
+ alias_method :down , :decrement
102
135
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 )
136
+ # @!macro atomic_fixnum_method_compare_and_set
137
+ #
138
+ def compare_and_set ( expect , update )
139
+ @atomic . compare_and_set ( expect , update )
140
+ end
110
141
end
111
142
112
- if RUBY_PLATFORM == 'java'
113
- include JavaAtomicFixnum
114
- else
115
- include MutexAtomicFixnum
143
+ # @!macro atomic_fixnum
144
+ class AtomicFixnum < JavaAtomicFixnum
116
145
end
117
146
118
- alias_method :up , :increment
119
- alias_method :down , :decrement
147
+ else
148
+
149
+ # @!macro atomic_fixnum
150
+ class AtomicFixnum < MutexAtomicFixnum
151
+ end
120
152
end
121
153
end
0 commit comments