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 PTHREAD_ATTR_SETDETACHSTATE
285 err = pthread_attr_setstacksize(&attr, (1024*16));
288 err = PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
291 croak("panic: can't pthread_attr_setdetachstate");
295 err = PTHREAD_CREATE(&thr->self, attr, threadstart, (void*) thr);
299 MUTEX_UNLOCK(&thr->mutex);
300 DEBUG_S(PerlIO_printf(Perl_debug_log,
301 "%p: create of %p failed %d\n",
302 savethread, thr, err));
303 /* Thread creation failed--clean up */
304 SvREFCNT_dec(thr->cvcache);
305 remove_thread(aTHX_ thr);
306 for (i = 0; i <= AvFILL(initargs); i++)
307 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
308 SvREFCNT_dec(startsv);
312 #ifdef THREAD_POST_CREATE
313 THREAD_POST_CREATE(thr);
315 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
316 croak("panic: sigprocmask");
319 sv = newSViv(thr->tid);
320 sv_magic(sv, thr->oursv, '~', 0, 0);
321 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
322 sv = sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
325 MUTEX_UNLOCK(&thr->mutex);
330 croak("This perl was built for \"ithreads\", which currently does not support Thread.pm.\n"
331 "Run \"perldoc Thread\" for more information");
333 croak("This perl was not built with support for 5.005-style threads.\n"
334 "Run \"perldoc Thread\" for more information");
340 static Signal_t handle_thread_signal (int sig);
343 handle_thread_signal(int sig)
346 unsigned char c = (unsigned char) sig;
348 * We're not really allowed to call fprintf in a signal handler
349 * so don't be surprised if this isn't robust while debugging
352 DEBUG_S(PerlIO_printf(Perl_debug_log,
353 "handle_thread_signal: got signal %d\n", sig););
354 write(sig_pipe[1], &c, 1);
357 MODULE = Thread PACKAGE = Thread
361 new(classname, startsv, ...)
364 AV * av = av_make(items - 2, &ST(2));
366 XPUSHs(sv_2mortal(newthread(aTHX_ startsv, av, classname)));
376 croak("Attempt to join self");
377 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: joining %p (state %u)\n",
378 thr, t, ThrSTATE(t)););
379 MUTEX_LOCK(&t->mutex);
380 switch (ThrSTATE(t)) {
381 case THRf_R_JOINABLE:
383 ThrSETSTATE(t, THRf_R_JOINED);
384 MUTEX_UNLOCK(&t->mutex);
387 ThrSETSTATE(t, THRf_DEAD);
388 MUTEX_UNLOCK(&t->mutex);
389 remove_thread(aTHX_ t);
392 MUTEX_UNLOCK(&t->mutex);
393 croak("can't join with thread");
400 if (SvTRUE(*av_fetch(av, 0, FALSE))) {
401 /* Could easily speed up the following if necessary */
402 for (i = 1; i <= AvFILL(av); i++)
403 XPUSHs(*av_fetch(av, i, FALSE));
407 char *mess = SvPV(*av_fetch(av, 1, FALSE), n_a);
408 DEBUG_S(PerlIO_printf(Perl_debug_log,
409 "%p: join propagating die message: %s\n",
420 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: detaching %p (state %u)\n",
421 thr, t, ThrSTATE(t)););
422 MUTEX_LOCK(&t->mutex);
423 switch (ThrSTATE(t)) {
424 case THRf_R_JOINABLE:
425 ThrSETSTATE(t, THRf_R_DETACHED);
427 case THRf_R_DETACHED:
429 MUTEX_UNLOCK(&t->mutex);
432 ThrSETSTATE(t, THRf_DEAD);
434 MUTEX_UNLOCK(&t->mutex);
435 remove_thread(aTHX_ t);
438 MUTEX_UNLOCK(&t->mutex);
439 croak("can't detach thread");
449 PUSHs((t1 == t2) ? &PL_sv_yes : &PL_sv_no);
456 PUSHs(sv_2mortal(newSViv(t->flags)));
464 PUSHs(t->thr_done ? &PL_sv_yes : &PL_sv_no);
474 sv = newSViv(thr->tid);
475 sv_magic(sv, thr->oursv, '~', 0, 0);
476 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
477 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
478 gv_stashpv(classname, TRUE))));
486 MUTEX_LOCK(&t->mutex);
488 MUTEX_UNLOCK(&t->mutex);
519 mg = condpair_magic(sv);
520 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_wait %p\n", thr, sv));
521 MUTEX_LOCK(MgMUTEXP(mg));
522 if (MgOWNER(mg) != thr) {
523 MUTEX_UNLOCK(MgMUTEXP(mg));
524 croak("cond_wait for lock that we don't own\n");
527 COND_SIGNAL(MgOWNERCONDP(mg));
528 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
530 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
532 MUTEX_UNLOCK(MgMUTEXP(mg));
544 mg = condpair_magic(sv);
545 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_signal %p\n",thr,sv));
546 MUTEX_LOCK(MgMUTEXP(mg));
547 if (MgOWNER(mg) != thr) {
548 MUTEX_UNLOCK(MgMUTEXP(mg));
549 croak("cond_signal for lock that we don't own\n");
551 COND_SIGNAL(MgCONDP(mg));
552 MUTEX_UNLOCK(MgMUTEXP(mg));
564 mg = condpair_magic(sv);
565 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_broadcast %p\n",
567 MUTEX_LOCK(MgMUTEXP(mg));
568 if (MgOWNER(mg) != thr) {
569 MUTEX_UNLOCK(MgMUTEXP(mg));
570 croak("cond_broadcast for lock that we don't own\n");
572 COND_BROADCAST(MgCONDP(mg));
573 MUTEX_UNLOCK(MgMUTEXP(mg));
588 * Iterate until we have enough dynamic storage for all threads.
589 * We mustn't do any allocation while holding threads_mutex though.
591 MUTEX_LOCK(&PL_threads_mutex);
594 MUTEX_UNLOCK(&PL_threads_mutex);
595 if (AvFILL(av) < n - 1) {
597 for (i = AvFILL(av); i < n - 1; i++) {
598 SV *sv = newSViv(0); /* fill in tid later */
599 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
600 av_push(av, sv_bless(newRV_noinc(sv),
601 gv_stashpv(classname, TRUE)));
605 MUTEX_LOCK(&PL_threads_mutex);
606 } while (n < PL_nthreads);
607 n = PL_nthreads; /* Get the final correct value */
610 * At this point, there's enough room to fill in av.
611 * Note that we are holding threads_mutex so the list
612 * won't change out from under us but all the remaining
613 * processing is "fast" (no blocking, malloc etc.)
618 SV *sv = (SV*)SvRV(*svp);
619 sv_setiv(sv, t->tid);
620 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
621 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
622 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
627 MUTEX_UNLOCK(&PL_threads_mutex);
628 /* Truncate any unneeded slots in av */
630 /* Finally, push all the new objects onto the stack and drop av */
632 for (svp = AvARRAY(av); n > 0; n--, svp++)
634 (void)sv_2mortal((SV*)av);
638 MODULE = Thread PACKAGE = Thread::Signal
641 kill_sighandler_thread()
643 write(sig_pipe[1], "\0", 1);
647 init_thread_signals()
649 PL_sighandlerp = handle_thread_signal;
650 if (pipe(sig_pipe) == -1)
661 ret = read(sig_pipe[0], &c, 1);
662 } while (ret == -1 && errno == EINTR);
664 croak("panic: await_signal");
665 ST(0) = sv_newmortal();
667 sv_setsv(ST(0), c ? PL_psig_ptr[c] : &PL_sv_no);
668 DEBUG_S(PerlIO_printf(Perl_debug_log,
669 "await_signal returning %s\n", SvPEEK(ST(0))););
671 MODULE = Thread PACKAGE = Thread::Specific
674 data(classname = "Thread::Specific")
678 if (AvFILL(thr->specific) == -1) {
679 GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV);
680 av_store(thr->specific, 0, newRV((SV*)GvHV(gv)));
682 XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));