ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/t/15_semaphore.t
(Generate patch)

Comparing Coro/t/15_semaphore.t (file contents):
Revision 1.1 by root, Sat Nov 15 06:26:52 2008 UTC vs.
Revision 1.3 by root, Sun Nov 16 10:33:08 2008 UTC

1$|=1; 1$|=1;
2print "1..1\n"; 2print "1..2\n";
3 3
4use Coro; 4use Coro;
5use Coro::Semaphore; 5use Coro::Semaphore;
6 6
7{
7my $sem = new Coro::Semaphore 2; 8 my $sem = new Coro::Semaphore 2;
8 9
9my $rand = 0; 10 my $rand = 0;
10 11
11sub 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
16my $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
33print $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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines