This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix yet another USE_THREADS leak due to failure to free stacks
[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
85ced67f 15static int sig_pipe[2];
f0f333f4
NIS
16
17#ifndef THREAD_RET_TYPE
f0f333f4
NIS
18#define THREAD_RET_TYPE void *
19#define THREAD_RET_CAST(x) ((THREAD_RET_TYPE) x)
458fb581 20#endif
683929b4 21
7d901afa 22static void
52e1cb5e 23remove_thread(struct perl_thread *t)
7d901afa 24{
f0f333f4 25#ifdef USE_THREADS
7d901afa
MB
26 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
27 "%p: remove_thread %p\n", thr, t)));
28 MUTEX_LOCK(&threads_mutex);
0a00ffdb 29 MUTEX_DESTROY(&t->mutex);
7d901afa
MB
30 nthreads--;
31 t->prev->next = t->next;
32 t->next->prev = t->prev;
33 COND_BROADCAST(&nthreads_cond);
34 MUTEX_UNLOCK(&threads_mutex);
f0f333f4 35#endif
7d901afa
MB
36}
37
ea0efc06 38static THREAD_RET_TYPE
f0f333f4 39threadstart(void *arg)
d9bb3666 40{
f0f333f4 41#ifdef USE_THREADS
783070da
MB
42#ifdef FAKE_THREADS
43 Thread savethread = thr;
44 LOGOP myop;
45 dSP;
46 I32 oldscope = scopestack_ix;
47 I32 retval;
458fb581 48 AV *av;
783070da
MB
49 int i;
50
683929b4
MB
51 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
52 thr, SvPEEK(TOPs)));
783070da
MB
53 thr = (Thread) arg;
54 savemark = TOPMARK;
55 thr->prev = thr->prev_run = savethread;
56 thr->next = savethread->next;
57 thr->next_run = savethread->next_run;
58 savethread->next = savethread->next_run = thr;
59 thr->wait_queue = 0;
60 thr->private = 0;
61
62 /* Now duplicate most of perl_call_sv but with a few twists */
63 op = (OP*)&myop;
64 Zero(op, 1, LOGOP);
65 myop.op_flags = OPf_STACKED;
66 myop.op_next = Nullop;
67 myop.op_flags |= OPf_KNOW;
68 myop.op_flags |= OPf_WANT_LIST;
69 op = pp_entersub(ARGS);
70 DEBUG_L(if (!op)
71 PerlIO_printf(PerlIO_stderr(), "thread starts at Nullop\n"));
72 /*
73 * When this thread is next scheduled, we start in the right
74 * place. When the thread runs off the end of the sub, perl.c
75 * handles things, using savemark to figure out how much of the
76 * stack is the return value for any join.
77 */
78 thr = savethread; /* back to the old thread */
79 return 0;
80#else
d9bb3666
MB
81 Thread thr = (Thread) arg;
82 LOGOP myop;
4e35701f 83 djSP;
d9bb3666
MB
84 I32 oldmark = TOPMARK;
85 I32 oldscope = scopestack_ix;
86 I32 retval;
458fb581
MB
87 SV *sv;
88 AV *av = newAV();
14fcddff 89 int i, ret;
783070da 90 dJMPENV;
0b9678a8
NIS
91 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p waiting to start\n",
92 thr));
783070da 93
0b9678a8 94 /* Don't call *anything* requiring dTHR until after SET_THR() */
d9bb3666
MB
95 /*
96 * Wait until our creator releases us. If we didn't do this, then
97 * it would be potentially possible for out thread to carry on and
98 * do stuff before our creator fills in our "self" field. For example,
ea0efc06
MB
99 * if we went and created another thread which tried to JOIN with us,
100 * then we'd be in a mess.
d9bb3666 101 */
50112d62
MB
102 MUTEX_LOCK(&thr->mutex);
103 MUTEX_UNLOCK(&thr->mutex);
d9bb3666 104
d9bb3666
MB
105 /*
106 * It's safe to wait until now to set the thread-specific pointer
52e1cb5e
JH
107 * from our pthread_t structure to our struct perl_thread, since
108 * we're the only thread who can get at it anyway.
d9bb3666 109 */
ea0efc06 110 SET_THR(thr);
d9bb3666 111
783070da 112 /* Only now can we use SvPEEK (which calls sv_newmortal which does dTHR) */
683929b4
MB
113 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
114 thr, SvPEEK(TOPs)));
783070da 115
458fb581
MB
116 sv = POPs;
117 PUTBACK;
118 perl_call_sv(sv, G_ARRAY|G_EVAL);
734689b1 119 SPAGAIN;
924508f0
GS
120 retval = SP - (stack_base + oldmark);
121 SP = stack_base + oldmark + 1;
458fb581
MB
122 if (SvCUR(thr->errsv)) {
123 MUTEX_LOCK(&thr->mutex);
124 thr->flags |= THRf_DID_DIE;
125 MUTEX_UNLOCK(&thr->mutex);
126 av_store(av, 0, &sv_no);
127 av_store(av, 1, newSVsv(thr->errsv));
128 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p died: %s\n",
640cc986 129 thr, SvPV(thr->errsv, na)));
458fb581
MB
130 } else {
131 DEBUG_L(STMT_START {
132 for (i = 1; i <= retval; i++) {
133 PerlIO_printf(PerlIO_stderr(), "%p return[%d] = %s\n",
924508f0 134 thr, i, SvPEEK(SP[i - 1]));
458fb581
MB
135 }
136 } STMT_END);
137 av_store(av, 0, &sv_yes);
924508f0
GS
138 for (i = 1; i <= retval; i++, SP++)
139 sv_setsv(*av_fetch(av, i, TRUE), SvREFCNT_inc(*SP));
458fb581
MB
140 }
141
d9bb3666 142 finishoff:
783070da
MB
143#if 0
144 /* removed for debug */
145 SvREFCNT_dec(curstack);
146#endif
199100c8 147 SvREFCNT_dec(thr->cvcache);
54b9620d 148 SvREFCNT_dec(thr->threadsv);
554b3eca 149 SvREFCNT_dec(thr->specific);
38a03e6e
MB
150 SvREFCNT_dec(thr->errsv);
151 SvREFCNT_dec(thr->errhv);
5c0ca799 152
f7ac0805
GS
153 /*Safefree(cxstack);*/
154 while (curstackinfo->si_next)
155 curstackinfo = curstackinfo->si_next;
156 while (curstackinfo) {
157 PERL_SI *p = curstackinfo->si_prev;
158 SvREFCNT_dec(curstackinfo->si_stack);
159 Safefree(curstackinfo->si_cxstack);
160 Safefree(curstackinfo);
161 curstackinfo = p;
162 }
d9bb3666
MB
163 Safefree(markstack);
164 Safefree(scopestack);
165 Safefree(savestack);
166 Safefree(retstack);
d9bb3666 167 Safefree(tmps_stack);
199100c8 168 Safefree(ofs);
d9bb3666 169
dc92893f
GS
170 SvREFCNT_dec(rs);
171 SvREFCNT_dec(nrs);
5c0ca799
GS
172 SvREFCNT_dec(statname);
173 Safefree(screamfirst);
174 Safefree(screamnext);
175 Safefree(reg_start_tmp);
176 SvREFCNT_dec(lastscream);
f7ac0805 177 /*SvREFCNT_dec(defoutgv);*/
5c0ca799 178
14fcddff 179 MUTEX_LOCK(&thr->mutex);
50112d62
MB
180 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
181 "%p: threadstart finishing: state is %u\n",
182 thr, ThrSTATE(thr)));
14fcddff
MB
183 switch (ThrSTATE(thr)) {
184 case THRf_R_JOINABLE:
185 ThrSETSTATE(thr, THRf_ZOMBIE);
186 MUTEX_UNLOCK(&thr->mutex);
783070da 187 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
14fcddff
MB
188 "%p: R_JOINABLE thread finished\n", thr));
189 break;
190 case THRf_R_JOINED:
191 ThrSETSTATE(thr, THRf_DEAD);
192 MUTEX_UNLOCK(&thr->mutex);
50112d62 193 remove_thread(thr);
14fcddff
MB
194 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
195 "%p: R_JOINED thread finished\n", thr));
196 break;
50112d62 197 case THRf_R_DETACHED:
683929b4 198 ThrSETSTATE(thr, THRf_DEAD);
14fcddff 199 MUTEX_UNLOCK(&thr->mutex);
458fb581 200 SvREFCNT_dec(av);
14fcddff
MB
201 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
202 "%p: DETACHED thread finished\n", thr));
50112d62 203 remove_thread(thr); /* This might trigger main thread to finish */
14fcddff
MB
204 break;
205 default:
206 MUTEX_UNLOCK(&thr->mutex);
207 croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
208 /* NOTREACHED */
734689b1 209 }
458fb581 210 return THREAD_RET_CAST(av); /* Available for anyone to join with */
ea0efc06
MB
211 /* us unless we're detached, in which */
212 /* case noone sees the value anyway. */
783070da 213#endif
f0f333f4
NIS
214#else
215 return THREAD_RET_CAST(NULL);
216#endif
d9bb3666
MB
217}
218
683929b4 219static SV *
458fb581 220newthread (SV *startsv, AV *initargs, char *classname)
d9bb3666 221{
f0f333f4 222#ifdef USE_THREADS
d9bb3666
MB
223 dSP;
224 Thread savethread;
225 int i;
683929b4 226 SV *sv;
ea0efc06
MB
227 int err;
228#ifndef THREAD_CREATE
940cb80d
MB
229 static pthread_attr_t attr;
230 static int attr_inited = 0;
f152979c 231 sigset_t fullmask, oldmask;
ea0efc06 232#endif
d9bb3666
MB
233
234 savethread = thr;
a863c7d1 235 thr = new_struct_thread(thr);
d9bb3666 236 SPAGAIN;
50112d62 237 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
0b9678a8
NIS
238 "%p: newthread (%p), tid is %u, preparing stack\n",
239 savethread, thr, thr->tid));
d9bb3666 240 /* The following pushes the arg list and startsv onto the *new* stack */
924508f0 241 PUSHMARK(SP);
d9bb3666 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;
ea0efc06 247#ifdef THREAD_CREATE
f0f333f4 248 err = THREAD_CREATE(thr, threadstart);
783070da 249#else
d9bb3666 250 /* On your marks... */
14fcddff 251 MUTEX_LOCK(&thr->mutex);
ea0efc06 252 /* Get set... */
f152979c
MB
253 sigfillset(&fullmask);
254 if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
255 croak("panic: sigprocmask");
940cb80d
MB
256 err = 0;
257 if (!attr_inited) {
258 attr_inited = 1;
52e1cb5e 259 err = pthread_attr_init(&attr);
940cb80d
MB
260 if (err == 0)
261 err = pthread_attr_setdetachstate(&attr, ATTR_JOINABLE);
52e1cb5e 262 }
940cb80d
MB
263 if (err == 0)
264 err = pthread_create(&thr->self, &attr, threadstart, (void*) thr);
d9bb3666 265 /* Go */
14fcddff 266 MUTEX_UNLOCK(&thr->mutex);
ea0efc06
MB
267#endif
268 if (err) {
0b9678a8 269 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
940cb80d
MB
270 "%p: create of %p failed %d\n",
271 savethread, thr, err));
ea0efc06 272 /* Thread creation failed--clean up */
199100c8 273 SvREFCNT_dec(thr->cvcache);
ea0efc06
MB
274 remove_thread(thr);
275 MUTEX_DESTROY(&thr->mutex);
276 for (i = 0; i <= AvFILL(initargs); i++)
277 SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
278 SvREFCNT_dec(startsv);
279 return NULL;
280 }
281#ifdef THREAD_POST_CREATE
282 THREAD_POST_CREATE(thr);
283#else
f152979c
MB
284 if (sigprocmask(SIG_SETMASK, &oldmask, 0))
285 croak("panic: sigprocmask");
783070da 286#endif
7d901afa 287 sv = newSViv(thr->tid);
199100c8 288 sv_magic(sv, thr->oursv, '~', 0, 0);
7d901afa 289 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
458fb581 290 return sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
f0f333f4
NIS
291#else
292 croak("No threads in this perl");
293 return &sv_undef;
294#endif
d9bb3666
MB
295}
296
f0f333f4
NIS
297static Signal_t handle_thread_signal _((int sig));
298
f152979c 299static Signal_t
f0f333f4 300handle_thread_signal(int sig)
f152979c 301{
3aeed370
MB
302 unsigned char c = (unsigned char) sig;
303 /*
304 * We're not really allowed to call fprintf in a signal handler
305 * so don't be surprised if this isn't robust while debugging
306 * with -DL.
307 */
308 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
309 "handle_thread_signal: got signal %d\n", sig););
310 write(sig_pipe[1], &c, 1);
f152979c
MB
311}
312
d9bb3666 313MODULE = Thread PACKAGE = Thread
0b9678a8 314PROTOTYPES: DISABLE
d9bb3666 315
683929b4 316void
458fb581
MB
317new(classname, startsv, ...)
318 char * classname
d9bb3666 319 SV * startsv
734689b1 320 AV * av = av_make(items - 2, &ST(2));
683929b4 321 PPCODE:
458fb581 322 XPUSHs(sv_2mortal(newthread(startsv, av, classname)));
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
458fb581
MB
352 if (SvTRUE(*av_fetch(av, 0, FALSE))) {
353 /* Could easily speed up the following if necessary */
354 for (i = 1; i <= AvFILL(av); i++)
355 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
356 } else {
357 char *mess = SvPV(*av_fetch(av, 1, FALSE), na);
358 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
359 "%p: join propagating die message: %s\n",
360 thr, mess));
361 croak(mess);
362 }
f0f333f4 363#endif
d9bb3666
MB
364
365void
734689b1 366detach(t)
d9bb3666
MB
367 Thread t
368 CODE:
f0f333f4 369#ifdef USE_THREADS
7d901afa
MB
370 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: detaching %p (state %u)\n",
371 thr, t, ThrSTATE(t)););
50112d62
MB
372 MUTEX_LOCK(&t->mutex);
373 switch (ThrSTATE(t)) {
14fcddff 374 case THRf_R_JOINABLE:
50112d62 375 ThrSETSTATE(t, THRf_R_DETACHED);
14fcddff 376 /* fall through */
50112d62 377 case THRf_R_DETACHED:
14fcddff 378 DETACH(t);
50112d62 379 MUTEX_UNLOCK(&t->mutex);
14fcddff
MB
380 break;
381 case THRf_ZOMBIE:
50112d62 382 ThrSETSTATE(t, THRf_DEAD);
7d901afa 383 DETACH(t);
50112d62
MB
384 MUTEX_UNLOCK(&t->mutex);
385 remove_thread(t);
14fcddff
MB
386 break;
387 default:
50112d62 388 MUTEX_UNLOCK(&t->mutex);
14fcddff
MB
389 croak("can't detach thread");
390 /* NOTREACHED */
734689b1 391 }
f0f333f4 392#endif
d9bb3666
MB
393
394void
7d901afa
MB
395equal(t1, t2)
396 Thread t1
397 Thread t2
398 PPCODE:
399 PUSHs((t1 == t2) ? &sv_yes : &sv_no);
400
401void
402flags(t)
403 Thread t
404 PPCODE:
f0f333f4 405#ifdef USE_THREADS
7d901afa 406 PUSHs(sv_2mortal(newSViv(t->flags)));
f0f333f4 407#endif
7d901afa
MB
408
409void
458fb581
MB
410self(classname)
411 char * classname
7d901afa
MB
412 PREINIT:
413 SV *sv;
f0f333f4
NIS
414 PPCODE:
415#ifdef USE_THREADS
7d901afa 416 sv = newSViv(thr->tid);
199100c8 417 sv_magic(sv, thr->oursv, '~', 0, 0);
7d901afa 418 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
458fb581
MB
419 PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
420 gv_stashpv(classname, TRUE))));
f0f333f4 421#endif
7d901afa 422
50112d62
MB
423U32
424tid(t)
425 Thread t
426 CODE:
f0f333f4 427#ifdef USE_THREADS
50112d62
MB
428 MUTEX_LOCK(&t->mutex);
429 RETVAL = t->tid;
430 MUTEX_UNLOCK(&t->mutex);
f0f333f4
NIS
431#else
432 RETVAL = 0;
433#endif
50112d62
MB
434 OUTPUT:
435 RETVAL
436
437void
438DESTROY(t)
439 SV * t
440 PPCODE:
441 PUSHs(&sv_yes);
442
7d901afa 443void
734689b1 444yield()
d9bb3666 445 CODE:
f0f333f4
NIS
446{
447#ifdef USE_THREADS
ea0efc06 448 YIELD;
f0f333f4
NIS
449#endif
450}
d9bb3666
MB
451
452void
734689b1
MB
453cond_wait(sv)
454 SV * sv
455 MAGIC * mg = NO_INIT
f0f333f4
NIS
456CODE:
457#ifdef USE_THREADS
2c127b02 458 if (SvROK(sv))
734689b1 459 sv = SvRV(sv);
2c127b02 460
734689b1 461 mg = condpair_magic(sv);
683929b4 462 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
734689b1
MB
463 MUTEX_LOCK(MgMUTEXP(mg));
464 if (MgOWNER(mg) != thr) {
465 MUTEX_UNLOCK(MgMUTEXP(mg));
466 croak("cond_wait for lock that we don't own\n");
467 }
468 MgOWNER(mg) = 0;
469 COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
50112d62
MB
470 while (MgOWNER(mg))
471 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
734689b1
MB
472 MgOWNER(mg) = thr;
473 MUTEX_UNLOCK(MgMUTEXP(mg));
f0f333f4
NIS
474#endif
475
734689b1
MB
476void
477cond_signal(sv)
478 SV * sv
479 MAGIC * mg = NO_INIT
480CODE:
f0f333f4 481#ifdef USE_THREADS
50112d62 482 if (SvROK(sv))
734689b1 483 sv = SvRV(sv);
50112d62 484
734689b1 485 mg = condpair_magic(sv);
683929b4 486 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
734689b1
MB
487 MUTEX_LOCK(MgMUTEXP(mg));
488 if (MgOWNER(mg) != thr) {
489 MUTEX_UNLOCK(MgMUTEXP(mg));
490 croak("cond_signal for lock that we don't own\n");
491 }
492 COND_SIGNAL(MgCONDP(mg));
493 MUTEX_UNLOCK(MgMUTEXP(mg));
f0f333f4 494#endif
d9bb3666 495
734689b1
MB
496void
497cond_broadcast(sv)
498 SV * sv
499 MAGIC * mg = NO_INIT
f0f333f4
NIS
500CODE:
501#ifdef USE_THREADS
783070da 502 if (SvROK(sv))
734689b1 503 sv = SvRV(sv);
783070da 504
734689b1 505 mg = condpair_magic(sv);
683929b4
MB
506 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
507 thr, sv));
734689b1
MB
508 MUTEX_LOCK(MgMUTEXP(mg));
509 if (MgOWNER(mg) != thr) {
510 MUTEX_UNLOCK(MgMUTEXP(mg));
511 croak("cond_broadcast for lock that we don't own\n");
512 }
513 COND_BROADCAST(MgCONDP(mg));
514 MUTEX_UNLOCK(MgMUTEXP(mg));
f0f333f4 515#endif
f152979c 516
7d901afa 517void
458fb581
MB
518list(classname)
519 char * classname
7d901afa
MB
520 PREINIT:
521 Thread t;
522 AV * av;
523 SV ** svp;
524 int n = 0;
525 PPCODE:
f0f333f4 526#ifdef USE_THREADS
7d901afa
MB
527 av = newAV();
528 /*
529 * Iterate until we have enough dynamic storage for all threads.
530 * We mustn't do any allocation while holding threads_mutex though.
531 */
532 MUTEX_LOCK(&threads_mutex);
533 do {
534 n = nthreads;
535 MUTEX_UNLOCK(&threads_mutex);
536 if (AvFILL(av) < n - 1) {
537 int i = AvFILL(av);
538 for (i = AvFILL(av); i < n - 1; i++) {
539 SV *sv = newSViv(0); /* fill in tid later */
540 sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
541 av_push(av, sv_bless(newRV_noinc(sv),
458fb581 542 gv_stashpv(classname, TRUE)));
50112d62 543
7d901afa
MB
544 }
545 }
546 MUTEX_LOCK(&threads_mutex);
547 } while (n < nthreads);
50112d62 548 n = nthreads; /* Get the final correct value */
7d901afa
MB
549
550 /*
551 * At this point, there's enough room to fill in av.
552 * Note that we are holding threads_mutex so the list
553 * won't change out from under us but all the remaining
554 * processing is "fast" (no blocking, malloc etc.)
555 */
556 t = thr;
557 svp = AvARRAY(av);
558 do {
0a00ffdb 559 SV *sv = (SV*)SvRV(*svp);
7d901afa 560 sv_setiv(sv, t->tid);
199100c8 561 SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
7d901afa
MB
562 SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
563 SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
564 t = t->next;
0a00ffdb 565 svp++;
7d901afa 566 } while (t != thr);
50112d62 567 /* */
7d901afa
MB
568 MUTEX_UNLOCK(&threads_mutex);
569 /* Truncate any unneeded slots in av */
50112d62 570 av_fill(av, n - 1);
7d901afa 571 /* Finally, push all the new objects onto the stack and drop av */
924508f0 572 EXTEND(SP, n);
7d901afa
MB
573 for (svp = AvARRAY(av); n > 0; n--, svp++)
574 PUSHs(*svp);
575 (void)sv_2mortal((SV*)av);
f0f333f4 576#endif
7d901afa
MB
577
578
f152979c
MB
579MODULE = Thread PACKAGE = Thread::Signal
580
581void
582kill_sighandler_thread()
583 PPCODE:
3aeed370 584 write(sig_pipe[1], "\0", 1);
f152979c
MB
585 PUSHs(&sv_yes);
586
587void
588init_thread_signals()
589 PPCODE:
590 sighandlerp = handle_thread_signal;
591 if (pipe(sig_pipe) == -1)
592 XSRETURN_UNDEF;
593 PUSHs(&sv_yes);
594
3aeed370 595void
f152979c
MB
596await_signal()
597 PREINIT:
3aeed370 598 unsigned char c;
ea0efc06 599 SSize_t ret;
f152979c
MB
600 CODE:
601 do {
3aeed370 602 ret = read(sig_pipe[0], &c, 1);
f152979c
MB
603 } while (ret == -1 && errno == EINTR);
604 if (ret == -1)
605 croak("panic: await_signal");
3aeed370
MB
606 ST(0) = sv_newmortal();
607 if (ret)
608 sv_setsv(ST(0), c ? psig_ptr[c] : &sv_no);
609 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
610 "await_signal returning %s\n", SvPEEK(ST(0))););
4e35701f 611
458fb581
MB
612MODULE = Thread PACKAGE = Thread::Specific
613
614void
615data(classname = "Thread::Specific")
616 char * classname
617 PPCODE:
fb223100 618#ifdef USE_THREADS
458fb581
MB
619 if (AvFILL(thr->specific) == -1) {
620 GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV);
621 av_store(thr->specific, 0, newRV((SV*)GvHV(gv)));
622 }
623 XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));
fb223100 624#endif