Skip to content

Commit 98e685f

Browse files
committed
Implemented JavaCountDownLatch
1 parent 3e0a18d commit 98e685f

File tree

2 files changed

+67
-28
lines changed

2 files changed

+67
-28
lines changed

lib/concurrent/atomic/count_down_latch.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,35 @@ def count_down
5757
def count
5858
@mutex.synchronize { @count }
5959
end
60+
end
61+
62+
if RUBY_PLATFORM == 'java'
6063

64+
class JavaCountDownLatch
65+
66+
def initialize(count)
67+
unless count.is_a?(Fixnum) && count >= 0
68+
raise ArgumentError.new('count must be in integer greater than or equal zero')
69+
end
70+
@latch = java.util.concurrent.CountDownLatch.new(count)
71+
end
72+
73+
def wait(timeout = nil)
74+
if timeout.nil?
75+
@latch.await
76+
true
77+
else
78+
@latch.await(timeout, java.util.concurrent.TimeUnit::SECONDS)
79+
end
80+
end
81+
82+
def count_down
83+
@latch.countDown
84+
end
85+
86+
def count
87+
@latch.getCount
88+
end
89+
end
6190
end
6291
end

spec/concurrent/atomic/count_down_latch_spec.rb

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -81,49 +81,59 @@
8181
end
8282
end
8383
end
84+
end
8485

85-
context 'spurious wake ups' do
86+
module Concurrent
8687

87-
before(:each) do
88-
def latch.simulate_spurious_wake_up
89-
@mutex.synchronize do
90-
@condition.signal
91-
@condition.broadcast
88+
describe CountDownLatch do
89+
90+
it_should_behave_like :count_down_latch
91+
92+
context 'spurious wake ups' do
93+
94+
subject { described_class.new(3) }
95+
96+
before(:each) do
97+
def subject.simulate_spurious_wake_up
98+
@mutex.synchronize do
99+
@condition.signal
100+
@condition.broadcast
101+
end
92102
end
93103
end
94-
end
95104

96-
it 'should resist to spurious wake ups without timeout' do
97-
@expected = false
98-
Thread.new { latch.wait; @expected = true }
105+
it 'should resist to spurious wake ups without timeout' do
106+
@expected = false
107+
Thread.new { subject.wait; @expected = true }
99108

100-
sleep(0.1)
101-
latch.simulate_spurious_wake_up
109+
sleep(0.1)
110+
subject.simulate_spurious_wake_up
102111

103-
sleep(0.1)
104-
@expected.should be_false
105-
end
112+
sleep(0.1)
113+
@expected.should be_false
114+
end
106115

107-
it 'should resist to spurious wake ups with timeout' do
108-
@expected = false
109-
Thread.new { latch.wait(0.5); @expected = true }
116+
it 'should resist to spurious wake ups with timeout' do
117+
@expected = false
118+
Thread.new { subject.wait(0.5); @expected = true }
110119

111-
sleep(0.1)
112-
latch.simulate_spurious_wake_up
120+
sleep(0.1)
121+
subject.simulate_spurious_wake_up
113122

114-
sleep(0.1)
115-
@expected.should be_false
123+
sleep(0.1)
124+
@expected.should be_false
116125

117-
sleep(0.4)
118-
@expected.should be_true
126+
sleep(0.4)
127+
@expected.should be_true
128+
end
119129
end
120130
end
121-
end
122131

123-
module Concurrent
132+
if jruby?
124133

125-
describe CountDownLatch do
134+
describe JavaCountDownLatch do
126135

127-
it_should_behave_like :count_down_latch
136+
it_should_behave_like :count_down_latch
137+
end
128138
end
129139
end

0 commit comments

Comments
 (0)