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(PerlIO_stderr(),
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(PerlIO_stderr(), "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(PerlIO_stderr(), "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;
92 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "new thread %p waiting to start\n",
95 /* Don't call *anything* requiring dTHR until after SET_THR() */
97 * Wait until our creator releases us. If we didn't do this, then
98 * it would be potentially possible for out thread to carry on and
99 * do stuff before our creator fills in our "self" field. For example,
100 * if we went and created another thread which tried to JOIN with us,
101 * then we'd be in a mess.
103 MUTEX_LOCK(&thr->mutex);
104 MUTEX_UNLOCK(&thr->mutex);
107 * It's safe to wait until now to set the thread-specific pointer
108 * from our pthread_t structure to our struct perl_thread, since
109 * we're the only thread who can get at it anyway.
113 /* Only now can we use SvPEEK (which calls sv_newmortal which does dTHR) */
114 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
121 perl_call_sv(sv, G_ARRAY|G_EVAL);
123 retval = SP - (PL_stack_base + oldmark);
124 SP = PL_stack_base + oldmark + 1;
125 if (SvCUR(thr->errsv)) {
126 MUTEX_LOCK(&thr->mutex);
127 thr->flags |= THRf_DID_DIE;
128 MUTEX_UNLOCK(&thr->mutex);
129 av_store(av, 0, &PL_sv_no);
130 av_store(av, 1, newSVsv(thr->errsv));
131 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p died: %s\n",
132 thr, SvPV(thr->errsv, PL_na)));
135 for (i = 1; i <= retval; i++) {
136 PerlIO_printf(PerlIO_stderr(), "%p return[%d] = %s\n",
137 thr, i, SvPEEK(SP[i - 1]));
140 av_store(av, 0, &PL_sv_yes);
141 for (i = 1; i <= retval; i++, SP++)
142 sv_setsv(*av_fetch(av, i, TRUE), SvREFCNT_inc(*SP));
149 /* removed for debug */
150 SvREFCNT_dec(PL_curstack);
152 SvREFCNT_dec(thr->cvcache);
153 SvREFCNT_dec(thr->threadsv);
154 SvREFCNT_dec(thr->specific);
155 SvREFCNT_dec(thr->errsv);
156 SvREFCNT_dec(thr->errhv);
158 /*Safefree(cxstack);*/
159 while (PL_curstackinfo->si_next)
160 PL_curstackinfo = PL_curstackinfo->si_next;
161 while (PL_curstackinfo) {
162 PERL_SI *p = PL_curstackinfo->si_prev;
163 SvREFCNT_dec(PL_curstackinfo->si_stack);
164 Safefree(PL_curstackinfo->si_cxstack);
165 Safefree(PL_curstackinfo);
168 Safefree(PL_markstack);
169 Safefree(PL_scopestack);
170 Safefree(PL_savestack);
171 Safefree(PL_retstack);
172 Safefree(PL_tmps_stack);
176 SvREFCNT_dec(PL_nrs);
177 SvREFCNT_dec(PL_statname);
178 Safefree(PL_screamfirst);
179 Safefree(PL_screamnext);
180 Safefree(PL_reg_start_tmp);
181 SvREFCNT_dec(PL_lastscream);
182 SvREFCNT_dec(PL_defoutgv);
183 Safefree(PL_reg_poscache);
185 MUTEX_LOCK(&thr->mutex);
186 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
187 "%p: threadstart finishing: state is %u\n",
188 thr, ThrSTATE(thr)));
189 switch (ThrSTATE(thr)) {
190 case THRf_R_JOINABLE:
191 ThrSETSTATE(thr, THRf_ZOMBIE);
192 MUTEX_UNLOCK(&thr->mutex);
193 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
194 "%p: R_JOINABLE thread finished\n", thr));
197 ThrSETSTATE(thr, THRf_DEAD);
198 MUTEX_UNLOCK(&thr->mutex);
199 remove_thread(aTHX_ thr);
200 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
201 "%p: R_JOINED thread finished\n", thr));
203 case THRf_R_DETACHED:
204 ThrSETSTATE(thr, THRf_DEAD);
205 MUTEX_UNLOCK(&thr->mutex);
207 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
208 "%p: DETACHED thread finished\n", thr));
209 remove_thread(aTHX_ thr); /* This might trigger main thread to finish */
212 MUTEX_UNLOCK(&thr->mutex);
213 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
216 return THREAD_RET_CAST(av); /* Available for anyone to join with */
217 /* us unless we're detached, in which */
218 /* case noone sees the value anyway. */
221 return THREAD_RET_CAST(NULL);
226 newthread (pTHX_ SV *startsv, AV *initargs, char *classname)
234 #ifndef THREAD_CREATE
235 static pthread_attr_t attr;
236 static int attr_inited = 0;
237 sigset_t fullmask, oldmask;
238 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
242 thr = new_struct_thread(thr);
243 /* temporarily pretend to be the child thread in case the
244 * XPUSHs() below want to grow the child's stack. This is
245 * safe, since the other thread is not yet created, and we
246 * are the only ones who know about it */
249 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
250 "%p: newthread (%p), tid is %u, preparing stack\n",
251 savethread, thr, thr->tid));
252 /* The following pushes the arg list and startsv onto the *new* stack */
254 /* Could easily speed up the following greatly */
255 for (i = 0; i <= AvFILL(initargs); i++)
256 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
257 XPUSHs(SvREFCNT_inc(startsv));
260 /* On your marks... */
262 MUTEX_LOCK(&thr->mutex);
265 err = THREAD_CREATE(thr, threadstart);
268 sigfillset(&fullmask);
269 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
270 croak("panic: sigprocmask");
274 err = pthread_attr_init(&attr);
275 # ifdef PTHREAD_ATTR_SETDETACHSTATE
277 err = PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
280 croak("panic: can't pthread_attr_setdetachstate");
284 err = PTHREAD_CREATE(&thr->self, attr, threadstart, (void*) thr);
288 MUTEX_UNLOCK(&thr->mutex);
289 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
290 "%p: create of %p failed %d\n",
291 savethread, thr, err));
292 /* Thread creation failed--clean up */
293 SvREFCNT_dec(thr->cvcache);
294 remove_thread(aTHX_ thr);
295 MUTEX_DESTROY(&thr->mutex);
296 for (i = 0; i <= AvFILL(initargs); i++)
297 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
298 SvREFCNT_dec(startsv);
302 #ifdef THREAD_POST_CREATE
303 THREAD_POST_CREATE(thr);
305 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
306 croak("panic: sigprocmask");
309 sv = newSViv(thr->tid);
310 sv_magic(sv, thr->oursv, '~', 0, 0);
311 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
312 sv = sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
315 MUTEX_UNLOCK(&thr->mutex);
319 croak("No threads in this perl");
324 static Signal_t handle_thread_signal (int sig);
327 handle_thread_signal(int sig)
330 unsigned char c = (unsigned char) sig;
332 * We're not really allowed to call fprintf in a signal handler
333 * so don't be surprised if this isn't robust while debugging
336 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
337 "handle_thread_signal: got signal %d\n", sig););
338 write(sig_pipe[1], &c, 1);
341 MODULE = Thread PACKAGE = Thread
345 new(classname, startsv, ...)
348 AV * av = av_make(items - 2, &ST(2));
350 XPUSHs(sv_2mortal(newthread(aTHX_ startsv, av, classname)));
360 croak("Attempt to join self");
361 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: joining %p (state %u)\n",
362 thr, t, ThrSTATE(t)););
363 MUTEX_LOCK(&t->mutex);
364 switch (ThrSTATE(t)) {
365 case THRf_R_JOINABLE:
367 ThrSETSTATE(t, THRf_R_JOINED);
368 MUTEX_UNLOCK(&t->mutex);
371 ThrSETSTATE(t, THRf_DEAD);
372 MUTEX_UNLOCK(&t->mutex);
373 remove_thread(aTHX_ t);
376 MUTEX_UNLOCK(&t->mutex);
377 croak("can't join with thread");
382 if (SvTRUE(*av_fetch(av, 0, FALSE))) {
383 /* Could easily speed up the following if necessary */
384 for (i = 1; i <= AvFILL(av); i++)
385 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
388 char *mess = SvPV(*av_fetch(av, 1, FALSE), n_a);
389 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
390 "%p: join propagating die message: %s\n",
401 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: detaching %p (state %u)\n",
402 thr, t, ThrSTATE(t)););
403 MUTEX_LOCK(&t->mutex);
404 switch (ThrSTATE(t)) {
405 case THRf_R_JOINABLE:
406 ThrSETSTATE(t, THRf_R_DETACHED);
408 case THRf_R_DETACHED:
410 MUTEX_UNLOCK(&t->mutex);
413 ThrSETSTATE(t, THRf_DEAD);
415 MUTEX_UNLOCK(&t->mutex);
416 remove_thread(aTHX_ t);
419 MUTEX_UNLOCK(&t->mutex);
420 croak("can't detach thread");
430 PUSHs((t1 == t2) ? &PL_sv_yes : &PL_sv_no);
437 PUSHs(sv_2mortal(newSViv(t->flags)));
447 sv = newSViv(thr->tid);
448 sv_magic(sv, thr->oursv, '~', 0, 0);
449 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
450 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
451 gv_stashpv(classname, TRUE))));
459 MUTEX_LOCK(&t->mutex);
461 MUTEX_UNLOCK(&t->mutex);
492 mg = condpair_magic(sv);
493 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
494 MUTEX_LOCK(MgMUTEXP(mg));
495 if (MgOWNER(mg) != thr) {
496 MUTEX_UNLOCK(MgMUTEXP(mg));
497 croak("cond_wait for lock that we don't own\n");
500 COND_SIGNAL(MgOWNERCONDP(mg));
501 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
503 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
505 MUTEX_UNLOCK(MgMUTEXP(mg));
517 mg = condpair_magic(sv);
518 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
519 MUTEX_LOCK(MgMUTEXP(mg));
520 if (MgOWNER(mg) != thr) {
521 MUTEX_UNLOCK(MgMUTEXP(mg));
522 croak("cond_signal for lock that we don't own\n");
524 COND_SIGNAL(MgCONDP(mg));
525 MUTEX_UNLOCK(MgMUTEXP(mg));
537 mg = condpair_magic(sv);
538 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
540 MUTEX_LOCK(MgMUTEXP(mg));
541 if (MgOWNER(mg) != thr) {
542 MUTEX_UNLOCK(MgMUTEXP(mg));
543 croak("cond_broadcast for lock that we don't own\n");
545 COND_BROADCAST(MgCONDP(mg));
546 MUTEX_UNLOCK(MgMUTEXP(mg));
561 * Iterate until we have enough dynamic storage for all threads.
562 * We mustn't do any allocation while holding threads_mutex though.
564 MUTEX_LOCK(&PL_threads_mutex);
567 MUTEX_UNLOCK(&PL_threads_mutex);
568 if (AvFILL(av) < n - 1) {
570 for (i = AvFILL(av); i < n - 1; i++) {
571 SV *sv = newSViv(0); /* fill in tid later */
572 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
573 av_push(av, sv_bless(newRV_noinc(sv),
574 gv_stashpv(classname, TRUE)));
578 MUTEX_LOCK(&PL_threads_mutex);
579 } while (n < PL_nthreads);
580 n = PL_nthreads; /* Get the final correct value */
583 * At this point, there's enough room to fill in av.
584 * Note that we are holding threads_mutex so the list
585 * won't change out from under us but all the remaining
586 * processing is "fast" (no blocking, malloc etc.)
591 SV *sv = (SV*)SvRV(*svp);
592 sv_setiv(sv, t->tid);
593 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
594 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
595 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
600 MUTEX_UNLOCK(&PL_threads_mutex);
601 /* Truncate any unneeded slots in av */
603 /* Finally, push all the new objects onto the stack and drop av */
605 for (svp = AvARRAY(av); n > 0; n--, svp++)
607 (void)sv_2mortal((SV*)av);
611 MODULE = Thread PACKAGE = Thread::Signal
614 kill_sighandler_thread()
616 write(sig_pipe[1], "\0", 1);
620 init_thread_signals()
622 PL_sighandlerp = handle_thread_signal;
623 if (pipe(sig_pipe) == -1)
634 ret = read(sig_pipe[0], &c, 1);
635 } while (ret == -1 && errno == EINTR);
637 croak("panic: await_signal");
638 ST(0) = sv_newmortal();
640 sv_setsv(ST(0), c ? PL_psig_ptr[c] : &PL_sv_no);
641 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
642 "await_signal returning %s\n", SvPEEK(ST(0))););
644 MODULE = Thread PACKAGE = Thread::Specific
647 data(classname = "Thread::Specific")
651 if (AvFILL(thr->specific) == -1) {
652 GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV);
653 av_store(thr->specific, 0, newRV((SV*)GvHV(gv)));
655 XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));