use ExtUtils::testlib;
use threads;
-use threads::shared;
BEGIN {
+ eval {
+ require threads::shared;
+ import threads::shared;
+ };
+ if ($@ || ! $threads::shared::threads_shared) {
+ print("1..0 # Skip: threads::shared not available\n");
+ exit(0);
+ }
+
$| = 1;
print("1..29\n"); ### Number of tests that will be run ###
};
-my $TEST = 1;
-share($TEST);
+my $TEST;
+BEGIN {
+ share($TEST);
+ $TEST = 1;
+}
ok(1, 'Loaded');
# Tests freeing the Perl interperter for each thread
# See http://www.nntp.perl.org/group/perl.perl5.porters/110772 for details
-my $COUNT;
-share($COUNT);
+my ($COUNT, $STARTED) :shared;
sub threading_1 {
my $tid = threads->tid();
ok($tid, "Thread $tid started");
- if ($tid < 5) {
+ my $id;
+ {
+ lock($STARTED);
+ $STARTED++;
+ $id = $STARTED;
+ }
+ if ($STARTED < 5) {
sleep(1);
threads->create('threading_1')->detach();
}
- threads->yield();
-
- if ($tid == 1) {
+ if ($id == 1) {
sleep(2);
- } elsif ($tid == 2) {
+ } elsif ($id == 2) {
sleep(6);
- } elsif ($tid == 3) {
+ } elsif ($id == 3) {
sleep(3);
- } elsif ($tid == 4) {
+ } elsif ($id == 4) {
sleep(1);
} else {
sleep(2);
}
{
+ $STARTED = 0;
$COUNT = 0;
threads->create('threading_1')->detach();
{
- lock($COUNT);
- while ($COUNT < 3) {
- cond_wait($COUNT);
- }
- }
-}
-{
- {
- lock($COUNT);
- while ($COUNT < 5) {
- cond_wait($COUNT);
+ my $cnt = 0;
+ while ($cnt < 5) {
+ {
+ lock($COUNT);
+ cond_wait($COUNT) if ($COUNT < 5);
+ $cnt = $COUNT;
+ }
+ threads->create(sub {
+ threads->create(sub { })->join();
+ })->join();
}
}
- threads->yield();
sleep(1);
}
ok($COUNT == 5, "Done - $COUNT threads");
my $tid = threads->tid();
ok($tid, "Thread $tid started");
- if ($tid < 10) {
+ {
+ lock($STARTED);
+ $STARTED++;
+ }
+ if ($STARTED < 5) {
threads->create('threading_2')->detach();
}
-
threads->yield();
lock($COUNT);
}
{
+ $STARTED = 0;
$COUNT = 0;
threads->create('threading_2')->detach();
+ threads->create(sub {
+ threads->create(sub { })->join();
+ })->join();
{
lock($COUNT);
- while ($COUNT < 3) {
+ while ($COUNT < 5) {
cond_wait($COUNT);
}
}
- threads->yield();
sleep(1);
}
ok($COUNT == 5, "Done - $COUNT threads");
my $tid = threads->tid();
ok($tid, "Thread $tid started");
- threads->yield();
sleep(1);
lock($COUNT);
cond_signal($COUNT);
ok($tid, "Thread $tid done");
- })->join();
+ })->detach();
}
lock($COUNT);
}
}
})->join();
- threads->yield();
sleep(1);
}
ok($COUNT == 2, "Done - $COUNT threads");