1 #define PERL_NO_GET_CONTEXT
6 /* Magic signature for Thread's mg_private is "Th" */
7 #define Thread_MAGIC_SIGNATURE 0x5468
16 static int sig_pipe[2];
18 #ifndef THREAD_RET_TYPE
19 #define THREAD_RET_TYPE void *
20 #define THREAD_RET_CAST(x) ((THREAD_RET_TYPE) x)
24 remove_thread(pTHX_ Thread t)
27 DEBUG_S(WITH_THR(PerlIO_printf(Perl_debug_log,
28 "%p: remove_thread %p\n", thr, t)));
29 MUTEX_LOCK(&PL_threads_mutex);
30 MUTEX_DESTROY(&t->mutex);
32 t->prev->next = t->next;
33 t->next->prev = t->prev;
34 SvREFCNT_dec(t->oursv);
35 COND_BROADCAST(&PL_nthreads_cond);
36 MUTEX_UNLOCK(&PL_threads_mutex);
40 static THREAD_RET_TYPE
41 threadstart(void *arg)
45 Thread savethread = thr;
48 I32 oldscope = PL_scopestack_ix;
53 DEBUG_S(PerlIO_printf(Perl_debug_log, "new thread %p starting at %s\n",
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;
64 /* Now duplicate most of perl_call_sv but with a few twists */
66 Zero(PL_op, 1, LOGOP);
67 myop.op_flags = OPf_STACKED;
68 myop.op_next = Nullop;
69 myop.op_flags |= OPf_KNOW;
70 myop.op_flags |= OPf_WANT_LIST;
71 PL_op = pp_entersub(ARGS);
73 PerlIO_printf(Perl_debug_log, "thread starts at Nullop\n"));
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.
80 thr = savethread; /* back to the old thread */
83 Thread thr = (Thread) arg;
86 I32 oldmark = TOPMARK;
87 I32 oldscope = PL_scopestack_ix;
94 #if defined(MULTIPLICITY)
95 PERL_SET_INTERP(thr->interp);
98 DEBUG_S(PerlIO_printf(Perl_debug_log, "new thread %p waiting to start\n",
101 /* Don't call *anything* requiring dTHR until after PERL_SET_THX() */
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,
106 * if we went and created another thread which tried to JOIN with us,
107 * then we'd be in a mess.
109 MUTEX_LOCK(&thr->mutex);
110 MUTEX_UNLOCK(&thr->mutex);
113 * It's safe to wait until now to set the thread-specific pointer
114 * from our pthread_t structure to our struct perl_thread, since
115 * we're the only thread who can get at it anyway.
119 /* Only now can we use SvPEEK (which calls sv_newmortal which does dTHR) */
120 DEBUG_S(PerlIO_printf(Perl_debug_log, "new thread %p starting at %s\n",
128 perl_call_sv(sv, G_ARRAY|G_EVAL);
130 retval = SP - (PL_stack_base + oldmark);
131 SP = PL_stack_base + oldmark + 1;
132 if (SvCUR(thr->errsv)) {
133 MUTEX_LOCK(&thr->mutex);
134 thr->flags |= THRf_DID_DIE;
135 MUTEX_UNLOCK(&thr->mutex);
136 av_store(av, 0, &PL_sv_no);
137 av_store(av, 1, newSVsv(thr->errsv));
138 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p died: %s\n",
139 thr, SvPV(thr->errsv, PL_na)));
143 for (i = 1; i <= retval; i++) {
144 PerlIO_printf(Perl_debug_log, "%p return[%d] = %s\n",
145 thr, i, SvPEEK(SP[i - 1]));
148 av_store(av, 0, &PL_sv_yes);
149 for (i = 1; i <= retval; i++, SP++)
150 sv_setsv(*av_fetch(av, i, TRUE), SvREFCNT_inc(*SP));
157 /* removed for debug */
158 SvREFCNT_dec(PL_curstack);
160 SvREFCNT_dec(thr->cvcache);
161 SvREFCNT_dec(thr->threadsv);
162 SvREFCNT_dec(thr->specific);
163 SvREFCNT_dec(thr->errsv);
165 /*Safefree(cxstack);*/
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);
175 Safefree(PL_markstack);
176 Safefree(PL_scopestack);
177 Safefree(PL_savestack);
178 Safefree(PL_retstack);
179 Safefree(PL_tmps_stack);
183 SvREFCNT_dec(PL_nrs);
184 SvREFCNT_dec(PL_statname);
185 SvREFCNT_dec(PL_errors);
186 Safefree(PL_screamfirst);
187 Safefree(PL_screamnext);
188 Safefree(PL_reg_start_tmp);
189 SvREFCNT_dec(PL_lastscream);
190 SvREFCNT_dec(PL_defoutgv);
191 Safefree(PL_reg_poscache);
193 MUTEX_LOCK(&thr->mutex);
194 DEBUG_S(PerlIO_printf(Perl_debug_log,
195 "%p: threadstart finishing: state is %u\n",
196 thr, ThrSTATE(thr)));
197 switch (ThrSTATE(thr)) {
198 case THRf_R_JOINABLE:
199 ThrSETSTATE(thr, THRf_ZOMBIE);
200 MUTEX_UNLOCK(&thr->mutex);
201 DEBUG_S(PerlIO_printf(Perl_debug_log,
202 "%p: R_JOINABLE thread finished\n", thr));
205 ThrSETSTATE(thr, THRf_DEAD);
206 MUTEX_UNLOCK(&thr->mutex);
207 remove_thread(aTHX_ thr);
208 DEBUG_S(PerlIO_printf(Perl_debug_log,
209 "%p: R_JOINED thread finished\n", thr));
211 case THRf_R_DETACHED:
212 ThrSETSTATE(thr, THRf_DEAD);
213 MUTEX_UNLOCK(&thr->mutex);
215 DEBUG_S(PerlIO_printf(Perl_debug_log,
216 "%p: DETACHED thread finished\n", thr));
217 remove_thread(aTHX_ thr); /* This might trigger main thread to finish */
220 MUTEX_UNLOCK(&thr->mutex);
221 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
224 return THREAD_RET_CAST(av); /* Available for anyone to join with */
225 /* us unless we're detached, in which */
226 /* case noone sees the value anyway. */
229 return THREAD_RET_CAST(NULL);
234 newthread (pTHX_ SV *startsv, AV *initargs, char *classname)
242 #ifndef THREAD_CREATE
243 static pthread_attr_t attr;
244 static int attr_inited = 0;
245 sigset_t fullmask, oldmask;
246 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
250 thr = new_struct_thread(thr);
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 */
257 DEBUG_S(PerlIO_printf(Perl_debug_log,
258 "%p: newthread (%p), tid is %u, preparing stack\n",
259 savethread, thr, thr->tid));
260 /* The following pushes the arg list and startsv onto the *new* stack */
262 /* Could easily speed up the following greatly */
263 for (i = 0; i <= AvFILL(initargs); i++)
264 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
265 XPUSHs(SvREFCNT_inc(startsv));
268 /* On your marks... */
269 PERL_SET_THX(savethread);
270 MUTEX_LOCK(&thr->mutex);
273 err = THREAD_CREATE(thr, threadstart);
276 sigfillset(&fullmask);
277 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
278 croak("panic: sigprocmask");
282 err = pthread_attr_init(&attr);
283 # ifdef PTHREAD_ATTR_SETDETACHSTATE
285 err = PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
288 croak("panic: can't pthread_attr_setdetachstate");
292 err = PTHREAD_CREATE(&thr->self, attr, threadstart, (void*) thr);
296 MUTEX_UNLOCK(&thr->mutex);
297 DEBUG_S(PerlIO_printf(Perl_debug_log,
298 "%p: create of %p failed %d\n",
299 savethread, thr, err));
300 /* Thread creation failed--clean up */
301 SvREFCNT_dec(thr->cvcache);
302 remove_thread(aTHX_ thr);
303 for (i = 0; i <= AvFILL(initargs); i++)
304 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
305 SvREFCNT_dec(startsv);
309 #ifdef THREAD_POST_CREATE
310 THREAD_POST_CREATE(thr);
312 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
313 croak("panic: sigprocmask");
316 sv = newSViv(thr->tid);
317 sv_magic(sv, thr->oursv, '~', 0, 0);
318 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
319 sv = sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
322 MUTEX_UNLOCK(&thr->mutex);
326 croak("No threads in this perl");
331 static Signal_t handle_thread_signal (int sig);
334 handle_thread_signal(int sig)
337 unsigned char c = (unsigned char) sig;
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
343 DEBUG_S(PerlIO_printf(Perl_debug_log,
344 "handle_thread_signal: got signal %d\n", sig););
345 write(sig_pipe[1], &c, 1);
348 MODULE = Thread PACKAGE = Thread
352 new(classname, startsv, ...)
355 AV * av = av_make(items - 2, &ST(2));
357 XPUSHs(sv_2mortal(newthread(aTHX_ startsv, av, classname)));
367 croak("Attempt to join self");
368 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: joining %p (state %u)\n",
369 thr, t, ThrSTATE(t)););
370 MUTEX_LOCK(&t->mutex);
371 switch (ThrSTATE(t)) {
372 case THRf_R_JOINABLE:
374 ThrSETSTATE(t, THRf_R_JOINED);
375 MUTEX_UNLOCK(&t->mutex);
378 ThrSETSTATE(t, THRf_DEAD);
379 MUTEX_UNLOCK(&t->mutex);
380 remove_thread(aTHX_ t);
383 MUTEX_UNLOCK(&t->mutex);
384 croak("can't join with thread");
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++)
394 XPUSHs(*av_fetch(av, i, FALSE));
398 char *mess = SvPV(*av_fetch(av, 1, FALSE), n_a);
399 DEBUG_S(PerlIO_printf(Perl_debug_log,
400 "%p: join propagating die message: %s\n",
411 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: detaching %p (state %u)\n",
412 thr, t, ThrSTATE(t)););
413 MUTEX_LOCK(&t->mutex);
414 switch (ThrSTATE(t)) {
415 case THRf_R_JOINABLE:
416 ThrSETSTATE(t, THRf_R_DETACHED);
418 case THRf_R_DETACHED:
420 MUTEX_UNLOCK(&t->mutex);
423 ThrSETSTATE(t, THRf_DEAD);
425 MUTEX_UNLOCK(&t->mutex);
426 remove_thread(aTHX_ t);
429 MUTEX_UNLOCK(&t->mutex);
430 croak("can't detach thread");
440 PUSHs((t1 == t2) ? &PL_sv_yes : &PL_sv_no);
447 PUSHs(sv_2mortal(newSViv(t->flags)));
457 sv = newSViv(thr->tid);
458 sv_magic(sv, thr->oursv, '~', 0, 0);
459 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
460 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
461 gv_stashpv(classname, TRUE))));
469 MUTEX_LOCK(&t->mutex);
471 MUTEX_UNLOCK(&t->mutex);
502 mg = condpair_magic(sv);
503 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_wait %p\n", thr, sv));
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");
510 COND_SIGNAL(MgOWNERCONDP(mg));
511 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
513 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
515 MUTEX_UNLOCK(MgMUTEXP(mg));
527 mg = condpair_magic(sv);
528 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_signal %p\n",thr,sv));
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");
534 COND_SIGNAL(MgCONDP(mg));
535 MUTEX_UNLOCK(MgMUTEXP(mg));
547 mg = condpair_magic(sv);
548 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_broadcast %p\n",
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");
555 COND_BROADCAST(MgCONDP(mg));
556 MUTEX_UNLOCK(MgMUTEXP(mg));
571 * Iterate until we have enough dynamic storage for all threads.
572 * We mustn't do any allocation while holding threads_mutex though.
574 MUTEX_LOCK(&PL_threads_mutex);
577 MUTEX_UNLOCK(&PL_threads_mutex);
578 if (AvFILL(av) < n - 1) {
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),
584 gv_stashpv(classname, TRUE)));
588 MUTEX_LOCK(&PL_threads_mutex);
589 } while (n < PL_nthreads);
590 n = PL_nthreads; /* Get the final correct value */
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.)
601 SV *sv = (SV*)SvRV(*svp);
602 sv_setiv(sv, t->tid);
603 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
604 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
605 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
610 MUTEX_UNLOCK(&PL_threads_mutex);
611 /* Truncate any unneeded slots in av */
613 /* Finally, push all the new objects onto the stack and drop av */
615 for (svp = AvARRAY(av); n > 0; n--, svp++)
617 (void)sv_2mortal((SV*)av);
621 MODULE = Thread PACKAGE = Thread::Signal
624 kill_sighandler_thread()
626 write(sig_pipe[1], "\0", 1);
630 init_thread_signals()
632 PL_sighandlerp = handle_thread_signal;
633 if (pipe(sig_pipe) == -1)
644 ret = read(sig_pipe[0], &c, 1);
645 } while (ret == -1 && errno == EINTR);
647 croak("panic: await_signal");
648 ST(0) = sv_newmortal();
650 sv_setsv(ST(0), c ? PL_psig_ptr[c] : &PL_sv_no);
651 DEBUG_S(PerlIO_printf(Perl_debug_log,
652 "await_signal returning %s\n", SvPEEK(ST(0))););
654 MODULE = Thread PACKAGE = Thread::Specific
657 data(classname = "Thread::Specific")
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)));
665 XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));