ba256c940259e9c9ea8e2c5265d28c2abc3221c1
[p5sagit/p5-mst-13.2.git] / ext / Thread / Thread.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 /* Magic signature for Thread's mg_private is "Th" */ 
6 #define Thread_MAGIC_SIGNATURE 0x5468
7
8 #ifdef __cplusplus
9 #ifdef I_UNISTD
10 #include <unistd.h>
11 #endif
12 #endif
13 #include <fcntl.h>
14                         
15 static U32 threadnum = 0;
16 static int sig_pipe[2];
17             
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)
22 #endif
23
24 static void
25 remove_thread(struct thread *t)
26 {
27 #ifdef USE_THREADS
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);
32     nthreads--;
33     t->prev->next = t->next;
34     t->next->prev = t->prev;
35     COND_BROADCAST(&nthreads_cond);
36     MUTEX_UNLOCK(&threads_mutex);
37 #endif
38 }
39
40 static THREAD_RET_TYPE
41 threadstart(void *arg)
42 {
43 #ifdef USE_THREADS
44 #ifdef FAKE_THREADS
45     Thread savethread = thr;
46     LOGOP myop;
47     dSP;
48     I32 oldscope = scopestack_ix;
49     I32 retval;
50     AV *av;
51     int i;
52
53     DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
54                           thr, SvPEEK(TOPs)));
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
83     Thread thr = (Thread) arg;
84     LOGOP myop;
85     djSP;
86     I32 oldmark = TOPMARK;
87     I32 oldscope = scopestack_ix;
88     I32 retval;
89     SV *sv;
90     AV *av = newAV();
91     int i, ret;
92     dJMPENV;
93     DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p waiting to start\n",
94                           thr));
95
96     /* Don't call *anything* requiring dTHR until after SET_THR() */
97     /*
98      * Wait until our creator releases us. If we didn't do this, then
99      * it would be potentially possible for out thread to carry on and
100      * do stuff before our creator fills in our "self" field. For example,
101      * if we went and created another thread which tried to JOIN with us,
102      * then we'd be in a mess.
103      */
104     MUTEX_LOCK(&thr->mutex);
105     MUTEX_UNLOCK(&thr->mutex);
106
107     /*
108      * It's safe to wait until now to set the thread-specific pointer
109      * from our pthread_t structure to our struct thread, since we're
110      * the only thread who can get at it anyway.
111      */
112     SET_THR(thr);
113
114     /* Only now can we use SvPEEK (which calls sv_newmortal which does dTHR) */
115     DEBUG_L(PerlIO_printf(PerlIO_stderr(), "new thread %p starting at %s\n",
116                           thr, SvPEEK(TOPs)));
117
118 #ifdef OLD_WAY
119     JMPENV_PUSH(ret);
120     switch (ret) {
121     case 3:
122         PerlIO_printf(PerlIO_stderr(), "panic: threadstart\n");
123         /* fall through */
124     case 1:
125         STATUS_ALL_FAILURE;
126         /* fall through */
127     case 2:
128         /* my_exit() was called */
129         while (scopestack_ix > oldscope)
130             LEAVE;
131         JMPENV_POP;
132         MUTEX_LOCK(&thr->mutex);
133         thr->flags |= THRf_DID_DIE;
134         MUTEX_UNLOCK(&thr->mutex);
135         av = newSVpvf("Thread called exit with value %d", statusvalue);
136         goto finishoff;
137     }
138
139     CATCH_SET(TRUE);
140
141     /* Now duplicate most of perl_call_sv but with a few twists */
142     op = (OP*)&myop;
143     Zero(op, 1, LOGOP);
144     myop.op_flags = OPf_STACKED;
145     myop.op_next = Nullop;
146     myop.op_flags |= OPf_KNOW;
147     myop.op_flags |= OPf_WANT_LIST;
148     op = pp_entersub(ARGS);
149     if (op)
150         runops();
151 #else
152     sv = POPs;
153     PUTBACK;
154     perl_call_sv(sv, G_ARRAY|G_EVAL);
155 #endif
156     SPAGAIN;
157     retval = sp - (stack_base + oldmark);
158     sp = stack_base + oldmark + 1;
159     if (SvCUR(thr->errsv)) {
160         MUTEX_LOCK(&thr->mutex);
161         thr->flags |= THRf_DID_DIE;
162         MUTEX_UNLOCK(&thr->mutex);
163         av_store(av, 0, &sv_no);
164         av_store(av, 1, newSVsv(thr->errsv));
165         DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p died: %s\n",
166                               SvPV(thr->errsv, na));
167     } else {
168         DEBUG_L(STMT_START {
169             for (i = 1; i <= retval; i++) {
170                 PerlIO_printf(PerlIO_stderr(), "%p return[%d] = %s\n",
171                                 thr, i, SvPEEK(sp[i - 1]));)
172             }
173         } STMT_END);
174         av_store(av, 0, &sv_yes);
175         for (i = 1; i <= retval; i++, sp++)
176             sv_setsv(*av_fetch(av, i, TRUE), SvREFCNT_inc(*sp));
177     }
178
179   finishoff:
180 #if 0    
181     /* removed for debug */
182     SvREFCNT_dec(curstack);
183 #endif
184     SvREFCNT_dec(thr->cvcache);
185     SvREFCNT_dec(thr->magicals);
186     SvREFCNT_dec(thr->specific);
187     SvREFCNT_dec(thr->errsv);
188     SvREFCNT_dec(thr->errhv);
189     Safefree(markstack);
190     Safefree(scopestack);
191     Safefree(savestack);
192     Safefree(retstack);
193     Safefree(cxstack);
194     Safefree(tmps_stack);
195     Safefree(ofs);
196
197     MUTEX_LOCK(&thr->mutex);
198     DEBUG_L(PerlIO_printf(PerlIO_stderr(),
199                           "%p: threadstart finishing: state is %u\n",
200                           thr, ThrSTATE(thr)));
201     switch (ThrSTATE(thr)) {
202     case THRf_R_JOINABLE:
203         ThrSETSTATE(thr, THRf_ZOMBIE);
204         MUTEX_UNLOCK(&thr->mutex);
205         DEBUG_L(PerlIO_printf(PerlIO_stderr(),
206                               "%p: R_JOINABLE thread finished\n", thr));
207         break;
208     case THRf_R_JOINED:
209         ThrSETSTATE(thr, THRf_DEAD);
210         MUTEX_UNLOCK(&thr->mutex);
211         remove_thread(thr);
212         DEBUG_L(PerlIO_printf(PerlIO_stderr(),
213                               "%p: R_JOINED thread finished\n", thr));
214         break;
215     case THRf_R_DETACHED:
216         ThrSETSTATE(thr, THRf_DEAD);
217         MUTEX_UNLOCK(&thr->mutex);
218         SvREFCNT_dec(av);
219         DEBUG_L(PerlIO_printf(PerlIO_stderr(),
220                               "%p: DETACHED thread finished\n", thr));
221         remove_thread(thr);     /* This might trigger main thread to finish */
222         break;
223     default:
224         MUTEX_UNLOCK(&thr->mutex);
225         croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
226         /* NOTREACHED */
227     }
228     return THREAD_RET_CAST(av); /* Available for anyone to join with */
229                                         /* us unless we're detached, in which */
230                                         /* case noone sees the value anyway. */
231 #endif    
232 #else
233     return THREAD_RET_CAST(NULL);
234 #endif
235 }
236
237 static SV *
238 newthread (SV *startsv, AV *initargs, char *classname)
239 {
240 #ifdef USE_THREADS
241     dSP;
242     Thread savethread;
243     int i;
244     SV *sv;
245     int err;
246 #ifndef THREAD_CREATE
247     sigset_t fullmask, oldmask;
248 #endif
249     
250     savethread = thr;
251     thr = new_struct_thread(thr);
252     SPAGAIN;
253     DEBUG_L(PerlIO_printf(PerlIO_stderr(),
254                           "%p: newthread (%p), tid is %u, preparing stack\n",
255                           savethread, thr, thr->tid));
256     /* The following pushes the arg list and startsv onto the *new* stack */
257     PUSHMARK(sp);
258     /* Could easily speed up the following greatly */
259     for (i = 0; i <= AvFILL(initargs); i++)
260         XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
261     XPUSHs(SvREFCNT_inc(startsv));
262     PUTBACK;
263 #ifdef THREAD_CREATE
264     err = THREAD_CREATE(thr, threadstart);
265 #else    
266     /* On your marks... */
267     MUTEX_LOCK(&thr->mutex);
268     /* Get set...  */
269     sigfillset(&fullmask);
270     if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
271         croak("panic: sigprocmask");
272     err = pthread_create(&thr->self, pthread_attr_default,
273                          threadstart, (void*) thr);
274     /* Go */
275     MUTEX_UNLOCK(&thr->mutex);
276 #endif
277     if (err) {
278         DEBUG_L(PerlIO_printf(PerlIO_stderr(),
279                           "%p: create of %p failed %d\n", savethread, thr, err));
280         /* Thread creation failed--clean up */
281         SvREFCNT_dec(thr->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
292     if (sigprocmask(SIG_SETMASK, &oldmask, 0))
293         croak("panic: sigprocmask");
294 #endif
295     sv = newSViv(thr->tid);
296     sv_magic(sv, thr->oursv, '~', 0, 0);
297     SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
298     return sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
299 #else
300     croak("No threads in this perl");
301     return &sv_undef;
302 #endif
303 }
304
305 static Signal_t handle_thread_signal _((int sig));
306
307 static Signal_t
308 handle_thread_signal(int sig)
309 {
310     char c = (char) sig;
311     write(sig_pipe[0], &c, 1);
312 }
313
314 MODULE = Thread         PACKAGE = Thread
315 PROTOTYPES: DISABLE
316
317 void
318 new(classname, startsv, ...)
319         char *          classname
320         SV *            startsv
321         AV *            av = av_make(items - 2, &ST(2));
322     PPCODE:
323         XPUSHs(sv_2mortal(newthread(startsv, av, classname)));
324
325 void
326 join(t)
327         Thread  t
328         AV *    av = NO_INIT
329         int     i = NO_INIT
330     PPCODE:
331 #ifdef USE_THREADS
332         DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: joining %p (state %u)\n",
333                               thr, t, ThrSTATE(t)););
334         MUTEX_LOCK(&t->mutex);
335         switch (ThrSTATE(t)) {
336         case THRf_R_JOINABLE:
337         case THRf_R_JOINED:
338             ThrSETSTATE(t, THRf_R_JOINED);
339             MUTEX_UNLOCK(&t->mutex);
340             break;
341         case THRf_ZOMBIE:
342             ThrSETSTATE(t, THRf_DEAD);
343             MUTEX_UNLOCK(&t->mutex);
344             remove_thread(t);
345             break;
346         default:
347             MUTEX_UNLOCK(&t->mutex);
348             croak("can't join with thread");
349             /* NOTREACHED */
350         }
351         JOIN(t, &av);
352
353         if (SvTRUE(*av_fetch(av, 0, FALSE))) {
354             /* Could easily speed up the following if necessary */
355             for (i = 1; i <= AvFILL(av); i++)
356                 XPUSHs(sv_2mortal(*av_fetch(av, i, FALSE)));
357         } else {
358             char *mess = SvPV(*av_fetch(av, 1, FALSE), na);
359             DEBUG_L(PerlIO_printf(PerlIO_stderr(),
360                                   "%p: join propagating die message: %s\n",
361                                   thr, mess));
362             croak(mess);
363         }
364 #endif
365
366 void
367 detach(t)
368         Thread  t
369     CODE:
370 #ifdef USE_THREADS
371         DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: detaching %p (state %u)\n",
372                               thr, t, ThrSTATE(t)););
373         MUTEX_LOCK(&t->mutex);
374         switch (ThrSTATE(t)) {
375         case THRf_R_JOINABLE:
376             ThrSETSTATE(t, THRf_R_DETACHED);
377             /* fall through */
378         case THRf_R_DETACHED:
379             DETACH(t);
380             MUTEX_UNLOCK(&t->mutex);
381             break;
382         case THRf_ZOMBIE:
383             ThrSETSTATE(t, THRf_DEAD);
384             DETACH(t);
385             MUTEX_UNLOCK(&t->mutex);
386             remove_thread(t);
387             break;
388         default:
389             MUTEX_UNLOCK(&t->mutex);
390             croak("can't detach thread");
391             /* NOTREACHED */
392         }
393 #endif
394
395 void
396 equal(t1, t2)
397         Thread  t1
398         Thread  t2
399     PPCODE:
400         PUSHs((t1 == t2) ? &sv_yes : &sv_no);
401
402 void
403 flags(t)
404         Thread  t
405     PPCODE:
406 #ifdef USE_THREADS
407         PUSHs(sv_2mortal(newSViv(t->flags)));
408 #endif
409
410 void
411 self(classname)
412         char *  classname
413     PREINIT:
414         SV *sv;
415     PPCODE:        
416 #ifdef USE_THREADS
417         sv = newSViv(thr->tid);
418         sv_magic(sv, thr->oursv, '~', 0, 0);
419         SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
420         PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
421                                   gv_stashpv(classname, TRUE))));
422 #endif
423
424 U32
425 tid(t)
426         Thread  t
427     CODE:
428 #ifdef USE_THREADS
429         MUTEX_LOCK(&t->mutex);
430         RETVAL = t->tid;
431         MUTEX_UNLOCK(&t->mutex);
432 #else 
433         RETVAL = 0;
434 #endif
435     OUTPUT:
436         RETVAL
437
438 void
439 DESTROY(t)
440         SV *    t
441     PPCODE:
442         PUSHs(&sv_yes);
443
444 void
445 yield()
446     CODE:
447 {
448 #ifdef USE_THREADS
449         YIELD;
450 #endif
451 }
452
453 void
454 cond_wait(sv)
455         SV *    sv
456         MAGIC * mg = NO_INIT
457 CODE:                       
458 #ifdef USE_THREADS
459         if (SvROK(sv))
460             sv = SvRV(sv);
461
462         mg = condpair_magic(sv);
463         DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_wait %p\n", thr, sv));
464         MUTEX_LOCK(MgMUTEXP(mg));
465         if (MgOWNER(mg) != thr) {
466             MUTEX_UNLOCK(MgMUTEXP(mg));
467             croak("cond_wait for lock that we don't own\n");
468         }
469         MgOWNER(mg) = 0;
470         COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
471         while (MgOWNER(mg))
472             COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
473         MgOWNER(mg) = thr;
474         MUTEX_UNLOCK(MgMUTEXP(mg));
475 #endif
476
477 void
478 cond_signal(sv)
479         SV *    sv
480         MAGIC * mg = NO_INIT
481 CODE:
482 #ifdef USE_THREADS
483         if (SvROK(sv))
484             sv = SvRV(sv);
485
486         mg = condpair_magic(sv);
487         DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_signal %p\n",thr,sv));
488         MUTEX_LOCK(MgMUTEXP(mg));
489         if (MgOWNER(mg) != thr) {
490             MUTEX_UNLOCK(MgMUTEXP(mg));
491             croak("cond_signal for lock that we don't own\n");
492         }
493         COND_SIGNAL(MgCONDP(mg));
494         MUTEX_UNLOCK(MgMUTEXP(mg));
495 #endif
496
497 void
498 cond_broadcast(sv)
499         SV *    sv
500         MAGIC * mg = NO_INIT
501 CODE: 
502 #ifdef USE_THREADS
503         if (SvROK(sv))
504             sv = SvRV(sv);
505
506         mg = condpair_magic(sv);
507         DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: cond_broadcast %p\n",
508                               thr, sv));
509         MUTEX_LOCK(MgMUTEXP(mg));
510         if (MgOWNER(mg) != thr) {
511             MUTEX_UNLOCK(MgMUTEXP(mg));
512             croak("cond_broadcast for lock that we don't own\n");
513         }
514         COND_BROADCAST(MgCONDP(mg));
515         MUTEX_UNLOCK(MgMUTEXP(mg));
516 #endif
517
518 void
519 list(classname)
520         char *  classname
521     PREINIT:
522         Thread  t;
523         AV *    av;
524         SV **   svp;
525         int     n = 0;
526     PPCODE:
527 #ifdef USE_THREADS
528         av = newAV();
529         /*
530          * Iterate until we have enough dynamic storage for all threads.
531          * We mustn't do any allocation while holding threads_mutex though.
532          */
533         MUTEX_LOCK(&threads_mutex);
534         do {
535             n = nthreads;
536             MUTEX_UNLOCK(&threads_mutex);
537             if (AvFILL(av) < n - 1) {
538                 int i = AvFILL(av);
539                 for (i = AvFILL(av); i < n - 1; i++) {
540                     SV *sv = newSViv(0);        /* fill in tid later */
541                     sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
542                     av_push(av, sv_bless(newRV_noinc(sv),
543                                          gv_stashpv(classname, TRUE)));
544         
545                 }
546             }
547             MUTEX_LOCK(&threads_mutex);
548         } while (n < nthreads);
549         n = nthreads;   /* Get the final correct value */
550
551         /*
552          * At this point, there's enough room to fill in av.
553          * Note that we are holding threads_mutex so the list
554          * won't change out from under us but all the remaining
555          * processing is "fast" (no blocking, malloc etc.)
556          */
557         t = thr;
558         svp = AvARRAY(av);
559         do {
560             SV *sv = (SV*)SvRV(*svp);
561             sv_setiv(sv, t->tid);
562             SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
563             SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
564             SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
565             t = t->next;
566             svp++;
567         } while (t != thr);
568         /*  */
569         MUTEX_UNLOCK(&threads_mutex);
570         /* Truncate any unneeded slots in av */
571         av_fill(av, n - 1);
572         /* Finally, push all the new objects onto the stack and drop av */
573         EXTEND(sp, n);
574         for (svp = AvARRAY(av); n > 0; n--, svp++)
575             PUSHs(*svp);
576         (void)sv_2mortal((SV*)av);
577 #endif
578
579
580 MODULE = Thread         PACKAGE = Thread::Signal
581
582 void
583 kill_sighandler_thread()
584     PPCODE:
585         write(sig_pipe[0], "\0", 1);
586         PUSHs(&sv_yes);
587
588 void
589 init_thread_signals()
590     PPCODE:
591         sighandlerp = handle_thread_signal;
592         if (pipe(sig_pipe) == -1)
593             XSRETURN_UNDEF;
594         PUSHs(&sv_yes);
595
596 SV *
597 await_signal()
598     PREINIT:
599         char c;
600         SSize_t ret;
601     CODE:
602         do {
603             ret = read(sig_pipe[1], &c, 1);
604         } while (ret == -1 && errno == EINTR);
605         if (ret == -1)
606             croak("panic: await_signal");
607         if (ret == 0)
608             XSRETURN_UNDEF;
609         RETVAL = c ? psig_ptr[c] : &sv_no;
610     OUTPUT:
611         RETVAL
612
613 MODULE = Thread         PACKAGE = Thread::Specific
614
615 void
616 data(classname = "Thread::Specific")
617         char *  classname
618     PPCODE:
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)));