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 /* Don't call *anything* requiring dTHR until after pthread_setspecific */
95 * Wait until our creator releases us. If we didn't do this, then
96 * it would be potentially possible for out thread to carry on and
97 * do stuff before our creator fills in our "self" field. For example,
98 * if we went and created another thread which tried to JOIN with us,
99 * then we'd be in a mess.
101 MUTEX_LOCK(&thr->mutex);
102 MUTEX_UNLOCK(&thr->mutex);
105 * It's safe to wait until now to set the thread-specific pointer
106 * from our pthread_t structure to our struct thread, since we're
107 * the only thread who can get at it anyway.
111 /* Only now can we use SvPEEK (which calls sv_newmortal which does dTHR) */
112 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
118 PerlIO_printf(PerlIO_stderr(), "panic: threadstart\n");
124 /* my_exit() was called */
125 while (scopestack_ix > oldscope)
128 av_store(returnav, 0, newSViv(statusvalue));
134 /* Now duplicate most of perl_call_sv but with a few twists */
137 myop.op_flags = OPf_STACKED;
138 myop.op_next = Nullop;
139 myop.op_flags |= OPf_KNOW;
140 myop.op_flags |= OPf_WANT_LIST;
141 op = pp_entersub(ARGS);
145 retval = sp - (stack_base + oldmark);
146 sp = stack_base + oldmark + 1;
147 DEBUG_L(for (i = 1; i <= retval; i++)
148 PerlIO_printf(PerlIO_stderr(),
149 "%p returnav[%d] = %s\n",
150 thr, i, SvPEEK(sp[i - 1]));)
152 av_store(returnav, 0, newSVpv("", 0));
153 for (i = 1; i <= retval; i++, sp++)
154 sv_setsv(*av_fetch(returnav, i, TRUE), SvREFCNT_inc(*sp));
158 /* removed for debug */
159 SvREFCNT_dec(curstack);
161 SvREFCNT_dec(thr->cvcache);
162 SvREFCNT_dec(thr->magicals);
163 SvREFCNT_dec(thr->specific);
165 Safefree(scopestack);
169 Safefree(tmps_stack);
172 MUTEX_LOCK(&thr->mutex);
173 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
174 "%p: threadstart finishing: state is %u\n",
175 thr, ThrSTATE(thr)));
176 switch (ThrSTATE(thr)) {
177 case THRf_R_JOINABLE:
178 ThrSETSTATE(thr, THRf_ZOMBIE);
179 MUTEX_UNLOCK(&thr->mutex);
180 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
181 "%p: R_JOINABLE thread finished\n", thr));
184 ThrSETSTATE(thr, THRf_DEAD);
185 MUTEX_UNLOCK(&thr->mutex);
187 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
188 "%p: R_JOINED thread finished\n", thr));
190 case THRf_R_DETACHED:
191 ThrSETSTATE(thr, THRf_DEAD);
192 MUTEX_UNLOCK(&thr->mutex);
193 SvREFCNT_dec(returnav);
194 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
195 "%p: DETACHED thread finished\n", thr));
196 remove_thread(thr); /* This might trigger main thread to finish */
199 MUTEX_UNLOCK(&thr->mutex);
200 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
203 return THREAD_RET_CAST(returnav); /* Available for anyone to join with */
204 /* us unless we're detached, in which */
205 /* case noone sees the value anyway. */
208 return THREAD_RET_CAST(NULL);
213 newthread (SV *startsv, AV *initargs, char *Class)
221 #ifndef THREAD_CREATE
222 sigset_t fullmask, oldmask;
226 thr = new_struct_thread(thr);
228 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
229 "%p: newthread, tid is %u, preparing stack\n",
230 savethread, thr->tid));
231 /* The following pushes the arg list and startsv onto the *new* stack */
233 /* Could easily speed up the following greatly */
234 for (i = 0; i <= AvFILL(initargs); i++)
235 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
236 XPUSHs(SvREFCNT_inc(startsv));
240 err = THREAD_CREATE(thr, threadstart);
242 /* On your marks... */
243 MUTEX_LOCK(&thr->mutex);
245 sigfillset(&fullmask);
246 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
247 croak("panic: sigprocmask");
248 err = pthread_create(&thr->self, pthread_attr_default,
249 threadstart, (void*) thr);
251 MUTEX_UNLOCK(&thr->mutex);
254 /* Thread creation failed--clean up */
255 SvREFCNT_dec(thr->cvcache);
257 MUTEX_DESTROY(&thr->mutex);
258 for (i = 0; i <= AvFILL(initargs); i++)
259 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
260 SvREFCNT_dec(startsv);
263 #ifdef THREAD_POST_CREATE
264 THREAD_POST_CREATE(thr);
266 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
267 croak("panic: sigprocmask");
269 sv = newSViv(thr->tid);
270 sv_magic(sv, thr->oursv, '~', 0, 0);
271 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
272 return sv_bless(newRV_noinc(sv), gv_stashpv(Class, TRUE));
274 croak("No threads in this perl");
279 static Signal_t handle_thread_signal _((int sig));
282 handle_thread_signal(int sig)
285 write(sig_pipe[0], &c, 1);
288 MODULE = Thread PACKAGE = Thread
291 new(Class, startsv, ...)
294 AV * av = av_make(items - 2, &ST(2));
296 XPUSHs(sv_2mortal(newthread(startsv, av, Class)));
305 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: joining %p (state %u)\n",
306 thr, t, ThrSTATE(t)););
307 MUTEX_LOCK(&t->mutex);
308 switch (ThrSTATE(t)) {
309 case THRf_R_JOINABLE:
311 ThrSETSTATE(t, THRf_R_JOINED);
312 MUTEX_UNLOCK(&t->mutex);
315 ThrSETSTATE(t, THRf_DEAD);
316 MUTEX_UNLOCK(&t->mutex);
320 MUTEX_UNLOCK(&t->mutex);
321 croak("can't join with thread");
326 /* Could easily speed up the following if necessary */
327 for (i = 0; i <= AvFILL(av); i++)
328 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
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), gv_stashpv(Class, TRUE))));
393 MUTEX_LOCK(&t->mutex);
395 MUTEX_UNLOCK(&t->mutex);
426 mg = condpair_magic(sv);
427 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
428 MUTEX_LOCK(MgMUTEXP(mg));
429 if (MgOWNER(mg) != thr) {
430 MUTEX_UNLOCK(MgMUTEXP(mg));
431 croak("cond_wait for lock that we don't own\n");
434 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
436 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
438 MUTEX_UNLOCK(MgMUTEXP(mg));
450 mg = condpair_magic(sv);
451 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
452 MUTEX_LOCK(MgMUTEXP(mg));
453 if (MgOWNER(mg) != thr) {
454 MUTEX_UNLOCK(MgMUTEXP(mg));
455 croak("cond_signal for lock that we don't own\n");
457 COND_SIGNAL(MgCONDP(mg));
458 MUTEX_UNLOCK(MgMUTEXP(mg));
470 mg = condpair_magic(sv);
471 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
473 MUTEX_LOCK(MgMUTEXP(mg));
474 if (MgOWNER(mg) != thr) {
475 MUTEX_UNLOCK(MgMUTEXP(mg));
476 croak("cond_broadcast for lock that we don't own\n");
478 COND_BROADCAST(MgCONDP(mg));
479 MUTEX_UNLOCK(MgMUTEXP(mg));
494 * Iterate until we have enough dynamic storage for all threads.
495 * We mustn't do any allocation while holding threads_mutex though.
497 MUTEX_LOCK(&threads_mutex);
500 MUTEX_UNLOCK(&threads_mutex);
501 if (AvFILL(av) < n - 1) {
503 for (i = AvFILL(av); i < n - 1; i++) {
504 SV *sv = newSViv(0); /* fill in tid later */
505 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
506 av_push(av, sv_bless(newRV_noinc(sv),
507 gv_stashpv(Class, TRUE)));
511 MUTEX_LOCK(&threads_mutex);
512 } while (n < nthreads);
513 n = nthreads; /* Get the final correct value */
516 * At this point, there's enough room to fill in av.
517 * Note that we are holding threads_mutex so the list
518 * won't change out from under us but all the remaining
519 * processing is "fast" (no blocking, malloc etc.)
524 SV *sv = (SV*)SvRV(*svp);
525 sv_setiv(sv, t->tid);
526 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
527 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
528 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
533 MUTEX_UNLOCK(&threads_mutex);
534 /* Truncate any unneeded slots in av */
536 /* Finally, push all the new objects onto the stack and drop av */
538 for (svp = AvARRAY(av); n > 0; n--, svp++)
540 (void)sv_2mortal((SV*)av);
544 MODULE = Thread PACKAGE = Thread::Signal
547 kill_sighandler_thread()
549 write(sig_pipe[0], "\0", 1);
553 init_thread_signals()
555 sighandlerp = handle_thread_signal;
556 if (pipe(sig_pipe) == -1)
567 ret = read(sig_pipe[1], &c, 1);
568 } while (ret == -1 && errno == EINTR);
570 croak("panic: await_signal");
573 RETVAL = c ? psig_ptr[c] : &sv_no;