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);
195 DEBUG_S(PerlIO_printf(Perl_debug_log,
196 "%p: threadstart finishing: state is %u\n",
197 thr, ThrSTATE(thr)));
198 switch (ThrSTATE(thr)) {
199 case THRf_R_JOINABLE:
200 ThrSETSTATE(thr, THRf_ZOMBIE);
201 MUTEX_UNLOCK(&thr->mutex);
202 DEBUG_S(PerlIO_printf(Perl_debug_log,
203 "%p: R_JOINABLE thread finished\n", thr));
206 ThrSETSTATE(thr, THRf_DEAD);
207 MUTEX_UNLOCK(&thr->mutex);
208 remove_thread(aTHX_ thr);
209 DEBUG_S(PerlIO_printf(Perl_debug_log,
210 "%p: R_JOINED thread finished\n", thr));
212 case THRf_R_DETACHED:
213 ThrSETSTATE(thr, THRf_DEAD);
214 MUTEX_UNLOCK(&thr->mutex);
216 DEBUG_S(PerlIO_printf(Perl_debug_log,
217 "%p: DETACHED thread finished\n", thr));
218 remove_thread(aTHX_ thr); /* This might trigger main thread to finish */
221 MUTEX_UNLOCK(&thr->mutex);
222 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
225 return THREAD_RET_CAST(av); /* Available for anyone to join with */
226 /* us unless we're detached, in which */
227 /* case noone sees the value anyway. */
230 return THREAD_RET_CAST(NULL);
235 newthread (pTHX_ SV *startsv, AV *initargs, char *classname)
243 #ifndef THREAD_CREATE
244 static pthread_attr_t attr;
245 static int attr_inited = 0;
246 sigset_t fullmask, oldmask;
247 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
251 thr = new_struct_thread(thr);
252 /* temporarily pretend to be the child thread in case the
253 * XPUSHs() below want to grow the child's stack. This is
254 * safe, since the other thread is not yet created, and we
255 * are the only ones who know about it */
258 DEBUG_S(PerlIO_printf(Perl_debug_log,
259 "%p: newthread (%p), tid is %u, preparing stack\n",
260 savethread, thr, thr->tid));
261 /* The following pushes the arg list and startsv onto the *new* stack */
263 /* Could easily speed up the following greatly */
264 for (i = 0; i <= AvFILL(initargs); i++)
265 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
266 XPUSHs(SvREFCNT_inc(startsv));
269 /* On your marks... */
270 PERL_SET_THX(savethread);
271 MUTEX_LOCK(&thr->mutex);
274 err = THREAD_CREATE(thr, threadstart);
277 sigfillset(&fullmask);
278 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
279 croak("panic: sigprocmask");
283 err = pthread_attr_init(&attr);
284 # ifdef PTHREAD_ATTR_SETDETACHSTATE
286 err = PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
289 croak("panic: can't pthread_attr_setdetachstate");
293 err = PTHREAD_CREATE(&thr->self, attr, threadstart, (void*) thr);
297 MUTEX_UNLOCK(&thr->mutex);
298 DEBUG_S(PerlIO_printf(Perl_debug_log,
299 "%p: create of %p failed %d\n",
300 savethread, thr, err));
301 /* Thread creation failed--clean up */
302 SvREFCNT_dec(thr->cvcache);
303 remove_thread(aTHX_ thr);
304 for (i = 0; i <= AvFILL(initargs); i++)
305 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
306 SvREFCNT_dec(startsv);
310 #ifdef THREAD_POST_CREATE
311 THREAD_POST_CREATE(thr);
313 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
314 croak("panic: sigprocmask");
317 sv = newSViv(thr->tid);
318 sv_magic(sv, thr->oursv, '~', 0, 0);
319 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
320 sv = sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
323 MUTEX_UNLOCK(&thr->mutex);
327 croak("No threads in this perl");
332 static Signal_t handle_thread_signal (int sig);
335 handle_thread_signal(int sig)
338 unsigned char c = (unsigned char) sig;
340 * We're not really allowed to call fprintf in a signal handler
341 * so don't be surprised if this isn't robust while debugging
344 DEBUG_S(PerlIO_printf(Perl_debug_log,
345 "handle_thread_signal: got signal %d\n", sig););
346 write(sig_pipe[1], &c, 1);
349 MODULE = Thread PACKAGE = Thread
353 new(classname, startsv, ...)
356 AV * av = av_make(items - 2, &ST(2));
358 XPUSHs(sv_2mortal(newthread(aTHX_ startsv, av, classname)));
368 croak("Attempt to join self");
369 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: joining %p (state %u)\n",
370 thr, t, ThrSTATE(t)););
371 MUTEX_LOCK(&t->mutex);
372 switch (ThrSTATE(t)) {
373 case THRf_R_JOINABLE:
375 ThrSETSTATE(t, THRf_R_JOINED);
376 MUTEX_UNLOCK(&t->mutex);
379 ThrSETSTATE(t, THRf_DEAD);
380 MUTEX_UNLOCK(&t->mutex);
381 remove_thread(aTHX_ t);
384 MUTEX_UNLOCK(&t->mutex);
385 croak("can't join with thread");
392 if (SvTRUE(*av_fetch(av, 0, FALSE))) {
393 /* Could easily speed up the following if necessary */
394 for (i = 1; i <= AvFILL(av); i++)
395 XPUSHs(*av_fetch(av, i, FALSE));
399 char *mess = SvPV(*av_fetch(av, 1, FALSE), n_a);
400 DEBUG_S(PerlIO_printf(Perl_debug_log,
401 "%p: join propagating die message: %s\n",
412 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: detaching %p (state %u)\n",
413 thr, t, ThrSTATE(t)););
414 MUTEX_LOCK(&t->mutex);
415 switch (ThrSTATE(t)) {
416 case THRf_R_JOINABLE:
417 ThrSETSTATE(t, THRf_R_DETACHED);
419 case THRf_R_DETACHED:
421 MUTEX_UNLOCK(&t->mutex);
424 ThrSETSTATE(t, THRf_DEAD);
426 MUTEX_UNLOCK(&t->mutex);
427 remove_thread(aTHX_ t);
430 MUTEX_UNLOCK(&t->mutex);
431 croak("can't detach thread");
441 PUSHs((t1 == t2) ? &PL_sv_yes : &PL_sv_no);
448 PUSHs(sv_2mortal(newSViv(t->flags)));
456 PUSHs(t->thr_done ? &PL_sv_yes : &PL_sv_no);
466 sv = newSViv(thr->tid);
467 sv_magic(sv, thr->oursv, '~', 0, 0);
468 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
469 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
470 gv_stashpv(classname, TRUE))));
478 MUTEX_LOCK(&t->mutex);
480 MUTEX_UNLOCK(&t->mutex);
511 mg = condpair_magic(sv);
512 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_wait %p\n", thr, sv));
513 MUTEX_LOCK(MgMUTEXP(mg));
514 if (MgOWNER(mg) != thr) {
515 MUTEX_UNLOCK(MgMUTEXP(mg));
516 croak("cond_wait for lock that we don't own\n");
519 COND_SIGNAL(MgOWNERCONDP(mg));
520 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
522 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
524 MUTEX_UNLOCK(MgMUTEXP(mg));
536 mg = condpair_magic(sv);
537 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_signal %p\n",thr,sv));
538 MUTEX_LOCK(MgMUTEXP(mg));
539 if (MgOWNER(mg) != thr) {
540 MUTEX_UNLOCK(MgMUTEXP(mg));
541 croak("cond_signal for lock that we don't own\n");
543 COND_SIGNAL(MgCONDP(mg));
544 MUTEX_UNLOCK(MgMUTEXP(mg));
556 mg = condpair_magic(sv);
557 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_broadcast %p\n",
559 MUTEX_LOCK(MgMUTEXP(mg));
560 if (MgOWNER(mg) != thr) {
561 MUTEX_UNLOCK(MgMUTEXP(mg));
562 croak("cond_broadcast for lock that we don't own\n");
564 COND_BROADCAST(MgCONDP(mg));
565 MUTEX_UNLOCK(MgMUTEXP(mg));
580 * Iterate until we have enough dynamic storage for all threads.
581 * We mustn't do any allocation while holding threads_mutex though.
583 MUTEX_LOCK(&PL_threads_mutex);
586 MUTEX_UNLOCK(&PL_threads_mutex);
587 if (AvFILL(av) < n - 1) {
589 for (i = AvFILL(av); i < n - 1; i++) {
590 SV *sv = newSViv(0); /* fill in tid later */
591 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
592 av_push(av, sv_bless(newRV_noinc(sv),
593 gv_stashpv(classname, TRUE)));
597 MUTEX_LOCK(&PL_threads_mutex);
598 } while (n < PL_nthreads);
599 n = PL_nthreads; /* Get the final correct value */
602 * At this point, there's enough room to fill in av.
603 * Note that we are holding threads_mutex so the list
604 * won't change out from under us but all the remaining
605 * processing is "fast" (no blocking, malloc etc.)
610 SV *sv = (SV*)SvRV(*svp);
611 sv_setiv(sv, t->tid);
612 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
613 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
614 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
619 MUTEX_UNLOCK(&PL_threads_mutex);
620 /* Truncate any unneeded slots in av */
622 /* Finally, push all the new objects onto the stack and drop av */
624 for (svp = AvARRAY(av); n > 0; n--, svp++)
626 (void)sv_2mortal((SV*)av);
630 MODULE = Thread PACKAGE = Thread::Signal
633 kill_sighandler_thread()
635 write(sig_pipe[1], "\0", 1);
639 init_thread_signals()
641 PL_sighandlerp = handle_thread_signal;
642 if (pipe(sig_pipe) == -1)
653 ret = read(sig_pipe[0], &c, 1);
654 } while (ret == -1 && errno == EINTR);
656 croak("panic: await_signal");
657 ST(0) = sv_newmortal();
659 sv_setsv(ST(0), c ? PL_psig_ptr[c] : &PL_sv_no);
660 DEBUG_S(PerlIO_printf(Perl_debug_log,
661 "await_signal returning %s\n", SvPEEK(ST(0))););
663 MODULE = Thread PACKAGE = Thread::Specific
666 data(classname = "Thread::Specific")
670 if (AvFILL(thr->specific) == -1) {
671 GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV);
672 av_store(thr->specific, 0, newRV((SV*)GvHV(gv)));
674 XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));