4ecb48873d7bdf453e800448720fd6d6c47ed9b5
[p5sagit/p5-mst-13.2.git] / ext / threads / threads.xs
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5
6 #ifdef USE_ITHREADS
7
8
9 #ifdef WIN32
10 #include <windows.h>
11 #include <win32thread.h>
12 #else
13 #ifdef OS2
14 typedef perl_os_thread pthread_t;
15 #else
16 #include <pthread.h>
17 #endif
18 #include <thread.h>
19 #define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
20 #ifdef OLD_PTHREADS_API
21 #define PERL_THREAD_DETACH(t) pthread_detach(&(t))
22 #else
23 #define PERL_THREAD_DETACH(t) pthread_detach((t))
24 #endif  /* OLD_PTHREADS_API */
25 #endif
26
27
28
29
30 /* Values for 'state' member */
31 #define PERL_ITHR_JOINABLE              0
32 #define PERL_ITHR_DETACHED              1
33 #define PERL_ITHR_FINISHED              4
34 #define PERL_ITHR_JOINED                2
35
36 typedef struct ithread_s {
37     struct ithread_s *next;     /* Next thread in the list */
38     struct ithread_s *prev;     /* Prev thread in the list */
39     PerlInterpreter *interp;    /* The threads interpreter */
40     I32 tid;                    /* Threads module's thread id */
41     perl_mutex mutex;           /* Mutex for updating things in this struct */
42     I32 count;                  /* How many SVs have a reference to us */
43     signed char state;          /* Are we detached ? */
44     int gimme;                  /* Context of create */
45     SV* init_function;          /* Code to run */
46     SV* params;                 /* Args to pass function */
47 #ifdef WIN32
48         DWORD   thr;            /* OS's idea if thread id */
49         HANDLE handle;          /* OS's waitable handle */
50 #else
51         pthread_t thr;          /* OS's handle for the thread */
52 #endif
53 } ithread;
54
55 #define MY_CXT_KEY "threads::_guts" XS_VERSION
56
57 typedef struct {
58     ithread *thread;
59 } my_cxt_t;
60
61 START_MY_CXT
62
63
64 ithread *threads;
65
66 /* Macros to supply the aTHX_ in an embed.h like manner */
67 #define ithread_join(thread)            Perl_ithread_join(aTHX_ thread)
68 #define ithread_DESTROY(thread)         Perl_ithread_DESTROY(aTHX_ thread)
69 #define ithread_CLONE(thread)           Perl_ithread_CLONE(aTHX_ thread)
70 #define ithread_detach(thread)          Perl_ithread_detach(aTHX_ thread)
71 #define ithread_tid(thread)             ((thread)->tid)
72 #define ithread_yield(thread)           (YIELD);
73
74 static perl_mutex create_destruct_mutex;  /* protects the creation and destruction of threads*/
75
76 I32 tid_counter = 0;
77 I32 known_threads = 0;
78 I32 active_threads = 0;
79
80
81 void Perl_ithread_set (pTHX_ ithread* thread)
82 {
83     dMY_CXT;
84     MY_CXT.thread = thread;
85 }
86
87 ithread* Perl_ithread_get (pTHX) {
88     dMY_CXT;
89     return MY_CXT.thread;
90 }
91
92
93 /* free any data (such as the perl interpreter) attached to an
94  * ithread structure. This is a bit like undef on SVs, where the SV
95  * isn't freed, but the PVX is.
96  * Must be called with thread->mutex already held
97  */
98
99 static void
100 S_ithread_clear(pTHX_ ithread* thread)
101 {
102     PerlInterpreter *interp;
103     assert(thread->state & PERL_ITHR_FINISHED &&
104             (thread->state & PERL_ITHR_DETACHED ||
105             thread->state & PERL_ITHR_JOINED));
106
107     interp = thread->interp;
108     if (interp) {
109         dTHXa(interp);
110         ithread* current_thread;
111 #ifdef OEMVS
112         void *ptr;
113 #endif
114         PERL_SET_CONTEXT(interp);
115         current_thread = Perl_ithread_get(aTHX);
116         Perl_ithread_set(aTHX_ thread);
117         
118         SvREFCNT_dec(thread->params);
119
120         thread->params = Nullsv;
121         perl_destruct(interp);
122         thread->interp = NULL;
123     }
124     if (interp)
125         perl_free(interp);
126     PERL_SET_CONTEXT(aTHX);
127 }
128
129
130 /*
131  *  free an ithread structure and any attached data if its count == 0
132  */
133 void
134 Perl_ithread_destruct (pTHX_ ithread* thread, const char *why)
135 {
136         MUTEX_LOCK(&thread->mutex);
137         if (!thread->next) {
138             MUTEX_UNLOCK(&thread->mutex);
139             Perl_croak(aTHX_ "panic: destruct destroyed thread %p (%s)",thread, why);
140         }
141         if (thread->count != 0) {
142                 MUTEX_UNLOCK(&thread->mutex);
143                 return;
144         }
145         MUTEX_LOCK(&create_destruct_mutex);
146         /* Remove from circular list of threads */
147         if (thread->next == thread) {
148             /* last one should never get here ? */
149             threads = NULL;
150         }
151         else {
152             thread->next->prev = thread->prev;
153             thread->prev->next = thread->next;
154             if (threads == thread) {
155                 threads = thread->next;
156             }
157             thread->next = NULL;
158             thread->prev = NULL;
159         }
160         known_threads--;
161         assert( known_threads >= 0 );
162 #if 0
163         Perl_warn(aTHX_ "destruct %d @ %p by %p now %d",
164                   thread->tid,thread->interp,aTHX, known_threads);
165 #endif
166         MUTEX_UNLOCK(&create_destruct_mutex);
167         /* Thread is now disowned */
168
169         S_ithread_clear(aTHX_ thread);
170         aTHX = PL_curinterp;
171         MUTEX_UNLOCK(&thread->mutex);
172         MUTEX_DESTROY(&thread->mutex);
173 #ifdef WIN32
174         if (thread->handle)
175             CloseHandle(thread->handle);
176         thread->handle = 0;
177 #endif
178         PerlMemShared_free(thread);
179 }
180
181 int
182 Perl_ithread_hook(pTHX)
183 {
184     int veto_cleanup = 0;
185     MUTEX_LOCK(&create_destruct_mutex);
186     if (aTHX == PL_curinterp && active_threads != 1) {
187         if (ckWARN_d(WARN_THREADS))
188             Perl_warn(aTHX_ "A thread exited while %" IVdf " threads were running",
189                                                       (IV)active_threads);
190         veto_cleanup = 1;
191     }
192     MUTEX_UNLOCK(&create_destruct_mutex);
193     return veto_cleanup;
194 }
195
196 void
197 Perl_ithread_detach(pTHX_ ithread *thread)
198 {
199     MUTEX_LOCK(&thread->mutex);
200     if (!(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) {
201         thread->state |= PERL_ITHR_DETACHED;
202 #ifdef WIN32
203         CloseHandle(thread->handle);
204         thread->handle = 0;
205 #else
206         PERL_THREAD_DETACH(thread->thr);
207 #endif
208     }
209     if ((thread->state & PERL_ITHR_FINISHED) &&
210         (thread->state & PERL_ITHR_DETACHED)) {
211         MUTEX_UNLOCK(&thread->mutex);
212         Perl_ithread_destruct(aTHX_ thread, "detach");
213     }
214     else {
215         MUTEX_UNLOCK(&thread->mutex);
216     }
217 }
218
219 /* MAGIC (in mg.h sense) hooks */
220
221 int
222 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
223 {
224     ithread *thread = (ithread *) mg->mg_ptr;
225     SvIV_set(sv, PTR2IV(thread));
226     SvIOK_on(sv);
227     return 0;
228 }
229
230 int
231 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
232 {
233     ithread *thread = (ithread *) mg->mg_ptr;
234     MUTEX_LOCK(&thread->mutex);
235     thread->count--;
236     if (thread->count == 0) {
237        if(thread->state & PERL_ITHR_FINISHED &&
238           (thread->state & PERL_ITHR_DETACHED ||
239            thread->state & PERL_ITHR_JOINED))
240        {
241             MUTEX_UNLOCK(&thread->mutex);
242             Perl_ithread_destruct(aTHX_ thread, "no reference");
243        }
244        else {
245             MUTEX_UNLOCK(&thread->mutex);
246        }    
247     }
248     else {
249         MUTEX_UNLOCK(&thread->mutex);
250     }
251     return 0;
252 }
253
254 int
255 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
256 {
257     ithread *thread = (ithread *) mg->mg_ptr;
258     MUTEX_LOCK(&thread->mutex);
259     thread->count++;
260     MUTEX_UNLOCK(&thread->mutex);
261     return 0;
262 }
263
264 MGVTBL ithread_vtbl = {
265  ithread_mg_get,        /* get */
266  0,                     /* set */
267  0,                     /* len */
268  0,                     /* clear */
269  ithread_mg_free,       /* free */
270  0,                     /* copy */
271  ithread_mg_dup         /* dup */
272 };
273
274
275 /*
276  *      Starts executing the thread. Needs to clean up memory a tad better.
277  *      Passed as the C level function to run in the new thread
278  */
279
280 #ifdef WIN32
281 THREAD_RET_TYPE
282 Perl_ithread_run(LPVOID arg) {
283 #else
284 void*
285 Perl_ithread_run(void * arg) {
286 #endif
287         ithread* thread = (ithread*) arg;
288         dTHXa(thread->interp);
289         PERL_SET_CONTEXT(thread->interp);
290         Perl_ithread_set(aTHX_ thread);
291
292 #if 0
293         /* Far from clear messing with ->thr child-side is a good idea */
294         MUTEX_LOCK(&thread->mutex);
295 #ifdef WIN32
296         thread->thr = GetCurrentThreadId();
297 #else
298         thread->thr = pthread_self();
299 #endif
300         MUTEX_UNLOCK(&thread->mutex);
301 #endif
302
303         PL_perl_destruct_level = 2;
304
305         {
306                 AV* params = (AV*) SvRV(thread->params);
307                 I32 len = av_len(params)+1;
308                 int i;
309                 dSP;
310                 ENTER;
311                 SAVETMPS;
312                 PUSHMARK(SP);
313                 for(i = 0; i < len; i++) {
314                     XPUSHs(av_shift(params));
315                 }
316                 PUTBACK;
317                 len = call_sv(thread->init_function, thread->gimme|G_EVAL);
318
319                 SPAGAIN;
320                 for (i=len-1; i >= 0; i--) {
321                   SV *sv = POPs;
322                   av_store(params, i, SvREFCNT_inc(sv));
323                 }
324                 if (SvTRUE(ERRSV) && ckWARN_d(WARN_THREADS)) {
325                     Perl_warn(aTHX_ "thread failed to start: %" SVf, ERRSV);
326                 }
327                 FREETMPS;
328                 LEAVE;
329                 SvREFCNT_dec(thread->init_function);
330         }
331
332         PerlIO_flush((PerlIO*)NULL);
333         MUTEX_LOCK(&thread->mutex);
334         thread->state |= PERL_ITHR_FINISHED;
335
336         if (thread->state & PERL_ITHR_DETACHED) {
337                 MUTEX_UNLOCK(&thread->mutex);
338                 Perl_ithread_destruct(aTHX_ thread, "detached finish");
339         } else {
340                 MUTEX_UNLOCK(&thread->mutex);
341         }
342         MUTEX_LOCK(&create_destruct_mutex);
343         active_threads--;
344         assert( active_threads >= 0 );
345         MUTEX_UNLOCK(&create_destruct_mutex);
346
347 #ifdef WIN32
348         return (DWORD)0;
349 #else
350         return 0;
351 #endif
352 }
353
354 SV *
355 ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
356 {
357     SV *sv;
358     MAGIC *mg;
359     if (inc) {
360         MUTEX_LOCK(&thread->mutex);
361         thread->count++;
362         MUTEX_UNLOCK(&thread->mutex);
363     }
364     if (!obj)
365      obj = newSV(0);
366     sv = newSVrv(obj,classname);
367     sv_setiv(sv,PTR2IV(thread));
368     mg = sv_magicext(sv,Nullsv,PERL_MAGIC_shared_scalar,&ithread_vtbl,(char *)thread,0);
369     mg->mg_flags |= MGf_DUP;
370     SvREADONLY_on(sv);
371     return obj;
372 }
373
374 ithread *
375 SV_to_ithread(pTHX_ SV *sv)
376 {
377     if (SvROK(sv))
378      {
379       return INT2PTR(ithread*, SvIV(SvRV(sv)));
380      }
381     else
382      {
383       return Perl_ithread_get(aTHX);
384      }
385 }
386
387 /*
388  * ithread->create(); ( aka ithread->new() )
389  * Called in context of parent thread
390  */
391
392 SV *
393 Perl_ithread_create(pTHX_ SV *obj, char* classname, SV* init_function, SV* params)
394 {
395         ithread*        thread;
396         CLONE_PARAMS    clone_param;
397         ithread*        current_thread = Perl_ithread_get(aTHX);
398
399         SV**            tmps_tmp = PL_tmps_stack;
400         I32             tmps_ix  = PL_tmps_ix;
401 #ifndef WIN32
402         int             failure;
403         const char*     panic = NULL;
404 #endif
405
406
407         MUTEX_LOCK(&create_destruct_mutex);
408         thread = (ithread *) PerlMemShared_malloc(sizeof(ithread));
409         if (!thread) {  
410             MUTEX_UNLOCK(&create_destruct_mutex);
411             PerlLIO_write(PerlIO_fileno(Perl_error_log),
412                           PL_no_mem, strlen(PL_no_mem));
413             my_exit(1);
414         }
415         Zero(thread,1,ithread);
416         thread->next = threads;
417         thread->prev = threads->prev;
418         threads->prev = thread;
419         thread->prev->next = thread;
420         /* Set count to 1 immediately in case thread exits before
421          * we return to caller !
422          */
423         thread->count = 1;
424         MUTEX_INIT(&thread->mutex);
425         thread->tid = tid_counter++;
426         thread->gimme = GIMME_V;
427
428         /* "Clone" our interpreter into the thread's interpreter
429          * This gives thread access to "static data" and code.
430          */
431
432         PerlIO_flush((PerlIO*)NULL);
433         Perl_ithread_set(aTHX_ thread);
434
435         SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct
436                                       value */
437         PL_srand_called = FALSE; /* Set it to false so we can detect
438                                     if it gets set during the clone */
439
440 #ifdef WIN32
441         thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
442 #else
443         thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
444 #endif
445         /* perl_clone leaves us in new interpreter's context.
446            As it is tricky to spot an implicit aTHX, create a new scope
447            with aTHX matching the context for the duration of
448            our work for new interpreter.
449          */
450         {
451             dTHXa(thread->interp);
452
453             MY_CXT_CLONE;
454
455             /* Here we remove END blocks since they should only run
456                in the thread they are created
457             */
458             SvREFCNT_dec(PL_endav);
459             PL_endav = newAV();
460             clone_param.flags = 0;
461             thread->init_function = sv_dup(init_function, &clone_param);
462             if (SvREFCNT(thread->init_function) == 0) {
463                 SvREFCNT_inc(thread->init_function);
464             }
465             
466
467
468             thread->params = sv_dup(params, &clone_param);
469             SvREFCNT_inc(thread->params);
470
471
472             /* The code below checks that anything living on
473                the tmps stack and has been cloned (so it lives in the
474                ptr_table) has a refcount higher than 0
475
476                If the refcount is 0 it means that a something on the
477                stack/context was holding a reference to it and
478                since we init_stacks() in perl_clone that won't get
479                cleaned and we will get a leaked scalar.
480                The reason it was cloned was that it lived on the
481                @_ stack.
482
483                Example of this can be found in bugreport 15837
484                where calls in the parameter list end up as a temp
485
486                One could argue that this fix should be in perl_clone
487             */
488                
489
490             while (tmps_ix > 0) { 
491               SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
492               tmps_ix--;
493               if (sv && SvREFCNT(sv) == 0) {
494                 SvREFCNT_inc(sv);
495                 SvREFCNT_dec(sv);
496               }
497             }
498             
499
500
501             SvTEMP_off(thread->init_function);
502             ptr_table_free(PL_ptr_table);
503             PL_ptr_table = NULL;
504             PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
505         }
506         Perl_ithread_set(aTHX_ current_thread);
507         PERL_SET_CONTEXT(aTHX);
508
509         /* Start the thread */
510
511 #ifdef WIN32
512         thread->handle = CreateThread(NULL, 0, Perl_ithread_run,
513                         (LPVOID)thread, 0, &thread->thr);
514 #else
515         {
516           static pthread_attr_t attr;
517           static int attr_inited = 0;
518           static int attr_joinable = PTHREAD_CREATE_JOINABLE;
519           if (!attr_inited) {
520             attr_inited = 1;
521             pthread_attr_init(&attr);
522           }
523 #  ifdef PTHREAD_ATTR_SETDETACHSTATE
524             PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
525 #  endif
526 #  ifdef THREAD_CREATE_NEEDS_STACK
527             if(pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK))
528               panic = "panic: pthread_attr_setstacksize failed";
529 #  endif
530
531 #ifdef OLD_PTHREADS_API
532             failure
533               = panic ? 1 : pthread_create( &thread->thr, attr,
534                                             Perl_ithread_run, (void *)thread);
535 #else
536 #  if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
537           pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
538 #  endif
539           failure
540             = panic ? 1 : pthread_create( &thread->thr, &attr,
541                                           Perl_ithread_run, (void *)thread);
542 #endif
543         }
544 #endif
545         known_threads++;
546         if (
547 #ifdef WIN32
548             thread->handle == NULL
549 #else
550             failure
551 #endif
552             ) {
553           MUTEX_UNLOCK(&create_destruct_mutex);
554           sv_2mortal(params);
555           Perl_ithread_destruct(aTHX_ thread, "create failed");
556 #ifndef WIN32
557           if (panic)
558             Perl_croak(aTHX_ panic);
559 #endif
560           return &PL_sv_undef;
561         }
562         active_threads++;
563         MUTEX_UNLOCK(&create_destruct_mutex);
564         sv_2mortal(params);
565
566         return ithread_to_SV(aTHX_ obj, thread, classname, FALSE);
567 }
568
569 SV*
570 Perl_ithread_self (pTHX_ SV *obj, char* Class)
571 {
572    ithread *thread = Perl_ithread_get(aTHX);
573    if (thread)
574         return ithread_to_SV(aTHX_ obj, thread, Class, TRUE);
575    else
576         Perl_croak(aTHX_ "panic: cannot find thread data");
577    return NULL; /* silence compiler warning */
578 }
579
580 /*
581  * Joins the thread this code needs to take the returnvalue from the
582  * call_sv and send it back
583  */
584
585 void
586 Perl_ithread_CLONE(pTHX_ SV *obj)
587 {
588     if (SvROK(obj)) {
589         ithread *thread = SV_to_ithread(aTHX_ obj);
590     }
591     else if (ckWARN_d(WARN_THREADS)) {
592         Perl_warn(aTHX_ "CLONE %" SVf,obj);
593     }
594 }
595
596 AV*
597 Perl_ithread_join(pTHX_ SV *obj)
598 {
599     ithread *thread = SV_to_ithread(aTHX_ obj);
600     MUTEX_LOCK(&thread->mutex);
601     if (thread->state & PERL_ITHR_DETACHED) {
602         MUTEX_UNLOCK(&thread->mutex);
603         Perl_croak(aTHX_ "Cannot join a detached thread");
604     }
605     else if (thread->state & PERL_ITHR_JOINED) {
606         MUTEX_UNLOCK(&thread->mutex);
607         Perl_croak(aTHX_ "Thread already joined");
608     }
609     else {
610         AV* retparam;
611 #ifdef WIN32
612         DWORD waitcode;
613 #else
614         void *retval;
615 #endif
616         MUTEX_UNLOCK(&thread->mutex);
617 #ifdef WIN32
618         waitcode = WaitForSingleObject(thread->handle, INFINITE);
619         CloseHandle(thread->handle);
620         thread->handle = 0;
621 #else
622         pthread_join(thread->thr,&retval);
623 #endif
624         MUTEX_LOCK(&thread->mutex);
625         
626         /* sv_dup over the args */
627         {
628           ithread*        current_thread;
629           AV* params = (AV*) SvRV(thread->params);      
630           PerlInterpreter *other_perl = thread->interp;
631           CLONE_PARAMS clone_params;
632           clone_params.stashes = newAV();
633           clone_params.flags = CLONEf_JOIN_IN;
634           PL_ptr_table = ptr_table_new();
635           current_thread = Perl_ithread_get(aTHX);
636           Perl_ithread_set(aTHX_ thread);
637           /* ensure 'meaningful' addresses retain their meaning */
638           ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
639           ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
640           ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
641
642 #if 0
643           {
644             I32 len = av_len(params)+1;
645             I32 i;
646             for(i = 0; i < len; i++) {
647               sv_dump(SvRV(AvARRAY(params)[i]));
648             }
649           }
650 #endif
651           retparam = (AV*) sv_dup((SV*)params, &clone_params);
652 #if 0
653           {
654             I32 len = av_len(retparam)+1;
655             I32 i;
656             for(i = 0; i < len; i++) {
657                 sv_dump(SvRV(AvARRAY(retparam)[i]));
658             }
659           }
660 #endif
661           Perl_ithread_set(aTHX_ current_thread);
662           SvREFCNT_dec(clone_params.stashes);
663           SvREFCNT_inc(retparam);
664           ptr_table_free(PL_ptr_table);
665           PL_ptr_table = NULL;
666
667         }
668         /* We are finished with it */
669         thread->state |= PERL_ITHR_JOINED;
670         S_ithread_clear(aTHX_ thread);
671         MUTEX_UNLOCK(&thread->mutex);
672         
673         return retparam;
674     }
675     return (AV*)NULL;
676 }
677
678 void
679 Perl_ithread_DESTROY(pTHX_ SV *sv)
680 {
681     ithread *thread = SV_to_ithread(aTHX_ sv);
682     sv_unmagic(SvRV(sv),PERL_MAGIC_shared_scalar);
683 }
684
685 #endif /* USE_ITHREADS */
686
687 MODULE = threads                PACKAGE = threads       PREFIX = ithread_
688 PROTOTYPES: DISABLE
689
690 #ifdef USE_ITHREADS
691
692 void
693 ithread_new (classname, function_to_call, ...)
694 char *  classname
695 SV *    function_to_call
696 CODE:
697 {
698     AV* params = newAV();
699     if (items > 2) {
700         int i;
701         for(i = 2; i < items ; i++) {
702             av_push(params, SvREFCNT_inc(ST(i)));
703         }
704     }
705     ST(0) = sv_2mortal(Perl_ithread_create(aTHX_ Nullsv, classname, function_to_call, newRV_noinc((SV*) params)));
706     XSRETURN(1);
707 }
708
709 void
710 ithread_list(char *classname)
711 PPCODE:
712 {
713   ithread *curr_thread;
714   MUTEX_LOCK(&create_destruct_mutex);
715   curr_thread = threads;
716   if(curr_thread->tid != 0)     
717     XPUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE)));
718   while(curr_thread) {
719     curr_thread = curr_thread->next;
720     if(curr_thread == threads)
721       break;
722     if(curr_thread->state & PERL_ITHR_DETACHED ||
723        curr_thread->state & PERL_ITHR_JOINED)
724          continue;
725      XPUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE)));
726   }     
727   MUTEX_UNLOCK(&create_destruct_mutex);
728 }
729
730
731 void
732 ithread_self(char *classname)
733 CODE:
734 {
735         ST(0) = sv_2mortal(Perl_ithread_self(aTHX_ Nullsv,classname));
736         XSRETURN(1);
737 }
738
739 int
740 ithread_tid(ithread *thread)
741
742 void
743 ithread_join(SV *obj)
744 PPCODE:
745 {
746   AV* params = Perl_ithread_join(aTHX_ obj);
747   int i;
748   I32 len = AvFILL(params);
749   for (i = 0; i <= len; i++) {
750     SV* tmp = av_shift(params);
751     XPUSHs(tmp);
752     sv_2mortal(tmp);
753   }
754   SvREFCNT_dec(params);
755 }
756
757 void
758 yield(...)
759 CODE:
760 {
761     YIELD;
762 }
763         
764
765 void
766 ithread_detach(ithread *thread)
767
768 void
769 ithread_DESTROY(SV *thread)
770
771 #endif /* USE_ITHREADS */
772
773 BOOT:
774 {
775 #ifdef USE_ITHREADS
776         MY_CXT_INIT;
777         ithread* thread;
778         PL_perl_destruct_level = 2;
779         MUTEX_INIT(&create_destruct_mutex);
780         MUTEX_LOCK(&create_destruct_mutex);
781         PL_threadhook = &Perl_ithread_hook;
782         thread  = (ithread *) PerlMemShared_malloc(sizeof(ithread));
783         if (!thread) {
784             PerlLIO_write(PerlIO_fileno(Perl_error_log),
785                           PL_no_mem, strlen(PL_no_mem));
786             my_exit(1);
787         }
788         Zero(thread,1,ithread);
789         PL_perl_destruct_level = 2;
790         MUTEX_INIT(&thread->mutex);
791         threads = thread;
792         thread->next = thread;
793         thread->prev = thread;
794         thread->interp = aTHX;
795         thread->count  = 1;  /* Immortal. */
796         thread->tid = tid_counter++;
797         known_threads++;
798         active_threads++;
799         thread->state = PERL_ITHR_DETACHED;
800 #ifdef WIN32
801         thread->thr = GetCurrentThreadId();
802 #else
803         thread->thr = pthread_self();
804 #endif
805
806         Perl_ithread_set(aTHX_ thread);
807         MUTEX_UNLOCK(&create_destruct_mutex);
808 #endif /* USE_ITHREADS */
809 }
810