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));
132 /* Now duplicate most of perl_call_sv but with a few twists */
135 myop.op_flags = OPf_STACKED;
136 myop.op_next = Nullop;
137 myop.op_flags |= OPf_KNOW;
138 myop.op_flags |= OPf_WANT_LIST;
139 op = pp_entersub(ARGS);
143 retval = sp - (stack_base + oldmark);
144 sp = stack_base + oldmark + 1;
145 DEBUG_L(for (i = 1; i <= retval; i++)
146 PerlIO_printf(PerlIO_stderr(),
147 "%p returnav[%d] = %s\n",
148 thr, i, SvPEEK(sp[i - 1]));)
150 av_store(returnav, 0, newSVpv("", 0));
151 for (i = 1; i <= retval; i++, sp++)
152 sv_setsv(*av_fetch(returnav, i, TRUE), SvREFCNT_inc(*sp));
156 /* removed for debug */
157 SvREFCNT_dec(curstack);
159 SvREFCNT_dec(cvcache);
161 Safefree(scopestack);
165 Safefree(tmps_stack);
167 MUTEX_LOCK(&thr->mutex);
168 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
169 "%p: threadstart finishing: state is %u\n",
170 thr, ThrSTATE(thr)));
171 switch (ThrSTATE(thr)) {
172 case THRf_R_JOINABLE:
173 ThrSETSTATE(thr, THRf_ZOMBIE);
174 MUTEX_UNLOCK(&thr->mutex);
175 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
176 "%p: R_JOINABLE thread finished\n", thr));
179 ThrSETSTATE(thr, THRf_DEAD);
180 MUTEX_UNLOCK(&thr->mutex);
182 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
183 "%p: R_JOINED thread finished\n", thr));
185 case THRf_R_DETACHED:
186 ThrSETSTATE(thr, THRf_DEAD);
187 MUTEX_UNLOCK(&thr->mutex);
188 SvREFCNT_dec(returnav);
189 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
190 "%p: DETACHED thread finished\n", thr));
191 remove_thread(thr); /* This might trigger main thread to finish */
194 MUTEX_UNLOCK(&thr->mutex);
195 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
198 return THREAD_RET_CAST(returnav); /* Available for anyone to join with */
199 /* us unless we're detached, in which */
200 /* case noone sees the value anyway. */
203 return THREAD_RET_CAST(NULL);
208 newthread (SV *startsv, AV *initargs, char *Class)
217 #ifndef THREAD_CREATE
218 sigset_t fullmask, oldmask;
222 thr = new_struct_thread(thr);
225 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
226 "%p: newthread, tid is %u, preparing stack\n",
227 savethread, thr->tid));
228 /* The following pushes the arg list and startsv onto the *new* stack */
230 /* Could easily speed up the following greatly */
231 for (i = 0; i <= AvFILL(initargs); i++)
232 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
233 XPUSHs(SvREFCNT_inc(startsv));
237 err = THREAD_CREATE(thr, threadstart);
239 /* On your marks... */
240 MUTEX_LOCK(&thr->mutex);
242 sigfillset(&fullmask);
243 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
244 croak("panic: sigprocmask");
245 err = pthread_create(&thr->self, pthread_attr_default,
246 threadstart, (void*) thr);
248 MUTEX_UNLOCK(&thr->mutex);
251 /* Thread creation failed--clean up */
252 SvREFCNT_dec(cvcache);
254 MUTEX_DESTROY(&thr->mutex);
255 for (i = 0; i <= AvFILL(initargs); i++)
256 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
257 SvREFCNT_dec(startsv);
260 #ifdef THREAD_POST_CREATE
261 THREAD_POST_CREATE(thr);
263 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
264 croak("panic: sigprocmask");
266 sv = newSViv(thr->tid);
267 sv_magic(sv, oursv, '~', 0, 0);
268 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
269 return sv_bless(newRV_noinc(sv), gv_stashpv(Class, TRUE));
271 croak("No threads in this perl");
276 static Signal_t handle_thread_signal _((int sig));
279 handle_thread_signal(int sig)
282 write(sig_pipe[0], &c, 1);
285 MODULE = Thread PACKAGE = Thread
288 new(Class, startsv, ...)
291 AV * av = av_make(items - 2, &ST(2));
293 XPUSHs(sv_2mortal(newthread(startsv, av, Class)));
302 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: joining %p (state %u)\n",
303 thr, t, ThrSTATE(t)););
304 MUTEX_LOCK(&t->mutex);
305 switch (ThrSTATE(t)) {
306 case THRf_R_JOINABLE:
308 ThrSETSTATE(t, THRf_R_JOINED);
309 MUTEX_UNLOCK(&t->mutex);
312 ThrSETSTATE(t, THRf_DEAD);
313 MUTEX_UNLOCK(&t->mutex);
317 MUTEX_UNLOCK(&t->mutex);
318 croak("can't join with thread");
323 /* Could easily speed up the following if necessary */
324 for (i = 0; i <= AvFILL(av); i++)
325 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
333 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: detaching %p (state %u)\n",
334 thr, t, ThrSTATE(t)););
335 MUTEX_LOCK(&t->mutex);
336 switch (ThrSTATE(t)) {
337 case THRf_R_JOINABLE:
338 ThrSETSTATE(t, THRf_R_DETACHED);
340 case THRf_R_DETACHED:
342 MUTEX_UNLOCK(&t->mutex);
345 ThrSETSTATE(t, THRf_DEAD);
347 MUTEX_UNLOCK(&t->mutex);
351 MUTEX_UNLOCK(&t->mutex);
352 croak("can't detach thread");
362 PUSHs((t1 == t2) ? &sv_yes : &sv_no);
369 PUSHs(sv_2mortal(newSViv(t->flags)));
379 sv = newSViv(thr->tid);
380 sv_magic(sv, oursv, '~', 0, 0);
381 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
382 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv), gv_stashpv(Class, TRUE))));
390 MUTEX_LOCK(&t->mutex);
392 MUTEX_UNLOCK(&t->mutex);
423 mg = condpair_magic(sv);
424 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
425 MUTEX_LOCK(MgMUTEXP(mg));
426 if (MgOWNER(mg) != thr) {
427 MUTEX_UNLOCK(MgMUTEXP(mg));
428 croak("cond_wait for lock that we don't own\n");
431 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
433 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
435 MUTEX_UNLOCK(MgMUTEXP(mg));
447 mg = condpair_magic(sv);
448 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
449 MUTEX_LOCK(MgMUTEXP(mg));
450 if (MgOWNER(mg) != thr) {
451 MUTEX_UNLOCK(MgMUTEXP(mg));
452 croak("cond_signal for lock that we don't own\n");
454 COND_SIGNAL(MgCONDP(mg));
455 MUTEX_UNLOCK(MgMUTEXP(mg));
467 mg = condpair_magic(sv);
468 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
470 MUTEX_LOCK(MgMUTEXP(mg));
471 if (MgOWNER(mg) != thr) {
472 MUTEX_UNLOCK(MgMUTEXP(mg));
473 croak("cond_broadcast for lock that we don't own\n");
475 COND_BROADCAST(MgCONDP(mg));
476 MUTEX_UNLOCK(MgMUTEXP(mg));
491 * Iterate until we have enough dynamic storage for all threads.
492 * We mustn't do any allocation while holding threads_mutex though.
494 MUTEX_LOCK(&threads_mutex);
497 MUTEX_UNLOCK(&threads_mutex);
498 if (AvFILL(av) < n - 1) {
500 for (i = AvFILL(av); i < n - 1; i++) {
501 SV *sv = newSViv(0); /* fill in tid later */
502 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
503 av_push(av, sv_bless(newRV_noinc(sv),
504 gv_stashpv(Class, TRUE)));
508 MUTEX_LOCK(&threads_mutex);
509 } while (n < nthreads);
510 n = nthreads; /* Get the final correct value */
513 * At this point, there's enough room to fill in av.
514 * Note that we are holding threads_mutex so the list
515 * won't change out from under us but all the remaining
516 * processing is "fast" (no blocking, malloc etc.)
521 SV *sv = (SV*)SvRV(*svp);
522 sv_setiv(sv, t->tid);
523 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->Toursv);
524 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
525 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
530 MUTEX_UNLOCK(&threads_mutex);
531 /* Truncate any unneeded slots in av */
533 /* Finally, push all the new objects onto the stack and drop av */
535 for (svp = AvARRAY(av); n > 0; n--, svp++)
537 (void)sv_2mortal((SV*)av);
541 MODULE = Thread PACKAGE = Thread::Signal
544 kill_sighandler_thread()
546 write(sig_pipe[0], "\0", 1);
550 init_thread_signals()
552 sighandlerp = handle_thread_signal;
553 if (pipe(sig_pipe) == -1)
564 ret = read(sig_pipe[1], &c, 1);
565 } while (ret == -1 && errno == EINTR);
567 croak("panic: await_signal");
570 RETVAL = c ? psig_ptr[c] : &sv_no;