Skip to content

Commit f31e7ac

Browse files
committed
Moved atomic boolean classes into own files.
1 parent 4775b93 commit f31e7ac

File tree

2 files changed

+142
-116
lines changed

2 files changed

+142
-116
lines changed
Lines changed: 82 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,80 @@
1+
require 'concurrent/atomic/mutex_atomic_boolean'
12
require 'concurrent/utility/native_extension_loader'
2-
require 'concurrent/synchronization'
33

44
module Concurrent
55

6-
# @!macro [attach] atomic_boolean
6+
###################################################################
7+
8+
# @!macro [new] atomic_boolean_method_initialize
79
#
8-
# A boolean value that can be updated atomically. Reads and writes to an atomic
9-
# boolean and thread-safe and guaranteed to succeed. Reads and writes may block
10-
# briefly but no explicit locking is required.
10+
# Creates a new `AtomicBoolean` with the given initial value.
1111
#
12-
# Testing with ruby 2.1.2
13-
# Testing with Concurrent::MutexAtomicBoolean...
14-
# 2.790000 0.000000 2.790000 ( 2.791454)
15-
# Testing with Concurrent::CAtomicBoolean...
16-
# 0.740000 0.000000 0.740000 ( 0.740206)
12+
# @param [Boolean] initial the initial value
13+
14+
# @!macro [new] atomic_boolean_method_value_get
1715
#
18-
# Testing with jruby 1.9.3
19-
# Testing with Concurrent::MutexAtomicBoolean...
20-
# 5.240000 2.520000 7.760000 ( 3.683000)
21-
# Testing with Concurrent::JavaAtomicBoolean...
22-
# 3.340000 0.010000 3.350000 ( 0.855000)
16+
# Retrieves the current `Boolean` value.
2317
#
24-
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicBoolean.html java.util.concurrent.atomic.AtomicBoolean
18+
# @return [Boolean] the current value
19+
20+
# @!macro [new] atomic_boolean_method_value_set
2521
#
26-
# @!visibility private
22+
# Explicitly sets the value.
2723
#
28-
# @!macro internal_implementation_note
29-
class MutexAtomicBoolean < Synchronization::Object
30-
31-
# @!macro [attach] atomic_boolean_method_initialize
32-
#
33-
# Creates a new `AtomicBoolean` with the given initial value.
34-
#
35-
# @param [Boolean] initial the initial value
36-
def initialize(initial = false)
37-
super()
38-
synchronize { ns_initialize(initial) }
39-
end
40-
41-
# @!macro [attach] atomic_boolean_method_value_get
42-
#
43-
# Retrieves the current `Boolean` value.
44-
#
45-
# @return [Boolean] the current value
46-
def value
47-
synchronize { @value }
48-
end
49-
50-
# @!macro [attach] atomic_boolean_method_value_set
51-
#
52-
# Explicitly sets the value.
53-
#
54-
# @param [Boolean] value the new value to be set
55-
#
56-
# @return [Boolean] the current value
57-
def value=(value)
58-
synchronize { @value = !!value }
59-
end
60-
61-
# @!macro [attach] atomic_boolean_method_true_question
62-
#
63-
# Is the current value `true`
64-
#
65-
# @return [Boolean] true if the current value is `true`, else false
66-
def true?
67-
synchronize { @value }
68-
end
24+
# @param [Boolean] value the new value to be set
25+
#
26+
# @return [Boolean] the current value
6927

70-
# @!macro [attach] atomic_boolean_method_false_question
71-
#
72-
# Is the current value `false`
73-
#
74-
# @return [Boolean] true if the current value is `false`, else false
75-
def false?
76-
synchronize { !@value }
77-
end
28+
# @!macro [new] atomic_boolean_method_true_question
29+
#
30+
# Is the current value `true`
31+
#
32+
# @return [Boolean] true if the current value is `true`, else false
7833

79-
# @!macro [attach] atomic_boolean_method_make_true
80-
#
81-
# Explicitly sets the value to true.
82-
#
83-
# @return [Boolean] true is value has changed, otherwise false
84-
def make_true
85-
synchronize { ns_make_value(true) }
86-
end
34+
# @!macro [new] atomic_boolean_method_false_question
35+
#
36+
# Is the current value `false`
37+
#
38+
# @return [Boolean] true if the current value is `false`, else false
8739

88-
# @!macro [attach] atomic_boolean_method_make_false
89-
#
90-
# Explicitly sets the value to false.
91-
#
92-
# @return [Boolean] true is value has changed, otherwise false
93-
def make_false
94-
synchronize { ns_make_value(false) }
95-
end
40+
# @!macro [new] atomic_boolean_method_make_true
41+
#
42+
# Explicitly sets the value to true.
43+
#
44+
# @return [Boolean] true is value has changed, otherwise false
9645

