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