6 if (! $Config{'useithreads'}) {
7 print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
12 use ExtUtils::testlib;
15 my ($id, $ok, $name) = @_;
17 # You have to do it this way or VMS will get confused.
19 print("ok $id - $name\n");
21 print("not ok $id - $name\n");
22 printf("# Failed test at line %d\n", (caller)[2]);
30 print("1..33\n"); ### Number of tests that will be run ###
35 if ($threads::VERSION && ! $ENV{'PERL_CORE'}) {
36 print(STDERR "# Testing threads $threads::VERSION\n");
41 ### Start of Testing ###
43 ok(2, 1 == $threads::threads, "Check that threads::threads is true");
46 ok(3,'bar' eq $_[0], "Test that argument passing works");
48 threads->create('test1', 'bar')->join();
51 ok(4,'bar' eq $_[0]->[0]->{'foo'}, "Test that passing arguments as references work");
53 threads->create(\&test2, [{'foo' => 'bar'}])->join();
56 ok(5, shift() == 1, "Test a normal sub");
58 threads->create(\&test3, 1)->join();
62 ok(6, 1, "Detach test");
65 my $thread1 = threads->create('test4');
67 while ($thread1->is_running()) {
72 ok(7, 1, "Detach test");
76 threads->create('test6')->join();
77 ok(9, 1, "Nested thread test");
81 ok(8, 1, "Nested thread test");
84 threads->create('test5')->join();
88 my $self = threads->self();
89 ok(10, $self->tid == 7, "Wanted 7, got ".$self->tid);
90 ok(11, threads->tid() == 7, "Wanted 7, got ".threads->tid());
92 threads->create('test7')->join;
95 my $self = threads->self();
96 ok(12, $self->tid == 8, "Wanted 8, got ".$self->tid);
97 ok(13, threads->tid() == 8, "Wanted 8, got ".threads->tid());
99 threads->create('test8')->join;
102 ok(14, 0 == threads->self->tid(), "Check so that tid for threads work for main thread");
103 ok(15, 0 == threads->tid(), "Check so that tid for threads work for main thread");
108 ok(16, threads->tid() == 9, "Tid should be correct in the clone");
110 threads->create(sub {
111 ok(17, threads->tid() == 9, "And tid be 9 here too");
117 ok(19, threads->tid() == 10, "In destroy it should be correct too" )
120 threads->create(sub {
121 ok(18, threads->tid() == 10, "And tid be 10 here");
122 $foo = bless {}, 'Foo';
128 my $thr1 = threads->create(sub {});
129 my $thr2 = threads->create(sub {});
130 my $thr3 = threads->object($thr1->tid());
132 # Make sure both overloaded '==' and '!=' are working correctly
133 ok(20, $thr1 != $thr2, 'Treads not equal');
134 ok(21, !($thr1 == $thr2), 'Treads not equal');
135 ok(22, $thr1 == $thr3, 'Threads equal');
136 ok(23, !($thr1 != $thr3), 'Threads equal');
138 ok(24, $thr1->_handle(), 'Handle method');
139 ok(25, $thr2->_handle(), 'Handle method');
141 ok(26, threads->object($thr1->tid())->tid() == 11, 'Object method');
142 ok(27, threads->object($thr2->tid())->tid() == 12, 'Object method');
147 my $sub = sub { ok(28, shift() == 1, "Test code ref"); };
148 threads->create($sub, 1)->join();
150 my $thrx = threads->object(99);
151 ok(29, ! defined($thrx), 'No object');
152 $thrx = threads->object();
153 ok(30, ! defined($thrx), 'No object');
154 $thrx = threads->object(undef);
155 ok(31, ! defined($thrx), 'No object');
156 $thrx = threads->object(0);
157 ok(32, ! defined($thrx), 'No object');
159 threads->import('stringify');
160 $thr1 = threads->create(sub {});
161 ok(33, "$thr1" eq $thr1->tid(), 'Stringify');