97-
protected
46+
# @!macro [new] atomic_boolean_method_make_false
47+
#
48+
# Explicitly sets the value to false.
49+
#
50+
# @return [Boolean] true is value has changed, otherwise false
51+
52+
###################################################################
9853

99-
# @!visibility private
100-
def ns_initialize(initial)
101-
@value = !!initial
102-
end
54+
# @!macro [new] atomic_boolean_public_api
55+
#
56+
# @!method initialize(initial = false)
57+
# @!macro atomic_boolean_method_initialize
58+
#
59+
# @!method value
60+
# @!macro atomic_boolean_method_value_get
61+
#
62+
# @!method value=(value)
63+
# @!macro atomic_boolean_method_value_set
64+
#
65+
# @!method true?
66+
# @!macro atomic_boolean_method_true_question
67+
#
68+
# @!method false?
69+
# @!macro atomic_boolean_method_false_question
70+
#
71+
# @!method make_true
72+
# @!macro atomic_boolean_method_make_true
73+
#
74+
# @!method make_false
75+
# @!macro atomic_boolean_method_make_false
10376

104-
# @!visibility private
105-
def ns_make_value(value)
106-
old = @value
107-
@value = value
108-
old != @value
109-
end
110-
end
77+
###################################################################
11178

11279
# @!visibility private
11380
# @!macro internal_implementation_note
@@ -121,31 +88,30 @@ def ns_make_value(value)
12188
end
12289
private_constant :AtomicBooleanImplementation
12390

124-
# @!macro atomic_boolean
91+
# @!macro [attach] atomic_boolean
92+
#
93+
# A boolean value that can be updated atomically. Reads and writes to an atomic
94+
# boolean and thread-safe and guaranteed to succeed. Reads and writes may block
95+
# briefly but no explicit locking is required.
96+
#
97+
# Testing with ruby 2.1.2
98+
# Testing with Concurrent::MutexAtomicBoolean...
99+
# 2.790000 0.000000 2.790000 ( 2.791454)
100+
# Testing with Concurrent::CAtomicBoolean...
101+
# 0.740000 0.000000 0.740000 ( 0.740206)
102+
#
103+
# Testing with jruby 1.9.3
104+
# Testing with Concurrent::MutexAtomicBoolean...
105+
# 5.240000 2.520000 7.760000 ( 3.683000)
106+
# Testing with Concurrent::JavaAtomicBoolean...
107+
# 3.340000 0.010000 3.350000 ( 0.855000)
108+
#
109+
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicBoolean.html java.util.concurrent.atomic.AtomicBoolean
125110
#
126111
# @see Concurrent::MutexAtomicBoolean
112+
#
113+
# @!macro atomic_boolean_public_api
127114
class AtomicBoolean < AtomicBooleanImplementation
128115

129-
# @!method initialize(initial = false)
130-
# @!macro atomic_boolean_method_initialize
131-
132-
# @!method value
133-
# @!macro atomic_boolean_method_value_get
134-
135-
# @!method value=(value)
136-
# @!macro atomic_boolean_method_value_set
137-
138-
# @!method true?
139-
# @!macro atomic_boolean_method_true_question
140-
141-
# @!method false?
142-
# @!macro atomic_boolean_method_false_question
143-
144-
# @!method make_true
145-
# @!macro atomic_boolean_method_make_true
146-
147-
# @!method make_false
148-
# @!macro atomic_boolean_method_make_false
149-
150116
end
151117
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
require 'concurrent/synchronization'
2+
3+
module Concurrent
4+
5+
# @!macro atomic_boolean
6+
# @!visibility private
7+
# @!macro internal_implementation_note
8+
class MutexAtomicBoolean < Synchronization::Object
9+
10+
# @!macro atomic_boolean_method_initialize
11+
def initialize(initial = false)
12+
super()
13+
synchronize { ns_initialize(initial) }
14+
end
15+
16+
# @!macro atomic_boolean_method_value_get
17+
def value
18+
synchronize { @value }
19+
end
20+
21+
# @!macro atomic_boolean_method_value_set
22+
def value=(value)
23+
synchronize { @value = !!value }
24+
end
25+
26+
# @!macro atomic_boolean_method_true_question
27+
def true?
28+
synchronize { @value }
29+
end
30+
31+
# @!macro atomic_boolean_method_false_question
32+
def false?
33+
synchronize { !@value }
34+
end
35+
36+
# @!macro atomic_boolean_method_make_true
37+
def make_true
38+
synchronize { ns_make_value(true) }
39+
end
40+
41+
# @!macro atomic_boolean_method_make_false
42+
def make_false
43+
synchronize { ns_make_value(false) }
44+
end
45+
46+
protected
47+
48+
# @!visibility private
49+
def ns_initialize(initial)
50+
@value = !!initial
51+
end
52+
53+
# @!visibility private
54+
def ns_make_value(value)
55+
old = @value
56+
@value = value
57+
old != @value
58+
end
59+
end
60+
end

0 commit comments

Comments
 (0)