ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/t/15_semaphore.t
Revision: 1.2
Committed: Sun Nov 16 09:43:18 2008 UTC (15 years, 7 months ago) by root
Content type: application/x-troff
Branch: MAIN
Changes since 1.1: +24 -2 lines
Log Message:
be good citizen

File Contents

# Content
1 $|=1;
2 print "1..2\n";
3
4 use Coro;
5 use Coro::Semaphore;
6
7 my $sem = new Coro::Semaphore 2;
8
9 my $rand = 0;
10
11 sub xrand {
12 $rand = ($rand * 121 + 2121) % 212121;
13 $rand / 212120
14 }
15
16 my $counter;
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 {
36 my $sem = new Coro::Semaphore 0;
37
38 $as1 = async {
39 my $g = $sem->guard;
40 print "not ok 2\n";
41 };
42
43 $as2 = async {
44 my $g = $sem->guard;
45 print "ok 2\n";
46 };
47
48 cede;
49
50 $sem->up; # wake up as1
51 $as1->cancel; # destroy as1 before it could ->guard
52 $as1->join;
53 $as2->join;
54 }
55
56