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