ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/t/15_semaphore.t
Revision: 1.5
Committed: Tue Mar 4 05:07:34 2014 UTC (10 years, 3 months ago) by root
Content type: application/x-troff
Branch: MAIN
CVS Tags: rel-6_512, rel-6_513, rel-6_511, rel-6_514, rel-6_5, rel-6_36, rel-6_37, rel-6_38, rel-6_39, rel-6_51, rel-6_52, rel-6_53, rel-6_54, rel-6_55, rel-6_56, rel-6_57, rel-6_43, rel-6_42, rel-6_41, rel-6_47, rel-6_46, rel-6_45, rel-6_44, rel-6_49, rel-6_48, HEAD
Changes since 1.4: +6 -6 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 $|=1;
2 root 1.4 print "1..6\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 root 1.4 # check terminate
38 root 1.2 {
39     my $sem = new Coro::Semaphore 0;
40    
41     $as1 = async {
42     my $g = $sem->guard;
43     print "not ok 2\n";
44 root 1.5 };
45 root 1.2
46     $as2 = async {
47     my $g = $sem->guard;
48     print "ok 2\n";
49 root 1.5 };
50 root 1.2
51     cede;
52    
53     $sem->up; # wake up as1
54     $as1->cancel; # destroy as1 before it could ->guard
55     $as1->join;
56     $as2->join;
57     }
58    
59 root 1.4 # check throw
60     {
61     my $sem = new Coro::Semaphore 0;
62    
63     $as1 = async {
64     my $g = eval {
65     $sem->guard;
66     };
67     print $@ ? "" : "not ", "ok 3\n";
68 root 1.5 };
69 root 1.4
70     $as2 = async {
71     my $g = $sem->guard;
72     print "ok 4\n";
73 root 1.5 };
74 root 1.4
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 root 1.5 };
91 root 1.4
92     $as2 = async {
93     my $g = $sem->guard;
94     print "ok 6\n";
95 root 1.5 };
96 root 1.4
97     cede;
98    
99     $sem->up; # wake up as1
100     $as1->join;
101     $as2->join;
102     }
103    
104 root 1.1