6 require Config; import Config;
7 if (! $Config{'use5005threads'}) {
8 print "1..0 # Skip: no use5005threads\n";
12 # XXX known trouble with global destruction
13 $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
26 # create a thread passing args and immedaietly wait for it.
27 my $t = new Thread \&content,("ok 2\n","ok 3\n", 1..1000);
30 # check that lock works ...
32 $t = new Thread sub { lock $foo; print "ok 5\n" };
44 $ret = Thread->new(\&dorecurse, @_);
49 $t = new Thread \&dorecurse, map { "ok $_\n" } 6..10;
52 # test that sleep lets other thread run
53 $t = new Thread \&dorecurse,"ok 11\n";
58 sub islocked : locked {
64 $ret = Thread->new(\&islocked, shift);
69 $t = Thread->new(\&islocked, "ok 13\n", "ok 14\n");
74 sub new { bless [], shift }
75 sub monster : locked : method {
79 sub gollum { &monster }
81 Loch::Ness->monster(15);
82 Loch::Ness->new->monster(16);
83 Loch::Ness->gollum(17);
84 Loch::Ness->new->gollum(18);
86 my $short = "This is a long string that goes on and on.";
87 my $shorte = " a long string that goes on and on.";
88 my $long = "This is short.";
89 my $longe = " short.";
90 my $thr1 = new Thread \&threaded, $short, $shorte, "19";
91 my $thr2 = new Thread \&threaded, $long, $longe, "20";
92 my $thr3 = new Thread \&testsprintf, "21";
96 # this may coredump if thread vars are not properly initialised
97 my $same = sprintf "%.0f", $testno;
98 if ($testno eq $same) {
101 print "not ok $testno\t# '$testno' ne '$same'\n";
106 my ($string, $string_end, $testno) = @_;
108 # Do the match, saving the output in appropriate variables
109 $string =~ /(.*)(is)(.*)/;
110 # Yield control, allowing the other thread to fill in the match variables
112 # Examine the match variable contents; on broken perls this fails
113 if ($3 eq $string_end) {
114 print "ok $testno\n";
120 # This is a KNOWN FAILURE, and one of the reasons why threading
121 # is still an experimental feature. It is here to stop people
122 # from deploying threads in production. ;-)
125 print "not ok $testno # other thread filled in match variables\n";
134 my $THRf_STATE_MASK = 7;
135 my $THRf_R_JOINABLE = 0;
136 my $THRf_R_JOINED = 1;
137 my $THRf_R_DETACHED = 2;
140 my $THRf_DID_DIE = 8;
142 my($test, $t, $state, $die) = @_;
143 my $flags = $t->flags;
144 if (($flags & $THRf_STATE_MASK) == $state
145 && !($flags & $THRf_DID_DIE) == !$die) {
149 not ok $test\t# got flags $flags not @{[ $state + ($die ? $THRf_DID_DIE : 0) ]}
156 Thread->new(sub { sleep 4; die "thread die\n" }),
157 Thread->new(sub { die "thread die\n" }),
158 Thread->new(sub { sleep 4; 1 }),
159 Thread->new(sub { 1 }),
161 $_->detach for @t[grep $_ & 4, 0..$#t];
168 ? ($_ & 4) ? $THRf_DEAD : $THRf_ZOMBIE
169 : ($_ & 4) ? $THRf_R_DETACHED : $THRf_R_JOINABLE;
170 _test($test++, $t, $flags, (($_ & 3) != 1) ? 0 : $THRf_DID_DIE);
171 printf "%sok %s\n", !$t->done == !($_ & 1) ? "" : "not ", $test++;
174 for (grep $_ & 1, 0..$#t) {
175 next if $_ & 4; # can't join detached threads
177 my $die = ($_ & 2) ? "" : "thread die\n";
178 printf "%sok %s\n", $@ eq $die ? "" : "not ", $test++;
184 ? ($_ & 4) ? $THRf_DEAD : $THRf_DEAD
185 : ($_ & 4) ? $THRf_R_DETACHED : $THRf_R_JOINABLE;
186 _test($test++, $t, $flags, (($_ & 3) != 1) ? 0 : $THRf_DID_DIE);
187 printf "%sok %s\n", !$t->done == !($_ & 1) ? "" : "not ", $test++;
190 for (grep !($_ & 1), 0..$#t) {
191 next if $_ & 4; # can't join detached threads
193 my $die = ($_ & 2) ? "" : "thread die\n";
194 printf "%sok %s\n", $@ eq $die ? "" : "not ", $test++;
196 sleep 1; # make sure even the detached threads are done sleeping
201 ? ($_ & 4) ? $THRf_DEAD : $THRf_DEAD
202 : ($_ & 4) ? $THRf_DEAD : $THRf_DEAD;
203 _test($test++, $t, $flags, ($_ & 2) ? 0 : $THRf_DID_DIE);
204 printf "%sok %s\n", $t->done ? "" : "not ", $test++;