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",
118 perl_call_sv(sv, G_ARRAY|G_EVAL);
120 retval = SP - (PL_stack_base + oldmark);
121 SP = PL_stack_base + oldmark + 1;
122 if (SvCUR(thr->errsv)) {
123 MUTEX_LOCK(&thr->mutex);
124 thr->flags |= THRf_DID_DIE;
125 MUTEX_UNLOCK(&thr->mutex);
126 av_store(av, 0, &PL_sv_no);
127 av_store(av, 1, newSVsv(thr->errsv));
128 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p died: %s\n",
129 thr, SvPV(thr->errsv, PL_na)));
132 for (i = 1; i <= retval; i++) {
133 PerlIO_printf(PerlIO_stderr(), "%p return[%d] = %s\n",
134 thr, i, SvPEEK(SP[i - 1]));
137 av_store(av, 0, &PL_sv_yes);
138 for (i = 1; i <= retval; i++, SP++)
139 sv_setsv(*av_fetch(av, i, TRUE), SvREFCNT_inc(*SP));
144 /* removed for debug */
145 SvREFCNT_dec(PL_curstack);
147 SvREFCNT_dec(thr->cvcache);
148 SvREFCNT_dec(thr->threadsv);
149 SvREFCNT_dec(thr->specific);
150 SvREFCNT_dec(thr->errsv);
151 SvREFCNT_dec(thr->errhv);
153 /*Safefree(cxstack);*/
154 while (PL_curstackinfo->si_next)
155 PL_curstackinfo = PL_curstackinfo->si_next;
156 while (PL_curstackinfo) {
157 PERL_SI *p = PL_curstackinfo->si_prev;
158 SvREFCNT_dec(PL_curstackinfo->si_stack);
159 Safefree(PL_curstackinfo->si_cxstack);
160 Safefree(PL_curstackinfo);
163 Safefree(PL_markstack);
164 Safefree(PL_scopestack);
165 Safefree(PL_savestack);
166 Safefree(PL_retstack);
167 Safefree(PL_tmps_stack);
171 SvREFCNT_dec(PL_nrs);
172 SvREFCNT_dec(PL_statname);
173 Safefree(PL_screamfirst);
174 Safefree(PL_screamnext);
175 Safefree(PL_reg_start_tmp);
176 SvREFCNT_dec(PL_lastscream);
177 /*SvREFCNT_dec(PL_defoutgv);*/
179 MUTEX_LOCK(&thr->mutex);
180 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
181 "%p: threadstart finishing: state is %u\n",
182 thr, ThrSTATE(thr)));
183 switch (ThrSTATE(thr)) {
184 case THRf_R_JOINABLE:
185 ThrSETSTATE(thr, THRf_ZOMBIE);
186 MUTEX_UNLOCK(&thr->mutex);
187 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
188 "%p: R_JOINABLE thread finished\n", thr));
191 ThrSETSTATE(thr, THRf_DEAD);
192 MUTEX_UNLOCK(&thr->mutex);
194 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
195 "%p: R_JOINED thread finished\n", thr));
197 case THRf_R_DETACHED:
198 ThrSETSTATE(thr, THRf_DEAD);
199 MUTEX_UNLOCK(&thr->mutex);
201 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
202 "%p: DETACHED thread finished\n", thr));
203 remove_thread(thr); /* This might trigger main thread to finish */
206 MUTEX_UNLOCK(&thr->mutex);
207 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
210 return THREAD_RET_CAST(av); /* Available for anyone to join with */
211 /* us unless we're detached, in which */
212 /* case noone sees the value anyway. */
215 return THREAD_RET_CAST(NULL);
220 newthread (SV *startsv, AV *initargs, char *classname)
228 #ifndef THREAD_CREATE
229 static pthread_attr_t attr;
230 static int attr_inited = 0;
231 sigset_t fullmask, oldmask;
235 thr = new_struct_thread(thr);
237 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
238 "%p: newthread (%p), tid is %u, preparing stack\n",
239 savethread, thr, thr->tid));
240 /* The following pushes the arg list and startsv onto the *new* stack */
242 /* Could easily speed up the following greatly */
243 for (i = 0; i <= AvFILL(initargs); i++)
244 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
245 XPUSHs(SvREFCNT_inc(startsv));
248 err = THREAD_CREATE(thr, threadstart);
250 /* On your marks... */
251 MUTEX_LOCK(&thr->mutex);
253 sigfillset(&fullmask);
254 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
255 croak("panic: sigprocmask");
259 #ifdef OLD_PTHREADS_API
260 err = pthread_attr_create(&attr);
262 err = pthread_attr_init(&attr);
264 #ifdef OLD_PTHREADS_API
266 /* This is available with the old pthreads API, but only with */
267 /* DecThreads (VMS and Digital Unix) */
269 err = pthread_attr_setdetach_np(&attr, ATTR_JOINABLE);
273 err = pthread_attr_setdetachstate(&attr, ATTR_JOINABLE);
277 #ifdef OLD_PTHREADS_API
278 err = pthread_create(&thr->self, attr, threadstart, (void*) thr);
280 err = pthread_create(&thr->self, &attr, threadstart, (void*) thr);
283 MUTEX_UNLOCK(&thr->mutex);
286 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
287 "%p: create of %p failed %d\n",
288 savethread, thr, err));
289 /* Thread creation failed--clean up */
290 SvREFCNT_dec(thr->cvcache);
292 MUTEX_DESTROY(&thr->mutex);
293 for (i = 0; i <= AvFILL(initargs); i++)
294 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
295 SvREFCNT_dec(startsv);
298 #ifdef THREAD_POST_CREATE
299 THREAD_POST_CREATE(thr);
301 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
302 croak("panic: sigprocmask");
304 sv = newSViv(thr->tid);
305 sv_magic(sv, thr->oursv, '~', 0, 0);
306 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
307 return sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
309 croak("No threads in this perl");
314 static Signal_t handle_thread_signal _((int sig));
317 handle_thread_signal(int sig)
319 unsigned char c = (unsigned char) sig;
321 * We're not really allowed to call fprintf in a signal handler
322 * so don't be surprised if this isn't robust while debugging
325 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
326 "handle_thread_signal: got signal %d\n", sig););
327 write(sig_pipe[1], &c, 1);
330 MODULE = Thread PACKAGE = Thread
334 new(classname, startsv, ...)
337 AV * av = av_make(items - 2, &ST(2));
339 XPUSHs(sv_2mortal(newthread(startsv, av, classname)));
348 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: joining %p (state %u)\n",
349 thr, t, ThrSTATE(t)););
350 MUTEX_LOCK(&t->mutex);
351 switch (ThrSTATE(t)) {
352 case THRf_R_JOINABLE:
354 ThrSETSTATE(t, THRf_R_JOINED);
355 MUTEX_UNLOCK(&t->mutex);
358 ThrSETSTATE(t, THRf_DEAD);
359 MUTEX_UNLOCK(&t->mutex);
363 MUTEX_UNLOCK(&t->mutex);
364 croak("can't join with thread");
369 if (SvTRUE(*av_fetch(av, 0, FALSE))) {
370 /* Could easily speed up the following if necessary */
371 for (i = 1; i <= AvFILL(av); i++)
372 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
374 char *mess = SvPV(*av_fetch(av, 1, FALSE), PL_na);
375 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
376 "%p: join propagating die message: %s\n",
387 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: detaching %p (state %u)\n",
388 thr, t, ThrSTATE(t)););
389 MUTEX_LOCK(&t->mutex);
390 switch (ThrSTATE(t)) {
391 case THRf_R_JOINABLE:
392 ThrSETSTATE(t, THRf_R_DETACHED);
394 case THRf_R_DETACHED:
396 MUTEX_UNLOCK(&t->mutex);
399 ThrSETSTATE(t, THRf_DEAD);
401 MUTEX_UNLOCK(&t->mutex);
405 MUTEX_UNLOCK(&t->mutex);
406 croak("can't detach thread");
416 PUSHs((t1 == t2) ? &PL_sv_yes : &PL_sv_no);
423 PUSHs(sv_2mortal(newSViv(t->flags)));
433 sv = newSViv(thr->tid);
434 sv_magic(sv, thr->oursv, '~', 0, 0);
435 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
436 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
437 gv_stashpv(classname, TRUE))));
445 MUTEX_LOCK(&t->mutex);
447 MUTEX_UNLOCK(&t->mutex);
478 mg = condpair_magic(sv);
479 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
480 MUTEX_LOCK(MgMUTEXP(mg));
481 if (MgOWNER(mg) != thr) {
482 MUTEX_UNLOCK(MgMUTEXP(mg));
483 croak("cond_wait for lock that we don't own\n");
486 COND_SIGNAL(MgOWNERCONDP(mg));
487 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
489 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
491 MUTEX_UNLOCK(MgMUTEXP(mg));
503 mg = condpair_magic(sv);
504 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
505 MUTEX_LOCK(MgMUTEXP(mg));
506 if (MgOWNER(mg) != thr) {
507 MUTEX_UNLOCK(MgMUTEXP(mg));
508 croak("cond_signal for lock that we don't own\n");
510 COND_SIGNAL(MgCONDP(mg));
511 MUTEX_UNLOCK(MgMUTEXP(mg));
523 mg = condpair_magic(sv);
524 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
526 MUTEX_LOCK(MgMUTEXP(mg));
527 if (MgOWNER(mg) != thr) {
528 MUTEX_UNLOCK(MgMUTEXP(mg));
529 croak("cond_broadcast for lock that we don't own\n");
531 COND_BROADCAST(MgCONDP(mg));
532 MUTEX_UNLOCK(MgMUTEXP(mg));
547 * Iterate until we have enough dynamic storage for all threads.
548 * We mustn't do any allocation while holding threads_mutex though.
550 MUTEX_LOCK(&PL_threads_mutex);
553 MUTEX_UNLOCK(&PL_threads_mutex);
554 if (AvFILL(av) < n - 1) {
556 for (i = AvFILL(av); i < n - 1; i++) {
557 SV *sv = newSViv(0); /* fill in tid later */
558 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
559 av_push(av, sv_bless(newRV_noinc(sv),
560 gv_stashpv(classname, TRUE)));
564 MUTEX_LOCK(&PL_threads_mutex);
565 } while (n < PL_nthreads);
566 n = PL_nthreads; /* Get the final correct value */
569 * At this point, there's enough room to fill in av.
570 * Note that we are holding threads_mutex so the list
571 * won't change out from under us but all the remaining
572 * processing is "fast" (no blocking, malloc etc.)
577 SV *sv = (SV*)SvRV(*svp);
578 sv_setiv(sv, t->tid);
579 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
580 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
581 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
586 MUTEX_UNLOCK(&PL_threads_mutex);
587 /* Truncate any unneeded slots in av */
589 /* Finally, push all the new objects onto the stack and drop av */
591 for (svp = AvARRAY(av); n > 0; n--, svp++)
593 (void)sv_2mortal((SV*)av);
597 MODULE = Thread PACKAGE = Thread::Signal
600 kill_sighandler_thread()
602 write(sig_pipe[1], "\0", 1);
606 init_thread_signals()
608 PL_sighandlerp = handle_thread_signal;
609 if (pipe(sig_pipe) == -1)
620 ret = read(sig_pipe[0], &c, 1);
621 } while (ret == -1 && errno == EINTR);
623 croak("panic: await_signal");
624 ST(0) = sv_newmortal();
626 sv_setsv(ST(0), c ? psig_ptr[c] : &PL_sv_no);
627 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
628 "await_signal returning %s\n", SvPEEK(ST(0))););
630 MODULE = Thread PACKAGE = Thread::Specific
633 data(classname = "Thread::Specific")
637 if (AvFILL(thr->specific) == -1) {
638 GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV);
639 av_store(thr->specific, 0, newRV((SV*)GvHV(gv)));
641 XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));