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 package Thread::Semaphore;
27 my $val : shared = @_ ? shift : 1;
34 my $inc = @_ ? shift : 1;
35 cond_wait $$s until $$s >= $inc;
42 my $inc = @_ ? shift : 1;
43 ($$s += $inc) > 0 and cond_broadcast $$s;
49 print("1..18\n"); ### Number of tests that will be run ###
63 # You have to do it this way or VMS will get confused.
65 print("ok $id - $name\n");
67 print("not ok $id - $name\n");
68 printf("# Failed test at line %d\n", (caller)[2]);
75 ### Start of Testing ###
79 # Set up to capture warning when thread terminates
81 $SIG{__WARN__} = sub { push(@errs, @_); };
85 # Thread 'cancellation' signal handler
87 ok(1, 'Thread received signal');
88 die("Thread killed\n");
91 # Thread sleeps until signalled
92 ok(1, 'Thread sleeping');
94 local $SIG{'INT'} = sub {};
97 # Should not go past here
98 ok(0, 'Thread terminated normally');
104 my $thr = threads->create('thr_func');
105 ok($thr && $thr->tid() == 1, 'Created thread');
110 ok($thr->kill('KILL'), 'Signalled thread');
113 # Interrupt thread's sleep call
115 # We can't be sure whether the signal itself will get delivered to this
116 # thread or the sleeping thread
117 local $SIG{'INT'} = sub {};
118 ok(kill('INT', $$) || $^O eq 'MSWin32', q/Interrupt thread's sleep call/);
122 my $rc = $thr->join();
123 ok(! $rc, 'No thread return value');
125 # Check for thread termination message
126 ok(@errs && $errs[0] =~ /Thread killed/, 'Thread termination warning');
129 ### Thread suspend/resume ###
134 ok($sema, 'Thread received semaphore');
136 # Set up the signal handler for suspension/resumption
138 ok(1, 'Thread suspending');
140 ok(1, 'Thread resuming');
144 # Set up the signal handler for graceful termination
147 ok(1, 'Thread caught termination signal');
151 # Do work until signalled to terminate
156 ok(1, 'Thread done');
161 # Create a semaphore for use in suspending the thread
162 my $sema = Thread::Semaphore->new();
163 ok($sema, 'Semaphore created');
165 # Create a thread and send it the semaphore
166 $thr = threads->create('thr_func2', $sema);
167 ok($thr && $thr->tid() == 2, 'Created thread');
173 ok($thr->kill('STOP'), 'Suspended thread');
178 # Allow the thread to continue
184 # Terminate the thread
185 ok($thr->kill('TERM'), 'Signalled thread to terminate');
188 ok($rc eq 'OKAY', 'Thread return value');