5 /* Magic signature for Thread's mg_private is "Th" */
6 #define Thread_MAGIC_SIGNATURE 0x5468
15 static U32 threadnum = 0;
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(struct perl_thread *t)
27 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
28 "%p: remove_thread %p\n", thr, t)));
29 MUTEX_LOCK(&threads_mutex);
30 MUTEX_DESTROY(&t->mutex);
32 t->prev->next = t->next;
33 t->next->prev = t->prev;
34 COND_BROADCAST(&nthreads_cond);
35 MUTEX_UNLOCK(&threads_mutex);
39 static THREAD_RET_TYPE
40 threadstart(void *arg)
44 Thread savethread = thr;
47 I32 oldscope = scopestack_ix;
52 DEBUG_L(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 */
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 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 = scopestack_ix;
92 DEBUG_L(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_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
119 perl_call_sv(sv, G_ARRAY|G_EVAL);
121 retval = sp - (stack_base + oldmark);
122 sp = stack_base + oldmark + 1;
123 if (SvCUR(thr->errsv)) {
124 MUTEX_LOCK(&thr->mutex);
125 thr->flags |= THRf_DID_DIE;
126 MUTEX_UNLOCK(&thr->mutex);
127 av_store(av, 0, &sv_no);
128 av_store(av, 1, newSVsv(thr->errsv));
129 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p died: %s\n",
130 SvPV(thr->errsv, na)));
133 for (i = 1; i <= retval; i++) {
134 PerlIO_printf(PerlIO_stderr(), "%p return[%d] = %s\n",
135 thr, i, SvPEEK(sp[i - 1]));
138 av_store(av, 0, &sv_yes);
139 for (i = 1; i <= retval; i++, sp++)
140 sv_setsv(*av_fetch(av, i, TRUE), SvREFCNT_inc(*sp));
145 /* removed for debug */
146 SvREFCNT_dec(curstack);
148 SvREFCNT_dec(thr->cvcache);
149 SvREFCNT_dec(thr->threadsv);
150 SvREFCNT_dec(thr->specific);
151 SvREFCNT_dec(thr->errsv);
152 SvREFCNT_dec(thr->errhv);
154 Safefree(scopestack);
158 Safefree(tmps_stack);
161 MUTEX_LOCK(&thr->mutex);
162 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
163 "%p: threadstart finishing: state is %u\n",
164 thr, ThrSTATE(thr)));
165 switch (ThrSTATE(thr)) {
166 case THRf_R_JOINABLE:
167 ThrSETSTATE(thr, THRf_ZOMBIE);
168 MUTEX_UNLOCK(&thr->mutex);
169 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
170 "%p: R_JOINABLE thread finished\n", thr));
173 ThrSETSTATE(thr, THRf_DEAD);
174 MUTEX_UNLOCK(&thr->mutex);
176 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
177 "%p: R_JOINED thread finished\n", thr));
179 case THRf_R_DETACHED:
180 ThrSETSTATE(thr, THRf_DEAD);
181 MUTEX_UNLOCK(&thr->mutex);
183 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
184 "%p: DETACHED thread finished\n", thr));
185 remove_thread(thr); /* This might trigger main thread to finish */
188 MUTEX_UNLOCK(&thr->mutex);
189 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
192 return THREAD_RET_CAST(av); /* Available for anyone to join with */
193 /* us unless we're detached, in which */
194 /* case noone sees the value anyway. */
197 return THREAD_RET_CAST(NULL);
202 newthread (SV *startsv, AV *initargs, char *classname)
210 #ifndef THREAD_CREATE
211 sigset_t fullmask, oldmask;
215 thr = new_struct_thread(thr);
217 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
218 "%p: newthread (%p), tid is %u, preparing stack\n",
219 savethread, thr, thr->tid));
220 /* The following pushes the arg list and startsv onto the *new* stack */
222 /* Could easily speed up the following greatly */
223 for (i = 0; i <= AvFILL(initargs); i++)
224 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
225 XPUSHs(SvREFCNT_inc(startsv));
228 err = THREAD_CREATE(thr, threadstart);
230 /* On your marks... */
231 MUTEX_LOCK(&thr->mutex);
233 sigfillset(&fullmask);
234 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
235 croak("panic: sigprocmask");
236 #ifdef PTHREADS_CREATED_JOINABLE
237 err = pthread_create(&thr->self, pthread_attr_default,
238 threadstart, (void*) thr);
243 err = pthread_attr_init(&attr);
245 #ifdef PTHREAD_CREATE_UNDETACHED
246 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_UNDETACHED);
248 croak("panic: pthread_attr_setdetachstate");
251 err = pthread_create(&thr->self, &attr,
252 threadstart, (void*) thr);
254 pthread_attr_destroy(&attr);
258 MUTEX_UNLOCK(&thr->mutex);
261 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
262 "%p: create of %p failed %d\n", savethread, thr, err));
263 /* Thread creation failed--clean up */
264 SvREFCNT_dec(thr->cvcache);
266 MUTEX_DESTROY(&thr->mutex);
267 for (i = 0; i <= AvFILL(initargs); i++)
268 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
269 SvREFCNT_dec(startsv);
272 #ifdef THREAD_POST_CREATE
273 THREAD_POST_CREATE(thr);
275 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
276 croak("panic: sigprocmask");
278 sv = newSViv(thr->tid);
279 sv_magic(sv, thr->oursv, '~', 0, 0);
280 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
281 return sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
283 croak("No threads in this perl");
288 static Signal_t handle_thread_signal _((int sig));
291 handle_thread_signal(int sig)
294 write(sig_pipe[0], &c, 1);
297 MODULE = Thread PACKAGE = Thread
301 new(classname, startsv, ...)
304 AV * av = av_make(items - 2, &ST(2));
306 XPUSHs(sv_2mortal(newthread(startsv, av, classname)));
315 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: joining %p (state %u)\n",
316 thr, t, ThrSTATE(t)););
317 MUTEX_LOCK(&t->mutex);
318 switch (ThrSTATE(t)) {
319 case THRf_R_JOINABLE:
321 ThrSETSTATE(t, THRf_R_JOINED);
322 MUTEX_UNLOCK(&t->mutex);
325 ThrSETSTATE(t, THRf_DEAD);
326 MUTEX_UNLOCK(&t->mutex);
330 MUTEX_UNLOCK(&t->mutex);
331 croak("can't join with thread");
336 if (SvTRUE(*av_fetch(av, 0, FALSE))) {
337 /* Could easily speed up the following if necessary */
338 for (i = 1; i <= AvFILL(av); i++)
339 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
341 char *mess = SvPV(*av_fetch(av, 1, FALSE), na);
342 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
343 "%p: join propagating die message: %s\n",
354 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: detaching %p (state %u)\n",
355 thr, t, ThrSTATE(t)););
356 MUTEX_LOCK(&t->mutex);
357 switch (ThrSTATE(t)) {
358 case THRf_R_JOINABLE:
359 ThrSETSTATE(t, THRf_R_DETACHED);
361 case THRf_R_DETACHED:
363 MUTEX_UNLOCK(&t->mutex);
366 ThrSETSTATE(t, THRf_DEAD);
368 MUTEX_UNLOCK(&t->mutex);
372 MUTEX_UNLOCK(&t->mutex);
373 croak("can't detach thread");
383 PUSHs((t1 == t2) ? &sv_yes : &sv_no);
390 PUSHs(sv_2mortal(newSViv(t->flags)));
400 sv = newSViv(thr->tid);
401 sv_magic(sv, thr->oursv, '~', 0, 0);
402 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
403 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
404 gv_stashpv(classname, TRUE))));
412 MUTEX_LOCK(&t->mutex);
414 MUTEX_UNLOCK(&t->mutex);
445 mg = condpair_magic(sv);
446 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
447 MUTEX_LOCK(MgMUTEXP(mg));
448 if (MgOWNER(mg) != thr) {
449 MUTEX_UNLOCK(MgMUTEXP(mg));
450 croak("cond_wait for lock that we don't own\n");
453 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
455 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
457 MUTEX_UNLOCK(MgMUTEXP(mg));
469 mg = condpair_magic(sv);
470 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
471 MUTEX_LOCK(MgMUTEXP(mg));
472 if (MgOWNER(mg) != thr) {
473 MUTEX_UNLOCK(MgMUTEXP(mg));
474 croak("cond_signal for lock that we don't own\n");
476 COND_SIGNAL(MgCONDP(mg));
477 MUTEX_UNLOCK(MgMUTEXP(mg));
489 mg = condpair_magic(sv);
490 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
492 MUTEX_LOCK(MgMUTEXP(mg));
493 if (MgOWNER(mg) != thr) {
494 MUTEX_UNLOCK(MgMUTEXP(mg));
495 croak("cond_broadcast for lock that we don't own\n");
497 COND_BROADCAST(MgCONDP(mg));
498 MUTEX_UNLOCK(MgMUTEXP(mg));
513 * Iterate until we have enough dynamic storage for all threads.
514 * We mustn't do any allocation while holding threads_mutex though.
516 MUTEX_LOCK(&threads_mutex);
519 MUTEX_UNLOCK(&threads_mutex);
520 if (AvFILL(av) < n - 1) {
522 for (i = AvFILL(av); i < n - 1; i++) {
523 SV *sv = newSViv(0); /* fill in tid later */
524 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
525 av_push(av, sv_bless(newRV_noinc(sv),
526 gv_stashpv(classname, TRUE)));
530 MUTEX_LOCK(&threads_mutex);
531 } while (n < nthreads);
532 n = nthreads; /* Get the final correct value */
535 * At this point, there's enough room to fill in av.
536 * Note that we are holding threads_mutex so the list
537 * won't change out from under us but all the remaining
538 * processing is "fast" (no blocking, malloc etc.)
543 SV *sv = (SV*)SvRV(*svp);
544 sv_setiv(sv, t->tid);
545 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
546 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
547 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
552 MUTEX_UNLOCK(&threads_mutex);
553 /* Truncate any unneeded slots in av */
555 /* Finally, push all the new objects onto the stack and drop av */
557 for (svp = AvARRAY(av); n > 0; n--, svp++)
559 (void)sv_2mortal((SV*)av);
563 MODULE = Thread PACKAGE = Thread::Signal
566 kill_sighandler_thread()
568 write(sig_pipe[0], "\0", 1);
572 init_thread_signals()
574 sighandlerp = handle_thread_signal;
575 if (pipe(sig_pipe) == -1)
586 ret = read(sig_pipe[1], &c, 1);
587 } while (ret == -1 && errno == EINTR);
589 croak("panic: await_signal");
592 RETVAL = c ? psig_ptr[c] : &sv_no;
596 MODULE = Thread PACKAGE = Thread::Specific
599 data(classname = "Thread::Specific")
603 if (AvFILL(thr->specific) == -1) {
604 GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV);
605 av_store(thr->specific, 0, newRV((SV*)GvHV(gv)));
607 XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));