1 |
root |
1.1 |
$|=1; |
2 |
root |
1.2 |
print "1..2\n"; |
3 |
root |
1.1 |
|
4 |
|
|
use Coro; |
5 |
|
|
use Coro::Semaphore; |
6 |
|
|
|
7 |
root |
1.3 |
{ |
8 |
|
|
my $sem = new Coro::Semaphore 2; |
9 |
root |
1.1 |
|
10 |
root |
1.3 |
my $rand = 0; |
11 |
root |
1.1 |
|
12 |
root |
1.3 |
sub xrand { |
13 |
|
|
$rand = ($rand * 121 + 2121) % 212121; |
14 |
|
|
$rand / 212120 |
15 |
|
|
} |
16 |
|
|
|
17 |
|
|
my $counter; |
18 |
|
|
|
19 |
|
|
$_->join for |
20 |
|
|
map { |
21 |
|
|
async { |
22 |
|
|
my $current = $Coro::current; |
23 |
|
|
for (1..100) { |
24 |
|
|
cede if 0.2 > xrand; |
25 |
|
|
Coro::async_pool { $current->ready } if 0.2 > xrand; |
26 |
|
|
$counter += $sem->count; |
27 |
|
|
my $guard = $sem->guard; |
28 |
|
|
cede; cede; cede; cede; |
29 |
|
|
} |
30 |
root |
1.1 |
} |
31 |
root |
1.3 |
} 1..15 |
32 |
|
|
; |
33 |
root |
1.1 |
|
34 |
root |
1.3 |
print $counter == 998 ? "" : "not ", "ok 1 # $counter\n"; |
35 |
|
|
} |
36 |
root |
1.2 |
|
37 |
|
|
{ |
38 |
|
|
my $sem = new Coro::Semaphore 0; |
39 |
|
|
|
40 |
|
|
$as1 = async { |
41 |
|
|
my $g = $sem->guard; |
42 |
|
|
print "not ok 2\n"; |
43 |
|
|
}; |
44 |
|
|
|
45 |
|
|
$as2 = async { |
46 |
|
|
my $g = $sem->guard; |
47 |
|
|
print "ok 2\n"; |
48 |
|
|
}; |
49 |
|
|
|
50 |
|
|
cede; |
51 |
|
|
|
52 |
|
|
$sem->up; # wake up as1 |
53 |
|
|
$as1->cancel; # destroy as1 before it could ->guard |
54 |
|
|
$as1->join; |
55 |
|
|
$as2->join; |
56 |
|
|
} |
57 |
|
|
|
58 |
root |
1.1 |
|