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;
223 SvGROW(sv, sizeof(struct thread) + 1);
224 SvCUR_set(sv, sizeof(struct thread));
225 thr = (Thread) SvPVX(sv);
226 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: newthread(%s) = %p)\n",
227 savethread, SvPEEK(startsv), thr));
229 /* If we don't zero these foostack pointers, init_stacks won't init them */
235 curcop = savethread->Tcurcop; /* XXX As good a guess as any? */
237 defstash = savethread->Tdefstash; /* XXX maybe these should */
238 curstash = savethread->Tcurstash; /* always be set to main? */
242 thr->flags = THRf_R_JOINABLE;
243 MUTEX_INIT(&thr->mutex);
244 thr->tid = ++threadnum;
245 /* Insert new thread into the circular linked list and bump nthreads */
246 MUTEX_LOCK(&threads_mutex);
247 thr->next = savethread->next;
248 thr->prev = savethread;
249 savethread->next = thr;
250 thr->next->prev = thr;
252 MUTEX_UNLOCK(&threads_mutex);
254 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
255 "%p: newthread, tid is %u, preparing stack\n",
256 savethread, thr->tid));
257 /* The following pushes the arg list and startsv onto the *new* stack */
259 /* Could easily speed up the following greatly */
260 for (i = 0; i <= AvFILL(initargs); i++)
261 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
262 XPUSHs(SvREFCNT_inc(startsv));
266 err = THREAD_CREATE(thr, threadstart);
268 /* On your marks... */
269 MUTEX_LOCK(&thr->mutex);
271 sigfillset(&fullmask);
272 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
273 croak("panic: sigprocmask");
274 err = pthread_create(&thr->self, pthread_attr_default,
275 threadstart, (void*) thr);
277 MUTEX_UNLOCK(&thr->mutex);
280 /* Thread creation failed--clean up */
281 SvREFCNT_dec(cvcache);
283 MUTEX_DESTROY(&thr->mutex);
284 for (i = 0; i <= AvFILL(initargs); i++)
285 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
286 SvREFCNT_dec(startsv);
289 #ifdef THREAD_POST_CREATE
290 THREAD_POST_CREATE(thr);
292 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
293 croak("panic: sigprocmask");
295 sv = newSViv(thr->tid);
296 sv_magic(sv, oursv, '~', 0, 0);
297 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
298 return sv_bless(newRV_noinc(sv), gv_stashpv(Class, TRUE));
300 croak("No threads in this perl");
305 static Signal_t handle_thread_signal _((int sig));
308 handle_thread_signal(int sig)
311 write(sig_pipe[0], &c, 1);
314 MODULE = Thread PACKAGE = Thread
317 new(Class, startsv, ...)
320 AV * av = av_make(items - 2, &ST(2));
322 XPUSHs(sv_2mortal(newthread(startsv, av, Class)));
331 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: joining %p (state %u)\n",
332 thr, t, ThrSTATE(t)););
333 MUTEX_LOCK(&t->mutex);
334 switch (ThrSTATE(t)) {
335 case THRf_R_JOINABLE:
337 ThrSETSTATE(t, THRf_R_JOINED);
338 MUTEX_UNLOCK(&t->mutex);
341 ThrSETSTATE(t, THRf_DEAD);
342 MUTEX_UNLOCK(&t->mutex);
346 MUTEX_UNLOCK(&t->mutex);
347 croak("can't join with thread");
352 /* Could easily speed up the following if necessary */
353 for (i = 0; i <= AvFILL(av); i++)
354 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
362 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: detaching %p (state %u)\n",
363 thr, t, ThrSTATE(t)););
364 MUTEX_LOCK(&t->mutex);
365 switch (ThrSTATE(t)) {
366 case THRf_R_JOINABLE:
367 ThrSETSTATE(t, THRf_R_DETACHED);
369 case THRf_R_DETACHED:
371 MUTEX_UNLOCK(&t->mutex);
374 ThrSETSTATE(t, THRf_DEAD);
376 MUTEX_UNLOCK(&t->mutex);
380 MUTEX_UNLOCK(&t->mutex);
381 croak("can't detach thread");
391 PUSHs((t1 == t2) ? &sv_yes : &sv_no);
398 PUSHs(sv_2mortal(newSViv(t->flags)));
408 sv = newSViv(thr->tid);
409 sv_magic(sv, oursv, '~', 0, 0);
410 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
411 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv), gv_stashpv(Class, TRUE))));
419 MUTEX_LOCK(&t->mutex);
421 MUTEX_UNLOCK(&t->mutex);
452 mg = condpair_magic(sv);
453 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
454 MUTEX_LOCK(MgMUTEXP(mg));
455 if (MgOWNER(mg) != thr) {
456 MUTEX_UNLOCK(MgMUTEXP(mg));
457 croak("cond_wait for lock that we don't own\n");
460 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
462 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
464 MUTEX_UNLOCK(MgMUTEXP(mg));
476 mg = condpair_magic(sv);
477 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
478 MUTEX_LOCK(MgMUTEXP(mg));
479 if (MgOWNER(mg) != thr) {
480 MUTEX_UNLOCK(MgMUTEXP(mg));
481 croak("cond_signal for lock that we don't own\n");
483 COND_SIGNAL(MgCONDP(mg));
484 MUTEX_UNLOCK(MgMUTEXP(mg));
496 mg = condpair_magic(sv);
497 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
499 MUTEX_LOCK(MgMUTEXP(mg));
500 if (MgOWNER(mg) != thr) {
501 MUTEX_UNLOCK(MgMUTEXP(mg));
502 croak("cond_broadcast for lock that we don't own\n");
504 COND_BROADCAST(MgCONDP(mg));
505 MUTEX_UNLOCK(MgMUTEXP(mg));
520 * Iterate until we have enough dynamic storage for all threads.
521 * We mustn't do any allocation while holding threads_mutex though.
523 MUTEX_LOCK(&threads_mutex);
526 MUTEX_UNLOCK(&threads_mutex);
527 if (AvFILL(av) < n - 1) {
529 for (i = AvFILL(av); i < n - 1; i++) {
530 SV *sv = newSViv(0); /* fill in tid later */
531 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
532 av_push(av, sv_bless(newRV_noinc(sv),
533 gv_stashpv(Class, TRUE)));
537 MUTEX_LOCK(&threads_mutex);
538 } while (n < nthreads);
539 n = nthreads; /* Get the final correct value */
542 * At this point, there's enough room to fill in av.
543 * Note that we are holding threads_mutex so the list
544 * won't change out from under us but all the remaining
545 * processing is "fast" (no blocking, malloc etc.)
550 SV *sv = (SV*)SvRV(*svp);
551 sv_setiv(sv, t->tid);
552 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->Toursv);
553 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
554 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
559 MUTEX_UNLOCK(&threads_mutex);
560 /* Truncate any unneeded slots in av */
562 /* Finally, push all the new objects onto the stack and drop av */
564 for (svp = AvARRAY(av); n > 0; n--, svp++)
566 (void)sv_2mortal((SV*)av);
570 MODULE = Thread PACKAGE = Thread::Signal
573 kill_sighandler_thread()
575 write(sig_pipe[0], "\0", 1);
579 init_thread_signals()
581 sighandlerp = handle_thread_signal;
582 if (pipe(sig_pipe) == -1)
593 ret = read(sig_pipe[1], &c, 1);
594 } while (ret == -1 && errno == EINTR);
596 croak("panic: await_signal");
599 RETVAL = c ? psig_ptr[c] : &sv_no;