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