1 # cond_wait and cond_timedwait extended tests
9 require Config; import Config;
10 unless ($Config{'useithreads'}) {
11 print "1..0 # Skip: no threads\n";
21 use ExtUtils::testlib;
26 my ($offset, $bool, $text) = @_;
28 $not = "not " unless $bool;
29 print "${not}ok " . ($Base + $offset) . " - $text\n";
34 ok(1, defined &cond_wait, "cond_wait() present");
35 ok(2, (prototype(\&cond_wait) eq '\[$@%];\[$@%]'),
36 q|cond_wait() prototype '\[$@%];\[$@%]'|);
37 ok(3, defined &cond_timedwait, "cond_timedwait() present");
38 ok(4, (prototype(\&cond_timedwait) eq '\[$@%]$;\[$@%]'),
39 q|cond_timedwait() prototype '\[$@%]$;\[$@%]'|);
44 "simple", # cond var == lock var; implicit lock; e.g.: cond_wait($c)
45 "repeat", # cond var == lock var; explicit lock; e.g.: cond_wait($c, $c)
46 "twain" # cond var != lock var; explicit lock; e.g.: cond_wait($c, $l)
50 my $test : shared; # simple|repeat|twain
54 print "# testing my \$var : shared\n";
55 ok(1, 1, "Shared synchronization tests preparation");
59 ok(2,1,"$test: child before lock");
60 $test =~ /twain/ ? lock($lock) : lock($cond);
61 ok(3,1,"$test: child obtained lock");
62 if ($test =~ 'twain') {
63 no warnings 'threads'; # lock var != cond var, so disable warnings
68 ok(4,1,"$test: child signalled condition");
73 $test = "cond_wait [$_]";
74 threads->create(\&cw)->join;
81 { # -- begin lock scope; which lock to obtain?
82 $test =~ /twain/ ? lock($lock) : lock($cond);
83 ok(1,1, "$test: obtained initial lock");
85 $thr = threads->create(\&signaller);
87 cond_wait($cond), last if /simple/;
88 cond_wait($cond, $cond), last if /repeat/;
89 cond_wait($cond, $lock), last if /twain/;
90 die "$test: unknown test\n";
92 ok(5,1, "$test: condition obtained");
96 ok(6,1, "$test: join completed");
99 # - TEST cond_timedwait success
101 foreach (@wait_how) {
102 $test = "cond_timedwait [$_]";
103 threads->create(\&ctw, 5)->join;
111 { # -- begin lock scope; which lock to obtain?
112 $test =~ /twain/ ? lock($lock) : lock($cond);
113 ok(1,1, "$test: obtained initial lock");
115 $thr = threads->create(\&signaller);
118 $ok=cond_timedwait($cond, time() + $to), last if /simple/;
119 $ok=cond_timedwait($cond, time() + $to, $cond), last if /repeat/;
120 $ok=cond_timedwait($cond, time() + $to, $lock), last if /twain/;
121 die "$test: unknown test\n";
123 ok(5,$ok, "$test: condition obtained");
124 } # -- end lock scope
127 ok(6,1, "$test: join completed");
130 # - TEST cond_timedwait timeout
132 foreach (@wait_how) {
133 $test = "cond_timedwait pause, timeout [$_]";
134 threads->create(\&ctw_fail, 3)->join;
138 foreach (@wait_how) {
139 $test = "cond_timedwait instant timeout [$_]";
140 threads->create(\&ctw_fail, -60)->join;
144 # cond_timedwait timeout (relative timeout)
148 $test =~ /twain/ ? lock($lock) : lock($cond);
149 ok(1,1, "$test: obtained initial lock");
152 $ok=cond_timedwait($cond, time() + $to), last if /simple/;
153 $ok=cond_timedwait($cond, time() + $to, $cond), last if /repeat/;
154 $ok=cond_timedwait($cond, time() + $to, $lock), last if /twain/;
155 die "$test: unknown test\n";
157 ok(2,!defined($ok), "$test: timeout");
160 } # -- SYNCH_SHARED block
163 # same as above, but with references to lock and cond vars
166 my $test : shared; # simple|repeat|twain
168 my $true_cond; share($true_cond);
169 my $true_lock; share($true_lock);
171 my $cond = \$true_cond;
172 my $lock = \$true_lock;
174 print "# testing reference to shared(\$var)\n";
175 ok(1, 1, "Synchronization reference tests preparation");
179 ok(2,1,"$test: child before lock");
180 $test =~ /twain/ ? lock($lock) : lock($cond);
181 ok(3,1,"$test: child obtained lock");
182 if ($test =~ 'twain') {
183 no warnings 'threads'; # lock var != cond var, so disable warnings
188 ok(4,1,"$test: child signalled condition");
192 foreach (@wait_how) {
193 $test = "cond_wait [$_]";
194 threads->create(\&cw2)->join;
201 { # -- begin lock scope; which lock to obtain?
202 $test =~ /twain/ ? lock($lock) : lock($cond);
203 ok(1,1, "$test: obtained initial lock");
205 $thr = threads->create(\&signaller2);
207 cond_wait($cond), last if /simple/;
208 cond_wait($cond, $cond), last if /repeat/;
209 cond_wait($cond, $lock), last if /twain/;
210 die "$test: unknown test\n";
212 ok(5,1, "$test: condition obtained");
213 } # -- end lock scope
216 ok(6,1, "$test: join completed");
219 # - TEST cond_timedwait success
221 foreach (@wait_how) {
222 $test = "cond_timedwait [$_]";
223 threads->create(\&ctw2, 5)->join;
231 { # -- begin lock scope; which lock to obtain?
232 $test =~ /twain/ ? lock($lock) : lock($cond);
233 ok(1,1, "$test: obtained initial lock");
235 $thr = threads->create(\&signaller2);
238 $ok=cond_timedwait($cond, time() + $to), last if /simple/;
239 $ok=cond_timedwait($cond, time() + $to, $cond), last if /repeat/;
240 $ok=cond_timedwait($cond, time() + $to, $lock), last if /twain/;
241 die "$test: unknown test\n";
243 ok(5,$ok, "$test: condition obtained");
244 } # -- end lock scope
247 ok(6,1, "$test: join completed");
250 # - TEST cond_timedwait timeout
252 foreach (@wait_how) {
253 $test = "cond_timedwait pause, timeout [$_]";
254 threads->create(\&ctw_fail2, 3)->join;
258 foreach (@wait_how) {
259 $test = "cond_timedwait instant timeout [$_]";
260 threads->create(\&ctw_fail2, -60)->join;
267 $test =~ /twain/ ? lock($lock) : lock($cond);
268 ok(1,1, "$test: obtained initial lock");
271 $ok=cond_timedwait($cond, time() + $to), last if /simple/;
272 $ok=cond_timedwait($cond, time() + $to, $cond), last if /repeat/;
273 $ok=cond_timedwait($cond, time() + $to, $lock), last if /twain/;
274 die "$test: unknown test\n";
276 ok(2,!$ok, "$test: timeout");
279 } # -- SYNCH_REFS block