6 if (! $Config{'useithreads'}) {
7 print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
12 use ExtUtils::testlib;
17 if (! eval 'use threads::shared; 1') {
18 print("1..0 # SKIP threads::shared not available\n");
22 local $SIG{'HUP'} = sub {};
23 my $thr = threads->create(sub {});
24 eval { $thr->kill('HUP') };
26 if ($@ && $@ =~ /safe signals/) {
27 print("1..0 # SKIP Not using safe signals\n");
31 require Thread::Queue;
32 require Thread::Semaphore;
35 print("1..18\n"); ### Number of tests that will be run ###
39 my $q = Thread::Queue->new();
46 while ($q->pending()) {
47 my $ok = $q->dequeue();
48 my $name = $q->dequeue();
52 print("ok $id - $name\n");
54 print("not ok $id - $name\n");
55 printf("# Failed test at line %d\n", (caller)[2]);
61 ### Start of Testing ###
66 # Set up to capture warning when thread terminates
68 $SIG{__WARN__} = sub { push(@errs, @_); };
73 # Thread 'cancellation' signal handler
75 $q->enqueue(1, 'Thread received signal');
76 die("Thread killed\n");
79 # Thread sleeps until signalled
80 $q->enqueue(1, 'Thread sleeping');
82 # Should not go past here
83 $q->enqueue(0, 'Thread terminated normally');
88 my $thr = threads->create('thr_func', $q);
89 ok($thr && $thr->tid() == 2, 'Created thread');
94 ok($thr->kill('KILL') == $thr, 'Signalled thread');
98 my $rc = $thr->join();
99 ok(! $rc, 'No thread return value');
101 # Check for thread termination message
102 ok(@errs && $errs[0] =~ /Thread killed/, 'Thread termination warning');
105 ### Thread suspend/resume ###
112 $q->enqueue($sema, 'Thread received semaphore');
114 # Set up the signal handler for suspension/resumption
116 $q->enqueue(1, 'Thread suspending');
118 $q->enqueue(1, 'Thread resuming');
122 # Set up the signal handler for graceful termination
125 $q->enqueue(1, 'Thread caught termination signal');
129 # Do work until signalled to terminate
134 $q->enqueue(1, 'Thread done');
139 # Create a semaphore for use in suspending the thread
140 my $sema = Thread::Semaphore->new();
141 ok($sema, 'Semaphore created');
143 # Create a thread and send it the semaphore
144 $thr = threads->create('thr_func2', $q, $sema);
145 ok($thr && $thr->tid() == 3, 'Created thread');
151 ok($thr->kill('STOP') == $thr, 'Suspended thread');
156 # Allow the thread to continue
162 # Terminate the thread
163 ok($thr->kill('TERM') == $thr, 'Signalled thread to terminate');
166 ok($rc eq 'OKAY', 'Thread return value');
168 ok($thr->kill('TERM') == $thr, 'Ignore signal to terminated thread');