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'/);
22 use ExtUtils::testlib;
27 if (! eval 'use threads::shared; 1') {
28 Test::skip_all(q/threads::shared not available/);
31 if (($] < 5.008002) && ($threads::shared::VERSION < 0.92)) {
32 Test::skip_all(q/Needs threads::shared 0.92 or later/);
35 require Thread::Queue;
38 print("1..78\n"); ### Number of tests that will be run ###
41 Test::watchdog(60); # In case we get stuck
43 my $q = Thread::Queue->new();
48 $q->enqueue(@_) if @_;
50 while ($q->pending()) {
51 my $ok = $q->dequeue();
52 my $name = $q->dequeue();
56 print("ok $id - $name\n");
58 print("not ok $id - $name\n");
59 printf("# Failed test at line %d\n", (caller)[2]);
66 ### Start of Testing ###
69 # Tests freeing the Perl interperter for each thread
70 # See http://www.nntp.perl.org/group/perl.perl5.porters/110772 for details
81 my $tid = threads->tid();
82 $q->enqueue($tid, "Thread $tid started");
92 my $next = 'th' . $tid;
93 my $th = threads->create($next, $q);
95 # Last thread signals first
99 # Wait until signalled by another thread
100 while (! exists($READY{$tid})) {
103 $other = delete($READY{$tid});
105 $q->enqueue($tid, "Thread $tid received signal from $other");
113 my $tid = threads->tid();
119 $q->enqueue($tid, "Thread $tid done");
122 # Signal another thread to go
128 my $tid = threads->tid();
130 $q->enqueue($tid, "Thread $tid signalling $other");
133 $READY{$other} = $tid;
134 cond_broadcast(%READY);
229 my $tid = threads->tid();
234 th_signal($q, $other);
236 $q->enqueue(1, "Thread $tid getting return from thread $other");
237 my $ret = threads->object($other+1)->join();
238 $q->enqueue($ret == $other+1, "Thread $tid saw that thread $other returned $ret");
247 return (threads->tid());
254 my $tid = threads->tid();
259 th_signal($q, $other);
260 $q->enqueue(1, "Thread $tid getting return from thread $other");
261 my $ret = threads->object($other+1)->join();
262 $q->enqueue($ret == $other+1, "Thread $tid saw that thread $other returned $ret");
272 return (threads->tid());
279 my $tid = threads->tid();
284 th_signal($q, $other);
286 $q->enqueue(1, "Thread $tid getting return from thread $other");
287 my $ret = threads->object($other+1)->join();
288 $q->enqueue($ret == $other+1, "Thread $tid saw that thread $other returned $ret");
297 return (threads->tid());
304 my $tid = threads->tid();
309 th_signal($q, $other);
310 $q->enqueue(1, "Thread $tid getting return from thread $other");
311 my $ret = threads->object($other+1)->join();
312 $q->enqueue($ret == $other+1, "Thread $tid saw that thread $other returned $ret");
322 return (threads->tid());
329 threads->create('th1', $q);
332 while ($COUNT < 17) {
334 ok(); # Prints out any intermediate results
339 ok($COUNT == 17, "Done - $COUNT threads");