This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
-Wall cleanup.
[perl5.git] / ext / Thread / Thread.xs
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5
6 /* Magic signature for Thread's mg_private is "Th" */ 
7 #define Thread_MAGIC_SIGNATURE 0x5468
8
9 #ifdef __cplusplus
10 #ifdef I_UNISTD
11 #include <unistd.h>
12 #endif
13 #endif
14 #include <fcntl.h>
15                         
16 static int sig_pipe[2];
17             
18 #ifndef THREAD_RET_TYPE
19 #define THREAD_RET_TYPE void *
20 #define THREAD_RET_CAST(x) ((THREAD_RET_TYPE) x)
21 #endif
22
23 static void
24 remove_thread(pTHX_ Thread t)
25 {
26 #ifdef USE_THREADS
27     DEBUG_S(WITH_THR(PerlIO_printf(Perl_debug_log,
28                                    "%p: remove_thread %p\n", thr, t)));
29     MUTEX_LOCK(&PL_threads_mutex);
30     MUTEX_DESTROY(&t->mutex);
31     PL_nthreads--;
32     t->prev->next = t->next;
33     t->next->prev = t->prev;
34     SvREFCNT_dec(t->oursv);
35     COND_BROADCAST(&PL_nthreads_cond);
36     MUTEX_UNLOCK(&PL_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 = PL_scopestack_ix;
49     I32 retval;
50     AV *av;
51     int i;
52
53     DEBUG_S(PerlIO_printf(Perl_debug_log, "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     PL_op = (OP*)&myop;
66     Zero(PL_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     PL_op = pp_entersub(ARGS);
72     DEBUG_S(if (!PL_op)
73             PerlIO_printf(Perl_debug_log, "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     dSP;
85     I32 oldmark = TOPMARK;
86     I32 retval;
87     SV *sv;
88     AV *av;
89     int i;
90
91 #if defined(MULTIPLICITY)
92     PERL_SET_INTERP(thr->interp);
93 #endif
94
95     DEBUG_S(PerlIO_printf(Perl_debug_log, "new thread %p waiting to start\n",
96                           thr));
97
98     /*
99      * Wait until our creator releases us. If we didn't do this, then
100      * it would be potentially possible for out thread to carry on and
101      * do stuff before our creator fills in our "self" field. For example,
102      * if we went and created another thread which tried to JOIN with us,
103      * then we'd be in a mess.
104      */
105     MUTEX_LOCK(&thr->mutex);
106     MUTEX_UNLOCK(&thr->mutex);
107
108     /*
109      * It's safe to wait until now to set the thread-specific pointer
110      * from our pthread_t structure to our struct perl_thread, since
111      * we're the only thread who can get at it anyway.
112      */
113     PERL_SET_THX(thr);
114
115     DEBUG_S(PerlIO_printf(Perl_debug_log, "new thread %p starting at %s\n",
116                           thr, SvPEEK(TOPs)));
117
118     av = newAV();
119     sv = POPs;
120     PUTBACK;
121     ENTER;
122     SAVETMPS;
123     perl_call_sv(sv, G_ARRAY|G_EVAL);
124     SPAGAIN;
125     retval = SP - (PL_stack_base + oldmark);
126     SP = PL_stack_base + oldmark + 1;
127     if (SvCUR(thr->errsv)) {
128         MUTEX_LOCK(&thr->mutex);
129         thr->flags |= THRf_DID_DIE;
130         MUTEX_UNLOCK(&thr->mutex);
131         av_store(av, 0, &PL_sv_no);
132         av_store(av, 1, newSVsv(thr->errsv));
133         DEBUG_S(PerlIO_printf(Perl_debug_log, "%p died: %s\n",
134                               thr, SvPV(thr->errsv, PL_na)));
135     }
136     else {
137         DEBUG_S(STMT_START {
138             for (i = 1; i <= retval; i++) {
139                 PerlIO_printf(Perl_debug_log, "%p return[%d] = %s\n",
140                                 thr, i, SvPEEK(SP[i - 1]));
141             }
142         } STMT_END);
143         av_store(av, 0, &PL_sv_yes);
144         for (i = 1; i <= retval; i++, SP++)
145             sv_setsv(*av_fetch(av, i, TRUE), SvREFCNT_inc(*SP));
146     }
147     FREETMPS;
148     LEAVE;
149
150 #if 0    
151     /* removed for debug */
152     SvREFCNT_dec(PL_curstack);
153 #endif
154     SvREFCNT_dec(thr->cvcache);
155     SvREFCNT_dec(thr->threadsv);
156     SvREFCNT_dec(thr->specific);
157     SvREFCNT_dec(thr->errsv);
158
159     /*Safefree(cxstack);*/
160     while (PL_curstackinfo->si_next)
161         PL_curstackinfo = PL_curstackinfo->si_next;
162     while (PL_curstackinfo) {
163         PERL_SI *p = PL_curstackinfo->si_prev;
164         SvREFCNT_dec(PL_curstackinfo->si_stack);
165         Safefree(PL_curstackinfo->si_cxstack);
166         Safefree(PL_curstackinfo);
167         PL_curstackinfo = p;
168     }    
169     Safefree(PL_markstack);
170     Safefree(PL_scopestack);
171     Safefree(PL_savestack);
172     Safefree(PL_retstack);
173     Safefree(PL_tmps_stack);
174     SvREFCNT_dec(PL_ofs_sv);
175
176     SvREFCNT_dec(PL_rs);
177     SvREFCNT_dec(PL_nrs);
178     SvREFCNT_dec(PL_statname);
179     SvREFCNT_dec(PL_errors);
180     Safefree(PL_screamfirst);
181     Safefree(PL_screamnext);
182     Safefree(PL_reg_start_tmp);
183     SvREFCNT_dec(PL_lastscream);
184     SvREFCNT_dec(PL_defoutgv);
185     Safefree(PL_reg_poscache);
186
187     MUTEX_LOCK(&thr->mutex);
188     thr->thr_done = 1;
189     DEBUG_S(PerlIO_printf(Perl_debug_log,
190                           "%p: threadstart finishing: state is %u\n",
191                           thr, ThrSTATE(thr)));
192     switch (ThrSTATE(thr)) {
193     case THRf_R_JOINABLE:
194         ThrSETSTATE(thr, THRf_ZOMBIE);
195         MUTEX_UNLOCK(&thr->mutex);
196         DEBUG_S(PerlIO_printf(Perl_debug_log,
197                               "%p: R_JOINABLE thread finished\n", thr));
198         break;
199     case THRf_R_JOINED:
200         ThrSETSTATE(thr, THRf_DEAD);
201         MUTEX_UNLOCK(&thr->mutex);
202         remove_thread(aTHX_ thr);
203         DEBUG_S(PerlIO_printf(Perl_debug_log,
204                               "%p: R_JOINED thread finished\n", thr));
205         break;
206     case THRf_R_DETACHED:
207         ThrSETSTATE(thr, THRf_DEAD);
208         MUTEX_UNLOCK(&thr->mutex);
209         SvREFCNT_dec(av);
210         DEBUG_S(PerlIO_printf(Perl_debug_log,
211                               "%p: DETACHED thread finished\n", thr));
212         remove_thread(aTHX_ thr);       /* This might trigger main thread to finish */
213         break;
214     default:
215         MUTEX_UNLOCK(&thr->mutex);
216         croak("panic: illegal state %u at end of threadstart", ThrSTATE(thr));
217         /* NOTREACHED */
218     }
219     return THREAD_RET_CAST(av); /* Available for anyone to join with */
220                                         /* us unless we're detached, in which */
221                                         /* case noone sees the value anyway. */
222 #endif    
223 #else
224     return THREAD_RET_CAST(NULL);
225 #endif
226 }
227
228 static SV *
229 newthread (pTHX_ SV *startsv, AV *initargs, char *classname)
230 {
231 #ifdef USE_THREADS
232     dSP;
233     Thread savethread;
234     int i;
235     SV *sv;
236     int err;
237 #ifndef THREAD_CREATE
238     static pthread_attr_t attr;
239     static int attr_inited = 0;
240     sigset_t fullmask, oldmask;
241     static int attr_joinable = PTHREAD_CREATE_JOINABLE;
242 #endif
243
244     savethread = thr;
245     thr = new_struct_thread(thr);
246     /* temporarily pretend to be the child thread in case the
247      * XPUSHs() below want to grow the child's stack.  This is
248      * safe, since the other thread is not yet created, and we
249      * are the only ones who know about it */
250     PERL_SET_THX(thr);
251     SPAGAIN;
252     DEBUG_S(PerlIO_printf(Perl_debug_log,
253                           "%p: newthread (%p), tid is %u, preparing stack\n",
254                           savethread, thr, thr->tid));
255     /* The following pushes the arg list and startsv onto the *new* stack */
256     PUSHMARK(SP);
257     /* Could easily speed up the following greatly */
258     for (i = 0; i <= AvFILL(initargs); i++)
259         XPUSHs(SvREFCNT_inc(*av_fetch(initargs, i, FALSE)));
260     XPUSHs(SvREFCNT_inc(startsv));
261     PUTBACK;
262
263     /* On your marks... */
264     PERL_SET_THX(savethread);
265     MUTEX_LOCK(&thr->mutex);
266
267 #ifdef THREAD_CREATE
268     err = THREAD_CREATE(thr, threadstart);
269 #else    
270     /* Get set...  */
271     sigfillset(&fullmask);
272     if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1)
273         croak("panic: sigprocmask");
274     err = 0;
275     if (!attr_inited) {
276         attr_inited = 1;
277         err = pthread_attr_init(&attr);
278 #  ifdef THREAD_CREATE_NEEDS_STACK
279        if (err == 0)
280             err = pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK);
281        if (err)
282            croak("panic: pthread_attr_setstacksize failed");
283 #  endif
284 #  ifdef PTHREAD_ATTR_SETDETACHSTATE
285         if (err == 0)
286             err = PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
287        if (err)
288            croak("panic: pthread_attr_setdetachstate failed");
289 #  else
290         croak("panic: can't pthread_attr_setdetachstate");
291 #  endif
292     }
293     if (err == 0)
294         err = PTHREAD_CREATE(&thr->self, attr, threadstart, (void*) thr);
295 #endif
296
297     if (err) {
298         MUTEX_UNLOCK(&thr->mutex);
299         DEBUG_S(PerlIO_printf(Perl_debug_log,
300                               "%p: create of %p failed %d\n",
301                               savethread, thr, err));
302         /* Thread creation failed--clean up */
303         SvREFCNT_dec(thr->cvcache);
304         remove_thread(aTHX_ thr);
305         for (i = 0; i <= AvFILL(initargs); i++)
306             SvREFCNT_dec(*av_fetch(initargs, i, FALSE));
307         SvREFCNT_dec(startsv);
308         return NULL;
309     }
310
311 #ifdef THREAD_POST_CREATE
312     THREAD_POST_CREATE(thr);
313 #else
314     if (sigprocmask(SIG_SETMASK, &oldmask, 0))
315         croak("panic: sigprocmask");
316 #endif
317
318     sv = newSViv(thr->tid);
319     sv_magic(sv, thr->oursv, '~', 0, 0);
320     SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
321     sv = sv_bless(newRV_noinc(sv), gv_stashpv(classname, TRUE));
322
323     /* Go */
324     MUTEX_UNLOCK(&thr->mutex);
325
326     return sv;
327 #else
328 #  ifdef USE_ITHREADS
329     croak("This perl was built for \"ithreads\", which currently does not support Thread.pm.\n"
330           "Run \"perldoc Thread\" for more information");
331 #  else
332     croak("This perl was not built with support for 5.005-style threads.\n"
333           "Run \"perldoc Thread\" for more information");
334 #  endif
335     return &PL_sv_undef;
336 #endif
337 }
338
339 static Signal_t handle_thread_signal (int sig);
340
341 static Signal_t
342 handle_thread_signal(int sig)
343 {
344     unsigned char c = (unsigned char) sig;
345     /*
346      * We're not really allowed to call fprintf in a signal handler
347      * so don't be surprised if this isn't robust while debugging
348      * with -DL.
349      */
350     DEBUG_S(PerlIO_printf(Perl_debug_log,
351             "handle_thread_signal: got signal %d\n", sig););
352     write(sig_pipe[1], &c, 1);
353 }
354
355 MODULE = Thread         PACKAGE = Thread
356 PROTOTYPES: DISABLE
357
358 void
359 new(classname, startsv, ...)
360         char *          classname
361         SV *            startsv
362         AV *            av = av_make(items - 2, &ST(2));
363     PPCODE:
364         XPUSHs(sv_2mortal(newthread(aTHX_ startsv, av, classname)));
365
366 void
367 join(t)
368         Thread  t
369         AV *    av = NO_INIT
370         int     i = NO_INIT
371     PPCODE:
372 #ifdef USE_THREADS
373         if (t == thr)
374             croak("Attempt to join self");
375         DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: joining %p (state %u)\n",
376                               thr, t, ThrSTATE(t)););
377         MUTEX_LOCK(&t->mutex);
378         switch (ThrSTATE(t)) {
379         case THRf_R_JOINABLE:
380         case THRf_R_JOINED:
381             ThrSETSTATE(t, THRf_R_JOINED);
382             MUTEX_UNLOCK(&t->mutex);
383             break;
384         case THRf_ZOMBIE:
385             ThrSETSTATE(t, THRf_DEAD);
386             MUTEX_UNLOCK(&t->mutex);
387             remove_thread(aTHX_ t);
388             break;
389         default:
390             MUTEX_UNLOCK(&t->mutex);
391             croak("can't join with thread");
392             /* NOTREACHED */
393         }
394         JOIN(t, &av);
395
396         sv_2mortal((SV*)av);
397
398         if (SvTRUE(*av_fetch(av, 0, FALSE))) {
399             /* Could easily speed up the following if necessary */
400             for (i = 1; i <= AvFILL(av); i++)
401                 XPUSHs(*av_fetch(av, i, FALSE));
402         }
403         else {
404             STRLEN n_a;
405             char *mess = SvPV(*av_fetch(av, 1, FALSE), n_a);
406             DEBUG_S(PerlIO_printf(Perl_debug_log,
407                                   "%p: join propagating die message: %s\n",
408                                   thr, mess));
409             croak(mess);
410         }
411 #endif
412
413 void
414 detach(t)
415         Thread  t
416     CODE:
417 #ifdef USE_THREADS
418         DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: detaching %p (state %u)\n",
419                               thr, t, ThrSTATE(t)););
420         MUTEX_LOCK(&t->mutex);
421         switch (ThrSTATE(t)) {
422         case THRf_R_JOINABLE:
423             ThrSETSTATE(t, THRf_R_DETACHED);
424             /* fall through */
425         case THRf_R_DETACHED:
426             DETACH(t);
427             MUTEX_UNLOCK(&t->mutex);
428             break;
429         case THRf_ZOMBIE:
430             ThrSETSTATE(t, THRf_DEAD);
431             DETACH(t);
432             MUTEX_UNLOCK(&t->mutex);
433             remove_thread(aTHX_ t);
434             break;
435         default:
436             MUTEX_UNLOCK(&t->mutex);
437             croak("can't detach thread");
438             /* NOTREACHED */
439         }
440 #endif
441
442 void
443 equal(t1, t2)
444         Thread  t1
445         Thread  t2
446     PPCODE:
447         PUSHs((t1 == t2) ? &PL_sv_yes : &PL_sv_no);
448
449 void
450 flags(t)
451         Thread  t
452     PPCODE:
453 #ifdef USE_THREADS
454         PUSHs(sv_2mortal(newSViv(t->flags)));
455 #endif
456
457 void
458 done(t)
459         Thread  t
460     PPCODE:
461 #ifdef USE_THREADS
462         PUSHs(t->thr_done ? &PL_sv_yes : &PL_sv_no);
463 #endif
464
465 void
466 self(classname)
467         char *  classname
468     PREINIT:
469         SV *sv;
470     PPCODE:        
471 #ifdef USE_THREADS
472         sv = newSViv(thr->tid);
473         sv_magic(sv, thr->oursv, '~', 0, 0);
474         SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
475         PUSHs(sv_2mortal(sv_bless(newRV_noinc(sv),
476                                   gv_stashpv(classname, TRUE))));
477 #endif
478
479 U32
480 tid(t)
481         Thread  t
482     CODE:
483 #ifdef USE_THREADS
484         MUTEX_LOCK(&t->mutex);
485         RETVAL = t->tid;
486         MUTEX_UNLOCK(&t->mutex);
487 #else 
488         RETVAL = 0;
489 #endif
490     OUTPUT:
491         RETVAL
492
493 void
494 DESTROY(t)
495         SV *    t
496     PPCODE:
497         PUSHs(t ? &PL_sv_yes : &PL_sv_no);
498
499 void
500 yield()
501     CODE:
502 {
503 #ifdef USE_THREADS
504         YIELD;
505 #endif
506 }
507
508 void
509 cond_wait(sv)
510         SV *    sv
511         MAGIC * mg = NO_INIT
512 CODE:                       
513 #ifdef USE_THREADS
514         if (SvROK(sv))
515             sv = SvRV(sv);
516
517         mg = condpair_magic(sv);
518         DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_wait %p\n", thr, sv));
519         MUTEX_LOCK(MgMUTEXP(mg));
520         if (MgOWNER(mg) != thr) {
521             MUTEX_UNLOCK(MgMUTEXP(mg));
522             croak("cond_wait for lock that we don't own\n");
523         }
524         MgOWNER(mg) = 0;
525         COND_SIGNAL(MgOWNERCONDP(mg));
526         COND_WAIT(MgCONDP(mg), MgMUTEXP(mg));
527         while (MgOWNER(mg))
528             COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
529         MgOWNER(mg) = thr;
530         MUTEX_UNLOCK(MgMUTEXP(mg));
531 #endif
532
533 void
534 cond_signal(sv)
535         SV *    sv
536         MAGIC * mg = NO_INIT
537 CODE:
538 #ifdef USE_THREADS
539         if (SvROK(sv))
540             sv = SvRV(sv);
541
542         mg = condpair_magic(sv);
543         DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_signal %p\n",thr,sv));
544         MUTEX_LOCK(MgMUTEXP(mg));
545         if (MgOWNER(mg) != thr) {
546             MUTEX_UNLOCK(MgMUTEXP(mg));
547             croak("cond_signal for lock that we don't own\n");
548         }
549         COND_SIGNAL(MgCONDP(mg));
550         MUTEX_UNLOCK(MgMUTEXP(mg));
551 #endif
552
553 void
554 cond_broadcast(sv)
555         SV *    sv
556         MAGIC * mg = NO_INIT
557 CODE: 
558 #ifdef USE_THREADS
559         if (SvROK(sv))
560             sv = SvRV(sv);
561
562         mg = condpair_magic(sv);
563         DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: cond_broadcast %p\n",
564                               thr, sv));
565         MUTEX_LOCK(MgMUTEXP(mg));
566         if (MgOWNER(mg) != thr) {
567             MUTEX_UNLOCK(MgMUTEXP(mg));
568             croak("cond_broadcast for lock that we don't own\n");
569         }
570         COND_BROADCAST(MgCONDP(mg));
571         MUTEX_UNLOCK(MgMUTEXP(mg));
572 #endif
573
574 void
575 list(classname)
576         char *  classname
577     PREINIT:
578         Thread  t;
579         AV *    av;
580         SV **   svp;
581         int     n = 0;
582     PPCODE:
583 #ifdef USE_THREADS
584         av = newAV();
585         /*
586          * Iterate until we have enough dynamic storage for all threads.
587          * We mustn't do any allocation while holding threads_mutex though.
588          */
589         MUTEX_LOCK(&PL_threads_mutex);
590         do {
591             n = PL_nthreads;
592             MUTEX_UNLOCK(&PL_threads_mutex);
593             if (AvFILL(av) < n - 1) {
594                 int i = AvFILL(av);
595                 for (i = AvFILL(av); i < n - 1; i++) {
596                     SV *sv = newSViv(0);        /* fill in tid later */
597                     sv_magic(sv, 0, '~', 0, 0); /* fill in other magic later */
598                     av_push(av, sv_bless(newRV_noinc(sv),
599                                          gv_stashpv(classname, TRUE)));
600         
601                 }
602             }
603             MUTEX_LOCK(&PL_threads_mutex);
604         } while (n < PL_nthreads);
605         n = PL_nthreads;        /* Get the final correct value */
606
607         /*
608          * At this point, there's enough room to fill in av.
609          * Note that we are holding threads_mutex so the list
610          * won't change out from under us but all the remaining
611          * processing is "fast" (no blocking, malloc etc.)
612          */
613         t = thr;
614         svp = AvARRAY(av);
615         do {
616             SV *sv = (SV*)SvRV(*svp);
617             sv_setiv(sv, t->tid);
618             SvMAGIC(sv)->mg_obj = SvREFCNT_inc(t->oursv);
619             SvMAGIC(sv)->mg_flags |= MGf_REFCOUNTED;
620             SvMAGIC(sv)->mg_private = Thread_MAGIC_SIGNATURE;
621             t = t->next;
622             svp++;
623         } while (t != thr);
624         /*  */
625         MUTEX_UNLOCK(&PL_threads_mutex);
626         /* Truncate any unneeded slots in av */
627         av_fill(av, n - 1);
628         /* Finally, push all the new objects onto the stack and drop av */
629         EXTEND(SP, n);
630         for (svp = AvARRAY(av); n > 0; n--, svp++)
631             PUSHs(*svp);
632         (void)sv_2mortal((SV*)av);
633 #endif
634
635
636 MODULE = Thread         PACKAGE = Thread::Signal
637
638 void
639 kill_sighandler_thread()
640     PPCODE:
641         write(sig_pipe[1], "\0", 1);
642         PUSHs(&PL_sv_yes);
643
644 void
645 init_thread_signals()
646     PPCODE:
647         PL_sighandlerp = handle_thread_signal;
648         if (pipe(sig_pipe) == -1)
649             XSRETURN_UNDEF;
650         PUSHs(&PL_sv_yes);
651
652 void
653 await_signal()
654     PREINIT:
655         unsigned char c;
656         SSize_t ret;
657     CODE:
658         do {
659             ret = read(sig_pipe[0], &c, 1);
660         } while (ret == -1 && errno == EINTR);
661         if (ret == -1)
662             croak("panic: await_signal");
663         ST(0) = sv_newmortal();
664         if (ret)
665             sv_setsv(ST(0), c ? PL_psig_ptr[c] : &PL_sv_no);
666         DEBUG_S(PerlIO_printf(Perl_debug_log,
667                               "await_signal returning %s\n", SvPEEK(ST(0))););
668
669 MODULE = Thread         PACKAGE = Thread::Specific
670
671 void
672 data(classname = "Thread::Specific")
673         char *  classname
674     PPCODE:
675 #ifdef USE_THREADS
676         if (AvFILL(thr->specific) == -1) {
677             GV *gv = gv_fetchpv("Thread::Specific::FIELDS", TRUE, SVt_PVHV);
678             av_store(thr->specific, 0, newRV((SV*)GvHV(gv)));
679         }
680         XPUSHs(sv_bless(newRV((SV*)thr->specific),gv_stashpv(classname,TRUE)));
681 #endif