More about the dangers of indent.
[p5sagit/p5-mst-13.2.git] / ext / threads / threads.xs
CommitLineData
68795e93 1#define PERL_NO_GET_CONTEXT
2#include "EXTERN.h"
3#include "perl.h"
4#include "XSUB.h"
5
73e09c8f 6#ifdef USE_ITHREADS
7
68795e93 8#ifdef WIN32
9#include <windows.h>
10#include <win32thread.h>
11#define PERL_THREAD_SETSPECIFIC(k,v) TlsSetValue(k,v)
12#define PERL_THREAD_GETSPECIFIC(k,v) v = TlsGetValue(k)
13#define PERL_THREAD_ALLOC_SPECIFIC(k) \
14STMT_START {\
15 if((k = TlsAlloc()) == TLS_OUT_OF_INDEXES) {\
16 PerlIO_printf(PerlIO_stderr(),"panic threads.h: TlsAlloc");\
17 exit(1);\
18 }\
19} STMT_END
20#else
21#include <pthread.h>
22#include <thread.h>
23
24#define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
25#ifdef OLD_PTHREADS_API
26#define PERL_THREAD_DETACH(t) pthread_detach(&(t))
27#define PERL_THREAD_GETSPECIFIC(k,v) pthread_getspecific(k,&v)
28#define PERL_THREAD_ALLOC_SPECIFIC(k) STMT_START {\
29 if(pthread_keycreate(&(k),0)) {\
30 PerlIO_printf(PerlIO_stderr(), "panic threads.h: pthread_key_create");\
31 exit(1);\
32 }\
33} STMT_END
34#else
35#define PERL_THREAD_DETACH(t) pthread_detach((t))
36#define PERL_THREAD_GETSPECIFIC(k,v) v = pthread_getspecific(k)
37#define PERL_THREAD_ALLOC_SPECIFIC(k) STMT_START {\
38 if(pthread_key_create(&(k),0)) {\
39 PerlIO_printf(PerlIO_stderr(), "panic threads.h: pthread_key_create");\
40 exit(1);\
41 }\
42} STMT_END
43#endif
44#endif
45
62375a60 46/* Values for 'state' member */
47#define PERL_ITHR_JOINABLE 0
48#define PERL_ITHR_DETACHED 1
49#define PERL_ITHR_FINISHED 4
50#define PERL_ITHR_JOINED 2
51
68795e93 52typedef struct ithread_s {
53 struct ithread_s *next; /* next thread in the list */
54 struct ithread_s *prev; /* prev thread in the list */
55 PerlInterpreter *interp; /* The threads interpreter */
56 I32 tid; /* threads module's thread id */
57 perl_mutex mutex; /* mutex for updating things in this struct */
58 I32 count; /* how many SVs have a reference to us */
62375a60 59 signed char state; /* are we detached ? */
a446a88f 60 int gimme; /* Context of create */
68795e93 61 SV* init_function; /* Code to run */
62 SV* params; /* args to pass function */
63#ifdef WIN32
64 DWORD thr; /* OS's idea if thread id */
65 HANDLE handle; /* OS's waitable handle */
66#else
67 pthread_t thr; /* OS's handle for the thread */
68#endif
69} ithread;
70
71ithread *threads;
72
73/* Macros to supply the aTHX_ in an embed.h like manner */
74#define ithread_join(thread) Perl_ithread_join(aTHX_ thread)
75#define ithread_DESTROY(thread) Perl_ithread_DESTROY(aTHX_ thread)
76#define ithread_CLONE(thread) Perl_ithread_CLONE(aTHX_ thread)
77#define ithread_detach(thread) Perl_ithread_detach(aTHX_ thread)
78#define ithread_tid(thread) ((thread)->tid)
f9dff5f5 79#define ithread_yield(thread) (YIELD);
68795e93 80
58c2ef19 81static perl_mutex create_destruct_mutex; /* protects the creation and destruction of threads*/
68795e93 82
83I32 tid_counter = 0;
62375a60 84I32 known_threads = 0;
58c2ef19 85I32 active_threads = 0;
68795e93 86perl_key self_key;
87
88/*
89 * Clear up after thread is done with
90 */
91void
62375a60 92Perl_ithread_destruct (pTHX_ ithread* thread, const char *why)
68795e93 93{
94 MUTEX_LOCK(&thread->mutex);
62375a60 95 if (!thread->next) {
96 Perl_croak(aTHX_ "panic: destruct destroyed thread %p (%s)",thread, why);
97 }
68795e93 98 if (thread->count != 0) {
99 MUTEX_UNLOCK(&thread->mutex);
d1400e48 100 return;
68795e93 101 }
58c2ef19 102 MUTEX_LOCK(&create_destruct_mutex);
68795e93 103 /* Remove from circular list of threads */
104 if (thread->next == thread) {
105 /* last one should never get here ? */
106 threads = NULL;
107 }
108 else {
f42ad631 109 thread->next->prev = thread->prev;
110 thread->prev->next = thread->next;
68795e93 111 if (threads == thread) {
112 threads = thread->next;
113 }
62375a60 114 thread->next = NULL;
115 thread->prev = NULL;
68795e93 116 }
62375a60 117 known_threads--;
118 assert( known_threads >= 0 );
ba14dd9a 119#if 0
62375a60 120 Perl_warn(aTHX_ "destruct %d @ %p by %p now %d",
121 thread->tid,thread->interp,aTHX, known_threads);
ba14dd9a 122#endif
62375a60 123 MUTEX_UNLOCK(&create_destruct_mutex);
124 /* Thread is now disowned */
68795e93 125 if (thread->interp) {
1c3adb19 126 dTHXa(thread->interp);
68795e93 127 PERL_SET_CONTEXT(thread->interp);
1c3adb19 128 SvREFCNT_dec(thread->params);
129 thread->params = Nullsv;
68795e93 130 perl_destruct(thread->interp);
131 perl_free(thread->interp);
132 thread->interp = NULL;
133 }
134 PERL_SET_CONTEXT(aTHX);
d1400e48 135 MUTEX_UNLOCK(&thread->mutex);
1c3adb19 136 MUTEX_DESTROY(&thread->mutex);
137 PerlMemShared_free(thread);
68795e93 138}
139
62375a60 140int
141Perl_ithread_hook(pTHX)
142{
143 int veto_cleanup = 0;
144 MUTEX_LOCK(&create_destruct_mutex);
145 if (aTHX == PL_curinterp && active_threads != 1) {
c133c03f 146 Perl_warn(aTHX_ "A thread exited while %" IVdf " other threads were still running",
436c6dd3 147 (IV)active_threads);
62375a60 148 veto_cleanup = 1;
149 }
150 MUTEX_UNLOCK(&create_destruct_mutex);
151 return veto_cleanup;
152}
153
154void
155Perl_ithread_detach(pTHX_ ithread *thread)
156{
157 MUTEX_LOCK(&thread->mutex);
158 if (!(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) {
159 thread->state |= PERL_ITHR_DETACHED;
160#ifdef WIN32
161 CloseHandle(thread->handle);
162 thread->handle = 0;
163#else
164 PERL_THREAD_DETACH(thread->thr);
165#endif
166 }
167 if ((thread->state & PERL_ITHR_FINISHED) &&
168 (thread->state & PERL_ITHR_DETACHED)) {
169 MUTEX_UNLOCK(&thread->mutex);
170 Perl_ithread_destruct(aTHX_ thread, "detach");
171 }
172 else {
173 MUTEX_UNLOCK(&thread->mutex);
174 }
175}
68795e93 176
177/* MAGIC (in mg.h sense) hooks */
178
179int
180ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
181{
182 ithread *thread = (ithread *) mg->mg_ptr;
183 SvIVX(sv) = PTR2IV(thread);
184 SvIOK_on(sv);
185 return 0;
186}
187
188int
189ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
190{
191 ithread *thread = (ithread *) mg->mg_ptr;
192 MUTEX_LOCK(&thread->mutex);
68795e93 193 thread->count--;
62375a60 194 if (thread->count == 0) {
1c3adb19 195 if(thread->state & PERL_ITHR_FINISHED &&
196 (thread->state & PERL_ITHR_DETACHED ||
197 thread->state & PERL_ITHR_JOINED))
198 {
199 MUTEX_UNLOCK(&thread->mutex);
200 Perl_ithread_destruct(aTHX_ thread, "no reference");
201 }
1ea20f42 202 else {
203 MUTEX_UNLOCK(&thread->mutex);
204 }
62375a60 205 }
206 else {
207 MUTEX_UNLOCK(&thread->mutex);
208 }
68795e93 209 return 0;
210}
211
212int
213ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
214{
215 ithread *thread = (ithread *) mg->mg_ptr;
216 MUTEX_LOCK(&thread->mutex);
68795e93 217 thread->count++;
218 MUTEX_UNLOCK(&thread->mutex);
219 return 0;
220}
221
222MGVTBL ithread_vtbl = {
223 ithread_mg_get, /* get */
224 0, /* set */
225 0, /* len */
226 0, /* clear */
227 ithread_mg_free, /* free */
228 0, /* copy */
229 ithread_mg_dup /* dup */
230};
231
47ba8780 232
47ba8780 233/*
b1edfb69 234 * Starts executing the thread. Needs to clean up memory a tad better.
68795e93 235 * Passed as the C level function to run in the new thread
b1edfb69 236 */
47ba8780 237
238#ifdef WIN32
68795e93 239THREAD_RET_TYPE
240Perl_ithread_run(LPVOID arg) {
47ba8780 241#else
68795e93 242void*
243Perl_ithread_run(void * arg) {
47ba8780 244#endif
5b414d21 245 ithread* thread = (ithread*) arg;
47ba8780 246 dTHXa(thread->interp);
47ba8780 247 PERL_SET_CONTEXT(thread->interp);
68795e93 248 PERL_THREAD_SETSPECIFIC(self_key,thread);
47ba8780 249
68795e93 250#if 0
251 /* Far from clear messing with ->thr child-side is a good idea */
252 MUTEX_LOCK(&thread->mutex);
47ba8780 253#ifdef WIN32
254 thread->thr = GetCurrentThreadId();
255#else
256 thread->thr = pthread_self();
257#endif
68795e93 258 MUTEX_UNLOCK(&thread->mutex);
259#endif
47ba8780 260
47ba8780 261 PL_perl_destruct_level = 2;
4f896ddc 262
47ba8780 263 {
68795e93 264 AV* params = (AV*) SvRV(thread->params);
265 I32 len = av_len(params)+1;
47ba8780 266 int i;
267 dSP;
47ba8780 268 ENTER;
269 SAVETMPS;
270 PUSHMARK(SP);
68795e93 271 for(i = 0; i < len; i++) {
272 XPUSHs(av_shift(params));
47ba8780 273 }
274 PUTBACK;
a446a88f 275 len = call_sv(thread->init_function, thread->gimme|G_EVAL);
68795e93 276 SPAGAIN;
a446a88f 277 for (i=len-1; i >= 0; i--) {
e1c44605 278 SV *sv = POPs;
279 av_store(params, i, SvREFCNT_inc(sv));
a446a88f 280 }
281 PUTBACK;
282 if (SvTRUE(ERRSV)) {
6b3c7930 283 Perl_warn(aTHX_ "thread failed to start: %" SVf, ERRSV);
a446a88f 284 }
47ba8780 285 FREETMPS;
286 LEAVE;
68795e93 287 SvREFCNT_dec(thread->init_function);
47ba8780 288 }
289
fd58862f 290 PerlIO_flush((PerlIO*)NULL);
68795e93 291 MUTEX_LOCK(&thread->mutex);
62375a60 292 thread->state |= PERL_ITHR_FINISHED;
293
294 if (thread->state & PERL_ITHR_DETACHED) {
47ba8780 295 MUTEX_UNLOCK(&thread->mutex);
62375a60 296 Perl_ithread_destruct(aTHX_ thread, "detached finish");
47ba8780 297 } else {
62375a60 298 MUTEX_UNLOCK(&thread->mutex);
299 }
91604d21 300 MUTEX_LOCK(&create_destruct_mutex);
301 active_threads--;
302 assert( active_threads >= 0 );
303 MUTEX_UNLOCK(&create_destruct_mutex);
304
47ba8780 305#ifdef WIN32
306 return (DWORD)0;
e8f2bb9a 307#else
308 return 0;
47ba8780 309#endif
68795e93 310}
311
312SV *
313ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
314{
315 SV *sv;
316 MAGIC *mg;
317 if (inc) {
318 MUTEX_LOCK(&thread->mutex);
319 thread->count++;
68795e93 320 MUTEX_UNLOCK(&thread->mutex);
321 }
322 if (!obj)
323 obj = newSV(0);
324 sv = newSVrv(obj,classname);
325 sv_setiv(sv,PTR2IV(thread));
326 mg = sv_magicext(sv,Nullsv,PERL_MAGIC_shared_scalar,&ithread_vtbl,(char *)thread,0);
327 mg->mg_flags |= MGf_DUP;
328 SvREADONLY_on(sv);
329 return obj;
330}
47ba8780 331
68795e93 332ithread *
333SV_to_ithread(pTHX_ SV *sv)
334{
335 ithread *thread;
336 if (SvROK(sv))
337 {
338 thread = INT2PTR(ithread*, SvIV(SvRV(sv)));
339 }
340 else
341 {
342 PERL_THREAD_GETSPECIFIC(self_key,thread);
343 }
344 return thread;
47ba8780 345}
346
47ba8780 347/*
68795e93 348 * iThread->create(); ( aka iThread->new() )
349 * Called in context of parent thread
b1edfb69 350 */
47ba8780 351
68795e93 352SV *
353Perl_ithread_create(pTHX_ SV *obj, char* classname, SV* init_function, SV* params)
354{
355 ithread* thread;
356 CLONE_PARAMS clone_param;
357
58c2ef19 358 MUTEX_LOCK(&create_destruct_mutex);
68795e93 359 thread = PerlMemShared_malloc(sizeof(ithread));
360 Zero(thread,1,ithread);
361 thread->next = threads;
362 thread->prev = threads->prev;
f42ad631 363 threads->prev = thread;
68795e93 364 thread->prev->next = thread;
365 /* Set count to 1 immediately in case thread exits before
366 * we return to caller !
367 */
368 thread->count = 1;
369 MUTEX_INIT(&thread->mutex);
370 thread->tid = tid_counter++;
a446a88f 371 thread->gimme = GIMME_V;
4f896ddc 372
68795e93 373 /* "Clone" our interpreter into the thread's interpreter
374 * This gives thread access to "static data" and code.
375 */
47ba8780 376
68795e93 377 PerlIO_flush((PerlIO*)NULL);
cd8c9bf8 378
47ba8780 379#ifdef WIN32
68795e93 380 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
47ba8780 381#else
68795e93 382 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
47ba8780 383#endif
ba14dd9a 384 /* perl_clone leaves us in new interpreter's context.
385 As it is tricky to spot implcit aTHX create a new scope
a446a88f 386 with aTHX matching the context for the duration of
ba14dd9a 387 our work for new interpreter.
388 */
389 {
390 dTHXa(thread->interp);
58c2ef19 391 /* Here we remove END blocks since they should only run
62375a60 392 in the thread they are created
58c2ef19 393 */
394 SvREFCNT_dec(PL_endav);
395 PL_endav = newAV();
d1400e48 396 clone_param.flags = 0;
ba14dd9a 397 thread->init_function = sv_dup(init_function, &clone_param);
398 if (SvREFCNT(thread->init_function) == 0) {
399 SvREFCNT_inc(thread->init_function);
d1400e48 400 }
ba14dd9a 401
402 thread->params = sv_dup(params, &clone_param);
403 SvREFCNT_inc(thread->params);
404 SvTEMP_off(thread->init_function);
405 ptr_table_free(PL_ptr_table);
406 PL_ptr_table = NULL;
ffb29f90 407 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
ba14dd9a 408 }
d1400e48 409
68795e93 410 PERL_SET_CONTEXT(aTHX);
47ba8780 411
68795e93 412 /* Start the thread */
47ba8780 413
414#ifdef WIN32
415
68795e93 416 thread->handle = CreateThread(NULL, 0, Perl_ithread_run,
47ba8780 417 (LPVOID)thread, 0, &thread->thr);
418
82c40bf6 419#else
fa26028c 420 {
421 static pthread_attr_t attr;
422 static int attr_inited = 0;
fa26028c 423 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
424 if (!attr_inited) {
425 attr_inited = 1;
426 pthread_attr_init(&attr);
427 }
428# ifdef PTHREAD_ATTR_SETDETACHSTATE
429 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
430# endif
3eb37d38 431# ifdef THREAD_CREATE_NEEDS_STACK
432 if(pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK))
433 croak("panic: pthread_attr_setstacksize failed");
434# endif
435
3ad0b7d6 436#ifdef OLD_PTHREADS_API
68795e93 437 pthread_create( &thread->thr, attr, Perl_ithread_run, (void *)thread);
47ba8780 438#else
68795e93 439 pthread_create( &thread->thr, &attr, Perl_ithread_run, (void *)thread);
47ba8780 440#endif
3ad0b7d6 441 }
82c40bf6 442#endif
62375a60 443 known_threads++;
58c2ef19 444 active_threads++;
445 MUTEX_UNLOCK(&create_destruct_mutex);
95393226 446 sv_2mortal(params);
68795e93 447 return ithread_to_SV(aTHX_ obj, thread, classname, FALSE);
448}
47ba8780 449
68795e93 450SV*
451Perl_ithread_self (pTHX_ SV *obj, char* Class)
452{
453 ithread *thread;
454 PERL_THREAD_GETSPECIFIC(self_key,thread);
455 return ithread_to_SV(aTHX_ obj, thread, Class, TRUE);
47ba8780 456}
457
458/*
e1c44605 459 * Joins the thread this code needs to take the returnvalue from the
68795e93 460 * call_sv and send it back
b1edfb69 461 */
47ba8780 462
68795e93 463void
464Perl_ithread_CLONE(pTHX_ SV *obj)
465{
466 if (SvROK(obj))
467 {
468 ithread *thread = SV_to_ithread(aTHX_ obj);
469 }
470 else
471 {
436c6dd3 472 Perl_warn(aTHX_ "CLONE %" SVf,obj);
68795e93 473 }
47ba8780 474}
475
62375a60 476AV*
68795e93 477Perl_ithread_join(pTHX_ SV *obj)
478{
479 ithread *thread = SV_to_ithread(aTHX_ obj);
480 MUTEX_LOCK(&thread->mutex);
62375a60 481 if (thread->state & PERL_ITHR_DETACHED) {
a446a88f 482 MUTEX_UNLOCK(&thread->mutex);
483 Perl_croak(aTHX_ "Cannot join a detached thread");
484 }
62375a60 485 else if (thread->state & PERL_ITHR_JOINED) {
a446a88f 486 MUTEX_UNLOCK(&thread->mutex);
487 Perl_croak(aTHX_ "Thread already joined");
488 }
489 else {
e1c44605 490 AV* retparam;
47ba8780 491#ifdef WIN32
492 DWORD waitcode;
47ba8780 493#else
494 void *retval;
47ba8780 495#endif
47ba8780 496 MUTEX_UNLOCK(&thread->mutex);
68795e93 497#ifdef WIN32
498 waitcode = WaitForSingleObject(thread->handle, INFINITE);
499#else
500 pthread_join(thread->thr,&retval);
501#endif
47ba8780 502 MUTEX_LOCK(&thread->mutex);
e1c44605 503
62375a60 504 /* sv_dup over the args */
e1c44605 505 {
62375a60 506 AV* params = (AV*) SvRV(thread->params);
e1c44605 507 CLONE_PARAMS clone_params;
3275ba96 508 clone_params.stashes = newAV();
e1c44605 509 PL_ptr_table = ptr_table_new();
510 retparam = (AV*) sv_dup((SV*)params, &clone_params);
3275ba96 511 SvREFCNT_dec(clone_params.stashes);
e1c44605 512 SvREFCNT_inc(retparam);
513 ptr_table_free(PL_ptr_table);
514 PL_ptr_table = NULL;
515
516 }
a446a88f 517 /* We have finished with it */
62375a60 518 thread->state |= PERL_ITHR_JOINED;
47ba8780 519 MUTEX_UNLOCK(&thread->mutex);
9684265f 520 sv_unmagic(SvRV(obj),PERL_MAGIC_shared_scalar);
e1c44605 521 return retparam;
68795e93 522 }
e1c44605 523 return (AV*)NULL;
47ba8780 524}
525
68795e93 526void
68795e93 527Perl_ithread_DESTROY(pTHX_ SV *sv)
528{
529 ithread *thread = SV_to_ithread(aTHX_ sv);
68795e93 530 sv_unmagic(SvRV(sv),PERL_MAGIC_shared_scalar);
531}
8222d950 532
73e09c8f 533#endif /* USE_ITHREADS */
e1c44605 534
68795e93 535MODULE = threads PACKAGE = threads PREFIX = ithread_
536PROTOTYPES: DISABLE
8222d950 537
73e09c8f 538#ifdef USE_ITHREADS
539
68795e93 540void
541ithread_new (classname, function_to_call, ...)
542char * classname
543SV * function_to_call
544CODE:
545{
546 AV* params = newAV();
547 if (items > 2) {
548 int i;
549 for(i = 2; i < items ; i++) {
95393226 550 av_push(params, SvREFCNT_inc(ST(i)));
68795e93 551 }
552 }
553 ST(0) = sv_2mortal(Perl_ithread_create(aTHX_ Nullsv, classname, function_to_call, newRV_noinc((SV*) params)));
554 XSRETURN(1);
555}
8222d950 556
68795e93 557void
678a9b6c 558ithread_list(char *classname)
559PPCODE:
560{
561 ithread *curr_thread;
562 MUTEX_LOCK(&create_destruct_mutex);
563 curr_thread = threads;
6794f985 564 PUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE)));
678a9b6c 565 while(curr_thread) {
678a9b6c 566 curr_thread = curr_thread->next;
567 if(curr_thread == threads)
568 break;
6794f985 569 if(curr_thread->state & PERL_ITHR_DETACHED ||
570 curr_thread->state & PERL_ITHR_JOINED)
571 continue;
572 PUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE)));
678a9b6c 573 }
574 MUTEX_UNLOCK(&create_destruct_mutex);
575}
576
577
578void
68795e93 579ithread_self(char *classname)
580CODE:
581{
582 ST(0) = sv_2mortal(Perl_ithread_self(aTHX_ Nullsv,classname));
583 XSRETURN(1);
584}
47ba8780 585
586int
68795e93 587ithread_tid(ithread *thread)
47ba8780 588
589void
68795e93 590ithread_join(SV *obj)
e1c44605 591PPCODE:
592{
593 AV* params = Perl_ithread_join(aTHX_ obj);
594 int i;
595 I32 len = AvFILL(params);
596 for (i = 0; i <= len; i++) {
1c3adb19 597 SV* tmp = av_shift(params);
598 XPUSHs(tmp);
599 sv_2mortal(tmp);
e1c44605 600 }
601 SvREFCNT_dec(params);
602}
603
f9dff5f5 604void
9d7debe1 605yield(...)
70f2e746 606CODE:
607{
608 YIELD;
609}
610
47ba8780 611
612void
68795e93 613ithread_detach(ithread *thread)
47ba8780 614
47ba8780 615void
68795e93 616ithread_DESTROY(SV *thread)
617
73e09c8f 618#endif /* USE_ITHREADS */
619
68795e93 620BOOT:
621{
73e09c8f 622#ifdef USE_ITHREADS
68795e93 623 ithread* thread;
e1c44605 624 PL_perl_destruct_level = 2;
68795e93 625 PERL_THREAD_ALLOC_SPECIFIC(self_key);
58c2ef19 626 MUTEX_INIT(&create_destruct_mutex);
627 MUTEX_LOCK(&create_destruct_mutex);
62375a60 628 PL_threadhook = &Perl_ithread_hook;
68795e93 629 thread = PerlMemShared_malloc(sizeof(ithread));
630 Zero(thread,1,ithread);
631 PL_perl_destruct_level = 2;
632 MUTEX_INIT(&thread->mutex);
633 threads = thread;
634 thread->next = thread;
635 thread->prev = thread;
636 thread->interp = aTHX;
637 thread->count = 1; /* imortal */
638 thread->tid = tid_counter++;
62375a60 639 known_threads++;
58c2ef19 640 active_threads++;
62375a60 641 thread->state = 1;
68795e93 642#ifdef WIN32
643 thread->thr = GetCurrentThreadId();
644#else
645 thread->thr = pthread_self();
646#endif
62375a60 647
68795e93 648 PERL_THREAD_SETSPECIFIC(self_key,thread);
58c2ef19 649 MUTEX_UNLOCK(&create_destruct_mutex);
73e09c8f 650#endif /* USE_ITHREADS */
68795e93 651}
652