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
+ # @!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
5
40
6
41
def allocate_storage ( init )
7
42
@value = init
@@ -26,12 +61,14 @@ def increment
26
61
@value += 1
27
62
end
28
63
end
64
+ alias_method :up , :increment
29
65
30
66
def decrement
31
67
@mutex . synchronize do
32
68
@value -= 1
33
69
end
34
70
end
71
+ alias_method :down , :decrement
35
72
36
73
def compare_and_set ( expect , update )
37
74
@mutex . synchronize do
@@ -45,77 +82,54 @@ def compare_and_set(expect, update)
45
82
end
46
83
end
47
84
48
- # @!visibility private
49
- module JavaAtomicFixnum # :nodoc:
85
+ if RUBY_PLATFORM == 'java'
50
86
51
- def allocate_storage ( init )
52
- @atomic = java . util . concurrent . atomic . AtomicLong . new ( init )
53
- end
87
+ class JavaAtomicFixnum
54
88
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
58
97
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
63
100
64
- def increment
65
- @atomic . increment_and_get
66
- end
101
+ def value
102
+ @atomic . get
103
+ end
67
104
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
71
109
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
76
114
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
102
119
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
110
123
end
111
124
112
- if RUBY_PLATFORM == 'java'
113
- include JavaAtomicFixnum
114
- else
115
- include MutexAtomicFixnum
125
+ # @!macro atomic_fixnum
126
+ class AtomicFixnum < JavaAtomicFixnum
116
127
end
117
128
118
- alias_method :up , :increment
119
- alias_method :down , :decrement
129
+ else
130
+
131
+ # @!macro atomic_fixnum
132
+ class AtomicFixnum < MutexAtomicFixnum
133
+ end
120
134
end
121
135
end
0 commit comments