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",
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 DEBUG_S(PerlIO_printf(Perl_debug_log, "new thread %p starting at %s\n",
126 perl_call_sv(sv, G_ARRAY|G_EVAL);
128 retval = SP - (PL_stack_base + oldmark);
129 SP = PL_stack_base + oldmark + 1;
130 if (SvCUR(thr->errsv)) {
131 MUTEX_LOCK(&thr->mutex);
132 thr->flags |= THRf_DID_DIE;
133 MUTEX_UNLOCK(&thr->mutex);
134 av_store(av, 0, &PL_sv_no);
135 av_store(av, 1, newSVsv(thr->errsv));
136 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p died: %s\n",
137 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);
178 SvREFCNT_dec(PL_ofs_sv);
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);
193 DEBUG_S(PerlIO_printf(Perl_debug_log,
194 "%p: threadstart finishing: state is %u\n",
195 thr, ThrSTATE(thr)));
196 switch (ThrSTATE(thr)) {
197 case THRf_R_JOINABLE:
198 ThrSETSTATE(thr, THRf_ZOMBIE);
199 MUTEX_UNLOCK(&thr->mutex);
200 DEBUG_S(PerlIO_printf(Perl_debug_log,
201 "%p: R_JOINABLE thread finished\n", thr));
204 ThrSETSTATE(thr, THRf_DEAD);
205 MUTEX_UNLOCK(&thr->mutex);
206 remove_thread(aTHX_ thr);
207 DEBUG_S(PerlIO_printf(Perl_debug_log,
208 "%p: R_JOINED thread finished\n", thr));
210 case THRf_R_DETACHED:
211 ThrSETSTATE(thr, THRf_DEAD);
212 MUTEX_UNLOCK(&thr->mutex);
214 DEBUG_S(PerlIO_printf(Perl_debug_log,
215 "%p: DETACHED thread finished\n", thr));
216 remove_thread(aTHX_ thr); /* This might trigger main thread to finish */
219 MUTEX_UNLOCK(&thr->mutex);
220 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
223 return THREAD_RET_CAST(av); /* Available for anyone to join with */
224 /* us unless we're detached, in which */
225 /* case noone sees the value anyway. */
228 return THREAD_RET_CAST(NULL);
233 newthread (pTHX_ SV *startsv, AV *initargs, char *classname)
241 #ifndef THREAD_CREATE
242 static pthread_attr_t attr;
243 static int attr_inited = 0;
244 sigset_t fullmask, oldmask;
245 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
249 thr = new_struct_thread(thr);
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 */
256 DEBUG_S(PerlIO_printf(Perl_debug_log,
257 "%p: newthread (%p), tid is %u, preparing stack\n",
258 savethread, thr, thr->tid));
259 /* The following pushes the arg list and startsv onto the *new* stack */
261 /* Could easily speed up the following greatly */
262 for (i = 0; i <= AvFILL(initargs); i++)
263 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
264 XPUSHs(SvREFCNT_inc(startsv));
267 /* On your marks... */
268 PERL_SET_THX(savethread);
269 MUTEX_LOCK(&thr->mutex);
272 err = THREAD_CREATE(thr, threadstart);
275 sigfillset(&fullmask);
276 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
277 croak("panic: sigprocmask");
281 err = pthread_attr_init(&attr);
282 # ifdef THREAD_CREATE_NEEDS_STACK
284 err = pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK);
286 croak("panic: pthread_attr_setstacksize failed");
288 croak("panic: can't pthread_attr_setstacksize");
290 # ifdef PTHREAD_ATTR_SETDETACHSTATE
292 err = PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
294 croak("panic: pthread_attr_setdetachstate failed");
296 croak("panic: can't pthread_attr_setdetachstate");
300 err = PTHREAD_CREATE(&thr->self, attr, threadstart, (void*) thr);
304 MUTEX_UNLOCK(&thr->mutex);
305 DEBUG_S(PerlIO_printf(Perl_debug_log,
306 "%p: create of %p failed %d\n",
307 savethread, thr, err));
308 /* Thread creation failed--clean up */
309 SvREFCNT_dec(thr->cvcache);
310 remove_thread(aTHX_ thr);
311 for (i = 0; i <= AvFILL(initargs); i++)
312 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
313 SvREFCNT_dec(startsv);
317 #ifdef THREAD_POST_CREATE
318 THREAD_POST_CREATE(thr);
320 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
321 croak("panic: sigprocmask");
324 sv = newSViv(thr->tid);
325 sv_magic(sv, thr->oursv, '~', 0, 0);
326 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
327 sv = sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
330 MUTEX_UNLOCK(&thr->mutex);
335 croak("This perl was built for \"ithreads\", which currently does not support Thread.pm.\n"
336 "Run \"perldoc Thread\" for more information");
338 croak("This perl was not built with support for 5.005-style threads.\n"
339 "Run \"perldoc Thread\" for more information");
345 static Signal_t handle_thread_signal (int sig);
348 handle_thread_signal(int sig)
351 unsigned char c = (unsigned char) sig;
353 * We're not really allowed to call fprintf in a signal handler
354 * so don't be surprised if this isn't robust while debugging
357 DEBUG_S(PerlIO_printf(Perl_debug_log,
358 "handle_thread_signal: got signal %d\n", sig););
359 write(sig_pipe[1], &c, 1);
362 MODULE = Thread PACKAGE = Thread
366 new(classname, startsv, ...)
369 AV * av = av_make(items - 2, &ST(2));
371 XPUSHs(sv_2mortal(newthread(aTHX_ startsv, av, classname)));
381 croak("Attempt to join self");
382 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: joining %p (state %u)\n",
383 thr, t, ThrSTATE(t)););
384 MUTEX_LOCK(&t->mutex);
385 switch (ThrSTATE(t)) {
386 case THRf_R_JOINABLE:
388 ThrSETSTATE(t, THRf_R_JOINED);
389 MUTEX_UNLOCK(&t->mutex);
392 ThrSETSTATE(t, THRf_DEAD);
393 MUTEX_UNLOCK(&t->mutex);
394 remove_thread(aTHX_ t);
397 MUTEX_UNLOCK(&t->mutex);
398 croak("can't join with thread");
405 if (SvTRUE(*av_fetch(av, 0, FALSE))) {
406 /* Could easily speed up the following if necessary */
407 for (i = 1; i <= AvFILL(av); i++)
408 XPUSHs(*av_fetch(av, i, FALSE));
412 char *mess = SvPV(*av_fetch(av, 1, FALSE), n_a);
413 DEBUG_S(PerlIO_printf(Perl_debug_log,
414 "%p: join propagating die message: %s\n",
425 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: detaching %p (state %u)\n",
426 thr, t, ThrSTATE(t)););
427 MUTEX_LOCK(&t->mutex);
428 switch (ThrSTATE(t)) {
429 case THRf_R_JOINABLE:
430 ThrSETSTATE(t, THRf_R_DETACHED);
432 case THRf_R_DETACHED:
434 MUTEX_UNLOCK(&t->mutex);
437 ThrSETSTATE(t, THRf_DEAD);
439 MUTEX_UNLOCK(&t->mutex);
440 remove_thread(aTHX_ t);
443 MUTEX_UNLOCK(&t->mutex);
444 croak("can't detach thread");
454 PUSHs((t1 == t2) ? &PL_sv_yes : &PL_sv_no);
461 PUSHs(sv_2mortal(newSViv(t->flags)));
469 PUSHs(t->thr_done ? &PL_sv_yes : &PL_sv_no);
479 sv = newSViv(thr->tid);
480 sv_magic(sv, thr->oursv, '~', 0, 0);
481 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
482 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
483 gv_stashpv(classname, TRUE))));
491 MUTEX_LOCK(&t->mutex);
493 MUTEX_UNLOCK(&t->mutex);
524 mg = condpair_magic(sv);
525 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_wait %p\n", thr, sv));
526 MUTEX_LOCK(MgMUTEXP(mg));
527 if (MgOWNER(mg) != thr) {
528 MUTEX_UNLOCK(MgMUTEXP(mg));
529 croak("cond_wait for lock that we don't own\n");
532 COND_SIGNAL(MgOWNERCONDP(mg));
533 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
535 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
537 MUTEX_UNLOCK(MgMUTEXP(mg));
549 mg = condpair_magic(sv);
550 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_signal %p\n",thr,sv));
551 MUTEX_LOCK(MgMUTEXP(mg));
552 if (MgOWNER(mg) != thr) {
553 MUTEX_UNLOCK(MgMUTEXP(mg));
554 croak("cond_signal for lock that we don't own\n");
556 COND_SIGNAL(MgCONDP(mg));
557 MUTEX_UNLOCK(MgMUTEXP(mg));
569 mg = condpair_magic(sv);
570 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_broadcast %p\n",
572 MUTEX_LOCK(MgMUTEXP(mg));
573 if (MgOWNER(mg) != thr) {
574 MUTEX_UNLOCK(MgMUTEXP(mg));
575 croak("cond_broadcast for lock that we don't own\n");
577 COND_BROADCAST(MgCONDP(mg));
578 MUTEX_UNLOCK(MgMUTEXP(mg));
593 * Iterate until we have enough dynamic storage for all threads.
594 * We mustn't do any allocation while holding threads_mutex though.
596 MUTEX_LOCK(&PL_threads_mutex);
599 MUTEX_UNLOCK(&PL_threads_mutex);
600 if (AvFILL(av) < n - 1) {
602 for (i = AvFILL(av); i < n - 1; i++) {
603 SV *sv = newSViv(0); /* fill in tid later */
604 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
605 av_push(av, sv_bless(newRV_noinc(sv),
606 gv_stashpv(classname, TRUE)));
610 MUTEX_LOCK(&PL_threads_mutex);
611 } while (n < PL_nthreads);
612 n = PL_nthreads; /* Get the final correct value */
615 * At this point, there's enough room to fill in av.
616 * Note that we are holding threads_mutex so the list
617 * won't change out from under us but all the remaining
618 * processing is "fast" (no blocking, malloc etc.)
623 SV *sv = (SV*)SvRV(*svp);
624 sv_setiv(sv, t->tid);
625 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
626 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
627 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
632 MUTEX_UNLOCK(&PL_threads_mutex);
633 /* Truncate any unneeded slots in av */
635 /* Finally, push all the new objects onto the stack and drop av */
637 for (svp = AvARRAY(av); n > 0; n--, svp++)
639 (void)sv_2mortal((SV*)av);
643 MODULE = Thread PACKAGE = Thread::Signal
646 kill_sighandler_thread()
648 write(sig_pipe[1], "\0", 1);
652 init_thread_signals()
654 PL_sighandlerp = handle_thread_signal;
655 if (pipe(sig_pipe) == -1)
666 ret = read(sig_pipe[0], &c, 1);
667 } while (ret == -1 && errno == EINTR);
669 croak("panic: await_signal");
670 ST(0) = sv_newmortal();
672 sv_setsv(ST(0), c ? PL_psig_ptr[c] : &PL_sv_no);
673 DEBUG_S(PerlIO_printf(Perl_debug_log,
674 "await_signal returning %s\n", SvPEEK(ST(0))););
676 MODULE = Thread PACKAGE = Thread::Specific
679 data(classname = "Thread::Specific")
683 if (AvFILL(thr->specific) == -1) {
684 GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV);
685 av_store(thr->specific, 0, newRV((SV*)GvHV(gv)));
687 XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));