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