1b39c5158Smillertuse strict; 2b39c5158Smillertuse warnings; 3b39c5158Smillert 4b39c5158SmillertBEGIN { 5b39c5158Smillert use Config; 6b39c5158Smillert if (! $Config{'useithreads'}) { 7b39c5158Smillert print("1..0 # SKIP Perl not compiled with 'useithreads'\n"); 8b39c5158Smillert exit(0); 9b39c5158Smillert } 10b39c5158Smillert} 11b39c5158Smillert 12b39c5158Smillertuse ExtUtils::testlib; 13b39c5158Smillert 14b39c5158Smillertsub ok { 15b39c5158Smillert my ($id, $ok, $name) = @_; 16b39c5158Smillert 17b39c5158Smillert # You have to do it this way or VMS will get confused. 18b39c5158Smillert if ($ok) { 19b39c5158Smillert print("ok $id - $name\n"); 20b39c5158Smillert } else { 21b39c5158Smillert print("not ok $id - $name\n"); 22b39c5158Smillert printf("# Failed test at line %d\n", (caller)[2]); 23b39c5158Smillert } 24b39c5158Smillert 25b39c5158Smillert return ($ok); 26b39c5158Smillert} 27b39c5158Smillert 28b39c5158SmillertBEGIN { 29b39c5158Smillert $| = 1; 30*898184e3Ssthen print("1..34\n"); ### Number of tests that will be run ### 31b39c5158Smillert}; 32b39c5158Smillert 33b39c5158Smillertuse threads; 34b39c5158Smillert 35b39c5158Smillertif ($threads::VERSION && ! $ENV{'PERL_CORE'}) { 36b39c5158Smillert print(STDERR "# Testing threads $threads::VERSION\n"); 37b39c5158Smillert} 38b39c5158Smillert 39b39c5158Smillertok(1, 1, 'Loaded'); 40b39c5158Smillert 41b39c5158Smillert### Start of Testing ### 42b39c5158Smillert 43b39c5158Smillertok(2, 1 == $threads::threads, "Check that threads::threads is true"); 44b39c5158Smillert 45b39c5158Smillertsub test1 { 46b39c5158Smillert ok(3,'bar' eq $_[0], "Test that argument passing works"); 47b39c5158Smillert} 48b39c5158Smillertthreads->create('test1', 'bar')->join(); 49b39c5158Smillert 50b39c5158Smillertsub test2 { 51b39c5158Smillert ok(4,'bar' eq $_[0]->[0]->{'foo'}, "Test that passing arguments as references work"); 52b39c5158Smillert} 53b39c5158Smillertthreads->create(\&test2, [{'foo' => 'bar'}])->join(); 54b39c5158Smillert 55b39c5158Smillertsub test3 { 56b39c5158Smillert ok(5, shift() == 1, "Test a normal sub"); 57b39c5158Smillert} 58b39c5158Smillertthreads->create(\&test3, 1)->join(); 59b39c5158Smillert 60b39c5158Smillert 61b39c5158Smillertsub test4 { 62b39c5158Smillert ok(6, 1, "Detach test"); 63b39c5158Smillert} 64b39c5158Smillert{ 65b39c5158Smillert my $thread1 = threads->create('test4'); 66b39c5158Smillert $thread1->detach(); 67b39c5158Smillert while ($thread1->is_running()) { 68b39c5158Smillert threads->yield(); 69b39c5158Smillert sleep 1; 70b39c5158Smillert } 71b39c5158Smillert} 72b39c5158Smillertok(7, 1, "Detach test"); 73b39c5158Smillert 74b39c5158Smillert 75b39c5158Smillertsub test5 { 76b39c5158Smillert threads->create('test6')->join(); 77b39c5158Smillert ok(9, 1, "Nested thread test"); 78b39c5158Smillert} 79b39c5158Smillert 80b39c5158Smillertsub test6 { 81b39c5158Smillert ok(8, 1, "Nested thread test"); 82b39c5158Smillert} 83b39c5158Smillert 84b39c5158Smillertthreads->create('test5')->join(); 85b39c5158Smillert 86b39c5158Smillert 87b39c5158Smillertsub test7 { 88b39c5158Smillert my $self = threads->self(); 89b39c5158Smillert ok(10, $self->tid == 7, "Wanted 7, got ".$self->tid); 90b39c5158Smillert ok(11, threads->tid() == 7, "Wanted 7, got ".threads->tid()); 91b39c5158Smillert} 92b39c5158Smillertthreads->create('test7')->join; 93b39c5158Smillert 94b39c5158Smillertsub test8 { 95b39c5158Smillert my $self = threads->self(); 96b39c5158Smillert ok(12, $self->tid == 8, "Wanted 8, got ".$self->tid); 97b39c5158Smillert ok(13, threads->tid() == 8, "Wanted 8, got ".threads->tid()); 98b39c5158Smillert} 99b39c5158Smillertthreads->create('test8')->join; 100b39c5158Smillert 101b39c5158Smillert 102b39c5158Smillertok(14, 0 == threads->self->tid(), "Check so that tid for threads work for main thread"); 103b39c5158Smillertok(15, 0 == threads->tid(), "Check so that tid for threads work for main thread"); 104b39c5158Smillert 105b39c5158Smillert{ 106b39c5158Smillert no warnings; 107b39c5158Smillert local *CLONE = sub { 108b39c5158Smillert ok(16, threads->tid() == 9, "Tid should be correct in the clone"); 109b39c5158Smillert }; 110b39c5158Smillert threads->create(sub { 111b39c5158Smillert ok(17, threads->tid() == 9, "And tid be 9 here too"); 112b39c5158Smillert })->join(); 113b39c5158Smillert} 114b39c5158Smillert 115b39c5158Smillert{ 116b39c5158Smillert sub Foo::DESTROY { 117b39c5158Smillert ok(19, threads->tid() == 10, "In destroy it should be correct too" ) 118b39c5158Smillert } 119b39c5158Smillert my $foo; 120b39c5158Smillert threads->create(sub { 121b39c5158Smillert ok(18, threads->tid() == 10, "And tid be 10 here"); 122b39c5158Smillert $foo = bless {}, 'Foo'; 123b39c5158Smillert return undef; 124b39c5158Smillert })->join(); 125b39c5158Smillert} 126b39c5158Smillert 127b39c5158Smillert 128b39c5158Smillertmy $thr1 = threads->create(sub {}); 129b39c5158Smillertmy $thr2 = threads->create(sub {}); 130b39c5158Smillertmy $thr3 = threads->object($thr1->tid()); 131b39c5158Smillert 132b39c5158Smillert# Make sure both overloaded '==' and '!=' are working correctly 133b39c5158Smillertok(20, $thr1 != $thr2, 'Treads not equal'); 134b39c5158Smillertok(21, !($thr1 == $thr2), 'Treads not equal'); 135b39c5158Smillertok(22, $thr1 == $thr3, 'Threads equal'); 136b39c5158Smillertok(23, !($thr1 != $thr3), 'Threads equal'); 137b39c5158Smillert 138b39c5158Smillertok(24, $thr1->_handle(), 'Handle method'); 139b39c5158Smillertok(25, $thr2->_handle(), 'Handle method'); 140b39c5158Smillert 141b39c5158Smillertok(26, threads->object($thr1->tid())->tid() == 11, 'Object method'); 142b39c5158Smillertok(27, threads->object($thr2->tid())->tid() == 12, 'Object method'); 143b39c5158Smillert 144b39c5158Smillert$thr1->join(); 145b39c5158Smillert$thr2->join(); 146b39c5158Smillert 147b39c5158Smillertmy $sub = sub { ok(28, shift() == 1, "Test code ref"); }; 148b39c5158Smillertthreads->create($sub, 1)->join(); 149b39c5158Smillert 150b39c5158Smillertmy $thrx = threads->object(99); 151b39c5158Smillertok(29, ! defined($thrx), 'No object'); 152b39c5158Smillert$thrx = threads->object(); 153b39c5158Smillertok(30, ! defined($thrx), 'No object'); 154b39c5158Smillert$thrx = threads->object(undef); 155b39c5158Smillertok(31, ! defined($thrx), 'No object'); 156b39c5158Smillert 157b39c5158Smillertthreads->import('stringify'); 158b39c5158Smillert$thr1 = threads->create(sub {}); 159*898184e3Ssthenok(32, "$thr1" eq $thr1->tid(), 'Stringify'); 160b39c5158Smillert$thr1->join(); 161b39c5158Smillert 162*898184e3Ssthen# ->object($tid) works like ->self() when $tid is thread's TID 163*898184e3Ssthen$thrx = threads->object(threads->tid()); 164*898184e3Ssthenok(33, defined($thrx), 'Main thread object'); 165*898184e3Ssthenok(34, 0 == $thrx->tid(), "Check so that tid for threads work for main thread"); 166*898184e3Ssthen 167b39c5158Smillertexit(0); 168b39c5158Smillert 169b39c5158Smillert# EOF 170