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