8 #define THR_RET_TYPE DWORD
9 #define THR_FUNC_TYPE THR_RET_TYPE WINAPI
11 #define THR_RET_TYPE void *
12 #define THR_FUNC_TYPE THR_RET_TYPE
15 /* Magic signature for Thread's mg_private is "Th" */
16 #define Thread_MAGIC_SIGNATURE 0x5468
18 static U32 threadnum = 0;
19 static int sig_pipe[2];
21 static void remove_thread _((Thread t));
22 static THR_FUNC_TYPE threadstart _((void *));
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);
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 pthread_join
99 * with us, 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.
110 if (TlsSetValue(thr_key, (void *) thr) == 0)
112 if (pthread_setspecific(thr_key, (void *) thr))
114 croak("panic: pthread_setspecific");
116 /* Only now can we use SvPEEK (which calls sv_newmortal which does dTHR) */
117 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
123 PerlIO_printf(PerlIO_stderr(), "panic: threadstart\n");
129 /* my_exit() was called */
130 while (scopestack_ix > oldscope)
133 av_store(returnav, 0, newSViv(statusvalue));
137 /* Now duplicate most of perl_call_sv but with a few twists */
140 myop.op_flags = OPf_STACKED;
141 myop.op_next = Nullop;
142 myop.op_flags |= OPf_KNOW;
143 myop.op_flags |= OPf_WANT_LIST;
144 op = pp_entersub(ARGS);
148 retval = sp - (stack_base + oldmark);
149 sp = stack_base + oldmark + 1;
150 DEBUG_L(for (i = 1; i <= retval; i++)
151 PerlIO_printf(PerlIO_stderr(),
152 "%p returnav[%d] = %s\n",
153 thr, i, SvPEEK(sp[i - 1]));)
155 av_store(returnav, 0, newSVpv("", 0));
156 for (i = 1; i <= retval; i++, sp++)
157 sv_setsv(*av_fetch(returnav, i, TRUE), SvREFCNT_inc(*sp));
161 /* removed for debug */
162 SvREFCNT_dec(curstack);
164 SvREFCNT_dec(cvcache);
166 Safefree(scopestack);
170 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 (THR_RET_TYPE) returnav;/* Available for anyone to join with us */
204 /* unless we are detached in which case */
205 /* noone will see the value anyway. */
210 newthread(startsv, initargs, class)
221 sigset_t fullmask, oldmask;
228 SvGROW(sv, sizeof(struct thread) + 1);
229 SvCUR_set(sv, sizeof(struct thread));
230 thr = (Thread) SvPVX(sv);
231 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: newthread(%s) = %p)\n",
232 savethread, SvPEEK(startsv), thr));
234 /* If we don't zero these foostack pointers, init_stacks won't init them */
240 curcop = savethread->Tcurcop; /* XXX As good a guess as any? */
242 defstash = savethread->Tdefstash; /* XXX maybe these should */
243 curstash = savethread->Tcurstash; /* always be set to main? */
247 thr->flags = THRf_R_JOINABLE;
248 MUTEX_INIT(&thr->mutex);
249 thr->tid = ++threadnum;
250 /* Insert new thread into the circular linked list and bump nthreads */
251 MUTEX_LOCK(&threads_mutex);
252 thr->next = savethread->next;
253 thr->prev = savethread;
254 savethread->next = thr;
255 thr->next->prev = thr;
257 MUTEX_UNLOCK(&threads_mutex);
259 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
260 "%p: newthread, tid is %u, preparing stack\n",
261 savethread, thr->tid));
262 /* The following pushes the arg list and startsv onto the *new* stack */
264 /* Could easily speed up the following greatly */
265 for (i = 0; i <= AvFILL(initargs); i++)
266 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
267 XPUSHs(SvREFCNT_inc(startsv));
273 /* On your marks... */
274 MUTEX_LOCK(&thr->mutex);
276 * Increment the global thread count.
279 sigfillset(&fullmask);
280 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
281 croak("panic: sigprocmask");
282 if (pthread_create(&self, NULL, threadstart, (void*) thr))
284 if ((self = CreateThread(NULL,0,threadstart,(void*)thr,0,&junk)) == 0)
286 return NULL; /* XXX should clean up first */
288 MUTEX_UNLOCK(&thr->mutex);
290 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
291 croak("panic: sigprocmask");
294 sv = newSViv(thr->tid);
295 sv_magic(sv, oursv, '~', 0, 0);
296 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
297 return sv_bless(newRV_noinc(sv), gv_stashpv(class, TRUE));
301 handle_thread_signal(sig)
305 write(sig_pipe[0], &c, 1);
308 MODULE = Thread PACKAGE = Thread
311 new(class, startsv, ...)
314 AV * av = av_make(items - 2, &ST(2));
316 XPUSHs(sv_2mortal(newthread(startsv, av, class)));
324 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: joining %p (state %u)\n",
325 thr, t, ThrSTATE(t)););
326 MUTEX_LOCK(&t->mutex);
327 switch (ThrSTATE(t)) {
328 case THRf_R_JOINABLE:
330 ThrSETSTATE(t, THRf_R_JOINED);
331 MUTEX_UNLOCK(&t->mutex);
334 ThrSETSTATE(t, THRf_DEAD);
335 MUTEX_UNLOCK(&t->mutex);
339 MUTEX_UNLOCK(&t->mutex);
340 croak("can't join with thread");
344 if ((WaitForSingleObject(t->Tself,INFINITE) == WAIT_FAILED)
345 || (GetExitCodeThread(t->Tself,(LPDWORD)&av) == 0))
347 if (pthread_join(t->Tself, (void **) &av))
349 croak("pthread_join failed");
351 /* Could easily speed up the following if necessary */
352 for (i = 0; i <= AvFILL(av); i++)
353 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
359 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: detaching %p (state %u)\n",
360 thr, t, ThrSTATE(t)););
361 MUTEX_LOCK(&t->mutex);
362 switch (ThrSTATE(t)) {
363 case THRf_R_JOINABLE:
364 ThrSETSTATE(t, THRf_R_DETACHED);
366 case THRf_R_DETACHED:
368 MUTEX_UNLOCK(&t->mutex);
371 ThrSETSTATE(t, THRf_DEAD);
373 MUTEX_UNLOCK(&t->mutex);
377 MUTEX_UNLOCK(&t->mutex);
378 croak("can't detach thread");
387 PUSHs((t1 == t2) ? &sv_yes : &sv_no);
393 PUSHs(sv_2mortal(newSViv(t->flags)));
401 sv = newSViv(thr->tid);
402 sv_magic(sv, oursv, '~', 0, 0);
403 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
404 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv), gv_stashpv(class, TRUE))));
410 MUTEX_LOCK(&t->mutex);
412 MUTEX_UNLOCK(&t->mutex);
425 #ifdef OLD_PTHREADS_API
428 #ifndef NO_SCHED_YIELD
430 Sleep(0); /* same semantics as POSIX sched_yield() */
434 #endif /* NO_SCHED_YIELD */
435 #endif /* OLD_PTHREADS_API */
445 mg = condpair_magic(sv);
446 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
447 MUTEX_LOCK(MgMUTEXP(mg));
448 if (MgOWNER(mg) != thr) {
449 MUTEX_UNLOCK(MgMUTEXP(mg));
450 croak("cond_wait for lock that we don't own\n");
453 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
455 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
457 MUTEX_UNLOCK(MgMUTEXP(mg));
467 mg = condpair_magic(sv);
468 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
469 MUTEX_LOCK(MgMUTEXP(mg));
470 if (MgOWNER(mg) != thr) {
471 MUTEX_UNLOCK(MgMUTEXP(mg));
472 croak("cond_signal for lock that we don't own\n");
474 COND_SIGNAL(MgCONDP(mg));
475 MUTEX_UNLOCK(MgMUTEXP(mg));
485 mg = condpair_magic(sv);
486 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
488 MUTEX_LOCK(MgMUTEXP(mg));
489 if (MgOWNER(mg) != thr) {
490 MUTEX_UNLOCK(MgMUTEXP(mg));
491 croak("cond_broadcast for lock that we don't own\n");
493 COND_BROADCAST(MgCONDP(mg));
494 MUTEX_UNLOCK(MgMUTEXP(mg));
507 * Iterate until we have enough dynamic storage for all threads.
508 * We mustn't do any allocation while holding threads_mutex though.
510 MUTEX_LOCK(&threads_mutex);
513 MUTEX_UNLOCK(&threads_mutex);
514 if (AvFILL(av) < n - 1) {
516 for (i = AvFILL(av); i < n - 1; i++) {
517 SV *sv = newSViv(0); /* fill in tid later */
518 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
519 av_push(av, sv_bless(newRV_noinc(sv),
520 gv_stashpv(class, TRUE)));
524 MUTEX_LOCK(&threads_mutex);
525 } while (n < nthreads);
526 n = nthreads; /* Get the final correct value */
529 * At this point, there's enough room to fill in av.
530 * Note that we are holding threads_mutex so the list
531 * won't change out from under us but all the remaining
532 * processing is "fast" (no blocking, malloc etc.)
537 SV *sv = (SV*)SvRV(*svp);
538 sv_setiv(sv, t->tid);
539 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->Toursv);
540 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
541 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
546 MUTEX_UNLOCK(&threads_mutex);
547 /* Truncate any unneeded slots in av */
549 /* Finally, push all the new objects onto the stack and drop av */
551 for (svp = AvARRAY(av); n > 0; n--, svp++)
553 (void)sv_2mortal((SV*)av);
556 MODULE = Thread PACKAGE = Thread::Signal
559 kill_sighandler_thread()
561 write(sig_pipe[0], "\0", 1);
565 init_thread_signals()
567 sighandlerp = handle_thread_signal;
568 if (pipe(sig_pipe) == -1)
579 ret = read(sig_pipe[1], &c, 1);
580 } while (ret == -1 && errno == EINTR);
582 croak("panic: await_signal");
585 RETVAL = c ? psig_ptr[c] : &sv_no;