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_ struct perl_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 COND_BROADCAST(&PL_nthreads_cond);
35 MUTEX_UNLOCK(&PL_threads_mutex);
39 static THREAD_RET_TYPE
40 threadstart(void *arg)
44 Thread savethread = thr;
47 I32 oldscope = PL_scopestack_ix;
52 DEBUG_S(PerlIO_printf(Perl_debug_log, "new thread %p starting at %s\n",
56 thr->prev = thr->prev_run = savethread;
57 thr->next = savethread->next;
58 thr->next_run = savethread->next_run;
59 savethread->next = savethread->next_run = thr;
63 /* Now duplicate most of perl_call_sv but with a few twists */
65 Zero(PL_op, 1, LOGOP);
66 myop.op_flags = OPf_STACKED;
67 myop.op_next = Nullop;
68 myop.op_flags |= OPf_KNOW;
69 myop.op_flags |= OPf_WANT_LIST;
70 PL_op = pp_entersub(ARGS);
72 PerlIO_printf(Perl_debug_log, "thread starts at Nullop\n"));
74 * When this thread is next scheduled, we start in the right
75 * place. When the thread runs off the end of the sub, perl.c
76 * handles things, using savemark to figure out how much of the
77 * stack is the return value for any join.
79 thr = savethread; /* back to the old thread */
82 Thread thr = (Thread) arg;
85 I32 oldmark = TOPMARK;
86 I32 oldscope = PL_scopestack_ix;
93 #if defined(MULTIPLICITY)
94 PERL_SET_INTERP(thr->interp);
97 DEBUG_S(PerlIO_printf(Perl_debug_log, "new thread %p waiting to start\n",
100 /* Don't call *anything* requiring dTHR until after SET_THR() */
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,
105 * if we went and created another thread which tried to JOIN with us,
106 * then we'd be in a mess.
108 MUTEX_LOCK(&thr->mutex);
109 MUTEX_UNLOCK(&thr->mutex);
112 * It's safe to wait until now to set the thread-specific pointer
113 * from our pthread_t structure to our struct perl_thread, since
114 * we're the only thread who can get at it anyway.
118 /* Only now can we use SvPEEK (which calls sv_newmortal which does dTHR) */
119 DEBUG_S(PerlIO_printf(Perl_debug_log, "new thread %p starting at %s\n",
127 perl_call_sv(sv, G_ARRAY|G_EVAL);
129 retval = SP - (PL_stack_base + oldmark);
130 SP = PL_stack_base + oldmark + 1;
131 if (SvCUR(thr->errsv)) {
132 MUTEX_LOCK(&thr->mutex);
133 thr->flags |= THRf_DID_DIE;
134 MUTEX_UNLOCK(&thr->mutex);
135 av_store(av, 0, &PL_sv_no);
136 av_store(av, 1, newSVsv(thr->errsv));
137 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p died: %s\n",
138 thr, SvPV(thr->errsv, PL_na)));
141 for (i = 1; i <= retval; i++) {
142 PerlIO_printf(Perl_debug_log, "%p return[%d] = %s\n",
143 thr, i, SvPEEK(SP[i - 1]));
146 av_store(av, 0, &PL_sv_yes);
147 for (i = 1; i <= retval; i++, SP++)
148 sv_setsv(*av_fetch(av, i, TRUE), SvREFCNT_inc(*SP));
155 /* removed for debug */
156 SvREFCNT_dec(PL_curstack);
158 SvREFCNT_dec(thr->cvcache);
159 SvREFCNT_dec(thr->threadsv);
160 SvREFCNT_dec(thr->specific);
161 SvREFCNT_dec(thr->errsv);
163 /*Safefree(cxstack);*/
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);
173 Safefree(PL_markstack);
174 Safefree(PL_scopestack);
175 Safefree(PL_savestack);
176 Safefree(PL_retstack);
177 Safefree(PL_tmps_stack);
181 SvREFCNT_dec(PL_nrs);
182 SvREFCNT_dec(PL_statname);
183 SvREFCNT_dec(PL_errors);
184 Safefree(PL_screamfirst);
185 Safefree(PL_screamnext);
186 Safefree(PL_reg_start_tmp);
187 SvREFCNT_dec(PL_lastscream);
188 SvREFCNT_dec(PL_defoutgv);
189 Safefree(PL_reg_poscache);
191 MUTEX_LOCK(&thr->mutex);
192 DEBUG_S(PerlIO_printf(Perl_debug_log,
193 "%p: threadstart finishing: state is %u\n",
194 thr, ThrSTATE(thr)));
195 switch (ThrSTATE(thr)) {
196 case THRf_R_JOINABLE:
197 ThrSETSTATE(thr, THRf_ZOMBIE);
198 MUTEX_UNLOCK(&thr->mutex);
199 DEBUG_S(PerlIO_printf(Perl_debug_log,
200 "%p: R_JOINABLE thread finished\n", thr));
203 ThrSETSTATE(thr, THRf_DEAD);
204 MUTEX_UNLOCK(&thr->mutex);
205 remove_thread(aTHX_ thr);
206 DEBUG_S(PerlIO_printf(Perl_debug_log,
207 "%p: R_JOINED thread finished\n", thr));
209 case THRf_R_DETACHED:
210 ThrSETSTATE(thr, THRf_DEAD);
211 MUTEX_UNLOCK(&thr->mutex);
213 DEBUG_S(PerlIO_printf(Perl_debug_log,
214 "%p: DETACHED thread finished\n", thr));
215 remove_thread(aTHX_ thr); /* This might trigger main thread to finish */
218 MUTEX_UNLOCK(&thr->mutex);
219 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
222 return THREAD_RET_CAST(av); /* Available for anyone to join with */
223 /* us unless we're detached, in which */
224 /* case noone sees the value anyway. */
227 return THREAD_RET_CAST(NULL);
232 newthread (pTHX_ SV *startsv, AV *initargs, char *classname)
240 #ifndef THREAD_CREATE
241 static pthread_attr_t attr;
242 static int attr_inited = 0;
243 sigset_t fullmask, oldmask;
244 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
248 thr = new_struct_thread(thr);
249 /* temporarily pretend to be the child thread in case the
250 * XPUSHs() below want to grow the child's stack. This is
251 * safe, since the other thread is not yet created, and we
252 * are the only ones who know about it */
255 DEBUG_S(PerlIO_printf(Perl_debug_log,
256 "%p: newthread (%p), tid is %u, preparing stack\n",
257 savethread, thr, thr->tid));
258 /* The following pushes the arg list and startsv onto the *new* stack */
260 /* Could easily speed up the following greatly */
261 for (i = 0; i <= AvFILL(initargs); i++)
262 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
263 XPUSHs(SvREFCNT_inc(startsv));
266 /* On your marks... */
268 MUTEX_LOCK(&thr->mutex);
271 err = THREAD_CREATE(thr, threadstart);
274 sigfillset(&fullmask);
275 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
276 croak("panic: sigprocmask");
280 err = pthread_attr_init(&attr);
281 # ifdef PTHREAD_ATTR_SETDETACHSTATE
283 err = PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
286 croak("panic: can't pthread_attr_setdetachstate");
290 err = PTHREAD_CREATE(&thr->self, attr, threadstart, (void*) thr);
294 MUTEX_UNLOCK(&thr->mutex);
295 DEBUG_S(PerlIO_printf(Perl_debug_log,
296 "%p: create of %p failed %d\n",
297 savethread, thr, err));
298 /* Thread creation failed--clean up */
299 SvREFCNT_dec(thr->cvcache);
300 remove_thread(aTHX_ thr);
301 MUTEX_DESTROY(&thr->mutex);
302 for (i = 0; i <= AvFILL(initargs); i++)
303 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
304 SvREFCNT_dec(startsv);
308 #ifdef THREAD_POST_CREATE
309 THREAD_POST_CREATE(thr);
311 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
312 croak("panic: sigprocmask");
315 sv = newSViv(thr->tid);
316 sv_magic(sv, thr->oursv, '~', 0, 0);
317 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
318 sv = sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
321 MUTEX_UNLOCK(&thr->mutex);
325 croak("No threads in this perl");
330 static Signal_t handle_thread_signal (int sig);
333 handle_thread_signal(int sig)
336 unsigned char c = (unsigned char) sig;
338 * We're not really allowed to call fprintf in a signal handler
339 * so don't be surprised if this isn't robust while debugging
342 DEBUG_S(PerlIO_printf(Perl_debug_log,
343 "handle_thread_signal: got signal %d\n", sig););
344 write(sig_pipe[1], &c, 1);
347 MODULE = Thread PACKAGE = Thread
351 new(classname, startsv, ...)
354 AV * av = av_make(items - 2, &ST(2));
356 XPUSHs(sv_2mortal(newthread(aTHX_ startsv, av, classname)));
366 croak("Attempt to join self");
367 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: joining %p (state %u)\n",
368 thr, t, ThrSTATE(t)););
369 MUTEX_LOCK(&t->mutex);
370 switch (ThrSTATE(t)) {
371 case THRf_R_JOINABLE:
373 ThrSETSTATE(t, THRf_R_JOINED);
374 MUTEX_UNLOCK(&t->mutex);
377 ThrSETSTATE(t, THRf_DEAD);
378 MUTEX_UNLOCK(&t->mutex);
379 remove_thread(aTHX_ t);
382 MUTEX_UNLOCK(&t->mutex);
383 croak("can't join with thread");
388 if (SvTRUE(*av_fetch(av, 0, FALSE))) {
389 /* Could easily speed up the following if necessary */
390 for (i = 1; i <= AvFILL(av); i++)
391 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
394 char *mess = SvPV(*av_fetch(av, 1, FALSE), n_a);
395 DEBUG_S(PerlIO_printf(Perl_debug_log,
396 "%p: join propagating die message: %s\n",
407 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: detaching %p (state %u)\n",
408 thr, t, ThrSTATE(t)););
409 MUTEX_LOCK(&t->mutex);
410 switch (ThrSTATE(t)) {
411 case THRf_R_JOINABLE:
412 ThrSETSTATE(t, THRf_R_DETACHED);
414 case THRf_R_DETACHED:
416 MUTEX_UNLOCK(&t->mutex);
419 ThrSETSTATE(t, THRf_DEAD);
421 MUTEX_UNLOCK(&t->mutex);
422 remove_thread(aTHX_ t);
425 MUTEX_UNLOCK(&t->mutex);
426 croak("can't detach thread");
436 PUSHs((t1 == t2) ? &PL_sv_yes : &PL_sv_no);
443 PUSHs(sv_2mortal(newSViv(t->flags)));
453 sv = newSViv(thr->tid);
454 sv_magic(sv, thr->oursv, '~', 0, 0);
455 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
456 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
457 gv_stashpv(classname, TRUE))));
465 MUTEX_LOCK(&t->mutex);
467 MUTEX_UNLOCK(&t->mutex);
498 mg = condpair_magic(sv);
499 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_wait %p\n", thr, sv));
500 MUTEX_LOCK(MgMUTEXP(mg));
501 if (MgOWNER(mg) != thr) {
502 MUTEX_UNLOCK(MgMUTEXP(mg));
503 croak("cond_wait for lock that we don't own\n");
506 COND_SIGNAL(MgOWNERCONDP(mg));
507 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
509 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
511 MUTEX_UNLOCK(MgMUTEXP(mg));
523 mg = condpair_magic(sv);
524 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_signal %p\n",thr,sv));
525 MUTEX_LOCK(MgMUTEXP(mg));
526 if (MgOWNER(mg) != thr) {
527 MUTEX_UNLOCK(MgMUTEXP(mg));
528 croak("cond_signal for lock that we don't own\n");
530 COND_SIGNAL(MgCONDP(mg));
531 MUTEX_UNLOCK(MgMUTEXP(mg));
543 mg = condpair_magic(sv);
544 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_broadcast %p\n",
546 MUTEX_LOCK(MgMUTEXP(mg));
547 if (MgOWNER(mg) != thr) {
548 MUTEX_UNLOCK(MgMUTEXP(mg));
549 croak("cond_broadcast for lock that we don't own\n");
551 COND_BROADCAST(MgCONDP(mg));
552 MUTEX_UNLOCK(MgMUTEXP(mg));
567 * Iterate until we have enough dynamic storage for all threads.
568 * We mustn't do any allocation while holding threads_mutex though.
570 MUTEX_LOCK(&PL_threads_mutex);
573 MUTEX_UNLOCK(&PL_threads_mutex);
574 if (AvFILL(av) < n - 1) {
576 for (i = AvFILL(av); i < n - 1; i++) {
577 SV *sv = newSViv(0); /* fill in tid later */
578 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
579 av_push(av, sv_bless(newRV_noinc(sv),
580 gv_stashpv(classname, TRUE)));
584 MUTEX_LOCK(&PL_threads_mutex);
585 } while (n < PL_nthreads);
586 n = PL_nthreads; /* Get the final correct value */
589 * At this point, there's enough room to fill in av.
590 * Note that we are holding threads_mutex so the list
591 * won't change out from under us but all the remaining
592 * processing is "fast" (no blocking, malloc etc.)
597 SV *sv = (SV*)SvRV(*svp);
598 sv_setiv(sv, t->tid);
599 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
600 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
601 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
606 MUTEX_UNLOCK(&PL_threads_mutex);
607 /* Truncate any unneeded slots in av */
609 /* Finally, push all the new objects onto the stack and drop av */
611 for (svp = AvARRAY(av); n > 0; n--, svp++)
613 (void)sv_2mortal((SV*)av);
617 MODULE = Thread PACKAGE = Thread::Signal
620 kill_sighandler_thread()
622 write(sig_pipe[1], "\0", 1);
626 init_thread_signals()
628 PL_sighandlerp = handle_thread_signal;
629 if (pipe(sig_pipe) == -1)
640 ret = read(sig_pipe[0], &c, 1);
641 } while (ret == -1 && errno == EINTR);
643 croak("panic: await_signal");
644 ST(0) = sv_newmortal();
646 sv_setsv(ST(0), c ? PL_psig_ptr[c] : &PL_sv_no);
647 DEBUG_S(PerlIO_printf(Perl_debug_log,
648 "await_signal returning %s\n", SvPEEK(ST(0))););
650 MODULE = Thread PACKAGE = Thread::Specific
653 data(classname = "Thread::Specific")
657 if (AvFILL(thr->specific) == -1) {
658 GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV);
659 av_store(thr->specific, 0, newRV((SV*)GvHV(gv)));
661 XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));