Skip to content

Commit d94f3e9

Browse files
committed
Rewrite monitor specs to use queues to synchronize threads.
1 parent 2f7c998 commit d94f3e9

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

spec/ruby/library/monitor/enter_spec.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@
55
it "acquires the monitor" do
66
monitor = Monitor.new
77
10.times do
8-
locked = false
8+
wait_q = Queue.new
9+
continue_q = Queue.new
910

1011
thread = Thread.new do
1112
begin
1213
monitor.enter
13-
locked = true
14-
sleep # wait for wakeup.
14+
wait_q << true
15+
continue_q.pop
1516
ensure
1617
monitor.exit
1718
end
1819
end
1920

20-
Thread.pass until locked
21+
wait_q.pop
2122
monitor.mon_locked?.should == true
22-
thread.wakeup
23+
continue_q << true
2324
thread.join
2425
monitor.mon_locked?.should == false
2526
end

spec/ruby/library/monitor/new_cond_spec.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414

1515
10.times do
1616

17-
locked = false
17+
wait_q = Queue.new
1818
thread = Thread.new do
1919
m.synchronize do
20-
locked = true
20+
wait_q << true
2121
c.wait
2222
end
2323
:done
2424
end
2525

26-
Thread.pass until locked
27-
Thread.pass until thread.stop?
26+
wait_q.pop
2827

28+
# Synchronize can't happen until the other thread is waiting.
2929
m.synchronize { c.signal }
3030

3131
thread.join
@@ -39,20 +39,20 @@
3939

4040
10.times do
4141

42-
locked = false
42+
wait_q = Queue.new
4343
thread = Thread.new do
4444
m.synchronize do
4545
m.synchronize do
46-
locked = true
46+
wait_q << true
4747
c.wait
4848
end
4949
end
5050
:done
5151
end
5252

53-
Thread.pass until locked
54-
Thread.pass until thread.stop?
53+
wait_q.pop
5554

55+
#No need to wait here as we cannot synchronize until the other thread is waiting.
5656
m.synchronize { c.signal }
5757

5858
thread.join
@@ -66,18 +66,18 @@
6666

6767
10.times do
6868

69-
locked = false
69+
wait_q = Queue.new
7070
thread = Thread.new do
7171
m.synchronize do
72-
locked = true
72+
wait_q << true
7373
c.wait
7474
end
7575
:done
7676
end
7777

78-
Thread.pass until locked
79-
Thread.pass until thread.stop?
78+
wait_q.pop
8079

80+
# Synchronize can't happen until the other thread is waiting.
8181
m.synchronize { m.synchronize { c.signal } }
8282

8383
thread.join

spec/ruby/library/monitor/synchronize_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@
77

88
monitor = Monitor.new
99
10.times do
10-
locked = false
10+
wait_q = Queue.new
11+
continue_q = Queue.new
1112

1213
thread = Thread.new do
1314
begin
1415
monitor.synchronize do
15-
locked = true
16+
wait_q << true
1617
# Do not wait here, we are trying to interrupt the ensure part of #synchronize
1718
end
18-
sleep # wait for exception if it did not happen yet
19+
continue_q.pop
1920
rescue exc_class
2021
monitor.should_not.mon_locked?
2122
:ok
2223
end
2324
end
2425

25-
Thread.pass until locked
26+
wait_q.pop
2627
thread.raise exc_class, "interrupt"
28+
continue_q << true
2729
thread.value.should == :ok
2830
end
2931
end

0 commit comments

Comments
 (0)