File tree Expand file tree Collapse file tree 3 files changed +24
-21
lines changed
spec/ruby/library/monitor Expand file tree Collapse file tree 3 files changed +24
-21
lines changed Original file line number Diff line number Diff line change 5
5
it "acquires the monitor" do
6
6
monitor = Monitor . new
7
7
10 . times do
8
- locked = false
8
+ wait_q = Queue . new
9
+ continue_q = Queue . new
9
10
10
11
thread = Thread . new do
11
12
begin
12
13
monitor . enter
13
- locked = true
14
- sleep # wait for wakeup.
14
+ wait_q << true
15
+ continue_q . pop
15
16
ensure
16
17
monitor . exit
17
18
end
18
19
end
19
20
20
- Thread . pass until locked
21
+ wait_q . pop
21
22
monitor . mon_locked? . should == true
22
- thread . wakeup
23
+ continue_q << true
23
24
thread . join
24
25
monitor . mon_locked? . should == false
25
26
end
Original file line number Diff line number Diff line change 14
14
15
15
10 . times do
16
16
17
- locked = false
17
+ wait_q = Queue . new
18
18
thread = Thread . new do
19
19
m . synchronize do
20
- locked = true
20
+ wait_q << true
21
21
c . wait
22
22
end
23
23
:done
24
24
end
25
25
26
- Thread . pass until locked
27
- Thread . pass until thread . stop?
26
+ wait_q . pop
28
27
28
+ # Synchronize can't happen until the other thread is waiting.
29
29
m . synchronize { c . signal }
30
30
31
31
thread . join
39
39
40
40
10 . times do
41
41
42
- locked = false
42
+ wait_q = Queue . new
43
43
thread = Thread . new do
44
44
m . synchronize do
45
45
m . synchronize do
46
- locked = true
46
+ wait_q << true
47
47
c . wait
48
48
end
49
49
end
50
50
:done
51
51
end
52
52
53
- Thread . pass until locked
54
- Thread . pass until thread . stop?
53
+ wait_q . pop
55
54
55
+ #No need to wait here as we cannot synchronize until the other thread is waiting.
56
56
m . synchronize { c . signal }
57
57
58
58
thread . join
66
66
67
67
10 . times do
68
68
69
- locked = false
69
+ wait_q = Queue . new
70
70
thread = Thread . new do
71
71
m . synchronize do
72
- locked = true
72
+ wait_q << true
73
73
c . wait
74
74
end
75
75
:done
76
76
end
77
77
78
- Thread . pass until locked
79
- Thread . pass until thread . stop?
78
+ wait_q . pop
80
79
80
+ # Synchronize can't happen until the other thread is waiting.
81
81
m . synchronize { m . synchronize { c . signal } }
82
82
83
83
thread . join
Original file line number Diff line number Diff line change 7
7
8
8
monitor = Monitor . new
9
9
10 . times do
10
- locked = false
10
+ wait_q = Queue . new
11
+ continue_q = Queue . new
11
12
12
13
thread = Thread . new do
13
14
begin
14
15
monitor . synchronize do
15
- locked = true
16
+ wait_q << true
16
17
# Do not wait here, we are trying to interrupt the ensure part of #synchronize
17
18
end
18
- sleep # wait for exception if it did not happen yet
19
+ continue_q . pop
19
20
rescue exc_class
20
21
monitor . should_not . mon_locked?
21
22
:ok
22
23
end
23
24
end
24
25
25
- Thread . pass until locked
26
+ wait_q . pop
26
27
thread . raise exc_class , "interrupt"
28
+ continue_q << true
27
29
thread . value . should == :ok
28
30
end
29
31
end
You can’t perform that action at this time.
0 commit comments