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;
21 if (! eval 'use threads::shared; 1') {
22 print("1..0 # SKIP threads::shared not available\n");
27 print("1..59\n"); ### Number of tests that will be run ###
44 # You have to do it this way or VMS will get confused.
46 print("ok $id - $name\n");
48 print("not ok $id - $name\n");
49 printf("# Failed test at line %d\n", (caller)[2]);
56 ### Start of Testing ###
58 my ($READY, $GO, $DONE) :shared = (0, 0, 0);
110 my $thr = threads->create('do_thread');
112 ok($thr->is_running(), 'thread running');
113 ok(threads->list(threads::running) == 1, 'thread running list');
114 ok(! $thr->is_detached(), 'thread not detached');
115 ok(! $thr->is_joinable(), 'thread not joinable');
116 ok(threads->list(threads::joinable) == 0, 'thread joinable list');
117 ok(threads->list(threads::all) == 1, 'thread list');
120 ok(! $thr->is_running(), 'thread not running');
121 ok(threads->list(threads::running) == 0, 'thread running list');
122 ok(! $thr->is_detached(), 'thread not detached');
123 ok($thr->is_joinable(), 'thread joinable');
124 ok(threads->list(threads::joinable) == 1, 'thread joinable list');
125 ok(threads->list(threads::all) == 1, 'thread list');
128 ok(! $thr->is_running(), 'thread not running');
129 ok(threads->list(threads::running) == 0, 'thread running list');
130 ok(! $thr->is_detached(), 'thread not detached');
131 ok(! $thr->is_joinable(), 'thread not joinable');
132 ok(threads->list(threads::joinable) == 0, 'thread joinable list');
133 ok(threads->list(threads::all) == 0, 'thread list');
135 $thr = threads->create('do_thread');
137 ok($thr->is_running(), 'thread running');
138 ok(threads->list(threads::running) == 0, 'thread running list');
139 ok($thr->is_detached(), 'thread detached');
140 ok(! $thr->is_joinable(), 'thread not joinable');
141 ok(threads->list(threads::joinable) == 0, 'thread joinable list');
142 ok(threads->list(threads::all) == 0, 'thread list');
145 ok(! $thr->is_running(), 'thread not running');
146 ok(threads->list(threads::running) == 0, 'thread running list');
147 ok($thr->is_detached(), 'thread detached');
148 ok(! $thr->is_joinable(), 'thread not joinable');
149 ok(threads->list(threads::joinable) == 0, 'thread joinable list');
151 $thr = threads->create(sub {
152 ok(! threads->is_detached(), 'thread not detached');
153 ok(threads->list(threads::running) == 1, 'thread running list');
154 ok(threads->list(threads::joinable) == 0, 'thread joinable list');
155 ok(threads->list(threads::all) == 1, 'thread list');
158 ok(threads->is_detached(), 'thread detached');
159 ok(threads->list(threads::running) == 0, 'thread running list');
160 ok(threads->list(threads::joinable) == 0, 'thread joinable list');
161 ok(threads->list(threads::all) == 0, 'thread list');
165 ok($thr->is_running(), 'thread running');
166 ok(threads->list(threads::running) == 0, 'thread running list');
167 ok($thr->is_detached(), 'thread detached');
168 ok(! $thr->is_joinable(), 'thread not joinable');
169 ok(threads->list(threads::joinable) == 0, 'thread joinable list');
170 ok(threads->list(threads::all) == 0, 'thread list');
173 ok(! $thr->is_running(), 'thread not running');
174 ok(threads->list(threads::running) == 0, 'thread running list');
175 ok($thr->is_detached(), 'thread detached');
176 ok(! $thr->is_joinable(), 'thread not joinable');
177 ok(threads->list(threads::joinable) == 0, 'thread joinable list');
181 my $t = threads->create( sub {
182 ok(! threads->is_detached(), 'thread not detached');
183 ok(threads->list(threads::running) == 1, 'thread running list');
184 ok(threads->list(threads::joinable) == 0, 'thread joinable list');
185 ok(threads->list(threads::all) == 1, 'thread list');
186 lock($go); $go = 1; cond_signal($go);
189 { lock ($go); cond_wait($go) until $go; }
206 cond_wait($go) until $go;
209 my $done :shared = 0;
217 my $thr_routine = sub { thr_ready(); thr_wait(); thr_done(); };
220 # 3 running, blocking on $go
221 # 2 running, blocking on $go, join pending
222 # 2 running, blocking on join of above
223 # 1 finished, unjoined
225 for (1..3) { threads->create($thr_routine); }
227 foreach my $t (map {threads->create($thr_routine)} 1..2) {
228 threads->create(sub { thr_ready(); $_[0]->join; thr_done(); }, $t);
230 threads->create(sub { thr_ready(); thr_done(); });
233 cond_wait($done) until ($done == 1);
237 cond_wait($rdy) until ($rdy == 8);
242 ok(threads->list(threads::running) == 5, 'thread running list');
243 ok(threads->list(threads::joinable) == 1, 'thread joinable list');
244 ok(threads->list(threads::all) == 6, 'thread all list');
246 { lock($go); $go = 1; cond_broadcast($go); }
249 cond_wait($done) until ($done == 8);
254 ok(threads->list(threads::running) == 0, 'thread running list');
255 # Two awaiting join() have completed
256 ok(threads->list(threads::joinable) == 6, 'thread joinable list');
257 ok(threads->list(threads::all) == 6, 'thread all list');
259 for (threads->list) { $_->join; }