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;
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 thread, since we're
109 * 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",
120 PerlIO_printf(PerlIO_stderr(), "panic: threadstart\n");
126 /* my_exit() was called */
127 while (scopestack_ix > oldscope)
130 av_store(returnav, 0, newSViv(statusvalue));
136 /* Now duplicate most of perl_call_sv but with a few twists */
139 myop.op_flags = OPf_STACKED;
140 myop.op_next = Nullop;
141 myop.op_flags |= OPf_KNOW;
142 myop.op_flags |= OPf_WANT_LIST;
143 op = pp_entersub(ARGS);
147 retval = sp - (stack_base + oldmark);
148 sp = stack_base + oldmark + 1;
149 DEBUG_L(for (i = 1; i <= retval; i++)
150 PerlIO_printf(PerlIO_stderr(),
151 "%p returnav[%d] = %s\n",
152 thr, i, SvPEEK(sp[i - 1]));)
154 av_store(returnav, 0, newSVpv("", 0));
155 for (i = 1; i <= retval; i++, sp++)
156 sv_setsv(*av_fetch(returnav, i, TRUE), SvREFCNT_inc(*sp));
160 /* removed for debug */
161 SvREFCNT_dec(curstack);
163 SvREFCNT_dec(thr->cvcache);
164 SvREFCNT_dec(thr->magicals);
165 SvREFCNT_dec(thr->specific);
166 SvREFCNT_dec(thr->errsv);
167 SvREFCNT_dec(thr->errhv);
169 Safefree(scopestack);
173 Safefree(tmps_stack);
176 MUTEX_LOCK(&thr->mutex);
177 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
178 "%p: threadstart finishing: state is %u\n",
179 thr, ThrSTATE(thr)));
180 switch (ThrSTATE(thr)) {
181 case THRf_R_JOINABLE:
182 ThrSETSTATE(thr, THRf_ZOMBIE);
183 MUTEX_UNLOCK(&thr->mutex);
184 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
185 "%p: R_JOINABLE thread finished\n", thr));
188 ThrSETSTATE(thr, THRf_DEAD);
189 MUTEX_UNLOCK(&thr->mutex);
191 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
192 "%p: R_JOINED thread finished\n", thr));
194 case THRf_R_DETACHED:
195 ThrSETSTATE(thr, THRf_DEAD);
196 MUTEX_UNLOCK(&thr->mutex);
197 SvREFCNT_dec(returnav);
198 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
199 "%p: DETACHED thread finished\n", thr));
200 remove_thread(thr); /* This might trigger main thread to finish */
203 MUTEX_UNLOCK(&thr->mutex);
204 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
207 return THREAD_RET_CAST(returnav); /* Available for anyone to join with */
208 /* us unless we're detached, in which */
209 /* case noone sees the value anyway. */
212 return THREAD_RET_CAST(NULL);
217 newthread (SV *startsv, AV *initargs, char *Class)
225 #ifndef THREAD_CREATE
226 sigset_t fullmask, oldmask;
230 thr = new_struct_thread(thr);
232 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
233 "%p: newthread (%p), tid is %u, preparing stack\n",
234 savethread, thr, thr->tid));
235 /* The following pushes the arg list and startsv onto the *new* stack */
237 /* Could easily speed up the following greatly */
238 for (i = 0; i <= AvFILL(initargs); i++)
239 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
240 XPUSHs(SvREFCNT_inc(startsv));
243 err = THREAD_CREATE(thr, threadstart);
245 /* On your marks... */
246 MUTEX_LOCK(&thr->mutex);
248 sigfillset(&fullmask);
249 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
250 croak("panic: sigprocmask");
251 err = pthread_create(&thr->self, pthread_attr_default,
252 threadstart, (void*) thr);
254 MUTEX_UNLOCK(&thr->mutex);
257 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
258 "%p: create of %p failed %d\n", savethread, thr, err));
259 /* Thread creation failed--clean up */
260 SvREFCNT_dec(thr->cvcache);
262 MUTEX_DESTROY(&thr->mutex);
263 for (i = 0; i <= AvFILL(initargs); i++)
264 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
265 SvREFCNT_dec(startsv);
268 #ifdef THREAD_POST_CREATE
269 THREAD_POST_CREATE(thr);
271 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
272 croak("panic: sigprocmask");
274 sv = newSViv(thr->tid);
275 sv_magic(sv, thr->oursv, '~', 0, 0);
276 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
277 return sv_bless(newRV_noinc(sv), gv_stashpv(Class, TRUE));
279 croak("No threads in this perl");
284 static Signal_t handle_thread_signal _((int sig));
287 handle_thread_signal(int sig)
290 write(sig_pipe[0], &c, 1);
293 MODULE = Thread PACKAGE = Thread
297 new(Class, startsv, ...)
300 AV * av = av_make(items - 2, &ST(2));
302 XPUSHs(sv_2mortal(newthread(startsv, av, Class)));
311 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: joining %p (state %u)\n",
312 thr, t, ThrSTATE(t)););
313 MUTEX_LOCK(&t->mutex);
314 switch (ThrSTATE(t)) {
315 case THRf_R_JOINABLE:
317 ThrSETSTATE(t, THRf_R_JOINED);
318 MUTEX_UNLOCK(&t->mutex);
321 ThrSETSTATE(t, THRf_DEAD);
322 MUTEX_UNLOCK(&t->mutex);
326 MUTEX_UNLOCK(&t->mutex);
327 croak("can't join with thread");
332 /* Could easily speed up the following if necessary */
333 for (i = 0; i <= AvFILL(av); i++)
334 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
342 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: detaching %p (state %u)\n",
343 thr, t, ThrSTATE(t)););
344 MUTEX_LOCK(&t->mutex);
345 switch (ThrSTATE(t)) {
346 case THRf_R_JOINABLE:
347 ThrSETSTATE(t, THRf_R_DETACHED);
349 case THRf_R_DETACHED:
351 MUTEX_UNLOCK(&t->mutex);
354 ThrSETSTATE(t, THRf_DEAD);
356 MUTEX_UNLOCK(&t->mutex);
360 MUTEX_UNLOCK(&t->mutex);
361 croak("can't detach thread");
371 PUSHs((t1 == t2) ? &sv_yes : &sv_no);
378 PUSHs(sv_2mortal(newSViv(t->flags)));
388 sv = newSViv(thr->tid);
389 sv_magic(sv, thr->oursv, '~', 0, 0);
390 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
391 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv), gv_stashpv(Class, TRUE))));
399 MUTEX_LOCK(&t->mutex);
401 MUTEX_UNLOCK(&t->mutex);
432 mg = condpair_magic(sv);
433 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
434 MUTEX_LOCK(MgMUTEXP(mg));
435 if (MgOWNER(mg) != thr) {
436 MUTEX_UNLOCK(MgMUTEXP(mg));
437 croak("cond_wait for lock that we don't own\n");
440 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
442 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
444 MUTEX_UNLOCK(MgMUTEXP(mg));
456 mg = condpair_magic(sv);
457 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
458 MUTEX_LOCK(MgMUTEXP(mg));
459 if (MgOWNER(mg) != thr) {
460 MUTEX_UNLOCK(MgMUTEXP(mg));
461 croak("cond_signal for lock that we don't own\n");
463 COND_SIGNAL(MgCONDP(mg));
464 MUTEX_UNLOCK(MgMUTEXP(mg));
476 mg = condpair_magic(sv);
477 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
479 MUTEX_LOCK(MgMUTEXP(mg));
480 if (MgOWNER(mg) != thr) {
481 MUTEX_UNLOCK(MgMUTEXP(mg));
482 croak("cond_broadcast for lock that we don't own\n");
484 COND_BROADCAST(MgCONDP(mg));
485 MUTEX_UNLOCK(MgMUTEXP(mg));
500 * Iterate until we have enough dynamic storage for all threads.
501 * We mustn't do any allocation while holding threads_mutex though.
503 MUTEX_LOCK(&threads_mutex);
506 MUTEX_UNLOCK(&threads_mutex);
507 if (AvFILL(av) < n - 1) {
509 for (i = AvFILL(av); i < n - 1; i++) {
510 SV *sv = newSViv(0); /* fill in tid later */
511 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
512 av_push(av, sv_bless(newRV_noinc(sv),
513 gv_stashpv(Class, TRUE)));
517 MUTEX_LOCK(&threads_mutex);
518 } while (n < nthreads);
519 n = nthreads; /* Get the final correct value */
522 * At this point, there's enough room to fill in av.
523 * Note that we are holding threads_mutex so the list
524 * won't change out from under us but all the remaining
525 * processing is "fast" (no blocking, malloc etc.)
530 SV *sv = (SV*)SvRV(*svp);
531 sv_setiv(sv, t->tid);
532 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
533 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
534 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
539 MUTEX_UNLOCK(&threads_mutex);
540 /* Truncate any unneeded slots in av */
542 /* Finally, push all the new objects onto the stack and drop av */
544 for (svp = AvARRAY(av); n > 0; n--, svp++)
546 (void)sv_2mortal((SV*)av);
550 MODULE = Thread PACKAGE = Thread::Signal
553 kill_sighandler_thread()
555 write(sig_pipe[0], "\0", 1);
559 init_thread_signals()
561 sighandlerp = handle_thread_signal;
562 if (pipe(sig_pipe) == -1)
573 ret = read(sig_pipe[1], &c, 1);
574 } while (ret == -1 && errno == EINTR);
576 croak("panic: await_signal");
579 RETVAL = c ? psig_ptr[c] : &sv_no;