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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
use strict;
use warnings;
BEGIN {
if ($ENV{'PERL_CORE'}){
chdir 't';
unshift @INC, '../lib';
}
use Config;
if (! $Config{'useithreads'}) {
print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
exit(0);
}
}
use ExtUtils::testlib;
use threads;
BEGIN {
if (! eval 'use threads::shared; 1') {
print("1..0 # SKIP threads::shared not available\n");
exit(0);
}
local $SIG{'HUP'} = sub {};
my $thr = threads->create(sub {});
eval { $thr->kill('HUP') };
$thr->join();
if ($@ && $@ =~ /safe signals/) {
print("1..0 # SKIP Not using safe signals\n");
exit(0);
}
require Thread::Queue;
require Thread::Semaphore;
$| = 1;
print("1..18\n"); ### Number of tests that will be run ###
};
my $q = Thread::Queue->new();
my $TEST = 1;
sub ok
{
$q->enqueue(@_);
while ($q->pending()) {
my $ok = $q->dequeue();
my $name = $q->dequeue();
my $id = $TEST++;
if ($ok) {
print("ok $id - $name\n");
} else {
print("not ok $id - $name\n");
printf("# Failed test at line %d\n", (caller)[2]);
}
}
}
### Start of Testing ###
ok(1, 'Loaded');
### Thread cancel ###
# Set up to capture warning when thread terminates
my @errs :shared;
$SIG{__WARN__} = sub { push(@errs, @_); };
sub thr_func {
my $q = shift;
# Thread 'cancellation' signal handler
$SIG{'KILL'} = sub {
$q->enqueue(1, 'Thread received signal');
die("Thread killed\n");
};
# Thread sleeps until signalled
$q->enqueue(1, 'Thread sleeping');
sleep(1) for (1..10);
# Should not go past here
$q->enqueue(0, 'Thread terminated normally');
return ('ERROR');
}
# Create thread
my $thr = threads->create('thr_func', $q);
ok($thr && $thr->tid() == 2, 'Created thread');
threads->yield();
sleep(1);
# Signal thread
ok($thr->kill('KILL') == $thr, 'Signalled thread');
threads->yield();
# Cleanup
my $rc = $thr->join();
ok(! $rc, 'No thread return value');
# Check for thread termination message
ok(@errs && $errs[0] =~ /Thread killed/, 'Thread termination warning');
### Thread suspend/resume ###
sub thr_func2
{
my $q = shift;
my $sema = shift;
$q->enqueue($sema, 'Thread received semaphore');
# Set up the signal handler for suspension/resumption
$SIG{'STOP'} = sub {
$q->enqueue(1, 'Thread suspending');
$sema->down();
$q->enqueue(1, 'Thread resuming');
$sema->up();
};
# Set up the signal handler for graceful termination
my $term = 0;
$SIG{'TERM'} = sub {
$q->enqueue(1, 'Thread caught termination signal');
$term = 1;
};
# Do work until signalled to terminate
while (! $term) {
sleep(1);
}
$q->enqueue(1, 'Thread done');
return ('OKAY');
}
# Create a semaphore for use in suspending the thread
my $sema = Thread::Semaphore->new();
ok($sema, 'Semaphore created');
# Create a thread and send it the semaphore
$thr = threads->create('thr_func2', $q, $sema);
ok($thr && $thr->tid() == 3, 'Created thread');
threads->yield();
sleep(1);
# Suspend the thread
$sema->down();
ok($thr->kill('STOP') == $thr, 'Suspended thread');
threads->yield();
sleep(1);
# Allow the thread to continue
$sema->up();
threads->yield();
sleep(1);
# Terminate the thread
ok($thr->kill('TERM') == $thr, 'Signalled thread to terminate');
$rc = $thr->join();
ok($rc eq 'OKAY', 'Thread return value');
ok($thr->kill('TERM') == $thr, 'Ignore signal to terminated thread');
exit(0);
# EOF
|