1 | $|=1; |
1 | $|=1; |
2 | print "1..2\n"; |
2 | print "1..6\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 | # check terminate |
17 | |
|
|
18 | $_->join for |
|
|
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; |
|
|
27 | cede; cede; cede; cede; |
|
|
28 | } |
|
|
29 | } |
|
|
30 | } 1..15 |
|
|
31 | ; |
|
|
32 | |
|
|
33 | print $counter == 750 ? "" : "not ", "ok 1 # $counter\n"; |
|
|
34 | |
|
|
35 | { |
38 | { |
36 | my $sem = new Coro::Semaphore 0; |
39 | my $sem = new Coro::Semaphore 0; |
37 | |
40 | |
38 | $as1 = async { |
41 | $as1 = async { |
39 | my $g = $sem->guard; |
42 | my $g = $sem->guard; |
… | |
… | |
51 | $as1->cancel; # destroy as1 before it could ->guard |
54 | $as1->cancel; # destroy as1 before it could ->guard |
52 | $as1->join; |
55 | $as1->join; |
53 | $as2->join; |
56 | $as2->join; |
54 | } |
57 | } |
55 | |
58 | |
|
|
59 | # check throw |
|
|
60 | { |
|
|
61 | my $sem = new Coro::Semaphore 0; |
56 | |
62 | |
|
|
63 | $as1 = async { |
|
|
64 | my $g = eval { |
|
|
65 | $sem->guard; |
|
|
66 | }; |
|
|
67 | print $@ ? "" : "not ", "ok 3\n"; |
|
|
68 | }; |
|
|
69 | |
|
|
70 | $as2 = async { |
|
|
71 | my $g = $sem->guard; |
|
|
72 | print "ok 4\n"; |
|
|
73 | }; |
|
|
74 | |
|
|
75 | cede; |
|
|
76 | |
|
|
77 | $sem->up; # wake up as1 |
|
|
78 | $as1->throw (1); # destroy as1 before it could ->guard |
|
|
79 | $as1->join; |
|
|
80 | $as2->join; |
|
|
81 | } |
|
|
82 | |
|
|
83 | # check wait |
|
|
84 | { |
|
|
85 | my $sem = new Coro::Semaphore 0; |
|
|
86 | |
|
|
87 | $as1 = async { |
|
|
88 | $sem->wait; |
|
|
89 | print "ok 5\n"; |
|
|
90 | }; |
|
|
91 | |
|
|
92 | $as2 = async { |
|
|
93 | my $g = $sem->guard; |
|
|
94 | print "ok 6\n"; |
|
|
95 | }; |
|
|
96 | |
|
|
97 | cede; |
|
|
98 | |
|
|
99 | $sem->up; # wake up as1 |
|
|
100 | $as1->join; |
|
|
101 | $as2->join; |
|
|
102 | } |
|
|
103 | |
|
|
104 | |