Skip to content

Commit 0631fc9

Browse files
committed
AtomicBoolean is now a Synchronization::Object.
1 parent ccc62cd commit 0631fc9

File tree

3 files changed

+33
-50
lines changed

3 files changed

+33
-50
lines changed

examples/benchmark_atomic_boolean.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
require 'benchmark'
77
require 'rbconfig'
88

9+
THREADS = 1
10+
TESTS = 10_000_000
11+
912
def atomic_test(clazz, opts = {})
1013
threads = opts.fetch(:threads, 5)
1114
tests = opts.fetch(:tests, 100)
@@ -29,10 +32,10 @@ def atomic_test(clazz, opts = {})
2932

3033
puts "Testing with #{RbConfig::CONFIG['ruby_install_name']} #{RUBY_VERSION}"
3134

32-
atomic_test(Concurrent::MutexAtomicBoolean, threads: 10, tests: 1_000_000)
35+
atomic_test(Concurrent::MutexAtomicBoolean, threads: THREADS, tests: TESTS)
3336

3437
if defined? Concurrent::CAtomicBoolean
35-
atomic_test(Concurrent::CAtomicBoolean, threads: 10, tests: 1_000_000)
38+
atomic_test(Concurrent::CAtomicBoolean, threads: THREADS, tests: TESTS)
3639
elsif RUBY_PLATFORM == 'java'
37-
atomic_test(Concurrent::JavaAtomicBoolean, threads: 10, tests: 1_000_000)
40+
atomic_test(Concurrent::JavaAtomicBoolean, threads: THREADS, tests: TESTS)
3841
end

lib/concurrent/atomic/atomic_boolean.rb

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'concurrent/native_extensions'
2+
require 'concurrent/synchronization'
23

34
module Concurrent
45

@@ -21,16 +22,15 @@ module Concurrent
2122
# 3.340000 0.010000 3.350000 ( 0.855000)
2223
#
2324
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicBoolean.html java.util.concurrent.atomic.AtomicBoolean
24-
class MutexAtomicBoolean
25+
class MutexAtomicBoolean < Synchronization::Object
2526

2627
# @!macro [attach] atomic_boolean_method_initialize
2728
#
2829
# Creates a new `AtomicBoolean` with the given initial value.
2930
#
3031
# @param [Boolean] initial the initial value
3132
def initialize(initial = false)
32-
@value = !!initial
33-
@mutex = Mutex.new
33+
super(initial)
3434
end
3535

3636
# @!macro [attach] atomic_boolean_method_value_get
@@ -39,10 +39,7 @@ def initialize(initial = false)
3939
#
4040
# @return [Boolean] the current value
4141
def value
42-
@mutex.lock
43-
@value
44-
ensure
45-
@mutex.unlock
42+
synchronize { @value }
4643
end
4744

4845
# @!macro [attach] atomic_boolean_method_value_set
@@ -53,11 +50,7 @@ def value
5350
#
5451
# @return [Boolean] the current value
5552
def value=(value)
56-
@mutex.lock
57-
@value = !!value
58-
@value
59-
ensure
60-
@mutex.unlock
53+
synchronize { @value = !!value }
6154
end
6255

6356
# @!macro [attach] atomic_boolean_method_true_question
@@ -66,10 +59,7 @@ def value=(value)
6659
#
6760
# @return [Boolean] true if the current value is `true`, else false
6861
def true?
69-
@mutex.lock
70-
@value
71-
ensure
72-
@mutex.unlock
62+
synchronize { @value }
7363
end
7464

7565
# @!macro atomic_boolean_method_false_question
@@ -78,10 +68,7 @@ def true?
7868
#
7969
# @return [Boolean] true if the current value is `false`, else false
8070
def false?
81-
@mutex.lock
82-
!@value
83-
ensure
84-
@mutex.unlock
71+
synchronize { !@value }
8572
end
8673

8774
# @!macro [attach] atomic_boolean_method_make_true
@@ -90,12 +77,7 @@ def false?
9077
#
9178
# @return [Boolean] true is value has changed, otherwise false
9279
def make_true
93-
@mutex.lock
94-
old = @value
95-
@value = true
96-
!old
97-
ensure
98-
@mutex.unlock
80+
synchronize { ns_make_value(true) }
9981
end
10082

10183
# @!macro [attach] atomic_boolean_method_make_false
@@ -104,12 +86,19 @@ def make_true
10486
#
10587
# @return [Boolean] true is value has changed, otherwise false
10688
def make_false
107-
@mutex.lock
89+
synchronize { ns_make_value(false) }
90+
end
91+
92+
protected
93+
94+
def ns_initialize(initial)
95+
@value = !!initial
96+
end
97+
98+
def ns_make_value(value)
10899
old = @value
109-
@value = false
110-
old
111-
ensure
112-
@mutex.unlock
100+
@value = value
101+
old != @value
113102
end
114103
end
115104

spec/concurrent/atomic/atomic_boolean_spec.rb

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,43 +108,34 @@ module Concurrent
108108

109109
it_should_behave_like :atomic_boolean
110110

111-
specify 'construction is synchronized' do
112-
mutex = double('mutex')
113-
expect(Mutex).to receive(:new).once.with(no_args).and_return(mutex)
114-
described_class.new
115-
end
116-
117111
context 'instance methods' do
118112

119113
before(:each) do
120-
mutex = double('mutex')
121-
allow(Mutex).to receive(:new).with(no_args).and_return(mutex)
122-
expect(mutex).to receive(:lock)
123-
expect(mutex).to receive(:unlock)
114+
expect(subject).to receive(:synchronize).with(no_args).and_return(true)
124115
end
125116

126117
specify 'value is synchronized' do
127-
described_class.new.value
118+
subject.value
128119
end
129120

130121
specify 'value= is synchronized' do
131-
described_class.new.value = 10
122+
subject.value = 10
132123
end
133124

134125
specify 'true? is synchronized' do
135-
described_class.new.true?
126+
subject.true?
136127
end
137128

138129
specify 'false? is synchronized' do
139-
described_class.new.false?
130+
subject.false?
140131
end
141132

142133
specify 'make_true is synchronized' do
143-
described_class.new.make_true
134+
subject.make_true
144135
end
145136

146137
specify 'make_false is synchronized' do
147-
described_class.new.make_false
138+
subject.make_false
148139
end
149140
end
150141
end

0 commit comments

Comments
 (0)