remove _() non-ansism
[p5sagit/p5-mst-13.2.git] / ext / Thread / Thread.xs
CommitLineData
d9bb3666 1#include "EXTERN.h"
2#include "perl.h"
3#include "XSUB.h"
4
7d901afa 5/* Magic signature for Thread's mg_private is "Th" */
6#define Thread_MAGIC_SIGNATURE 0x5468
7
f0f333f4 8#ifdef __cplusplus
9#ifdef I_UNISTD
10#include <unistd.h>
11#endif
12#endif
13#include <fcntl.h>
14
85ced67f 15static int sig_pipe[2];
f0f333f4 16
17#ifndef THREAD_RET_TYPE
f0f333f4 18#define THREAD_RET_TYPE void *
19#define THREAD_RET_CAST(x) ((THREAD_RET_TYPE) x)
458fb581 20#endif
683929b4 21
7d901afa 22static void
52e1cb5e 23remove_thread(struct perl_thread *t)
7d901afa 24{
f0f333f4 25#ifdef USE_THREADS
8b73bbec 26 DEBUG_S(WITH_THR(PerlIO_printf(PerlIO_stderr(),
7d901afa 27 "%p: remove_thread %p\n", thr, t)));
533c011a 28 MUTEX_LOCK(&PL_threads_mutex);
0a00ffdb 29 MUTEX_DESTROY(&t->mutex);
533c011a 30 PL_nthreads--;
7d901afa 31 t->prev->next = t->next;
32 t->next->prev = t->prev;
533c011a 33 COND_BROADCAST(&PL_nthreads_cond);
34 MUTEX_UNLOCK(&PL_threads_mutex);
f0f333f4 35#endif
7d901afa 36}
37
ea0efc06 38static THREAD_RET_TYPE
f0f333f4 39threadstart(void *arg)
d9bb3666 40{
f0f333f4 41#ifdef USE_THREADS
783070da 42#ifdef FAKE_THREADS
43 Thread savethread = thr;
44 LOGOP myop;
45 dSP;
6b88bc9c 46 I32 oldscope = PL_scopestack_ix;
783070da 47 I32 retval;
458fb581 48 AV *av;
783070da 49 int i;
50
8b73bbec 51 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
683929b4 52 thr, SvPEEK(TOPs)));
783070da 53 thr = (Thread) arg;
54 savemark = TOPMARK;
55 thr->prev = thr->prev_run = savethread;
56 thr->next = savethread->next;
57 thr->next_run = savethread->next_run;
58 savethread->next = savethread->next_run = thr;
59 thr->wait_queue = 0;
60 thr->private = 0;
61
62 /* Now duplicate most of perl_call_sv but with a few twists */
6b88bc9c 63 PL_op = (OP*)&myop;
64 Zero(PL_op, 1, LOGOP);
783070da 65 myop.op_flags = OPf_STACKED;
66 myop.op_next = Nullop;
67 myop.op_flags |= OPf_KNOW;
68 myop.op_flags |= OPf_WANT_LIST;
6b88bc9c 69 PL_op = pp_entersub(ARGS);
8b73bbec 70 DEBUG_S(if (!PL_op)
783070da 71 PerlIO_printf(PerlIO_stderr(), "thread starts at Nullop\n"));
72 /*
73 * When this thread is next scheduled, we start in the right
74 * place. When the thread runs off the end of the sub, perl.c
75 * handles things, using savemark to figure out how much of the
76 * stack is the return value for any join.
77 */
78 thr = savethread; /* back to the old thread */
79 return 0;
80#else
d9bb3666 81 Thread thr = (Thread) arg;
82 LOGOP myop;
4e35701f 83 djSP;
d9bb3666 84 I32 oldmark = TOPMARK;
533c011a 85 I32 oldscope = PL_scopestack_ix;
d9bb3666 86 I32 retval;
458fb581 87 SV *sv;
88 AV *av = newAV();
14fcddff 89 int i, ret;
783070da 90 dJMPENV;
8b73bbec 91 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "new thread %p waiting to start\n",
0b9678a8 92 thr));
783070da 93
0b9678a8 94 /* Don't call *anything* requiring dTHR until after SET_THR() */
d9bb3666 95 /*
96 * Wait until our creator releases us. If we didn't do this, then
97 * it would be potentially possible for out thread to carry on and
98 * do stuff before our creator fills in our "self" field. For example,
ea0efc06 99 * if we went and created another thread which tried to JOIN with us,
100 * then we'd be in a mess.
d9bb3666 101 */
50112d62 102 MUTEX_LOCK(&thr->mutex);
103 MUTEX_UNLOCK(&thr->mutex);
d9bb3666 104
d9bb3666 105 /*
106 * It's safe to wait until now to set the thread-specific pointer
52e1cb5e 107 * from our pthread_t structure to our struct perl_thread, since
108 * we're the only thread who can get at it anyway.
d9bb3666 109 */
ea0efc06 110 SET_THR(thr);
d9bb3666 111
783070da 112 /* Only now can we use SvPEEK (which calls sv_newmortal which does dTHR) */
8b73bbec 113 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
683929b4 114 thr, SvPEEK(TOPs)));
783070da 115
458fb581 116 sv = POPs;
117 PUTBACK;
901b18f1 118 ENTER;
119 SAVETMPS;
458fb581 120 perl_call_sv(sv, G_ARRAY|G_EVAL);
734689b1 121 SPAGAIN;
533c011a 122 retval = SP - (PL_stack_base + oldmark);
123 SP = PL_stack_base + oldmark + 1;
458fb581 124 if (SvCUR(thr->errsv)) {
125 MUTEX_LOCK(&thr->mutex);
126 thr->flags |= THRf_DID_DIE;
127 MUTEX_UNLOCK(&thr->mutex);
6b88bc9c 128 av_store(av, 0, &PL_sv_no);
458fb581 129 av_store(av, 1, newSVsv(thr->errsv));
8b73bbec 130 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p died: %s\n",
6b88bc9c 131 thr, SvPV(thr->errsv, PL_na)));
458fb581 132 } else {
8b73bbec 133 DEBUG_S(STMT_START {
458fb581 134 for (i = 1; i <= retval; i++) {
135 PerlIO_printf(PerlIO_stderr(), "%p return[%d] = %s\n",
924508f0 136 thr, i, SvPEEK(SP[i - 1]));
458fb581 137 }
138 } STMT_END);
6b88bc9c 139 av_store(av, 0, &PL_sv_yes);
924508f0 140 for (i = 1; i <= retval; i++, SP++)
141 sv_setsv(*av_fetch(av, i, TRUE), SvREFCNT_inc(*SP));
458fb581 142 }
572eda1c 143 FREETMPS;
901b18f1 144 LEAVE;
458fb581 145
d9bb3666 146 finishoff:
783070da 147#if 0
148 /* removed for debug */
6b88bc9c 149 SvREFCNT_dec(PL_curstack);
783070da 150#endif
199100c8 151 SvREFCNT_dec(thr->cvcache);
54b9620d 152 SvREFCNT_dec(thr->threadsv);
554b3eca 153 SvREFCNT_dec(thr->specific);
38a03e6e 154 SvREFCNT_dec(thr->errsv);
155 SvREFCNT_dec(thr->errhv);
5c0ca799 156
f7ac0805 157 /*Safefree(cxstack);*/
84fee439 158 while (PL_curstackinfo->si_next)
159 PL_curstackinfo = PL_curstackinfo->si_next;
160 while (PL_curstackinfo) {
161 PERL_SI *p = PL_curstackinfo->si_prev;
162 SvREFCNT_dec(PL_curstackinfo->si_stack);
163 Safefree(PL_curstackinfo->si_cxstack);
164 Safefree(PL_curstackinfo);
165 PL_curstackinfo = p;
f7ac0805 166 }
84fee439 167 Safefree(PL_markstack);
168 Safefree(PL_scopestack);
169 Safefree(PL_savestack);
170 Safefree(PL_retstack);
171 Safefree(PL_tmps_stack);
172 Safefree(PL_ofs);
d9bb3666 173
84fee439 174 SvREFCNT_dec(PL_rs);
175 SvREFCNT_dec(PL_nrs);
176 SvREFCNT_dec(PL_statname);
177 Safefree(PL_screamfirst);
178 Safefree(PL_screamnext);
179 Safefree(PL_reg_start_tmp);
180 SvREFCNT_dec(PL_lastscream);
901b18f1 181 SvREFCNT_dec(PL_defoutgv);
5c0ca799 182
14fcddff 183 MUTEX_LOCK(&thr->mutex);
8b73bbec 184 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
50112d62 185 "%p: threadstart finishing: state is %u\n",
186 thr, ThrSTATE(thr)));
14fcddff 187 switch (ThrSTATE(thr)) {
188 case THRf_R_JOINABLE:
189 ThrSETSTATE(thr, THRf_ZOMBIE);
190 MUTEX_UNLOCK(&thr->mutex);
8b73bbec 191 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
14fcddff 192 "%p: R_JOINABLE thread finished\n", thr));
193 break;
194 case THRf_R_JOINED:
195 ThrSETSTATE(thr, THRf_DEAD);
196 MUTEX_UNLOCK(&thr->mutex);
50112d62 197 remove_thread(thr);
8b73bbec 198 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
14fcddff 199 "%p: R_JOINED thread finished\n", thr));
200 break;
50112d62 201 case THRf_R_DETACHED:
683929b4 202 ThrSETSTATE(thr, THRf_DEAD);
14fcddff 203 MUTEX_UNLOCK(&thr->mutex);
458fb581 204 SvREFCNT_dec(av);
8b73bbec 205 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
14fcddff 206 "%p: DETACHED thread finished\n", thr));
50112d62 207 remove_thread(thr); /* This might trigger main thread to finish */
14fcddff 208 break;
209 default:
210 MUTEX_UNLOCK(&thr->mutex);
211 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
212 /* NOTREACHED */
734689b1 213 }
458fb581 214 return THREAD_RET_CAST(av); /* Available for anyone to join with */
ea0efc06 215 /* us unless we're detached, in which */
216 /* case noone sees the value anyway. */
783070da 217#endif
f0f333f4 218#else
219 return THREAD_RET_CAST(NULL);
220#endif
d9bb3666 221}
222
683929b4 223static SV *
458fb581 224newthread (SV *startsv, AV *initargs, char *classname)
d9bb3666 225{
f0f333f4 226#ifdef USE_THREADS
d9bb3666 227 dSP;
228 Thread savethread;
229 int i;
683929b4 230 SV *sv;
ea0efc06 231 int err;
232#ifndef THREAD_CREATE
940cb80d 233 static pthread_attr_t attr;
234 static int attr_inited = 0;
f152979c 235 sigset_t fullmask, oldmask;
13666627 236 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
b86a2fa7 237#endif
1cfa4ec7 238
d9bb3666 239 savethread = thr;
a863c7d1 240 thr = new_struct_thread(thr);
c4e7bd8d 241 /* temporarily pretend to be the child thread in case the
242 * XPUSHs() below want to grow the child's stack. This is
243 * safe, since the other thread is not yet created, and we
244 * are the only ones who know about it */
245 SET_THR(thr);
d9bb3666 246 SPAGAIN;
8b73bbec 247 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
0b9678a8 248 "%p: newthread (%p), tid is %u, preparing stack\n",
249 savethread, thr, thr->tid));
d9bb3666 250 /* The following pushes the arg list and startsv onto the *new* stack */
924508f0 251 PUSHMARK(SP);
d9bb3666 252 /* Could easily speed up the following greatly */
734689b1 253 for (i = 0; i <= AvFILL(initargs); i++)
d9bb3666 254 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
255 XPUSHs(SvREFCNT_inc(startsv));
256 PUTBACK;
b099ddc0 257
258 /* On your marks... */
c4e7bd8d 259 SET_THR(savethread);
b099ddc0 260 MUTEX_LOCK(&thr->mutex);
261
ea0efc06 262#ifdef THREAD_CREATE
f0f333f4 263 err = THREAD_CREATE(thr, threadstart);
783070da 264#else
ea0efc06 265 /* Get set... */
f152979c 266 sigfillset(&fullmask);
267 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
268 croak("panic: sigprocmask");
940cb80d 269 err = 0;
270 if (!attr_inited) {
271 attr_inited = 1;
52e1cb5e 272 err = pthread_attr_init(&attr);
0d85d877 273# ifdef PTHREAD_ATTR_SETDETACHSTATE
940cb80d 274 if (err == 0)
0d85d877 275 err = PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
276
277# else
1cfa4ec7 278 croak("panic: can't pthread_attr_setdetachstate");
0d85d877 279# endif
52e1cb5e 280 }
940cb80d 281 if (err == 0)
0d85d877 282 err = PTHREAD_CREATE(&thr->self, attr, threadstart, (void*) thr);
ea0efc06 283#endif
b099ddc0 284
ea0efc06 285 if (err) {
b099ddc0 286 MUTEX_UNLOCK(&thr->mutex);
8b73bbec 287 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
940cb80d 288 "%p: create of %p failed %d\n",
289 savethread, thr, err));
ea0efc06 290 /* Thread creation failed--clean up */
199100c8 291 SvREFCNT_dec(thr->cvcache);
ea0efc06 292 remove_thread(thr);
293 MUTEX_DESTROY(&thr->mutex);
294 for (i = 0; i <= AvFILL(initargs); i++)
295 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
296 SvREFCNT_dec(startsv);
297 return NULL;
298 }
b099ddc0 299
ea0efc06 300#ifdef THREAD_POST_CREATE
301 THREAD_POST_CREATE(thr);
302#else
f152979c 303 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
304 croak("panic: sigprocmask");
783070da 305#endif
b099ddc0 306
7d901afa 307 sv = newSViv(thr->tid);
199100c8 308 sv_magic(sv, thr->oursv, '~', 0, 0);
7d901afa 309 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
b099ddc0 310 sv = sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
311
312 /* Go */
313 MUTEX_UNLOCK(&thr->mutex);
314
315 return sv;
f0f333f4 316#else
317 croak("No threads in this perl");
6b88bc9c 318 return &PL_sv_undef;
f0f333f4 319#endif
d9bb3666 320}
321
20ce7b12 322static Signal_t handle_thread_signal (int sig);
f0f333f4 323
f152979c 324static Signal_t
f0f333f4 325handle_thread_signal(int sig)
f152979c 326{
3aeed370 327 unsigned char c = (unsigned char) sig;
328 /*
329 * We're not really allowed to call fprintf in a signal handler
330 * so don't be surprised if this isn't robust while debugging
331 * with -DL.
332 */
8b73bbec 333 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
3aeed370 334 "handle_thread_signal: got signal %d\n", sig););
335 write(sig_pipe[1], &c, 1);
f152979c 336}
337
d9bb3666 338MODULE = Thread PACKAGE = Thread
0b9678a8 339PROTOTYPES: DISABLE
d9bb3666 340
683929b4 341void
458fb581 342new(classname, startsv, ...)
343 char * classname
d9bb3666 344 SV * startsv
734689b1 345 AV * av = av_make(items - 2, &ST(2));
683929b4 346 PPCODE:
458fb581 347 XPUSHs(sv_2mortal(newthread(startsv, av, classname)));
d9bb3666 348
349void
d9bb3666 350join(t)
351 Thread t
352 AV * av = NO_INIT
353 int i = NO_INIT
354 PPCODE:
f0f333f4 355#ifdef USE_THREADS
8b73bbec 356 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: joining %p (state %u)\n",
7d901afa 357 thr, t, ThrSTATE(t)););
50112d62 358 MUTEX_LOCK(&t->mutex);
359 switch (ThrSTATE(t)) {
14fcddff 360 case THRf_R_JOINABLE:
361 case THRf_R_JOINED:
50112d62 362 ThrSETSTATE(t, THRf_R_JOINED);
363 MUTEX_UNLOCK(&t->mutex);
14fcddff 364 break;
365 case THRf_ZOMBIE:
50112d62 366 ThrSETSTATE(t, THRf_DEAD);
367 MUTEX_UNLOCK(&t->mutex);
368 remove_thread(t);
14fcddff 369 break;
370 default:
50112d62 371 MUTEX_UNLOCK(&t->mutex);
14fcddff 372 croak("can't join with thread");
373 /* NOTREACHED */
374 }
ea0efc06 375 JOIN(t, &av);
7d901afa 376
458fb581 377 if (SvTRUE(*av_fetch(av, 0, FALSE))) {
378 /* Could easily speed up the following if necessary */
379 for (i = 1; i <= AvFILL(av); i++)
380 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
381 } else {
2d8e6c8d 382 STRLEN n_a;
383 char *mess = SvPV(*av_fetch(av, 1, FALSE), n_a);
8b73bbec 384 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
458fb581 385 "%p: join propagating die message: %s\n",
386 thr, mess));
387 croak(mess);
388 }
f0f333f4 389#endif
d9bb3666 390
391void
734689b1 392detach(t)
d9bb3666 393 Thread t
394 CODE:
f0f333f4 395#ifdef USE_THREADS
8b73bbec 396 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: detaching %p (state %u)\n",
7d901afa 397 thr, t, ThrSTATE(t)););
50112d62 398 MUTEX_LOCK(&t->mutex);
399 switch (ThrSTATE(t)) {
14fcddff 400 case THRf_R_JOINABLE:
50112d62 401 ThrSETSTATE(t, THRf_R_DETACHED);
14fcddff 402 /* fall through */
50112d62 403 case THRf_R_DETACHED:
14fcddff 404 DETACH(t);
50112d62 405 MUTEX_UNLOCK(&t->mutex);
14fcddff 406 break;
407 case THRf_ZOMBIE:
50112d62 408 ThrSETSTATE(t, THRf_DEAD);
7d901afa 409 DETACH(t);
50112d62 410 MUTEX_UNLOCK(&t->mutex);
411 remove_thread(t);
14fcddff 412 break;
413 default:
50112d62 414 MUTEX_UNLOCK(&t->mutex);
14fcddff 415 croak("can't detach thread");
416 /* NOTREACHED */
734689b1 417 }
f0f333f4 418#endif
d9bb3666 419
420void
7d901afa 421equal(t1, t2)
422 Thread t1
423 Thread t2
424 PPCODE:
6b88bc9c 425 PUSHs((t1 == t2) ? &PL_sv_yes : &PL_sv_no);
7d901afa 426
427void
428flags(t)
429 Thread t
430 PPCODE:
f0f333f4 431#ifdef USE_THREADS
7d901afa 432 PUSHs(sv_2mortal(newSViv(t->flags)));
f0f333f4 433#endif
7d901afa 434
435void
458fb581 436self(classname)
437 char * classname
7d901afa 438 PREINIT:
439 SV *sv;
f0f333f4 440 PPCODE:
441#ifdef USE_THREADS
7d901afa 442 sv = newSViv(thr->tid);
199100c8 443 sv_magic(sv, thr->oursv, '~', 0, 0);
7d901afa 444 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
458fb581 445 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
446 gv_stashpv(classname, TRUE))));
f0f333f4 447#endif
7d901afa 448
50112d62 449U32
450tid(t)
451 Thread t
452 CODE:
f0f333f4 453#ifdef USE_THREADS
50112d62 454 MUTEX_LOCK(&t->mutex);
455 RETVAL = t->tid;
456 MUTEX_UNLOCK(&t->mutex);
f0f333f4 457#else
458 RETVAL = 0;
459#endif
50112d62 460 OUTPUT:
461 RETVAL
462
463void
464DESTROY(t)
465 SV * t
466 PPCODE:
6b88bc9c 467 PUSHs(&PL_sv_yes);
50112d62 468
7d901afa 469void
734689b1 470yield()
d9bb3666 471 CODE:
f0f333f4 472{
473#ifdef USE_THREADS
ea0efc06 474 YIELD;
f0f333f4 475#endif
476}
d9bb3666 477
478void
734689b1 479cond_wait(sv)
480 SV * sv
481 MAGIC * mg = NO_INIT
f0f333f4 482CODE:
483#ifdef USE_THREADS
2c127b02 484 if (SvROK(sv))
734689b1 485 sv = SvRV(sv);
2c127b02 486
734689b1 487 mg = condpair_magic(sv);
8b73bbec 488 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
734689b1 489 MUTEX_LOCK(MgMUTEXP(mg));
490 if (MgOWNER(mg) != thr) {
491 MUTEX_UNLOCK(MgMUTEXP(mg));
492 croak("cond_wait for lock that we don't own\n");
493 }
494 MgOWNER(mg) = 0;
d3ef5668 495 COND_SIGNAL(MgOWNERCONDP(mg));
734689b1 496 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
50112d62 497 while (MgOWNER(mg))
498 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
734689b1 499 MgOWNER(mg) = thr;
500 MUTEX_UNLOCK(MgMUTEXP(mg));
f0f333f4 501#endif
502
734689b1 503void
504cond_signal(sv)
505 SV * sv
506 MAGIC * mg = NO_INIT
507CODE:
f0f333f4 508#ifdef USE_THREADS
50112d62 509 if (SvROK(sv))
734689b1 510 sv = SvRV(sv);
50112d62 511
734689b1 512 mg = condpair_magic(sv);
8b73bbec 513 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
734689b1 514 MUTEX_LOCK(MgMUTEXP(mg));
515 if (MgOWNER(mg) != thr) {
516 MUTEX_UNLOCK(MgMUTEXP(mg));
517 croak("cond_signal for lock that we don't own\n");
518 }
519 COND_SIGNAL(MgCONDP(mg));
520 MUTEX_UNLOCK(MgMUTEXP(mg));
f0f333f4 521#endif
d9bb3666 522
734689b1 523void
524cond_broadcast(sv)
525 SV * sv
526 MAGIC * mg = NO_INIT
f0f333f4 527CODE:
528#ifdef USE_THREADS
783070da 529 if (SvROK(sv))
734689b1 530 sv = SvRV(sv);
783070da 531
734689b1 532 mg = condpair_magic(sv);
8b73bbec 533 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
683929b4 534 thr, sv));
734689b1 535 MUTEX_LOCK(MgMUTEXP(mg));
536 if (MgOWNER(mg) != thr) {
537 MUTEX_UNLOCK(MgMUTEXP(mg));
538 croak("cond_broadcast for lock that we don't own\n");
539 }
540 COND_BROADCAST(MgCONDP(mg));
541 MUTEX_UNLOCK(MgMUTEXP(mg));
f0f333f4 542#endif
f152979c 543
7d901afa 544void
458fb581 545list(classname)
546 char * classname
7d901afa 547 PREINIT:
548 Thread t;
549 AV * av;
550 SV ** svp;
551 int n = 0;
552 PPCODE:
f0f333f4 553#ifdef USE_THREADS
7d901afa 554 av = newAV();
555 /*
556 * Iterate until we have enough dynamic storage for all threads.
557 * We mustn't do any allocation while holding threads_mutex though.
558 */
533c011a 559 MUTEX_LOCK(&PL_threads_mutex);
7d901afa 560 do {
533c011a 561 n = PL_nthreads;
562 MUTEX_UNLOCK(&PL_threads_mutex);
7d901afa 563 if (AvFILL(av) < n - 1) {
564 int i = AvFILL(av);
565 for (i = AvFILL(av); i < n - 1; i++) {
566 SV *sv = newSViv(0); /* fill in tid later */
567 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
568 av_push(av, sv_bless(newRV_noinc(sv),
458fb581 569 gv_stashpv(classname, TRUE)));
50112d62 570
7d901afa 571 }
572 }
533c011a 573 MUTEX_LOCK(&PL_threads_mutex);
574 } while (n < PL_nthreads);
575 n = PL_nthreads; /* Get the final correct value */
7d901afa 576
577 /*
578 * At this point, there's enough room to fill in av.
579 * Note that we are holding threads_mutex so the list
580 * won't change out from under us but all the remaining
581 * processing is "fast" (no blocking, malloc etc.)
582 */
583 t = thr;
584 svp = AvARRAY(av);
585 do {
0a00ffdb 586 SV *sv = (SV*)SvRV(*svp);
7d901afa 587 sv_setiv(sv, t->tid);
199100c8 588 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
7d901afa 589 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
590 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
591 t = t->next;
0a00ffdb 592 svp++;
7d901afa 593 } while (t != thr);
50112d62 594 /* */
533c011a 595 MUTEX_UNLOCK(&PL_threads_mutex);
7d901afa 596 /* Truncate any unneeded slots in av */
50112d62 597 av_fill(av, n - 1);
7d901afa 598 /* Finally, push all the new objects onto the stack and drop av */
924508f0 599 EXTEND(SP, n);
7d901afa 600 for (svp = AvARRAY(av); n > 0; n--, svp++)
601 PUSHs(*svp);
602 (void)sv_2mortal((SV*)av);
f0f333f4 603#endif
7d901afa 604
605
f152979c 606MODULE = Thread PACKAGE = Thread::Signal
607
608void
609kill_sighandler_thread()
610 PPCODE:
3aeed370 611 write(sig_pipe[1], "\0", 1);
6b88bc9c 612 PUSHs(&PL_sv_yes);
f152979c 613
614void
615init_thread_signals()
616 PPCODE:
533c011a 617 PL_sighandlerp = handle_thread_signal;
f152979c 618 if (pipe(sig_pipe) == -1)
619 XSRETURN_UNDEF;
6b88bc9c 620 PUSHs(&PL_sv_yes);
f152979c 621
3aeed370 622void
f152979c 623await_signal()
624 PREINIT:
3aeed370 625 unsigned char c;
ea0efc06 626 SSize_t ret;
f152979c 627 CODE:
628 do {
3aeed370 629 ret = read(sig_pipe[0], &c, 1);
f152979c 630 } while (ret == -1 && errno == EINTR);
631 if (ret == -1)
632 croak("panic: await_signal");
3aeed370 633 ST(0) = sv_newmortal();
634 if (ret)
22c35a8c 635 sv_setsv(ST(0), c ? PL_psig_ptr[c] : &PL_sv_no);
8b73bbec 636 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
3aeed370 637 "await_signal returning %s\n", SvPEEK(ST(0))););
4e35701f 638
458fb581 639MODULE = Thread PACKAGE = Thread::Specific
640
641void
642data(classname = "Thread::Specific")
643 char * classname
644 PPCODE:
fb223100 645#ifdef USE_THREADS
458fb581 646 if (AvFILL(thr->specific) == -1) {
647 GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV);
648 av_store(thr->specific, 0, newRV((SV*)GvHV(gv)));
649 }
650 XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));
fb223100 651#endif