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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
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);
}
$| = 1;
print("1..59\n"); ### Number of tests that will be run ###
};
my $TEST;
BEGIN {
share($TEST);
$TEST = 1;
}
ok(1, 'Loaded');
sub ok {
my ($ok, $name) = @_;
lock($TEST);
my $id = $TEST++;
# You have to do it this way or VMS will get confused.
if ($ok) {
print("ok $id - $name\n");
} else {
print("not ok $id - $name\n");
printf("# Failed test at line %d\n", (caller)[2]);
}
return ($ok);
}
### Start of Testing ###
my ($READY, $GO, $DONE) :shared = (0, 0, 0);
sub do_thread
{
{
lock($DONE);
$DONE = 0;
lock($READY);
$READY = 1;
cond_signal($READY);
}
lock($GO);
while (! $GO) {
cond_wait($GO);
}
$GO = 0;
lock($READY);
$READY = 0;
lock($DONE);
$DONE = 1;
cond_signal($DONE);
}
sub wait_until_ready
{
lock($READY);
while (! $READY) {
cond_wait($READY);
}
}
sub thread_go
{
{
lock($GO);
$GO = 1;
cond_signal($GO);
}
{
lock($DONE);
while (! $DONE) {
cond_wait($DONE);
}
}
threads->yield();
sleep(1);
}
my $thr = threads->create('do_thread');
wait_until_ready();
ok($thr->is_running(), 'thread running');
ok(threads->list(threads::running) == 1, 'thread running list');
ok(! $thr->is_detached(), 'thread not detached');
ok(! $thr->is_joinable(), 'thread not joinable');
ok(threads->list(threads::joinable) == 0, 'thread joinable list');
ok(threads->list(threads::all) == 1, 'thread list');
thread_go();
ok(! $thr->is_running(), 'thread not running');
ok(threads->list(threads::running) == 0, 'thread running list');
ok(! $thr->is_detached(), 'thread not detached');
ok($thr->is_joinable(), 'thread joinable');
ok(threads->list(threads::joinable) == 1, 'thread joinable list');
ok(threads->list(threads::all) == 1, 'thread list');
$thr->join();
ok(! $thr->is_running(), 'thread not running');
ok(threads->list(threads::running) == 0, 'thread running list');
ok(! $thr->is_detached(), 'thread not detached');
ok(! $thr->is_joinable(), 'thread not joinable');
ok(threads->list(threads::joinable) == 0, 'thread joinable list');
ok(threads->list(threads::all) == 0, 'thread list');
$thr = threads->create('do_thread');
$thr->detach();
ok($thr->is_running(), 'thread running');
ok(threads->list(threads::running) == 0, 'thread running list');
ok($thr->is_detached(), 'thread detached');
ok(! $thr->is_joinable(), 'thread not joinable');
ok(threads->list(threads::joinable) == 0, 'thread joinable list');
ok(threads->list(threads::all) == 0, 'thread list');
thread_go();
ok(! $thr->is_running(), 'thread not running');
ok(threads->list(threads::running) == 0, 'thread running list');
ok($thr->is_detached(), 'thread detached');
ok(! $thr->is_joinable(), 'thread not joinable');
ok(threads->list(threads::joinable) == 0, 'thread joinable list');
$thr = threads->create(sub {
ok(! threads->is_detached(), 'thread not detached');
ok(threads->list(threads::running) == 1, 'thread running list');
ok(threads->list(threads::joinable) == 0, 'thread joinable list');
ok(threads->list(threads::all) == 1, 'thread list');
threads->detach();
do_thread();
ok(threads->is_detached(), 'thread detached');
ok(threads->list(threads::running) == 0, 'thread running list');
ok(threads->list(threads::joinable) == 0, 'thread joinable list');
ok(threads->list(threads::all) == 0, 'thread list');
});
wait_until_ready();
ok($thr->is_running(), 'thread running');
ok(threads->list(threads::running) == 0, 'thread running list');
ok($thr->is_detached(), 'thread detached');
ok(! $thr->is_joinable(), 'thread not joinable');
ok(threads->list(threads::joinable) == 0, 'thread joinable list');
ok(threads->list(threads::all) == 0, 'thread list');
thread_go();
ok(! $thr->is_running(), 'thread not running');
ok(threads->list(threads::running) == 0, 'thread running list');
ok($thr->is_detached(), 'thread detached');
ok(! $thr->is_joinable(), 'thread not joinable');
ok(threads->list(threads::joinable) == 0, 'thread joinable list');
{
my $go : shared = 0;
my $t = threads->create( sub {
ok(! threads->is_detached(), 'thread not detached');
ok(threads->list(threads::running) == 1, 'thread running list');
ok(threads->list(threads::joinable) == 0, 'thread joinable list');
ok(threads->list(threads::all) == 1, 'thread list');
lock($go); $go = 1; cond_signal($go);
});
{ lock ($go); cond_wait($go) until $go; }
$t->join;
}
{
my $rdy :shared = 0;
sub thr_ready
{
lock($rdy);
$rdy++;
cond_signal($rdy);
}
my $go :shared = 0;
sub thr_wait
{
lock($go);
cond_wait($go) until $go;
}
my $done :shared = 0;
sub thr_done
{
lock($done);
$done++;
cond_signal($done);
}
my $thr_routine = sub { thr_ready(); thr_wait(); thr_done(); };
# Create 8 threads:
# 3 running, blocking on $go
# 2 running, blocking on $go, join pending
# 2 running, blocking on join of above
# 1 finished, unjoined
for (1..3) { threads->create($thr_routine); }
foreach my $t (map {threads->create($thr_routine)} 1..2) {
threads->create(sub { thr_ready(); $_[0]->join; thr_done(); }, $t);
}
threads->create(sub { thr_ready(); thr_done(); });
{
lock($done);
cond_wait($done) until ($done == 1);
}
{
lock($rdy);
cond_wait($rdy) until ($rdy == 8);
}
threads->yield();
sleep(1);
ok(threads->list(threads::running) == 5, 'thread running list');
ok(threads->list(threads::joinable) == 1, 'thread joinable list');
ok(threads->list(threads::all) == 6, 'thread all list');
{ lock($go); $go = 1; cond_broadcast($go); }
{
lock($done);
cond_wait($done) until ($done == 8);
}
threads->yield();
sleep(1);
ok(threads->list(threads::running) == 0, 'thread running list');
# Two awaiting join() have completed
ok(threads->list(threads::joinable) == 6, 'thread joinable list');
ok(threads->list(threads::all) == 6, 'thread all list');
for (threads->list) { $_->join; }
}
exit(0);
# EOF
|