This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix pp_hot.c:get_db_sub core dump when perl debugger used.
[perl5.git] / ext / Thread / Thread.xs
CommitLineData
d9bb3666
MB
1#include "EXTERN.h"
2#include "perl.h"
3#include "XSUB.h"
4
7d901afa
MB
5/* Magic signature for Thread's mg_private is "Th" */
6#define Thread_MAGIC_SIGNATURE 0x5468
7
8static U32 threadnum = 0;
85ced67f 9static int sig_pipe[2];
683929b4 10
7d901afa
MB
11static void
12remove_thread(t)
13Thread t;
14{
15 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
16 "%p: remove_thread %p\n", thr, t)));
17 MUTEX_LOCK(&threads_mutex);
0a00ffdb 18 MUTEX_DESTROY(&t->mutex);
7d901afa
MB
19 nthreads--;
20 t->prev->next = t->next;
21 t->next->prev = t->prev;
22 COND_BROADCAST(&nthreads_cond);
23 MUTEX_UNLOCK(&threads_mutex);
24}
25
d9bb3666
MB
26static void *
27threadstart(arg)
28void *arg;
29{
783070da
MB
30#ifdef FAKE_THREADS
31 Thread savethread = thr;
32 LOGOP myop;
33 dSP;
34 I32 oldscope = scopestack_ix;
35 I32 retval;
50112d62 36 AV *returnav;
783070da
MB
37 int i;
38
683929b4
MB
39 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
40 thr, SvPEEK(TOPs)));
783070da
MB
41 thr = (Thread) arg;
42 savemark = TOPMARK;
43 thr->prev = thr->prev_run = savethread;
44 thr->next = savethread->next;
45 thr->next_run = savethread->next_run;
46 savethread->next = savethread->next_run = thr;
47 thr->wait_queue = 0;
48 thr->private = 0;
49
50 /* Now duplicate most of perl_call_sv but with a few twists */
51 op = (OP*)&myop;
52 Zero(op, 1, LOGOP);
53 myop.op_flags = OPf_STACKED;
54 myop.op_next = Nullop;
55 myop.op_flags |= OPf_KNOW;
56 myop.op_flags |= OPf_WANT_LIST;
57 op = pp_entersub(ARGS);
58 DEBUG_L(if (!op)
59 PerlIO_printf(PerlIO_stderr(), "thread starts at Nullop\n"));
60 /*
61 * When this thread is next scheduled, we start in the right
62 * place. When the thread runs off the end of the sub, perl.c
63 * handles things, using savemark to figure out how much of the
64 * stack is the return value for any join.
65 */
66 thr = savethread; /* back to the old thread */
67 return 0;
68#else
d9bb3666
MB
69 Thread thr = (Thread) arg;
70 LOGOP myop;
71 dSP;
72 I32 oldmark = TOPMARK;
73 I32 oldscope = scopestack_ix;
74 I32 retval;
50112d62 75 AV *returnav;
14fcddff 76 int i, ret;
783070da 77 dJMPENV;
783070da
MB
78
79 /* Don't call *anything* requiring dTHR until after pthread_setspecific */
d9bb3666
MB
80 /*
81 * Wait until our creator releases us. If we didn't do this, then
82 * it would be potentially possible for out thread to carry on and
83 * do stuff before our creator fills in our "self" field. For example,
84 * if we went and created another thread which tried to pthread_join
85 * with us, then we'd be in a mess.
86 */
50112d62
MB
87 MUTEX_LOCK(&thr->mutex);
88 MUTEX_UNLOCK(&thr->mutex);
d9bb3666 89
d9bb3666
MB
90 /*
91 * It's safe to wait until now to set the thread-specific pointer
92 * from our pthread_t structure to our struct thread, since we're
93 * the only thread who can get at it anyway.
94 */
95 if (pthread_setspecific(thr_key, (void *) thr))
96 croak("panic: pthread_setspecific");
97
783070da 98 /* Only now can we use SvPEEK (which calls sv_newmortal which does dTHR) */
683929b4
MB
99 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
100 thr, SvPEEK(TOPs)));
783070da
MB
101
102 JMPENV_PUSH(ret);
103 switch (ret) {
104 case 3:
105 PerlIO_printf(PerlIO_stderr(), "panic: threadstart\n");
d9bb3666 106 /* fall through */
783070da
MB
107 case 1:
108 STATUS_ALL_FAILURE;
d9bb3666 109 /* fall through */
783070da
MB
110 case 2:
111 /* my_exit() was called */
112 while (scopestack_ix > oldscope)
113 LEAVE;
114 JMPENV_POP;
d9bb3666
MB
115 av_store(returnav, 0, newSViv(statusvalue));
116 goto finishoff;
117 }
118
119 /* Now duplicate most of perl_call_sv but with a few twists */
120 op = (OP*)&myop;
121 Zero(op, 1, LOGOP);
122 myop.op_flags = OPf_STACKED;
123 myop.op_next = Nullop;
124 myop.op_flags |= OPf_KNOW;
783070da 125 myop.op_flags |= OPf_WANT_LIST;
d9bb3666
MB
126 op = pp_entersub(ARGS);
127 if (op)
128 runops();
734689b1
MB
129 SPAGAIN;
130 retval = sp - (stack_base + oldmark);
131 sp = stack_base + oldmark + 1;
783070da
MB
132 DEBUG_L(for (i = 1; i <= retval; i++)
133 PerlIO_printf(PerlIO_stderr(),
134 "%p returnav[%d] = %s\n",
135 thr, i, SvPEEK(sp[i - 1]));)
50112d62 136 returnav = newAV();
d9bb3666 137 av_store(returnav, 0, newSVpv("", 0));
734689b1
MB
138 for (i = 1; i <= retval; i++, sp++)
139 sv_setsv(*av_fetch(returnav, i, TRUE), SvREFCNT_inc(*sp));
140
d9bb3666 141 finishoff:
783070da
MB
142#if 0
143 /* removed for debug */
144 SvREFCNT_dec(curstack);
145#endif
d9bb3666
MB
146 SvREFCNT_dec(cvcache);
147 Safefree(markstack);
148 Safefree(scopestack);
149 Safefree(savestack);
150 Safefree(retstack);
151 Safefree(cxstack);
152 Safefree(tmps_stack);
153
14fcddff 154 MUTEX_LOCK(&thr->mutex);
50112d62
MB
155 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
156 "%p: threadstart finishing: state is %u\n",
157 thr, ThrSTATE(thr)));
14fcddff
MB
158 switch (ThrSTATE(thr)) {
159 case THRf_R_JOINABLE:
160 ThrSETSTATE(thr, THRf_ZOMBIE);
161 MUTEX_UNLOCK(&thr->mutex);
783070da 162 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
14fcddff
MB
163 "%p: R_JOINABLE thread finished\n", thr));
164 break;
165 case THRf_R_JOINED:
166 ThrSETSTATE(thr, THRf_DEAD);
167 MUTEX_UNLOCK(&thr->mutex);
50112d62 168 remove_thread(thr);
14fcddff
MB
169 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
170 "%p: R_JOINED thread finished\n", thr));
171 break;
50112d62 172 case THRf_R_DETACHED:
683929b4 173 ThrSETSTATE(thr, THRf_DEAD);
14fcddff 174 MUTEX_UNLOCK(&thr->mutex);
14fcddff
MB
175 SvREFCNT_dec(returnav);
176 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
177 "%p: DETACHED thread finished\n", thr));
50112d62 178 remove_thread(thr); /* This might trigger main thread to finish */
14fcddff
MB
179 break;
180 default:
181 MUTEX_UNLOCK(&thr->mutex);
182 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
183 /* NOTREACHED */
734689b1 184 }
d9bb3666 185 return (void *) returnav; /* Available for anyone to join with us */
734689b1
MB
186 /* unless we are detached in which case */
187 /* noone will see the value anyway. */
783070da 188#endif
d9bb3666
MB
189}
190
683929b4
MB
191static SV *
192newthread(startsv, initargs, class)
d9bb3666
MB
193SV *startsv;
194AV *initargs;
683929b4 195char *class;
d9bb3666
MB
196{
197 dTHR;
198 dSP;
199 Thread savethread;
200 int i;
683929b4 201 SV *sv;
f152979c 202 sigset_t fullmask, oldmask;
d9bb3666
MB
203
204 savethread = thr;
683929b4
MB
205 sv = newSVpv("", 0);
206 SvGROW(sv, sizeof(struct thread) + 1);
207 SvCUR_set(sv, sizeof(struct thread));
208 thr = (Thread) SvPVX(sv);
50112d62 209 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: newthread(%s) = %p)\n",
7d901afa 210 savethread, SvPEEK(startsv), thr));
683929b4 211 oursv = sv;
783070da
MB
212 /* If we don't zero these foostack pointers, init_stacks won't init them */
213 markstack = 0;
214 scopestack = 0;
215 savestack = 0;
216 retstack = 0;
d9bb3666 217 init_stacks(ARGS);
783070da 218 curcop = savethread->Tcurcop; /* XXX As good a guess as any? */
d9bb3666
MB
219 SPAGAIN;
220 defstash = savethread->Tdefstash; /* XXX maybe these should */
221 curstash = savethread->Tcurstash; /* always be set to main? */
d9bb3666
MB
222 /* top_env? */
223 /* runlevel */
224 cvcache = newHV();
14fcddff
MB
225 thr->flags = THRf_R_JOINABLE;
226 MUTEX_INIT(&thr->mutex);
7d901afa
MB
227 thr->tid = ++threadnum;
228 /* Insert new thread into the circular linked list and bump nthreads */
229 MUTEX_LOCK(&threads_mutex);
230 thr->next = savethread->next;
231 thr->prev = savethread;
232 savethread->next = thr;
233 thr->next->prev = thr;
234 nthreads++;
235 MUTEX_UNLOCK(&threads_mutex);
d9bb3666 236
50112d62
MB
237 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
238 "%p: newthread, tid is %u, preparing stack\n",
239 savethread, thr->tid));
d9bb3666
MB
240 /* The following pushes the arg list and startsv onto the *new* stack */
241 PUSHMARK(sp);
242 /* Could easily speed up the following greatly */
734689b1 243 for (i = 0; i <= AvFILL(initargs); i++)
d9bb3666
MB
244 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
245 XPUSHs(SvREFCNT_inc(startsv));
246 PUTBACK;
247
783070da
MB
248#ifdef FAKE_THREADS
249 threadstart(thr);
250#else
d9bb3666 251 /* On your marks... */
14fcddff 252 MUTEX_LOCK(&thr->mutex);
d9bb3666 253 /* Get set...
50112d62 254 * Increment the global thread count.
d9bb3666 255 */
f152979c
MB
256 sigfillset(&fullmask);
257 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
258 croak("panic: sigprocmask");
d9bb3666
MB
259 if (pthread_create(&self, NULL, threadstart, (void*) thr))
260 return NULL; /* XXX should clean up first */
261 /* Go */
14fcddff 262 MUTEX_UNLOCK(&thr->mutex);
f152979c
MB
263 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
264 croak("panic: sigprocmask");
783070da 265#endif
7d901afa 266 sv = newSViv(thr->tid);
683929b4 267 sv_magic(sv, oursv, '~', 0, 0);
7d901afa
MB
268 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
269 return sv_bless(newRV_noinc(sv), gv_stashpv(class, TRUE));
d9bb3666
MB
270}
271
f152979c
MB
272static Signal_t
273handle_thread_signal(sig)
274int sig;
275{
276 char c = (char) sig;
277 write(sig_pipe[0], &c, 1);
278}
279
d9bb3666
MB
280MODULE = Thread PACKAGE = Thread
281
683929b4 282void
d9bb3666 283new(class, startsv, ...)
683929b4 284 char * class
d9bb3666 285 SV * startsv
734689b1 286 AV * av = av_make(items - 2, &ST(2));
683929b4
MB
287 PPCODE:
288 XPUSHs(sv_2mortal(newthread(startsv, av, class)));
d9bb3666
MB
289
290void
d9bb3666
MB
291join(t)
292 Thread t
293 AV * av = NO_INIT
294 int i = NO_INIT
295 PPCODE:
7d901afa
MB
296 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: joining %p (state %u)\n",
297 thr, t, ThrSTATE(t)););
50112d62
MB
298 MUTEX_LOCK(&t->mutex);
299 switch (ThrSTATE(t)) {
14fcddff
MB
300 case THRf_R_JOINABLE:
301 case THRf_R_JOINED:
50112d62
MB
302 ThrSETSTATE(t, THRf_R_JOINED);
303 MUTEX_UNLOCK(&t->mutex);
14fcddff
MB
304 break;
305 case THRf_ZOMBIE:
50112d62
MB
306 ThrSETSTATE(t, THRf_DEAD);
307 MUTEX_UNLOCK(&t->mutex);
308 remove_thread(t);
14fcddff
MB
309 break;
310 default:
50112d62 311 MUTEX_UNLOCK(&t->mutex);
14fcddff
MB
312 croak("can't join with thread");
313 /* NOTREACHED */
314 }
d9bb3666
MB
315 if (pthread_join(t->Tself, (void **) &av))
316 croak("pthread_join failed");
7d901afa 317
d9bb3666
MB
318 /* Could easily speed up the following if necessary */
319 for (i = 0; i <= AvFILL(av); i++)
320 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
321
322void
734689b1 323detach(t)
d9bb3666
MB
324 Thread t
325 CODE:
7d901afa
MB
326 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: detaching %p (state %u)\n",
327 thr, t, ThrSTATE(t)););
50112d62
MB
328 MUTEX_LOCK(&t->mutex);
329 switch (ThrSTATE(t)) {
14fcddff 330 case THRf_R_JOINABLE:
50112d62 331 ThrSETSTATE(t, THRf_R_DETACHED);
14fcddff 332 /* fall through */
50112d62 333 case THRf_R_DETACHED:
14fcddff 334 DETACH(t);
50112d62 335 MUTEX_UNLOCK(&t->mutex);
14fcddff
MB
336 break;
337 case THRf_ZOMBIE:
50112d62 338 ThrSETSTATE(t, THRf_DEAD);
7d901afa 339 DETACH(t);
50112d62
MB
340 MUTEX_UNLOCK(&t->mutex);
341 remove_thread(t);
14fcddff
MB
342 break;
343 default:
50112d62 344 MUTEX_UNLOCK(&t->mutex);
14fcddff
MB
345 croak("can't detach thread");
346 /* NOTREACHED */
734689b1 347 }
d9bb3666
MB
348
349void
7d901afa
MB
350equal(t1, t2)
351 Thread t1
352 Thread t2
353 PPCODE:
354 PUSHs((t1 == t2) ? &sv_yes : &sv_no);
355
356void
357flags(t)
358 Thread t
359 PPCODE:
360 PUSHs(sv_2mortal(newSViv(t->flags)));
361
362void
363self(class)
364 char * class
365 PREINIT:
366 SV *sv;
367 PPCODE:
368 sv = newSViv(thr->tid);
369 sv_magic(sv, oursv, '~', 0, 0);
370 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
371 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv), gv_stashpv(class, TRUE))));
372
50112d62
MB
373U32
374tid(t)
375 Thread t
376 CODE:
377 MUTEX_LOCK(&t->mutex);
378 RETVAL = t->tid;
379 MUTEX_UNLOCK(&t->mutex);
380 OUTPUT:
381 RETVAL
382
383void
384DESTROY(t)
385 SV * t
386 PPCODE:
387 PUSHs(&sv_yes);
388
7d901afa 389void
734689b1 390yield()
d9bb3666 391 CODE:
734689b1
MB
392#ifdef OLD_PTHREADS_API
393 pthread_yield();
394#else
395#ifndef NO_SCHED_YIELD
396 sched_yield();
397#endif /* NO_SCHED_YIELD */
398#endif /* OLD_PTHREADS_API */
d9bb3666
MB
399
400void
734689b1
MB
401cond_wait(sv)
402 SV * sv
403 MAGIC * mg = NO_INIT
404CODE:
2c127b02 405 if (SvROK(sv))
734689b1 406 sv = SvRV(sv);
2c127b02 407
734689b1 408 mg = condpair_magic(sv);
683929b4 409 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
734689b1
MB
410 MUTEX_LOCK(MgMUTEXP(mg));
411 if (MgOWNER(mg) != thr) {
412 MUTEX_UNLOCK(MgMUTEXP(mg));
413 croak("cond_wait for lock that we don't own\n");
414 }
415 MgOWNER(mg) = 0;
416 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
50112d62
MB
417 while (MgOWNER(mg))
418 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
734689b1
MB
419 MgOWNER(mg) = thr;
420 MUTEX_UNLOCK(MgMUTEXP(mg));
421
422void
423cond_signal(sv)
424 SV * sv
425 MAGIC * mg = NO_INIT
426CODE:
50112d62 427 if (SvROK(sv))
734689b1 428 sv = SvRV(sv);
50112d62 429
734689b1 430 mg = condpair_magic(sv);
683929b4 431 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
734689b1
MB
432 MUTEX_LOCK(MgMUTEXP(mg));
433 if (MgOWNER(mg) != thr) {
434 MUTEX_UNLOCK(MgMUTEXP(mg));
435 croak("cond_signal for lock that we don't own\n");
436 }
437 COND_SIGNAL(MgCONDP(mg));
438 MUTEX_UNLOCK(MgMUTEXP(mg));
d9bb3666 439
734689b1
MB
440void
441cond_broadcast(sv)
442 SV * sv
443 MAGIC * mg = NO_INIT
444CODE:
783070da 445 if (SvROK(sv))
734689b1 446 sv = SvRV(sv);
783070da 447
734689b1 448 mg = condpair_magic(sv);
683929b4
MB
449 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
450 thr, sv));
734689b1
MB
451 MUTEX_LOCK(MgMUTEXP(mg));
452 if (MgOWNER(mg) != thr) {
453 MUTEX_UNLOCK(MgMUTEXP(mg));
454 croak("cond_broadcast for lock that we don't own\n");
455 }
456 COND_BROADCAST(MgCONDP(mg));
457 MUTEX_UNLOCK(MgMUTEXP(mg));
f152979c 458
7d901afa
MB
459void
460list(class)
461 char * class
462 PREINIT:
463 Thread t;
464 AV * av;
465 SV ** svp;
466 int n = 0;
467 PPCODE:
468 av = newAV();
469 /*
470 * Iterate until we have enough dynamic storage for all threads.
471 * We mustn't do any allocation while holding threads_mutex though.
472 */
473 MUTEX_LOCK(&threads_mutex);
474 do {
475 n = nthreads;
476 MUTEX_UNLOCK(&threads_mutex);
477 if (AvFILL(av) < n - 1) {
478 int i = AvFILL(av);
479 for (i = AvFILL(av); i < n - 1; i++) {
480 SV *sv = newSViv(0); /* fill in tid later */
481 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
482 av_push(av, sv_bless(newRV_noinc(sv),
483 gv_stashpv(class, TRUE)));
50112d62 484
7d901afa
MB
485 }
486 }
487 MUTEX_LOCK(&threads_mutex);
488 } while (n < nthreads);
50112d62 489 n = nthreads; /* Get the final correct value */
7d901afa
MB
490
491 /*
492 * At this point, there's enough room to fill in av.
493 * Note that we are holding threads_mutex so the list
494 * won't change out from under us but all the remaining
495 * processing is "fast" (no blocking, malloc etc.)
496 */
497 t = thr;
498 svp = AvARRAY(av);
499 do {
0a00ffdb 500 SV *sv = (SV*)SvRV(*svp);
7d901afa
MB
501 sv_setiv(sv, t->tid);
502 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->Toursv);
503 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
504 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
505 t = t->next;
0a00ffdb 506 svp++;
7d901afa 507 } while (t != thr);
50112d62 508 /* */
7d901afa
MB
509 MUTEX_UNLOCK(&threads_mutex);
510 /* Truncate any unneeded slots in av */
50112d62 511 av_fill(av, n - 1);
7d901afa
MB
512 /* Finally, push all the new objects onto the stack and drop av */
513 EXTEND(sp, n);
514 for (svp = AvARRAY(av); n > 0; n--, svp++)
515 PUSHs(*svp);
516 (void)sv_2mortal((SV*)av);
517
518
f152979c
MB
519MODULE = Thread PACKAGE = Thread::Signal
520
521void
522kill_sighandler_thread()
523 PPCODE:
524 write(sig_pipe[0], "\0", 1);
525 PUSHs(&sv_yes);
526
527void
528init_thread_signals()
529 PPCODE:
530 sighandlerp = handle_thread_signal;
531 if (pipe(sig_pipe) == -1)
532 XSRETURN_UNDEF;
533 PUSHs(&sv_yes);
534
535SV *
536await_signal()
537 PREINIT:
538 char c;
539 ssize_t ret;
540 CODE:
541 do {
542 ret = read(sig_pipe[1], &c, 1);
543 } while (ret == -1 && errno == EINTR);
544 if (ret == -1)
545 croak("panic: await_signal");
546 if (ret == 0)
547 XSRETURN_UNDEF;
548 RETVAL = c ? psig_ptr[c] : &sv_no;
549 OUTPUT:
550 RETVAL