This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Further ANSI changes now builds and passes (most) tests
[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
f0f333f4
NIS
8#ifdef __cplusplus
9#ifdef I_UNISTD
10#include <unistd.h>
11#endif
12#endif
13#include <fcntl.h>
14
7d901afa 15static U32 threadnum = 0;
85ced67f 16static int sig_pipe[2];
f0f333f4
NIS
17
18#ifndef THREAD_RET_TYPE
19typedef struct thread *Thread;
20#define THREAD_RET_TYPE void *
21#define THREAD_RET_CAST(x) ((THREAD_RET_TYPE) x)
22#endif;
683929b4 23
7d901afa 24static void
f0f333f4 25remove_thread(struct thread *t)
7d901afa 26{
f0f333f4 27#ifdef USE_THREADS
7d901afa
MB
28 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
29 "%p: remove_thread %p\n", thr, t)));
30 MUTEX_LOCK(&threads_mutex);
0a00ffdb 31 MUTEX_DESTROY(&t->mutex);
7d901afa
MB
32 nthreads--;
33 t->prev->next = t->next;
34 t->next->prev = t->prev;
35 COND_BROADCAST(&nthreads_cond);
36 MUTEX_UNLOCK(&threads_mutex);
f0f333f4 37#endif
7d901afa
MB
38}
39
ea0efc06 40static THREAD_RET_TYPE
f0f333f4 41threadstart(void *arg)
d9bb3666 42{
f0f333f4 43#ifdef USE_THREADS
783070da
MB
44#ifdef FAKE_THREADS
45 Thread savethread = thr;
46 LOGOP myop;
47 dSP;
48 I32 oldscope = scopestack_ix;
49 I32 retval;
50112d62 50 AV *returnav;
783070da
MB
51 int i;
52
683929b4
MB
53 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
54 thr, SvPEEK(TOPs)));
783070da
MB
55 thr = (Thread) arg;
56 savemark = TOPMARK;
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;
61 thr->wait_queue = 0;
62 thr->private = 0;
63
64 /* Now duplicate most of perl_call_sv but with a few twists */
65 op = (OP*)&myop;
66 Zero(op, 1, LOGOP);
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);
72 DEBUG_L(if (!op)
73 PerlIO_printf(PerlIO_stderr(), "thread starts at Nullop\n"));
74 /*
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.
79 */
80 thr = savethread; /* back to the old thread */
81 return 0;
82#else
d9bb3666
MB
83 Thread thr = (Thread) arg;
84 LOGOP myop;
85 dSP;
86 I32 oldmark = TOPMARK;
87 I32 oldscope = scopestack_ix;
88 I32 retval;
50112d62 89 AV *returnav;
14fcddff 90 int i, ret;
783070da 91 dJMPENV;
783070da
MB
92
93 /* Don't call *anything* requiring dTHR until after pthread_setspecific */
d9bb3666
MB
94 /*
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,
ea0efc06
MB
98 * if we went and created another thread which tried to JOIN with us,
99 * then we'd be in a mess.
d9bb3666 100 */
50112d62
MB
101 MUTEX_LOCK(&thr->mutex);
102 MUTEX_UNLOCK(&thr->mutex);
d9bb3666 103
d9bb3666
MB
104 /*
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.
108 */
ea0efc06 109 SET_THR(thr);
d9bb3666 110
783070da 111 /* Only now can we use SvPEEK (which calls sv_newmortal which does dTHR) */
683929b4
MB
112 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
113 thr, SvPEEK(TOPs)));
783070da
MB
114
115 JMPENV_PUSH(ret);
116 switch (ret) {
117 case 3:
118 PerlIO_printf(PerlIO_stderr(), "panic: threadstart\n");
d9bb3666 119 /* fall through */
783070da
MB
120 case 1:
121 STATUS_ALL_FAILURE;
d9bb3666 122 /* fall through */
783070da
MB
123 case 2:
124 /* my_exit() was called */
125 while (scopestack_ix > oldscope)
126 LEAVE;
127 JMPENV_POP;
d9bb3666
MB
128 av_store(returnav, 0, newSViv(statusvalue));
129 goto finishoff;
130 }
131
132 /* Now duplicate most of perl_call_sv but with a few twists */
133 op = (OP*)&myop;
134 Zero(op, 1, LOGOP);
135 myop.op_flags = OPf_STACKED;
136 myop.op_next = Nullop;
137 myop.op_flags |= OPf_KNOW;
783070da 138 myop.op_flags |= OPf_WANT_LIST;
d9bb3666
MB
139 op = pp_entersub(ARGS);
140 if (op)
141 runops();
734689b1
MB
142 SPAGAIN;
143 retval = sp - (stack_base + oldmark);
144 sp = stack_base + oldmark + 1;
783070da
MB
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]));)
50112d62 149 returnav = newAV();
d9bb3666 150 av_store(returnav, 0, newSVpv("", 0));
734689b1
MB
151 for (i = 1; i <= retval; i++, sp++)
152 sv_setsv(*av_fetch(returnav, i, TRUE), SvREFCNT_inc(*sp));
153
d9bb3666 154 finishoff:
783070da
MB
155#if 0
156 /* removed for debug */
157 SvREFCNT_dec(curstack);
158#endif
d9bb3666
MB
159 SvREFCNT_dec(cvcache);
160 Safefree(markstack);
161 Safefree(scopestack);
162 Safefree(savestack);
163 Safefree(retstack);
164 Safefree(cxstack);
165 Safefree(tmps_stack);
166
14fcddff 167 MUTEX_LOCK(&thr->mutex);
50112d62
MB
168 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
169 "%p: threadstart finishing: state is %u\n",
170 thr, ThrSTATE(thr)));
14fcddff
MB
171 switch (ThrSTATE(thr)) {
172 case THRf_R_JOINABLE:
173 ThrSETSTATE(thr, THRf_ZOMBIE);
174 MUTEX_UNLOCK(&thr->mutex);
783070da 175 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
14fcddff
MB
176 "%p: R_JOINABLE thread finished\n", thr));
177 break;
178 case THRf_R_JOINED:
179 ThrSETSTATE(thr, THRf_DEAD);
180 MUTEX_UNLOCK(&thr->mutex);
50112d62 181 remove_thread(thr);
14fcddff
MB
182 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
183 "%p: R_JOINED thread finished\n", thr));
184 break;
50112d62 185 case THRf_R_DETACHED:
683929b4 186 ThrSETSTATE(thr, THRf_DEAD);
14fcddff 187 MUTEX_UNLOCK(&thr->mutex);
14fcddff
MB
188 SvREFCNT_dec(returnav);
189 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
190 "%p: DETACHED thread finished\n", thr));
50112d62 191 remove_thread(thr); /* This might trigger main thread to finish */
14fcddff
MB
192 break;
193 default:
194 MUTEX_UNLOCK(&thr->mutex);
195 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
196 /* NOTREACHED */
734689b1 197 }
ea0efc06
MB
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. */
783070da 201#endif
f0f333f4
NIS
202#else
203 return THREAD_RET_CAST(NULL);
204#endif
d9bb3666
MB
205}
206
683929b4 207static SV *
f0f333f4 208newthread (SV *startsv, AV *initargs, char *Class)
d9bb3666 209{
f0f333f4 210#ifdef USE_THREADS
d9bb3666
MB
211 dTHR;
212 dSP;
213 Thread savethread;
214 int i;
683929b4 215 SV *sv;
ea0efc06
MB
216 int err;
217#ifndef THREAD_CREATE
f152979c 218 sigset_t fullmask, oldmask;
ea0efc06 219#endif
d9bb3666
MB
220
221 savethread = thr;
683929b4
MB
222 sv = newSVpv("", 0);
223 SvGROW(sv, sizeof(struct thread) + 1);
224 SvCUR_set(sv, sizeof(struct thread));
225 thr = (Thread) SvPVX(sv);
50112d62 226 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: newthread(%s) = %p)\n",
7d901afa 227 savethread, SvPEEK(startsv), thr));
683929b4 228 oursv = sv;
783070da
MB
229 /* If we don't zero these foostack pointers, init_stacks won't init them */
230 markstack = 0;
231 scopestack = 0;
232 savestack = 0;
233 retstack = 0;
d9bb3666 234 init_stacks(ARGS);
783070da 235 curcop = savethread->Tcurcop; /* XXX As good a guess as any? */
d9bb3666
MB
236 SPAGAIN;
237 defstash = savethread->Tdefstash; /* XXX maybe these should */
238 curstash = savethread->Tcurstash; /* always be set to main? */
d9bb3666
MB
239 /* top_env? */
240 /* runlevel */
241 cvcache = newHV();
14fcddff
MB
242 thr->flags = THRf_R_JOINABLE;
243 MUTEX_INIT(&thr->mutex);
7d901afa
MB
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;
251 nthreads++;
252 MUTEX_UNLOCK(&threads_mutex);
d9bb3666 253
50112d62
MB
254 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
255 "%p: newthread, tid is %u, preparing stack\n",
256 savethread, thr->tid));
d9bb3666
MB
257 /* The following pushes the arg list and startsv onto the *new* stack */
258 PUSHMARK(sp);
259 /* Could easily speed up the following greatly */
734689b1 260 for (i = 0; i <= AvFILL(initargs); i++)
d9bb3666
MB
261 XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
262 XPUSHs(SvREFCNT_inc(startsv));
263 PUTBACK;
264
ea0efc06 265#ifdef THREAD_CREATE
f0f333f4 266 err = THREAD_CREATE(thr, threadstart);
783070da 267#else
d9bb3666 268 /* On your marks... */
14fcddff 269 MUTEX_LOCK(&thr->mutex);
ea0efc06 270 /* Get set... */
f152979c
MB
271 sigfillset(&fullmask);
272 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
273 croak("panic: sigprocmask");
46930d8f
MB
274 err = pthread_create(&thr->self, pthread_attr_default,
275 threadstart, (void*) thr);
d9bb3666 276 /* Go */
14fcddff 277 MUTEX_UNLOCK(&thr->mutex);
ea0efc06
MB
278#endif
279 if (err) {
280 /* Thread creation failed--clean up */
281 SvREFCNT_dec(cvcache);
282 remove_thread(thr);
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);
287 return NULL;
288 }
289#ifdef THREAD_POST_CREATE
290 THREAD_POST_CREATE(thr);
291#else
f152979c
MB
292 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
293 croak("panic: sigprocmask");
783070da 294#endif
7d901afa 295 sv = newSViv(thr->tid);
683929b4 296 sv_magic(sv, oursv, '~', 0, 0);
7d901afa 297 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
f0f333f4
NIS
298 return sv_bless(newRV_noinc(sv), gv_stashpv(Class, TRUE));
299#else
300 croak("No threads in this perl");
301 return &sv_undef;
302#endif
d9bb3666
MB
303}
304
f0f333f4
NIS
305static Signal_t handle_thread_signal _((int sig));
306
f152979c 307static Signal_t
f0f333f4 308handle_thread_signal(int sig)
f152979c
MB
309{
310 char c = (char) sig;
311 write(sig_pipe[0], &c, 1);
312}
313
d9bb3666
MB
314MODULE = Thread PACKAGE = Thread
315
683929b4 316void
f0f333f4
NIS
317new(Class, startsv, ...)
318 char * Class
d9bb3666 319 SV * startsv
734689b1 320 AV * av = av_make(items - 2, &ST(2));
683929b4 321 PPCODE:
f0f333f4 322 XPUSHs(sv_2mortal(newthread(startsv, av, Class)));
d9bb3666
MB
323
324void
d9bb3666
MB
325join(t)
326 Thread t
327 AV * av = NO_INIT
328 int i = NO_INIT
329 PPCODE:
f0f333f4 330#ifdef USE_THREADS
7d901afa
MB
331 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: joining %p (state %u)\n",
332 thr, t, ThrSTATE(t)););
50112d62
MB
333 MUTEX_LOCK(&t->mutex);
334 switch (ThrSTATE(t)) {
14fcddff
MB
335 case THRf_R_JOINABLE:
336 case THRf_R_JOINED:
50112d62
MB
337 ThrSETSTATE(t, THRf_R_JOINED);
338 MUTEX_UNLOCK(&t->mutex);
14fcddff
MB
339 break;
340 case THRf_ZOMBIE:
50112d62
MB
341 ThrSETSTATE(t, THRf_DEAD);
342 MUTEX_UNLOCK(&t->mutex);
343 remove_thread(t);
14fcddff
MB
344 break;
345 default:
50112d62 346 MUTEX_UNLOCK(&t->mutex);
14fcddff
MB
347 croak("can't join with thread");
348 /* NOTREACHED */
349 }
ea0efc06 350 JOIN(t, &av);
7d901afa 351
d9bb3666
MB
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)));
f0f333f4 355#endif
d9bb3666
MB
356
357void
734689b1 358detach(t)
d9bb3666
MB
359 Thread t
360 CODE:
f0f333f4 361#ifdef USE_THREADS
7d901afa
MB
362 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: detaching %p (state %u)\n",
363 thr, t, ThrSTATE(t)););
50112d62
MB
364 MUTEX_LOCK(&t->mutex);
365 switch (ThrSTATE(t)) {
14fcddff 366 case THRf_R_JOINABLE:
50112d62 367 ThrSETSTATE(t, THRf_R_DETACHED);
14fcddff 368 /* fall through */
50112d62 369 case THRf_R_DETACHED:
14fcddff 370 DETACH(t);
50112d62 371 MUTEX_UNLOCK(&t->mutex);
14fcddff
MB
372 break;
373 case THRf_ZOMBIE:
50112d62 374 ThrSETSTATE(t, THRf_DEAD);
7d901afa 375 DETACH(t);
50112d62
MB
376 MUTEX_UNLOCK(&t->mutex);
377 remove_thread(t);
14fcddff
MB
378 break;
379 default:
50112d62 380 MUTEX_UNLOCK(&t->mutex);
14fcddff
MB
381 croak("can't detach thread");
382 /* NOTREACHED */
734689b1 383 }
f0f333f4 384#endif
d9bb3666
MB
385
386void
7d901afa
MB
387equal(t1, t2)
388 Thread t1
389 Thread t2
390 PPCODE:
391 PUSHs((t1 == t2) ? &sv_yes : &sv_no);
392
393void
394flags(t)
395 Thread t
396 PPCODE:
f0f333f4 397#ifdef USE_THREADS
7d901afa 398 PUSHs(sv_2mortal(newSViv(t->flags)));
f0f333f4 399#endif
7d901afa
MB
400
401void
f0f333f4
NIS
402self(Class)
403 char * Class
7d901afa
MB
404 PREINIT:
405 SV *sv;
f0f333f4
NIS
406 PPCODE:
407#ifdef USE_THREADS
7d901afa
MB
408 sv = newSViv(thr->tid);
409 sv_magic(sv, oursv, '~', 0, 0);
410 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
f0f333f4
NIS
411 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv), gv_stashpv(Class, TRUE))));
412#endif
7d901afa 413
50112d62
MB
414U32
415tid(t)
416 Thread t
417 CODE:
f0f333f4 418#ifdef USE_THREADS
50112d62
MB
419 MUTEX_LOCK(&t->mutex);
420 RETVAL = t->tid;
421 MUTEX_UNLOCK(&t->mutex);
f0f333f4
NIS
422#else
423 RETVAL = 0;
424#endif
50112d62
MB
425 OUTPUT:
426 RETVAL
427
428void
429DESTROY(t)
430 SV * t
431 PPCODE:
432 PUSHs(&sv_yes);
433
7d901afa 434void
734689b1 435yield()
d9bb3666 436 CODE:
f0f333f4
NIS
437{
438#ifdef USE_THREADS
ea0efc06 439 YIELD;
f0f333f4
NIS
440#endif
441}
d9bb3666
MB
442
443void
734689b1
MB
444cond_wait(sv)
445 SV * sv
446 MAGIC * mg = NO_INIT
f0f333f4
NIS
447CODE:
448#ifdef USE_THREADS
2c127b02 449 if (SvROK(sv))
734689b1 450 sv = SvRV(sv);
2c127b02 451
734689b1 452 mg = condpair_magic(sv);
683929b4 453 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
734689b1
MB
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");
458 }
459 MgOWNER(mg) = 0;
460 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
50112d62
MB
461 while (MgOWNER(mg))
462 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
734689b1
MB
463 MgOWNER(mg) = thr;
464 MUTEX_UNLOCK(MgMUTEXP(mg));
f0f333f4
NIS
465#endif
466
734689b1
MB
467void
468cond_signal(sv)
469 SV * sv
470 MAGIC * mg = NO_INIT
471CODE:
f0f333f4 472#ifdef USE_THREADS
50112d62 473 if (SvROK(sv))
734689b1 474 sv = SvRV(sv);
50112d62 475
734689b1 476 mg = condpair_magic(sv);
683929b4 477 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
734689b1
MB
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");
482 }
483 COND_SIGNAL(MgCONDP(mg));
484 MUTEX_UNLOCK(MgMUTEXP(mg));
f0f333f4 485#endif
d9bb3666 486
734689b1
MB
487void
488cond_broadcast(sv)
489 SV * sv
490 MAGIC * mg = NO_INIT
f0f333f4
NIS
491CODE:
492#ifdef USE_THREADS
783070da 493 if (SvROK(sv))
734689b1 494 sv = SvRV(sv);
783070da 495
734689b1 496 mg = condpair_magic(sv);
683929b4
MB
497 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
498 thr, sv));
734689b1
MB
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");
503 }
504 COND_BROADCAST(MgCONDP(mg));
505 MUTEX_UNLOCK(MgMUTEXP(mg));
f0f333f4 506#endif
f152979c 507
7d901afa 508void
f0f333f4
NIS
509list(Class)
510 char * Class
7d901afa
MB
511 PREINIT:
512 Thread t;
513 AV * av;
514 SV ** svp;
515 int n = 0;
516 PPCODE:
f0f333f4 517#ifdef USE_THREADS
7d901afa
MB
518 av = newAV();
519 /*
520 * Iterate until we have enough dynamic storage for all threads.
521 * We mustn't do any allocation while holding threads_mutex though.
522 */
523 MUTEX_LOCK(&threads_mutex);
524 do {
525 n = nthreads;
526 MUTEX_UNLOCK(&threads_mutex);
527 if (AvFILL(av) < n - 1) {
528 int i = AvFILL(av);
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),
f0f333f4 533 gv_stashpv(Class, TRUE)));
50112d62 534
7d901afa
MB
535 }
536 }
537 MUTEX_LOCK(&threads_mutex);
538 } while (n < nthreads);
50112d62 539 n = nthreads; /* Get the final correct value */
7d901afa
MB
540
541 /*
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.)
546 */
547 t = thr;
548 svp = AvARRAY(av);
549 do {
0a00ffdb 550 SV *sv = (SV*)SvRV(*svp);
7d901afa
MB
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;
555 t = t->next;
0a00ffdb 556 svp++;
7d901afa 557 } while (t != thr);
50112d62 558 /* */
7d901afa
MB
559 MUTEX_UNLOCK(&threads_mutex);
560 /* Truncate any unneeded slots in av */
50112d62 561 av_fill(av, n - 1);
7d901afa
MB
562 /* Finally, push all the new objects onto the stack and drop av */
563 EXTEND(sp, n);
564 for (svp = AvARRAY(av); n > 0; n--, svp++)
565 PUSHs(*svp);
566 (void)sv_2mortal((SV*)av);
f0f333f4 567#endif
7d901afa
MB
568
569
f152979c
MB
570MODULE = Thread PACKAGE = Thread::Signal
571
572void
573kill_sighandler_thread()
574 PPCODE:
575 write(sig_pipe[0], "\0", 1);
576 PUSHs(&sv_yes);
577
578void
579init_thread_signals()
580 PPCODE:
581 sighandlerp = handle_thread_signal;
582 if (pipe(sig_pipe) == -1)
583 XSRETURN_UNDEF;
584 PUSHs(&sv_yes);
585
586SV *
587await_signal()
588 PREINIT:
589 char c;
ea0efc06 590 SSize_t ret;
f152979c
MB
591 CODE:
592 do {
593 ret = read(sig_pipe[1], &c, 1);
594 } while (ret == -1 && errno == EINTR);
595 if (ret == -1)
596 croak("panic: await_signal");
597 if (ret == 0)
598 XSRETURN_UNDEF;
599 RETVAL = c ? psig_ptr[c] : &sv_no;
600 OUTPUT:
601 RETVAL