/
lock.t
155 lines (137 loc) · 3.52 KB
/
lock.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use v6;
use Test;
#-------------------------------------------------------------------------------
# Tests for low level async functionality in Perl 6. Please do *not* use this
# as inspiration for a way to do things. Please look at using Promises (aka
# start {...}), Channels and Supplies. Thank you!
#-------------------------------------------------------------------------------
plan 23;
{
my $l = Lock.new;
$l.protect({
pass "Running code under lock";
});
$l.protect({
pass "Running another piece of code under lock";
});
}
{
my $l = Lock.new;
dies-ok { $l.protect({ die "oops" }) }, "code that dies under lock throws";
$l.protect({
pass "Code that dies in run does release the lock";
});
Thread.start({
$l.protect({
pass "Even from another thread";
});
}).finish();
}
{
# Attempt to check lock actually enforces some locking.
my $output = '';
my $l = Lock.new;
my $t1 = Thread.start({
$l.protect({
for 1..10000 {
$output ~= 'a'
}
});
});
my $t2 = Thread.start({
$l.protect({
for 1..10000 {
$output ~= 'b'
}
});
});
$t1.finish;
$t2.finish;
ok $output ~~ /^ [ a+: b+: | b+: a+: ] $/, 'Lock is at least somewhat effective';
}
{
my $l = Lock.new;
my $c = $l.condition;
my $now1;
my $now2 = now;
my @log;
my $t1 = Thread.start({
$l.protect({
@log.push('ale');
$c.wait();
@log.push('stout');
$now1 = now;
});
});
until $l.protect({ @log.elems }) == 1 { }
my $t2 = Thread.start({
$l.protect({
@log.push('porter');
$c.signal();
});
});
$t1.join();
$now2 = now;
$t2.join();
diag "log = {@log}{ $now1>$now2 ?? ', thread was running *after* join' !! ''}" if !
is @log.join(','), 'ale,porter,stout', 'Condition variable worked';
}
{
my $times = 100;
my $tried;
my $failed;
for ^$times {
my $l = Lock.new;
my $c = $l.condition;
my $now1;
my $now2;
my $counter = 0;
my $t1 = Thread.start({
$l.protect({
while $counter == 0 {
$c.wait();
}
$now1 = now;
});
});
my $t2 = Thread.start({
$l.protect({
$counter++;
$c.signal();
});
});
$t1.join();
$now2 = now;
$t2.join();
$tried++;
last if $failed = ( !$now1.defined or $now1 > $now2 );
}
ok !$failed, "Thread 1 never ran after it was tried $tried times";
}
{
for 1..15 -> $iter {
my $lock = Lock.new;
my $cond = $lock.condition;
my $todo = 0;
my $done = 0;
my @in = 1..100;
my @out;
loop ( my $i = 0; $i < @in; $i++ ) {
my $in := @in[$i];
my $out := @out[$i];
$lock.protect( {
$*SCHEDULER.cue( {
$out = $in * 10;
$lock.protect( {
$done++;
$cond.signal if $done == $todo;
} );
} );
$todo++;
} );
}
$lock.protect( { $cond.wait unless $done == $todo; } );
is @out, [10, 20 ... 1000],
"Correct result and no crash in lock/condvar use ($iter)";
}
}