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