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
284 err = PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
287 croak("panic: can't pthread_attr_setdetachstate");
291 err = PTHREAD_CREATE(&thr->self, attr, threadstart, (void*) thr);
295 MUTEX_UNLOCK(&thr->mutex);
296 DEBUG_S(PerlIO_printf(Perl_debug_log,
297 "%p: create of %p failed %d\n",
298 savethread, thr, err));
299 /* Thread creation failed--clean up */
300 SvREFCNT_dec(thr->cvcache);
301 remove_thread(aTHX_ thr);
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);
326 croak("This perl was built for \"ithreads\", which currently does not support Thread.pm.\n"
327 "Run \"perldoc Thread\" for more information");
329 croak("This perl was not built with support for 5.005-style threads.\n"
330 "Run \"perldoc Thread\" for more information");
336 static Signal_t handle_thread_signal (int sig);
339 handle_thread_signal(int sig)
342 unsigned char c = (unsigned char) sig;
344 * We're not really allowed to call fprintf in a signal handler
345 * so don't be surprised if this isn't robust while debugging
348 DEBUG_S(PerlIO_printf(Perl_debug_log,
349 "handle_thread_signal: got signal %d\n", sig););
350 write(sig_pipe[1], &c, 1);
353 MODULE = Thread PACKAGE = Thread
357 new(classname, startsv, ...)
360 AV * av = av_make(items - 2, &ST(2));
362 XPUSHs(sv_2mortal(newthread(aTHX_ startsv, av, classname)));
372 croak("Attempt to join self");
373 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: joining %p (state %u)\n",
374 thr, t, ThrSTATE(t)););
375 MUTEX_LOCK(&t->mutex);
376 switch (ThrSTATE(t)) {
377 case THRf_R_JOINABLE:
379 ThrSETSTATE(t, THRf_R_JOINED);
380 MUTEX_UNLOCK(&t->mutex);
383 ThrSETSTATE(t, THRf_DEAD);
384 MUTEX_UNLOCK(&t->mutex);
385 remove_thread(aTHX_ t);
388 MUTEX_UNLOCK(&t->mutex);
389 croak("can't join with thread");
396 if (SvTRUE(*av_fetch(av, 0, FALSE))) {
397 /* Could easily speed up the following if necessary */
398 for (i = 1; i <= AvFILL(av); i++)
399 XPUSHs(*av_fetch(av, i, FALSE));
403 char *mess = SvPV(*av_fetch(av, 1, FALSE), n_a);
404 DEBUG_S(PerlIO_printf(Perl_debug_log,
405 "%p: join propagating die message: %s\n",
416 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: detaching %p (state %u)\n",
417 thr, t, ThrSTATE(t)););
418 MUTEX_LOCK(&t->mutex);
419 switch (ThrSTATE(t)) {
420 case THRf_R_JOINABLE:
421 ThrSETSTATE(t, THRf_R_DETACHED);
423 case THRf_R_DETACHED:
425 MUTEX_UNLOCK(&t->mutex);
428 ThrSETSTATE(t, THRf_DEAD);
430 MUTEX_UNLOCK(&t->mutex);
431 remove_thread(aTHX_ t);
434 MUTEX_UNLOCK(&t->mutex);
435 croak("can't detach thread");
445 PUSHs((t1 == t2) ? &PL_sv_yes : &PL_sv_no);
452 PUSHs(sv_2mortal(newSViv(t->flags)));
460 PUSHs(t->thr_done ? &PL_sv_yes : &PL_sv_no);
470 sv = newSViv(thr->tid);
471 sv_magic(sv, thr->oursv, '~', 0, 0);
472 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
473 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
474 gv_stashpv(classname, TRUE))));
482 MUTEX_LOCK(&t->mutex);
484 MUTEX_UNLOCK(&t->mutex);
515 mg = condpair_magic(sv);
516 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_wait %p\n", thr, sv));
517 MUTEX_LOCK(MgMUTEXP(mg));
518 if (MgOWNER(mg) != thr) {
519 MUTEX_UNLOCK(MgMUTEXP(mg));
520 croak("cond_wait for lock that we don't own\n");
523 COND_SIGNAL(MgOWNERCONDP(mg));
524 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
526 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
528 MUTEX_UNLOCK(MgMUTEXP(mg));
540 mg = condpair_magic(sv);
541 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_signal %p\n",thr,sv));
542 MUTEX_LOCK(MgMUTEXP(mg));
543 if (MgOWNER(mg) != thr) {
544 MUTEX_UNLOCK(MgMUTEXP(mg));
545 croak("cond_signal for lock that we don't own\n");
547 COND_SIGNAL(MgCONDP(mg));
548 MUTEX_UNLOCK(MgMUTEXP(mg));
560 mg = condpair_magic(sv);
561 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_broadcast %p\n",
563 MUTEX_LOCK(MgMUTEXP(mg));
564 if (MgOWNER(mg) != thr) {
565 MUTEX_UNLOCK(MgMUTEXP(mg));
566 croak("cond_broadcast for lock that we don't own\n");
568 COND_BROADCAST(MgCONDP(mg));
569 MUTEX_UNLOCK(MgMUTEXP(mg));
584 * Iterate until we have enough dynamic storage for all threads.
585 * We mustn't do any allocation while holding threads_mutex though.
587 MUTEX_LOCK(&PL_threads_mutex);
590 MUTEX_UNLOCK(&PL_threads_mutex);
591 if (AvFILL(av) < n - 1) {
593 for (i = AvFILL(av); i < n - 1; i++) {
594 SV *sv = newSViv(0); /* fill in tid later */
595 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
596 av_push(av, sv_bless(newRV_noinc(sv),
597 gv_stashpv(classname, TRUE)));
601 MUTEX_LOCK(&PL_threads_mutex);
602 } while (n < PL_nthreads);
603 n = PL_nthreads; /* Get the final correct value */
606 * At this point, there's enough room to fill in av.
607 * Note that we are holding threads_mutex so the list
608 * won't change out from under us but all the remaining
609 * processing is "fast" (no blocking, malloc etc.)
614 SV *sv = (SV*)SvRV(*svp);
615 sv_setiv(sv, t->tid);
616 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
617 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
618 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
623 MUTEX_UNLOCK(&PL_threads_mutex);
624 /* Truncate any unneeded slots in av */
626 /* Finally, push all the new objects onto the stack and drop av */
628 for (svp = AvARRAY(av); n > 0; n--, svp++)
630 (void)sv_2mortal((SV*)av);
634 MODULE = Thread PACKAGE = Thread::Signal
637 kill_sighandler_thread()
639 write(sig_pipe[1], "\0", 1);
643 init_thread_signals()
645 PL_sighandlerp = handle_thread_signal;
646 if (pipe(sig_pipe) == -1)
657 ret = read(sig_pipe[0], &c, 1);
658 } while (ret == -1 && errno == EINTR);
660 croak("panic: await_signal");
661 ST(0) = sv_newmortal();
663 sv_setsv(ST(0), c ? PL_psig_ptr[c] : &PL_sv_no);
664 DEBUG_S(PerlIO_printf(Perl_debug_log,
665 "await_signal returning %s\n", SvPEEK(ST(0))););
667 MODULE = Thread PACKAGE = Thread::Specific
670 data(classname = "Thread::Specific")
674 if (AvFILL(thr->specific) == -1) {
675 GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV);
676 av_store(thr->specific, 0, newRV((SV*)GvHV(gv)));
678 XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));