5 if ($ENV{'PERL_CORE'}){
7 unshift @INC, '../lib';
10 # Import test.pl into its own package
13 require($ENV{PERL_CORE} ? './test.pl' : './t/test.pl');
17 if (! $Config{'useithreads'}) {
18 Test::skip_all(q/Perl not compiled with 'useithreads'/);
21 if (! eval 'use Time::HiRes "time"; 1') {
22 Test::skip_all('Time::HiRes not available');
26 use ExtUtils::testlib;
29 my ($id, $ok, $name) = @_;
31 # You have to do it this way or VMS will get confused.
33 print("ok $id - $name\n");
35 print("not ok $id - $name\n");
36 printf("# Failed test at line %d\n", (caller)[2]);
44 print("1..57\n"); ### Number of tests that will be run ###
50 Test::watchdog(60); # In case we get stuck
53 ok($TEST++, 1, 'Loaded');
55 ### Start of Testing ###
57 # subsecond cond_timedwait extended tests adapted from wait.t
59 # The two skips later on in these tests refer to this quote from the
60 # pod/perl583delta.pod:
62 # =head1 Platform Specific Problems
64 # The regression test ext/threads/shared/t/wait.t fails on early RedHat 9
65 # and HP-UX 10.20 due to bugs in their threading implementations.
66 # RedHat users should see https://rhn.redhat.com/errata/RHBA-2003-136.html
67 # and consider upgrading their glibc.
73 "simple", # cond var == lock var; implicit lock; e.g.: cond_wait($c)
74 "repeat", # cond var == lock var; explicit lock; e.g.: cond_wait($c, $c)
75 "twain" # cond var != lock var; explicit lock; e.g.: cond_wait($c, $l)
80 my $test_type :shared; # simple|repeat|twain
85 ok($TEST++, 1, "Shared synchronization tests preparation");
87 # - TEST cond_timedwait success
93 ok($testno++, 1, "$test_type: child before lock");
94 $test_type =~ /twain/ ? lock($lock) : lock($cond);
95 ok($testno++, 1, "$test_type: child obtained lock");
97 if ($test_type =~ 'twain') {
98 no warnings 'threads'; # lock var != cond var, so disable warnings
103 ok($testno++, 1, "$test_type: child signalled condition");
110 my ($testnum, $to) = @_;
112 # Which lock to obtain?
113 $test_type =~ /twain/ ? lock($lock) : lock($cond);
114 ok($testnum++, 1, "$test_type: obtained initial lock");
116 my $thr = threads->create(\&signaller, $testnum);
119 $ok = cond_timedwait($cond, time() + $to), last if /simple/;
120 $ok = cond_timedwait($cond, time() + $to, $cond), last if /repeat/;
121 $ok = cond_timedwait($cond, time() + $to, $lock), last if /twain/;
122 die "$test_type: unknown test\n";
124 $testnum = $thr->join();
125 ok($testnum++, $ok, "$test_type: condition obtained");
130 foreach (@wait_how) {
131 $test_type = "cond_timedwait [$_]";
132 my $thr = threads->create(\&ctw_ok, $TEST, 0.1);
133 $TEST = $thr->join();
136 # - TEST cond_timedwait timeout
140 my ($testnum, $to) = @_;
142 if ($^O eq "hpux" && $Config{osvers} <= 10.20) {
143 # The lock obtaining would pass, but the wait will not.
144 ok($testnum++, 1, "$test_type: obtained initial lock");
145 ok($testnum++, 0, "# SKIP see perl583delta");
148 $test_type =~ /twain/ ? lock($lock) : lock($cond);
149 ok($testnum++, 1, "$test_type: 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_type: unknown test\n";
157 ok($testnum++, ! defined($ok), "$test_type: timeout");
163 foreach (@wait_how) {
164 $test_type = "cond_timedwait pause, timeout [$_]";
165 my $thr = threads->create(\&ctw_fail, $TEST, 0.3);
166 $TEST = $thr->join();
169 foreach (@wait_how) {
170 $test_type = "cond_timedwait instant timeout [$_]";
171 my $thr = threads->create(\&ctw_fail, $TEST, -0.60);
172 $TEST = $thr->join();
175 } # -- SYNCH_SHARED block
178 # same as above, but with references to lock and cond vars
181 my $test_type :shared; # simple|repeat|twain
183 my $true_cond :shared;
184 my $true_lock :shared;
186 my $cond = \$true_cond;
187 my $lock = \$true_lock;
189 ok($TEST++, 1, "Synchronization reference tests preparation");
191 # - TEST cond_timedwait success
197 ok($testno++, 1, "$test_type: child before lock");
198 $test_type =~ /twain/ ? lock($lock) : lock($cond);
199 ok($testno++, 1, "$test_type: child obtained lock");
201 if ($test_type =~ 'twain') {
202 no warnings 'threads'; # lock var != cond var, so disable warnings
207 ok($testno++, 1, "$test_type: child signalled condition");
214 my ($testnum, $to) = @_;
216 # Which lock to obtain?
217 $test_type =~ /twain/ ? lock($lock) : lock($cond);
218 ok($testnum++, 1, "$test_type: obtained initial lock");
220 my $thr = threads->create(\&signaller2, $testnum);
223 $ok = cond_timedwait($cond, time() + $to), last if /simple/;
224 $ok = cond_timedwait($cond, time() + $to, $cond), last if /repeat/;
225 $ok = cond_timedwait($cond, time() + $to, $lock), last if /twain/;
226 die "$test_type: unknown test\n";
228 $testnum = $thr->join();
229 ok($testnum++, $ok, "$test_type: condition obtained");
234 foreach (@wait_how) {
235 $test_type = "cond_timedwait [$_]";
236 my $thr = threads->create(\&ctw_ok2, $TEST, 0.05);
237 $TEST = $thr->join();
240 # - TEST cond_timedwait timeout
244 my ($testnum, $to) = @_;
246 if ($^O eq "hpux" && $Config{osvers} <= 10.20) {
247 # The lock obtaining would pass, but the wait will not.
248 ok($testnum++, 1, "$test_type: obtained initial lock");
249 ok($testnum++, 0, "# SKIP see perl583delta");
252 $test_type =~ /twain/ ? lock($lock) : lock($cond);
253 ok($testnum++, 1, "$test_type: obtained initial lock");
256 $ok = cond_timedwait($cond, time() + $to), last if /simple/;
257 $ok = cond_timedwait($cond, time() + $to, $cond), last if /repeat/;
258 $ok = cond_timedwait($cond, time() + $to, $lock), last if /twain/;
259 die "$test_type: unknown test\n";
261 ok($testnum++, ! defined($ok), "$test_type: timeout");
267 foreach (@wait_how) {
268 $test_type = "cond_timedwait pause, timeout [$_]";
269 my $thr = threads->create(\&ctw_fail2, $TEST, 0.3);
270 $TEST = $thr->join();
273 foreach (@wait_how) {
274 $test_type = "cond_timedwait instant timeout [$_]";
275 my $thr = threads->create(\&ctw_fail2, $TEST, -0.60);
276 $TEST = $thr->join();
279 } # -- SYNCH_REFS block