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 # Should not go past here
95 ok(0, 'Thread terminated normally');
101 my $thr = threads->create('thr_func');
102 ok($thr && $thr->tid() == 1, 'Created thread');
107 ok($thr->kill('KILL'), 'Signalled thread');
110 # Interrupt thread's sleep call
112 local $SIG{'INT'} = sub {};
113 ok(kill('INT', $$) || $^O eq 'MSWin32', q/Interrupt thread's sleep call/);
117 my $rc = $thr->join();
118 ok(! $rc, 'No thread return value');
120 # Check for thread termination message
121 ok(@errs && $errs[0] =~ /Thread killed/, 'Thread termination warning');
124 ### Thread suspend/resume ###
129 ok($sema, 'Thread received semaphore');
131 # Set up the signal handler for suspension/resumption
133 ok(1, 'Thread suspending');
135 ok(1, 'Thread resuming');
139 # Set up the signal handler for graceful termination
142 ok(1, 'Thread caught termination signal');
146 # Do work until signalled to terminate
151 ok(1, 'Thread done');
156 # Create a semaphore for use in suspending the thread
157 my $sema = Thread::Semaphore->new();
158 ok($sema, 'Semaphore created');
160 # Create a thread and send it the semaphore
161 $thr = threads->create('thr_func2', $sema);
162 ok($thr && $thr->tid() == 2, 'Created thread');
168 ok($thr->kill('STOP'), 'Suspended thread');
173 # Allow the thread to continue
179 # Terminate the thread
180 ok($thr->kill('TERM'), 'Signalled thread to terminate');
183 ok($rc eq 'OKAY', 'Thread return value');