5 if ($ENV{'PERL_CORE'}){
7 unshift @INC, '../lib';
10 if (! $Config{'useithreads'}) {
11 print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
16 use ExtUtils::testlib;
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");
33 package Thread::Semaphore;
38 my $val : shared = @_ ? shift : 1;
45 my $inc = @_ ? shift : 1;
46 cond_wait $$s until $$s >= $inc;
53 my $inc = @_ ? shift : 1;
54 ($$s += $inc) > 0 and cond_broadcast $$s;
60 print("1..18\n"); ### Number of tests that will be run ###
74 # You have to do it this way or VMS will get confused.
76 print("ok $id - $name\n");
78 print("not ok $id - $name\n");
79 printf("# Failed test at line %d\n", (caller)[2]);
86 ### Start of Testing ###
90 # Set up to capture warning when thread terminates
92 $SIG{__WARN__} = sub { push(@errs, @_); };
96 # Thread 'cancellation' signal handler
98 ok(1, 'Thread received signal');
99 die("Thread killed\n");
102 # Thread sleeps until signalled
103 ok(1, 'Thread sleeping');
105 local $SIG{'INT'} = sub {};
108 # Should not go past here
109 ok(0, 'Thread terminated normally');
115 my $thr = threads->create('thr_func');
116 ok($thr && $thr->tid() == 2, 'Created thread');
121 ok($thr->kill('KILL'), 'Signalled thread');
124 # Interrupt thread's sleep call
126 # We can't be sure whether the signal itself will get delivered to this
127 # thread or the sleeping thread
128 local $SIG{'INT'} = sub {};
129 ok(kill('INT', $$) || $^O eq 'MSWin32', q/Interrupt thread's sleep call/);
133 my $rc = $thr->join();
134 ok(! $rc, 'No thread return value');
136 # Check for thread termination message
137 ok(@errs && $errs[0] =~ /Thread killed/, 'Thread termination warning');
140 ### Thread suspend/resume ###
145 ok($sema, 'Thread received semaphore');
147 # Set up the signal handler for suspension/resumption
149 ok(1, 'Thread suspending');
151 ok(1, 'Thread resuming');
155 # Set up the signal handler for graceful termination
158 ok(1, 'Thread caught termination signal');
162 # Do work until signalled to terminate
167 ok(1, 'Thread done');
172 # Create a semaphore for use in suspending the thread
173 my $sema = Thread::Semaphore->new();
174 ok($sema, 'Semaphore created');
176 # Create a thread and send it the semaphore
177 $thr = threads->create('thr_func2', $sema);
178 ok($thr && $thr->tid() == 3, 'Created thread');
184 ok($thr->kill('STOP'), 'Suspended thread');
189 # Allow the thread to continue
195 # Terminate the thread
196 ok($thr->kill('TERM'), 'Signalled thread to terminate');
199 ok($rc eq 'OKAY', 'Thread return value');