1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
use warnings;
use strict;
BEGIN {
eval { require threads; };
if($@ =~ /\AThis Perl not built to support threads/) {
require Test::More;
Test::More::plan(skip_all => "non-threading perl build");
}
if($@ ne "") {
require Test::More;
Test::More::plan(skip_all => "threads unavailable");
}
if("$]" < 5.008003) {
require Test::More;
Test::More::plan(skip_all =>
"threading breaks PL_sv_placeholder on this Perl");
}
if("$]" < 5.008009) {
require Test::More;
Test::More::plan(skip_all =>
"threading corrupts memory on this Perl");
}
if("$]" >= 5.009005 && "$]" < 5.010001) {
require Test::More;
Test::More::plan(skip_all =>
"threading breaks assertions on this Perl");
}
eval { require Thread::Semaphore; };
if($@ ne "") {
require Test::More;
Test::More::plan(skip_all => "Thread::Semaphore unavailable");
}
eval { require threads::shared; };
if($@ ne "") {
require Test::More;
Test::More::plan(skip_all => "threads::shared unavailable");
}
}
use threads;
use Test::More tests => 5;
use Thread::Semaphore ();
use threads::shared;
alarm 10; # failure mode may involve an infinite loop
my(@exit_sems, @threads);
sub test_in_thread($) {
my($test_code) = @_;
my $done_sem = Thread::Semaphore->new(0);
my $exit_sem = Thread::Semaphore->new(0);
push @exit_sems, $exit_sem;
my $ok :shared;
push @threads, threads->create(sub {
$ok = !!$test_code->();
$done_sem->up;
$exit_sem->down;
});
$done_sem->down;
ok $ok;
}
sub basic_test {
no strict;
our @values = ();
$foo = $foo = 3;
eval q{
push @values, $foo;
use Lexical::Var '$foo' => \4;
push @values, $foo;
no Lexical::Var '$foo';
push @values, $foo;
} or die $@;
return join(",", @values) eq "3,4,3";
}
test_in_thread(\&basic_test) foreach 0..1;
ok basic_test();
test_in_thread(\&basic_test);
$_->up foreach @exit_sems;
$_->join foreach @threads;
ok 1;
1;
|