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 typedef struct thread *Thread;
20 #define THREAD_RET_TYPE void *
21 #define THREAD_RET_CAST(x) ((THREAD_RET_TYPE) x)
25 remove_thread(struct thread *t)
28 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
29 "%p: remove_thread %p\n", thr, t)));
30 MUTEX_LOCK(&threads_mutex);
31 MUTEX_DESTROY(&t->mutex);
33 t->prev->next = t->next;
34 t->next->prev = t->prev;
35 COND_BROADCAST(&nthreads_cond);
36 MUTEX_UNLOCK(&threads_mutex);
40 static THREAD_RET_TYPE
41 threadstart(void *arg)
45 Thread savethread = thr;
48 I32 oldscope = scopestack_ix;
53 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "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 */
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 op = pp_entersub(ARGS);
73 PerlIO_printf(PerlIO_stderr(), "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 = scopestack_ix;
93 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p waiting to start\n",
96 /* Don't call *anything* requiring dTHR until after SET_THR() */
98 * Wait until our creator releases us. If we didn't do this, then
99 * it would be potentially possible for out thread to carry on and
100 * do stuff before our creator fills in our "self" field. For example,
101 * if we went and created another thread which tried to JOIN with us,
102 * then we'd be in a mess.
104 MUTEX_LOCK(&thr->mutex);
105 MUTEX_UNLOCK(&thr->mutex);
108 * It's safe to wait until now to set the thread-specific pointer
109 * from our pthread_t structure to our struct thread, since we're
110 * the only thread who can get at it anyway.
114 /* Only now can we use SvPEEK (which calls sv_newmortal which does dTHR) */
115 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
120 perl_call_sv(sv, G_ARRAY|G_EVAL);
122 retval = sp - (stack_base + oldmark);
123 sp = 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, &sv_no);
129 av_store(av, 1, newSVsv(thr->errsv));
130 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p died: %s\n",
131 SvPV(thr->errsv, 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, &sv_yes);
140 for (i = 1; i <= retval; i++, sp++)
141 sv_setsv(*av_fetch(av, i, TRUE), SvREFCNT_inc(*sp));
146 /* removed for debug */
147 SvREFCNT_dec(curstack);
149 SvREFCNT_dec(thr->cvcache);
150 SvREFCNT_dec(thr->threadsv);
151 SvREFCNT_dec(thr->specific);
152 SvREFCNT_dec(thr->errsv);
153 SvREFCNT_dec(thr->errhv);
155 Safefree(scopestack);
159 Safefree(tmps_stack);
162 MUTEX_LOCK(&thr->mutex);
163 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
164 "%p: threadstart finishing: state is %u\n",
165 thr, ThrSTATE(thr)));
166 switch (ThrSTATE(thr)) {
167 case THRf_R_JOINABLE:
168 ThrSETSTATE(thr, THRf_ZOMBIE);
169 MUTEX_UNLOCK(&thr->mutex);
170 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
171 "%p: R_JOINABLE thread finished\n", thr));
174 ThrSETSTATE(thr, THRf_DEAD);
175 MUTEX_UNLOCK(&thr->mutex);
177 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
178 "%p: R_JOINED thread finished\n", thr));
180 case THRf_R_DETACHED:
181 ThrSETSTATE(thr, THRf_DEAD);
182 MUTEX_UNLOCK(&thr->mutex);
184 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
185 "%p: DETACHED thread finished\n", thr));
186 remove_thread(thr); /* This might trigger main thread to finish */
189 MUTEX_UNLOCK(&thr->mutex);
190 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
193 return THREAD_RET_CAST(av); /* Available for anyone to join with */
194 /* us unless we're detached, in which */
195 /* case noone sees the value anyway. */
198 return THREAD_RET_CAST(NULL);
203 newthread (SV *startsv, AV *initargs, char *classname)
211 #ifndef THREAD_CREATE
212 sigset_t fullmask, oldmask;
216 thr = new_struct_thread(thr);
218 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
219 "%p: newthread (%p), tid is %u, preparing stack\n",
220 savethread, thr, thr->tid));
221 /* The following pushes the arg list and startsv onto the *new* stack */
223 /* Could easily speed up the following greatly */
224 for (i = 0; i <= AvFILL(initargs); i++)
225 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
226 XPUSHs(SvREFCNT_inc(startsv));
229 err = THREAD_CREATE(thr, threadstart);
231 /* On your marks... */
232 MUTEX_LOCK(&thr->mutex);
234 sigfillset(&fullmask);
235 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
236 croak("panic: sigprocmask");
237 err = pthread_create(&thr->self, pthread_attr_default,
238 threadstart, (void*) thr);
240 MUTEX_UNLOCK(&thr->mutex);
243 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
244 "%p: create of %p failed %d\n", savethread, thr, err));
245 /* Thread creation failed--clean up */
246 SvREFCNT_dec(thr->cvcache);
248 MUTEX_DESTROY(&thr->mutex);
249 for (i = 0; i <= AvFILL(initargs); i++)
250 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
251 SvREFCNT_dec(startsv);
254 #ifdef THREAD_POST_CREATE
255 THREAD_POST_CREATE(thr);
257 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
258 croak("panic: sigprocmask");
260 sv = newSViv(thr->tid);
261 sv_magic(sv, thr->oursv, '~', 0, 0);
262 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
263 return sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
265 croak("No threads in this perl");
270 static Signal_t handle_thread_signal _((int sig));
273 handle_thread_signal(int sig)
276 write(sig_pipe[0], &c, 1);
279 MODULE = Thread PACKAGE = Thread
283 new(classname, startsv, ...)
286 AV * av = av_make(items - 2, &ST(2));
288 XPUSHs(sv_2mortal(newthread(startsv, av, classname)));
297 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: joining %p (state %u)\n",
298 thr, t, ThrSTATE(t)););
299 MUTEX_LOCK(&t->mutex);
300 switch (ThrSTATE(t)) {
301 case THRf_R_JOINABLE:
303 ThrSETSTATE(t, THRf_R_JOINED);
304 MUTEX_UNLOCK(&t->mutex);
307 ThrSETSTATE(t, THRf_DEAD);
308 MUTEX_UNLOCK(&t->mutex);
312 MUTEX_UNLOCK(&t->mutex);
313 croak("can't join with thread");
318 if (SvTRUE(*av_fetch(av, 0, FALSE))) {
319 /* Could easily speed up the following if necessary */
320 for (i = 1; i <= AvFILL(av); i++)
321 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
323 char *mess = SvPV(*av_fetch(av, 1, FALSE), na);
324 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
325 "%p: join propagating die message: %s\n",
336 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: detaching %p (state %u)\n",
337 thr, t, ThrSTATE(t)););
338 MUTEX_LOCK(&t->mutex);
339 switch (ThrSTATE(t)) {
340 case THRf_R_JOINABLE:
341 ThrSETSTATE(t, THRf_R_DETACHED);
343 case THRf_R_DETACHED:
345 MUTEX_UNLOCK(&t->mutex);
348 ThrSETSTATE(t, THRf_DEAD);
350 MUTEX_UNLOCK(&t->mutex);
354 MUTEX_UNLOCK(&t->mutex);
355 croak("can't detach thread");
365 PUSHs((t1 == t2) ? &sv_yes : &sv_no);
372 PUSHs(sv_2mortal(newSViv(t->flags)));
382 sv = newSViv(thr->tid);
383 sv_magic(sv, thr->oursv, '~', 0, 0);
384 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
385 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
386 gv_stashpv(classname, TRUE))));
394 MUTEX_LOCK(&t->mutex);
396 MUTEX_UNLOCK(&t->mutex);
427 mg = condpair_magic(sv);
428 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
429 MUTEX_LOCK(MgMUTEXP(mg));
430 if (MgOWNER(mg) != thr) {
431 MUTEX_UNLOCK(MgMUTEXP(mg));
432 croak("cond_wait for lock that we don't own\n");
435 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
437 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
439 MUTEX_UNLOCK(MgMUTEXP(mg));
451 mg = condpair_magic(sv);
452 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
453 MUTEX_LOCK(MgMUTEXP(mg));
454 if (MgOWNER(mg) != thr) {
455 MUTEX_UNLOCK(MgMUTEXP(mg));
456 croak("cond_signal for lock that we don't own\n");
458 COND_SIGNAL(MgCONDP(mg));
459 MUTEX_UNLOCK(MgMUTEXP(mg));
471 mg = condpair_magic(sv);
472 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
474 MUTEX_LOCK(MgMUTEXP(mg));
475 if (MgOWNER(mg) != thr) {
476 MUTEX_UNLOCK(MgMUTEXP(mg));
477 croak("cond_broadcast for lock that we don't own\n");
479 COND_BROADCAST(MgCONDP(mg));
480 MUTEX_UNLOCK(MgMUTEXP(mg));
495 * Iterate until we have enough dynamic storage for all threads.
496 * We mustn't do any allocation while holding threads_mutex though.
498 MUTEX_LOCK(&threads_mutex);
501 MUTEX_UNLOCK(&threads_mutex);
502 if (AvFILL(av) < n - 1) {
504 for (i = AvFILL(av); i < n - 1; i++) {
505 SV *sv = newSViv(0); /* fill in tid later */
506 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
507 av_push(av, sv_bless(newRV_noinc(sv),
508 gv_stashpv(classname, TRUE)));
512 MUTEX_LOCK(&threads_mutex);
513 } while (n < nthreads);
514 n = nthreads; /* Get the final correct value */
517 * At this point, there's enough room to fill in av.
518 * Note that we are holding threads_mutex so the list
519 * won't change out from under us but all the remaining
520 * processing is "fast" (no blocking, malloc etc.)
525 SV *sv = (SV*)SvRV(*svp);
526 sv_setiv(sv, t->tid);
527 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
528 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
529 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
534 MUTEX_UNLOCK(&threads_mutex);
535 /* Truncate any unneeded slots in av */
537 /* Finally, push all the new objects onto the stack and drop av */
539 for (svp = AvARRAY(av); n > 0; n--, svp++)
541 (void)sv_2mortal((SV*)av);
545 MODULE = Thread PACKAGE = Thread::Signal
548 kill_sighandler_thread()
550 write(sig_pipe[0], "\0", 1);
554 init_thread_signals()
556 sighandlerp = handle_thread_signal;
557 if (pipe(sig_pipe) == -1)
568 ret = read(sig_pipe[1], &c, 1);
569 } while (ret == -1 && errno == EINTR);
571 croak("panic: await_signal");
574 RETVAL = c ? psig_ptr[c] : &sv_no;
578 MODULE = Thread PACKAGE = Thread::Specific
581 data(classname = "Thread::Specific")
585 if (AvFILL(thr->specific) == -1) {
586 GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV);
587 av_store(thr->specific, 0, newRV((SV*)GvHV(gv)));
589 XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));