5 /* Magic signature for Thread's mg_private is "Th" */
6 #define Thread_MAGIC_SIGNATURE 0x5468
15 static int sig_pipe[2];
17 #ifndef THREAD_RET_TYPE
18 #define THREAD_RET_TYPE void *
19 #define THREAD_RET_CAST(x) ((THREAD_RET_TYPE) x)
23 remove_thread(struct perl_thread *t)
26 DEBUG_S(WITH_THR(PerlIO_printf(PerlIO_stderr(),
27 "%p: remove_thread %p\n", thr, t)));
28 MUTEX_LOCK(&PL_threads_mutex);
29 MUTEX_DESTROY(&t->mutex);
31 t->prev->next = t->next;
32 t->next->prev = t->prev;
33 COND_BROADCAST(&PL_nthreads_cond);
34 MUTEX_UNLOCK(&PL_threads_mutex);
38 static THREAD_RET_TYPE
39 threadstart(void *arg)
43 Thread savethread = thr;
46 I32 oldscope = PL_scopestack_ix;
51 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
55 thr->prev = thr->prev_run = savethread;
56 thr->next = savethread->next;
57 thr->next_run = savethread->next_run;
58 savethread->next = savethread->next_run = thr;
62 /* Now duplicate most of perl_call_sv but with a few twists */
64 Zero(PL_op, 1, LOGOP);
65 myop.op_flags = OPf_STACKED;
66 myop.op_next = Nullop;
67 myop.op_flags |= OPf_KNOW;
68 myop.op_flags |= OPf_WANT_LIST;
69 PL_op = pp_entersub(ARGS);
71 PerlIO_printf(PerlIO_stderr(), "thread starts at Nullop\n"));
73 * When this thread is next scheduled, we start in the right
74 * place. When the thread runs off the end of the sub, perl.c
75 * handles things, using savemark to figure out how much of the
76 * stack is the return value for any join.
78 thr = savethread; /* back to the old thread */
81 Thread thr = (Thread) arg;
84 I32 oldmark = TOPMARK;
85 I32 oldscope = PL_scopestack_ix;
91 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "new thread %p waiting to start\n",
94 /* Don't call *anything* requiring dTHR until after SET_THR() */
96 * Wait until our creator releases us. If we didn't do this, then
97 * it would be potentially possible for out thread to carry on and
98 * do stuff before our creator fills in our "self" field. For example,
99 * if we went and created another thread which tried to JOIN with us,
100 * then we'd be in a mess.
102 MUTEX_LOCK(&thr->mutex);
103 MUTEX_UNLOCK(&thr->mutex);
106 * It's safe to wait until now to set the thread-specific pointer
107 * from our pthread_t structure to our struct perl_thread, since
108 * we're the only thread who can get at it anyway.
112 /* Only now can we use SvPEEK (which calls sv_newmortal which does dTHR) */
113 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
120 perl_call_sv(sv, G_ARRAY|G_EVAL);
122 retval = SP - (PL_stack_base + oldmark);
123 SP = PL_stack_base + oldmark + 1;
124 if (SvCUR(thr->errsv)) {
125 MUTEX_LOCK(&thr->mutex);
126 thr->flags |= THRf_DID_DIE;
127 MUTEX_UNLOCK(&thr->mutex);
128 av_store(av, 0, &PL_sv_no);
129 av_store(av, 1, newSVsv(thr->errsv));
130 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p died: %s\n",
131 thr, SvPV(thr->errsv, PL_na)));
134 for (i = 1; i <= retval; i++) {
135 PerlIO_printf(PerlIO_stderr(), "%p return[%d] = %s\n",
136 thr, i, SvPEEK(SP[i - 1]));
139 av_store(av, 0, &PL_sv_yes);
140 for (i = 1; i <= retval; i++, SP++)
141 sv_setsv(*av_fetch(av, i, TRUE), SvREFCNT_inc(*SP));
148 /* removed for debug */
149 SvREFCNT_dec(PL_curstack);
151 SvREFCNT_dec(thr->cvcache);
152 SvREFCNT_dec(thr->threadsv);
153 SvREFCNT_dec(thr->specific);
154 SvREFCNT_dec(thr->errsv);
155 SvREFCNT_dec(thr->errhv);
157 /*Safefree(cxstack);*/
158 while (PL_curstackinfo->si_next)
159 PL_curstackinfo = PL_curstackinfo->si_next;
160 while (PL_curstackinfo) {
161 PERL_SI *p = PL_curstackinfo->si_prev;
162 SvREFCNT_dec(PL_curstackinfo->si_stack);
163 Safefree(PL_curstackinfo->si_cxstack);
164 Safefree(PL_curstackinfo);
167 Safefree(PL_markstack);
168 Safefree(PL_scopestack);
169 Safefree(PL_savestack);
170 Safefree(PL_retstack);
171 Safefree(PL_tmps_stack);
175 SvREFCNT_dec(PL_nrs);
176 SvREFCNT_dec(PL_statname);
177 Safefree(PL_screamfirst);
178 Safefree(PL_screamnext);
179 Safefree(PL_reg_start_tmp);
180 SvREFCNT_dec(PL_lastscream);
181 SvREFCNT_dec(PL_defoutgv);
183 MUTEX_LOCK(&thr->mutex);
184 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
185 "%p: threadstart finishing: state is %u\n",
186 thr, ThrSTATE(thr)));
187 switch (ThrSTATE(thr)) {
188 case THRf_R_JOINABLE:
189 ThrSETSTATE(thr, THRf_ZOMBIE);
190 MUTEX_UNLOCK(&thr->mutex);
191 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
192 "%p: R_JOINABLE thread finished\n", thr));
195 ThrSETSTATE(thr, THRf_DEAD);
196 MUTEX_UNLOCK(&thr->mutex);
198 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
199 "%p: R_JOINED thread finished\n", thr));
201 case THRf_R_DETACHED:
202 ThrSETSTATE(thr, THRf_DEAD);
203 MUTEX_UNLOCK(&thr->mutex);
205 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
206 "%p: DETACHED thread finished\n", thr));
207 remove_thread(thr); /* This might trigger main thread to finish */
210 MUTEX_UNLOCK(&thr->mutex);
211 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
214 return THREAD_RET_CAST(av); /* Available for anyone to join with */
215 /* us unless we're detached, in which */
216 /* case noone sees the value anyway. */
219 return THREAD_RET_CAST(NULL);
224 newthread (SV *startsv, AV *initargs, char *classname)
232 #ifndef THREAD_CREATE
233 static pthread_attr_t attr;
234 static int attr_inited = 0;
235 sigset_t fullmask, oldmask;
236 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
240 thr = new_struct_thread(thr);
241 /* temporarily pretend to be the child thread in case the
242 * XPUSHs() below want to grow the child's stack. This is
243 * safe, since the other thread is not yet created, and we
244 * are the only ones who know about it */
247 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
248 "%p: newthread (%p), tid is %u, preparing stack\n",
249 savethread, thr, thr->tid));
250 /* The following pushes the arg list and startsv onto the *new* stack */
252 /* Could easily speed up the following greatly */
253 for (i = 0; i <= AvFILL(initargs); i++)
254 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
255 XPUSHs(SvREFCNT_inc(startsv));
258 /* On your marks... */
260 MUTEX_LOCK(&thr->mutex);
263 err = THREAD_CREATE(thr, threadstart);
266 sigfillset(&fullmask);
267 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
268 croak("panic: sigprocmask");
272 err = pthread_attr_init(&attr);
273 # ifdef PTHREAD_ATTR_SETDETACHSTATE
275 err = PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
278 croak("panic: can't pthread_attr_setdetachstate");
282 err = PTHREAD_CREATE(&thr->self, attr, threadstart, (void*) thr);
286 MUTEX_UNLOCK(&thr->mutex);
287 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
288 "%p: create of %p failed %d\n",
289 savethread, thr, err));
290 /* Thread creation failed--clean up */
291 SvREFCNT_dec(thr->cvcache);
293 MUTEX_DESTROY(&thr->mutex);
294 for (i = 0; i <= AvFILL(initargs); i++)
295 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
296 SvREFCNT_dec(startsv);
300 #ifdef THREAD_POST_CREATE
301 THREAD_POST_CREATE(thr);
303 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
304 croak("panic: sigprocmask");
307 sv = newSViv(thr->tid);
308 sv_magic(sv, thr->oursv, '~', 0, 0);
309 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
310 sv = sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
313 MUTEX_UNLOCK(&thr->mutex);
317 croak("No threads in this perl");
322 static Signal_t handle_thread_signal (int sig);
325 handle_thread_signal(int sig)
327 unsigned char c = (unsigned char) sig;
329 * We're not really allowed to call fprintf in a signal handler
330 * so don't be surprised if this isn't robust while debugging
333 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
334 "handle_thread_signal: got signal %d\n", sig););
335 write(sig_pipe[1], &c, 1);
338 MODULE = Thread PACKAGE = Thread
342 new(classname, startsv, ...)
345 AV * av = av_make(items - 2, &ST(2));
347 XPUSHs(sv_2mortal(newthread(startsv, av, classname)));
356 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: joining %p (state %u)\n",
357 thr, t, ThrSTATE(t)););
358 MUTEX_LOCK(&t->mutex);
359 switch (ThrSTATE(t)) {
360 case THRf_R_JOINABLE:
362 ThrSETSTATE(t, THRf_R_JOINED);
363 MUTEX_UNLOCK(&t->mutex);
366 ThrSETSTATE(t, THRf_DEAD);
367 MUTEX_UNLOCK(&t->mutex);
371 MUTEX_UNLOCK(&t->mutex);
372 croak("can't join with thread");
377 if (SvTRUE(*av_fetch(av, 0, FALSE))) {
378 /* Could easily speed up the following if necessary */
379 for (i = 1; i <= AvFILL(av); i++)
380 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
383 char *mess = SvPV(*av_fetch(av, 1, FALSE), n_a);
384 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
385 "%p: join propagating die message: %s\n",
396 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: detaching %p (state %u)\n",
397 thr, t, ThrSTATE(t)););
398 MUTEX_LOCK(&t->mutex);
399 switch (ThrSTATE(t)) {
400 case THRf_R_JOINABLE:
401 ThrSETSTATE(t, THRf_R_DETACHED);
403 case THRf_R_DETACHED:
405 MUTEX_UNLOCK(&t->mutex);
408 ThrSETSTATE(t, THRf_DEAD);
410 MUTEX_UNLOCK(&t->mutex);
414 MUTEX_UNLOCK(&t->mutex);
415 croak("can't detach thread");
425 PUSHs((t1 == t2) ? &PL_sv_yes : &PL_sv_no);
432 PUSHs(sv_2mortal(newSViv(t->flags)));
442 sv = newSViv(thr->tid);
443 sv_magic(sv, thr->oursv, '~', 0, 0);
444 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
445 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
446 gv_stashpv(classname, TRUE))));
454 MUTEX_LOCK(&t->mutex);
456 MUTEX_UNLOCK(&t->mutex);
487 mg = condpair_magic(sv);
488 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
489 MUTEX_LOCK(MgMUTEXP(mg));
490 if (MgOWNER(mg) != thr) {
491 MUTEX_UNLOCK(MgMUTEXP(mg));
492 croak("cond_wait for lock that we don't own\n");
495 COND_SIGNAL(MgOWNERCONDP(mg));
496 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
498 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
500 MUTEX_UNLOCK(MgMUTEXP(mg));
512 mg = condpair_magic(sv);
513 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
514 MUTEX_LOCK(MgMUTEXP(mg));
515 if (MgOWNER(mg) != thr) {
516 MUTEX_UNLOCK(MgMUTEXP(mg));
517 croak("cond_signal for lock that we don't own\n");
519 COND_SIGNAL(MgCONDP(mg));
520 MUTEX_UNLOCK(MgMUTEXP(mg));
532 mg = condpair_magic(sv);
533 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
535 MUTEX_LOCK(MgMUTEXP(mg));
536 if (MgOWNER(mg) != thr) {
537 MUTEX_UNLOCK(MgMUTEXP(mg));
538 croak("cond_broadcast for lock that we don't own\n");
540 COND_BROADCAST(MgCONDP(mg));
541 MUTEX_UNLOCK(MgMUTEXP(mg));
556 * Iterate until we have enough dynamic storage for all threads.
557 * We mustn't do any allocation while holding threads_mutex though.
559 MUTEX_LOCK(&PL_threads_mutex);
562 MUTEX_UNLOCK(&PL_threads_mutex);
563 if (AvFILL(av) < n - 1) {
565 for (i = AvFILL(av); i < n - 1; i++) {
566 SV *sv = newSViv(0); /* fill in tid later */
567 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
568 av_push(av, sv_bless(newRV_noinc(sv),
569 gv_stashpv(classname, TRUE)));
573 MUTEX_LOCK(&PL_threads_mutex);
574 } while (n < PL_nthreads);
575 n = PL_nthreads; /* Get the final correct value */
578 * At this point, there's enough room to fill in av.
579 * Note that we are holding threads_mutex so the list
580 * won't change out from under us but all the remaining
581 * processing is "fast" (no blocking, malloc etc.)
586 SV *sv = (SV*)SvRV(*svp);
587 sv_setiv(sv, t->tid);
588 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
589 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
590 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
595 MUTEX_UNLOCK(&PL_threads_mutex);
596 /* Truncate any unneeded slots in av */
598 /* Finally, push all the new objects onto the stack and drop av */
600 for (svp = AvARRAY(av); n > 0; n--, svp++)
602 (void)sv_2mortal((SV*)av);
606 MODULE = Thread PACKAGE = Thread::Signal
609 kill_sighandler_thread()
611 write(sig_pipe[1], "\0", 1);
615 init_thread_signals()
617 PL_sighandlerp = handle_thread_signal;
618 if (pipe(sig_pipe) == -1)
629 ret = read(sig_pipe[0], &c, 1);
630 } while (ret == -1 && errno == EINTR);
632 croak("panic: await_signal");
633 ST(0) = sv_newmortal();
635 sv_setsv(ST(0), c ? PL_psig_ptr[c] : &PL_sv_no);
636 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
637 "await_signal returning %s\n", SvPEEK(ST(0))););
639 MODULE = Thread PACKAGE = Thread::Specific
642 data(classname = "Thread::Specific")
646 if (AvFILL(thr->specific) == -1) {
647 GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV);
648 av_store(thr->specific, 0, newRV((SV*)GvHV(gv)));
650 XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));