Change existing uses of strlcpy()/strlcat() to use new my_strlcpy()/
[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"
4dcb9e53 5/* Workaround for XSUB.h bug under WIN32 */
6#ifdef WIN32
7# undef setjmp
8# define setjmp(x) _setjmp(x)
9#endif
0f1612a7 10#ifdef HAS_PPPORT_H
404aaa48 11# define NEED_PL_signals
0f1612a7 12# define NEED_newRV_noinc
13# define NEED_sv_2pv_nolen
14# include "ppport.h"
15# include "threads.h"
16#endif
68795e93 17
73e09c8f 18#ifdef USE_ITHREADS
19
68795e93 20#ifdef WIN32
fc04eb16 21# include <windows.h>
514612b7 22 /* Supposed to be in Winbase.h */
23# ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
24# define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
25# endif
fc04eb16 26# include <win32thread.h>
68795e93 27#else
fc04eb16 28# ifdef OS2
5c728af0 29typedef perl_os_thread pthread_t;
fc04eb16 30# else
31# include <pthread.h>
32# endif
33# include <thread.h>
34# define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
35# ifdef OLD_PTHREADS_API
36# define PERL_THREAD_DETACH(t) pthread_detach(&(t))
37# else
38# define PERL_THREAD_DETACH(t) pthread_detach((t))
39# endif
467f3f08 40#endif
d305c2c9 41#if !defined(HAS_GETPAGESIZE) && defined(I_SYS_PARAM)
42# include <sys/param.h>
43#endif
68795e93 44
62375a60 45/* Values for 'state' member */
fc04eb16 46#define PERL_ITHR_JOINABLE 0
47#define PERL_ITHR_DETACHED 1
48#define PERL_ITHR_JOINED 2
49#define PERL_ITHR_FINISHED 4
50
51typedef struct _ithread {
52 struct _ithread *next; /* Next thread in the list */
53 struct _ithread *prev; /* Prev thread in the list */
54 PerlInterpreter *interp; /* The threads interpreter */
55 UV tid; /* Threads module's thread id */
56 perl_mutex mutex; /* Mutex for updating things in this struct */
9feacc09 57 int count; /* How many SVs have a reference to us */
fc04eb16 58 int state; /* Detached, joined, finished, etc. */
59 int gimme; /* Context of create */
60 SV *init_function; /* Code to run */
61 SV *params; /* Args to pass function */
68795e93 62#ifdef WIN32
fc04eb16 63 DWORD thr; /* OS's idea if thread id */
64 HANDLE handle; /* OS's waitable handle */
68795e93 65#else
fc04eb16 66 pthread_t thr; /* OS's handle for the thread */
68795e93 67#endif
514612b7 68 IV stack_size;
68795e93 69} ithread;
70
fc04eb16 71
72/* Used by Perl interpreter for thread context switching */
628ab322 73#define MY_CXT_KEY "threads::_guts" XS_VERSION
74
75typedef struct {
76 ithread *thread;
77} my_cxt_t;
78
79START_MY_CXT
80
81
fc04eb16 82/* Linked list of all threads */
f4cc38af 83static ithread *threads;
68795e93 84
fc04eb16 85/* Protects the creation and destruction of threads*/
86static perl_mutex create_destruct_mutex;
68795e93 87
f4cc38af 88static UV tid_counter = 0;
5168baf3 89static IV joinable_threads = 0;
4dcb9e53 90static IV running_threads = 0;
91static IV detached_threads = 0;
514612b7 92#ifdef THREAD_CREATE_NEEDS_STACK
93static IV default_stack_size = THREAD_CREATE_NEEDS_STACK;
94#else
95static IV default_stack_size = 0;
96#endif
97static IV page_size = 0;
c05ae023 98
99
fc04eb16 100/* Used by Perl interpreter for thread context switching */
f4cc38af 101static void
fc04eb16 102S_ithread_set(pTHX_ ithread *thread)
c05ae023 103{
628ab322 104 dMY_CXT;
105 MY_CXT.thread = thread;
c05ae023 106}
107
fc04eb16 108static ithread *
109S_ithread_get(pTHX)
110{
628ab322 111 dMY_CXT;
fc04eb16 112 return (MY_CXT.thread);
c05ae023 113}
114
115
fc04eb16 116/* Free any data (such as the Perl interpreter) attached to an ithread
117 * structure. This is a bit like undef on SVs, where the SV isn't freed,
118 * but the PVX is. Must be called with thread->mutex already held.
2e676467 119 */
2e676467 120static void
fc04eb16 121S_ithread_clear(pTHX_ ithread *thread)
2e676467 122{
123 PerlInterpreter *interp;
fc04eb16 124
ead32952 125 assert((thread->state & PERL_ITHR_FINISHED) &&
126 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
2e676467 127
128 interp = thread->interp;
129 if (interp) {
fc04eb16 130 dTHXa(interp);
131
132 PERL_SET_CONTEXT(interp);
133 S_ithread_set(aTHX_ thread);
f2cba68d 134
fc04eb16 135 SvREFCNT_dec(thread->params);
136 thread->params = Nullsv;
2e676467 137
fc04eb16 138 perl_destruct(interp);
139 thread->interp = NULL;
2e676467 140 }
141 if (interp)
fc04eb16 142 perl_free(interp);
143
2e676467 144 PERL_SET_CONTEXT(aTHX);
145}
146
68795e93 147
fc04eb16 148/* Free an ithread structure and any attached data if its count == 0 */
bcd9ca9b 149static void
fc04eb16 150S_ithread_destruct(pTHX_ ithread *thread)
68795e93 151{
385d56e4 152#ifdef WIN32
fc04eb16 153 HANDLE handle;
385d56e4 154#endif
155
fc04eb16 156 MUTEX_LOCK(&thread->mutex);
bcd9ca9b 157
fc04eb16 158 /* Thread is still in use */
159 if (thread->count != 0) {
160 MUTEX_UNLOCK(&thread->mutex);
161 return;
162 }
9feacc09 163
fc04eb16 164 /* Main thread (0) is immortal and should never get here */
165 assert(thread->tid != 0);
166
167 /* Remove from circular list of threads */
4dcb9e53 168 MUTEX_LOCK(&create_destruct_mutex);
fc04eb16 169 thread->next->prev = thread->prev;
170 thread->prev->next = thread->next;
171 thread->next = NULL;
172 thread->prev = NULL;
173 MUTEX_UNLOCK(&create_destruct_mutex);
c2f2a82b 174
fc04eb16 175 /* Thread is now disowned */
176 S_ithread_clear(aTHX_ thread);
385d56e4 177
178#ifdef WIN32
fc04eb16 179 handle = thread->handle;
180 thread->handle = NULL;
385d56e4 181#endif
fc04eb16 182 MUTEX_UNLOCK(&thread->mutex);
183 MUTEX_DESTROY(&thread->mutex);
385d56e4 184
c7667023 185#ifdef WIN32
fc04eb16 186 if (handle)
187 CloseHandle(handle);
c7667023 188#endif
385d56e4 189
fc04eb16 190 /* Call PerlMemShared_free() in the context of the "first" interpreter
191 * per http://www.nntp.perl.org/group/perl.perl5.porters/110772
192 */
193 aTHX = PL_curinterp;
194 PerlMemShared_free(thread);
68795e93 195}
196
fc04eb16 197
198/* Called on exit */
62375a60 199int
200Perl_ithread_hook(pTHX)
201{
202 int veto_cleanup = 0;
203 MUTEX_LOCK(&create_destruct_mutex);
4dcb9e53 204 if ((aTHX == PL_curinterp) &&
205 (running_threads || joinable_threads))
206 {
fc04eb16 207 if (ckWARN_d(WARN_THREADS)) {
4dcb9e53 208 Perl_warn(aTHX_ "Perl exited with active threads:\n\t%"
209 IVdf " running and unjoined\n\t%"
210 IVdf " finished and unjoined\n\t%"
211 IVdf " running and detached\n",
212 running_threads,
213 joinable_threads,
214 detached_threads);
fc04eb16 215 }
216 veto_cleanup = 1;
62375a60 217 }
218 MUTEX_UNLOCK(&create_destruct_mutex);
fc04eb16 219 return (veto_cleanup);
62375a60 220}
221
68795e93 222
223/* MAGIC (in mg.h sense) hooks */
224
225int
226ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
227{
fc04eb16 228 ithread *thread = (ithread *)mg->mg_ptr;
45977657 229 SvIV_set(sv, PTR2IV(thread));
68795e93 230 SvIOK_on(sv);
fc04eb16 231 return (0);
68795e93 232}
233
234int
235ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
236{
f2cba68d 237 ithread *thread = (ithread *)mg->mg_ptr;
238 int cleanup;
239
68795e93 240 MUTEX_LOCK(&thread->mutex);
f2cba68d 241 cleanup = ((--thread->count == 0) &&
242 (thread->state & PERL_ITHR_FINISHED) &&
243 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
244 MUTEX_UNLOCK(&thread->mutex);
245
246 if (cleanup)
247 S_ithread_destruct(aTHX_ thread);
fc04eb16 248 return (0);
68795e93 249}
250
251int
252ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
253{
fc04eb16 254 ithread *thread = (ithread *)mg->mg_ptr;
68795e93 255 MUTEX_LOCK(&thread->mutex);
68795e93 256 thread->count++;
257 MUTEX_UNLOCK(&thread->mutex);
fc04eb16 258 return (0);
68795e93 259}
260
261MGVTBL ithread_vtbl = {
fc04eb16 262 ithread_mg_get, /* get */
263 0, /* set */
264 0, /* len */
265 0, /* clear */
266 ithread_mg_free, /* free */
267 0, /* copy */
268 ithread_mg_dup /* dup */
68795e93 269};
270
47ba8780 271
514612b7 272/* Provided default, minimum and rational stack sizes */
273static IV
274good_stack_size(pTHX_ IV stack_size)
275{
276 /* Use default stack size if no stack size specified */
277 if (! stack_size)
278 return (default_stack_size);
279
280#ifdef PTHREAD_STACK_MIN
281 /* Can't use less than minimum */
282 if (stack_size < PTHREAD_STACK_MIN) {
4dcb9e53 283 if (ckWARN(WARN_THREADS)) {
514612b7 284 Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
285 }
286 return (PTHREAD_STACK_MIN);
287 }
288#endif
289
290 /* Round up to page size boundary */
291 if (page_size <= 0) {
d305c2c9 292#if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
514612b7 293 SETERRNO(0, SS_NORMAL);
d305c2c9 294# ifdef _SC_PAGESIZE
514612b7 295 page_size = sysconf(_SC_PAGESIZE);
d305c2c9 296# else
514612b7 297 page_size = sysconf(_SC_MMAP_PAGE_SIZE);
d305c2c9 298# endif
514612b7 299 if ((long)page_size < 0) {
300 if (errno) {
301 SV * const error = get_sv("@", FALSE);
302 (void)SvUPGRADE(error, SVt_PV);
303 Perl_croak(aTHX_ "PANIC: sysconf: %s", SvPV_nolen(error));
304 } else {
305 Perl_croak(aTHX_ "PANIC: sysconf: pagesize unknown");
306 }
307 }
d305c2c9 308#else
309# ifdef HAS_GETPAGESIZE
514612b7 310 page_size = getpagesize();
514612b7 311# else
d305c2c9 312# if defined(I_SYS_PARAM) && defined(PAGESIZE)
313 page_size = PAGESIZE;
314# else
514612b7 315 page_size = 8192; /* A conservative default */
d305c2c9 316# endif
514612b7 317# endif
d305c2c9 318 if (page_size <= 0)
319 Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)page_size);
514612b7 320#endif
321 }
322 stack_size = ((stack_size + (page_size - 1)) / page_size) * page_size;
323
324 return (stack_size);
325}
326
327
fc04eb16 328/* Starts executing the thread.
329 * Passed as the C level function to run in the new thread.
b1edfb69 330 */
47ba8780 331#ifdef WIN32
f4cc38af 332static THREAD_RET_TYPE
fc04eb16 333S_ithread_run(LPVOID arg)
47ba8780 334#else
fc04eb16 335static void *
336S_ithread_run(void * arg)
47ba8780 337#endif
fc04eb16 338{
339 ithread *thread = (ithread *)arg;
340 int cleanup;
f2cba68d 341
fc04eb16 342 dTHXa(thread->interp);
343 PERL_SET_CONTEXT(thread->interp);
344 S_ithread_set(aTHX_ thread);
47ba8780 345
68795e93 346#if 0
fc04eb16 347 /* Far from clear messing with ->thr child-side is a good idea */
348 MUTEX_LOCK(&thread->mutex);
47ba8780 349#ifdef WIN32
fc04eb16 350 thread->thr = GetCurrentThreadId();
47ba8780 351#else
fc04eb16 352 thread->thr = pthread_self();
47ba8780 353#endif
fc04eb16 354 MUTEX_UNLOCK(&thread->mutex);
68795e93 355#endif
47ba8780 356
fc04eb16 357 PL_perl_destruct_level = 2;
f2cba68d 358
fc04eb16 359 {
360 AV *params = (AV *)SvRV(thread->params);
361 int len = (int)av_len(params)+1;
362 int ii;
4dcb9e53 363 int jmp_rc = 0;
364 I32 oldscope;
365
366 dJMPENV;
fc04eb16 367
368 dSP;
369 ENTER;
370 SAVETMPS;
371
372 /* Put args on the stack */
373 PUSHMARK(SP);
374 for (ii=0; ii < len; ii++) {
375 XPUSHs(av_shift(params));
376 }
377 PUTBACK;
378
4dcb9e53 379 oldscope = PL_scopestack_ix;
380 JMPENV_PUSH(jmp_rc);
381 if (jmp_rc == 0) {
382 /* Run the specified function */
383 len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
384 } else if (jmp_rc == 2) {
385 while (PL_scopestack_ix > oldscope) {
386 LEAVE;
387 }
388 }
389 JMPENV_POP;
fc04eb16 390
391 /* Remove args from stack and put back in params array */
392 SPAGAIN;
393 for (ii=len-1; ii >= 0; ii--) {
394 SV *sv = POPs;
4dcb9e53 395 if (jmp_rc == 0) {
396 av_store(params, ii, SvREFCNT_inc(sv));
397 }
fc04eb16 398 }
399
4dcb9e53 400 FREETMPS;
401 LEAVE;
402
fc04eb16 403 /* Check for failure */
404 if (SvTRUE(ERRSV) && ckWARN_d(WARN_THREADS)) {
4dcb9e53 405 oldscope = PL_scopestack_ix;
406 JMPENV_PUSH(jmp_rc);
407 if (jmp_rc == 0) {
408 Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV);
409 } else if (jmp_rc == 2) {
410 while (PL_scopestack_ix > oldscope) {
411 LEAVE;
412 }
413 }
414 JMPENV_POP;
fc04eb16 415 }
416
fc04eb16 417 /* Release function ref */
418 SvREFCNT_dec(thread->init_function);
419 thread->init_function = Nullsv;
420 }
62375a60 421
fc04eb16 422 PerlIO_flush((PerlIO *)NULL);
423
424 MUTEX_LOCK(&thread->mutex);
425 /* Mark as finished */
426 thread->state |= PERL_ITHR_FINISHED;
427 /* Cleanup if detached */
428 cleanup = (thread->state & PERL_ITHR_DETACHED);
429 MUTEX_UNLOCK(&thread->mutex);
430
4dcb9e53 431 if (cleanup) {
432 MUTEX_LOCK(&create_destruct_mutex);
433 detached_threads--;
434 MUTEX_UNLOCK(&create_destruct_mutex);
fc04eb16 435 S_ithread_destruct(aTHX_ thread);
4dcb9e53 436 } else {
437 MUTEX_LOCK(&create_destruct_mutex);
438 running_threads--;
439 joinable_threads++;
440 MUTEX_UNLOCK(&create_destruct_mutex);
5168baf3 441 }
91604d21 442
47ba8780 443#ifdef WIN32
fc04eb16 444 return ((DWORD)0);
e8f2bb9a 445#else
fc04eb16 446 return (0);
47ba8780 447#endif
68795e93 448}
449
fc04eb16 450
451/* Type conversion helper functions */
f4cc38af 452static SV *
68795e93 453ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
454{
455 SV *sv;
456 MAGIC *mg;
fc04eb16 457
68795e93 458 if (inc) {
fc04eb16 459 MUTEX_LOCK(&thread->mutex);
460 thread->count++;
461 MUTEX_UNLOCK(&thread->mutex);
462 }
463
464 if (! obj) {
465 obj = newSV(0);
68795e93 466 }
fc04eb16 467
468 sv = newSVrv(obj, classname);
469 sv_setiv(sv, PTR2IV(thread));
470 mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0);
68795e93 471 mg->mg_flags |= MGf_DUP;
472 SvREADONLY_on(sv);
fc04eb16 473
474 return (obj);
68795e93 475}
47ba8780 476
f4cc38af 477static ithread *
68795e93 478SV_to_ithread(pTHX_ SV *sv)
479{
fc04eb16 480 /* Argument is a thread */
481 if (SvROK(sv)) {
482 return (INT2PTR(ithread *, SvIV(SvRV(sv))));
483 }
484 /* Argument is classname, therefore return current thread */
485 return (S_ithread_get(aTHX));
47ba8780 486}
487
47ba8780 488
fc04eb16 489/* threads->create()
490 * Called in context of parent thread.
491 */
f4cc38af 492static SV *
fc04eb16 493S_ithread_create(
494 pTHX_ SV *obj,
495 char *classname,
496 SV *init_function,
514612b7 497 IV stack_size,
9d9ff5b1 498 int gimme,
fc04eb16 499 SV *params)
68795e93 500{
fc04eb16 501 ithread *thread;
502 CLONE_PARAMS clone_param;
503 ithread *current_thread = S_ithread_get(aTHX);
3b1c3273 504
fc04eb16 505 SV **tmps_tmp = PL_tmps_stack;
506 IV tmps_ix = PL_tmps_ix;
d94006e8 507#ifndef WIN32
fc04eb16 508 int rc_stack_size = 0;
509 int rc_thread_create = 0;
d94006e8 510#endif
3b1c3273 511
fc04eb16 512 MUTEX_LOCK(&create_destruct_mutex);
513
514 /* Allocate thread structure */
515 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
516 if (!thread) {
517 MUTEX_UNLOCK(&create_destruct_mutex);
518 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
519 my_exit(1);
520 }
521 Zero(thread, 1, ithread);
522
523 /* Add to threads list */
524 thread->next = threads;
525 thread->prev = threads->prev;
526 threads->prev = thread;
527 thread->prev->next = thread;
c05ae023 528
fc04eb16 529 /* Set count to 1 immediately in case thread exits before
530 * we return to caller!
531 */
532 thread->count = 1;
533
534 MUTEX_INIT(&thread->mutex);
535 thread->tid = tid_counter++;
514612b7 536 thread->stack_size = good_stack_size(aTHX_ stack_size);
9d9ff5b1 537 thread->gimme = gimme;
fc04eb16 538
539 /* "Clone" our interpreter into the thread's interpreter.
540 * This gives thread access to "static data" and code.
541 */
542 PerlIO_flush((PerlIO *)NULL);
543 S_ithread_set(aTHX_ thread);
544
545 SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct value */
546 PL_srand_called = FALSE; /* Set it to false so we can detect if it gets
547 set during the clone */
3b1c3273 548
47ba8780 549#ifdef WIN32
fc04eb16 550 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
47ba8780 551#else
fc04eb16 552 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
47ba8780 553#endif
47ba8780 554
fc04eb16 555 /* perl_clone() leaves us in new interpreter's context. As it is tricky
556 * to spot an implicit aTHX, create a new scope with aTHX matching the
557 * context for the duration of our work for new interpreter.
558 */
559 {
560 dTHXa(thread->interp);
561
562 MY_CXT_CLONE;
563
564 /* Here we remove END blocks since they should only run in the thread
565 * they are created
566 */
567 SvREFCNT_dec(PL_endav);
568 PL_endav = newAV();
404aaa48 569
f2e0bb91 570 if (SvPOK(init_function)) {
571 thread->init_function = newSV(0);
572 sv_copypv(thread->init_function, init_function);
573 } else {
574 clone_param.flags = 0;
575 thread->init_function = sv_dup(init_function, &clone_param);
576 if (SvREFCNT(thread->init_function) == 0) {
577 SvREFCNT_inc(thread->init_function);
578 }
fc04eb16 579 }
580
581 thread->params = sv_dup(params, &clone_param);
582 SvREFCNT_inc(thread->params);
583
584 /* The code below checks that anything living on the tmps stack and
585 * has been cloned (so it lives in the ptr_table) has a refcount
586 * higher than 0.
587 *
588 * If the refcount is 0 it means that a something on the stack/context
589 * was holding a reference to it and since we init_stacks() in
590 * perl_clone that won't get cleaned and we will get a leaked scalar.
591 * The reason it was cloned was that it lived on the @_ stack.
592 *
593 * Example of this can be found in bugreport 15837 where calls in the
594 * parameter list end up as a temp.
595 *
596 * One could argue that this fix should be in perl_clone.
597 */
598 while (tmps_ix > 0) {
599 SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
600 tmps_ix--;
601 if (sv && SvREFCNT(sv) == 0) {
602 SvREFCNT_inc(sv);
603 SvREFCNT_dec(sv);
604 }
605 }
606
607 SvTEMP_off(thread->init_function);
608 ptr_table_free(PL_ptr_table);
609 PL_ptr_table = NULL;
610 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
611 }
612 S_ithread_set(aTHX_ current_thread);
613 PERL_SET_CONTEXT(aTHX);
614
615 /* Create/start the thread */
47ba8780 616#ifdef WIN32
fc04eb16 617 thread->handle = CreateThread(NULL,
514612b7 618 (DWORD)thread->stack_size,
fc04eb16 619 S_ithread_run,
620 (LPVOID)thread,
514612b7 621 STACK_SIZE_PARAM_IS_A_RESERVATION,
fc04eb16 622 &thread->thr);
82c40bf6 623#else
fc04eb16 624 {
625 static pthread_attr_t attr;
626 static int attr_inited = 0;
627 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
628 if (! attr_inited) {
629 pthread_attr_init(&attr);
630 attr_inited = 1;
631 }
632
fa26028c 633# ifdef PTHREAD_ATTR_SETDETACHSTATE
fc04eb16 634 /* Threads start out joinable */
635 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
fa26028c 636# endif
fc04eb16 637
514612b7 638# ifdef _POSIX_THREAD_ATTR_STACKSIZE
fc04eb16 639 /* Set thread's stack size */
514612b7 640 if (thread->stack_size > 0) {
641 rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
642 }
3eb37d38 643# endif
644
fc04eb16 645 /* Create the thread */
646 if (! rc_stack_size) {
647# ifdef OLD_PTHREADS_API
648 rc_thread_create = pthread_create(&thread->thr,
649 attr,
650 S_ithread_run,
651 (void *)thread);
652# else
653# if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
654 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
655# endif
656 rc_thread_create = pthread_create(&thread->thr,
657 &attr,
658 S_ithread_run,
659 (void *)thread);
19a077f6 660# endif
fc04eb16 661 }
514612b7 662
663# ifdef _POSIX_THREAD_ATTR_STACKSIZE
664 /* Try to get thread's actual stack size */
665 {
666 size_t stacksize;
58a3a76c 667#ifdef HPUX1020
668 stacksize = pthread_attr_getstacksize(attr);
669#else
670 if (! pthread_attr_getstacksize(&attr, &stacksize))
671#endif
672 if (stacksize > 0) {
514612b7 673 thread->stack_size = (IV)stacksize;
674 }
514612b7 675 }
676# endif
fc04eb16 677 }
82c40bf6 678#endif
bcd9ca9b 679
fc04eb16 680 /* Check for errors */
d94006e8 681#ifdef WIN32
fc04eb16 682 if (thread->handle == NULL) {
d94006e8 683#else
fc04eb16 684 if (rc_stack_size || rc_thread_create) {
d94006e8 685#endif
fc04eb16 686 MUTEX_UNLOCK(&create_destruct_mutex);
687 sv_2mortal(params);
688 S_ithread_destruct(aTHX_ thread);
d94006e8 689#ifndef WIN32
514612b7 690 if (ckWARN_d(WARN_THREADS)) {
691 if (rc_stack_size)
692 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
693 else
694 Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
695 }
d94006e8 696#endif
fc04eb16 697 return (&PL_sv_undef);
698 }
699
4dcb9e53 700 running_threads++;
fc04eb16 701 MUTEX_UNLOCK(&create_destruct_mutex);
702
703 sv_2mortal(params);
3b1c3273 704
fc04eb16 705 return (ithread_to_SV(aTHX_ obj, thread, classname, FALSE));
68795e93 706}
47ba8780 707
73e09c8f 708#endif /* USE_ITHREADS */
e1c44605 709
fcea4b7c 710
fc04eb16 711MODULE = threads PACKAGE = threads PREFIX = ithread_
68795e93 712PROTOTYPES: DISABLE
8222d950 713
73e09c8f 714#ifdef USE_ITHREADS
715
68795e93 716void
f4cc38af 717ithread_create(...)
718 PREINIT:
719 char *classname;
514612b7 720 ithread *thread;
f4cc38af 721 SV *function_to_call;
722 AV *params;
514612b7 723 HV *specs;
724 IV stack_size;
9d9ff5b1 725 int context;
726 char *str;
514612b7 727 int idx;
f4cc38af 728 int ii;
729 CODE:
514612b7 730 if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
731 if (--items < 2)
732 Perl_croak(aTHX_ "Usage: threads->create(\\%specs, function, ...)");
733 specs = (HV*)SvRV(ST(1));
734 idx = 1;
735 } else {
736 if (items < 2)
737 Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
738 specs = NULL;
739 idx = 0;
740 }
f4cc38af 741
514612b7 742 if (sv_isobject(ST(0))) {
743 /* $thr->create() */
744 classname = HvNAME(SvSTASH(SvRV(ST(0))));
745 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
746 stack_size = thread->stack_size;
747 } else {
748 /* threads->create() */
749 classname = (char *)SvPV_nolen(ST(0));
750 stack_size = default_stack_size;
751 }
752
753 function_to_call = ST(idx+1);
754
9d9ff5b1 755 context = -1;
514612b7 756 if (specs) {
757 /* stack_size */
758 if (hv_exists(specs, "stack", 5)) {
759 stack_size = SvIV(*hv_fetch(specs, "stack", 5, 0));
760 } else if (hv_exists(specs, "stacksize", 9)) {
761 stack_size = SvIV(*hv_fetch(specs, "stacksize", 9, 0));
762 } else if (hv_exists(specs, "stack_size", 10)) {
763 stack_size = SvIV(*hv_fetch(specs, "stack_size", 10, 0));
764 }
9d9ff5b1 765
766 /* context */
767 if (hv_exists(specs, "context", 7)) {
768 str = (char *)SvPV_nolen(*hv_fetch(specs, "context", 7, 0));
769 switch (*str) {
770 case 'a':
771 case 'A':
772 context = G_ARRAY;
773 break;
774 case 's':
775 case 'S':
776 context = G_SCALAR;
777 break;
778 case 'v':
779 case 'V':
780 context = G_VOID;
781 break;
782 default:
783 Perl_croak(aTHX_ "Invalid context: %s", str);
784 }
785 } else if (hv_exists(specs, "array", 5)) {
786 if (SvTRUE(*hv_fetch(specs, "array", 5, 0))) {
787 context = G_ARRAY;
788 }
789 } else if (hv_exists(specs, "scalar", 6)) {
790 if (SvTRUE(*hv_fetch(specs, "scalar", 6, 0))) {
791 context = G_SCALAR;
792 }
793 } else if (hv_exists(specs, "void", 4)) {
794 if (SvTRUE(*hv_fetch(specs, "void", 4, 0))) {
795 context = G_VOID;
796 }
797 }
798 }
799 if (context == -1) {
800 context = GIMME_V; /* Implicit context */
801 } else {
802 context |= (GIMME_V & (~(G_ARRAY|G_SCALAR|G_VOID)));
514612b7 803 }
f4cc38af 804
805 /* Function args */
806 params = newAV();
807 if (items > 2) {
514612b7 808 for (ii=2; ii < items ; ii++) {
809 av_push(params, SvREFCNT_inc(ST(idx+ii)));
f4cc38af 810 }
811 }
812
813 /* Create thread */
bcd9ca9b 814 ST(0) = sv_2mortal(S_ithread_create(aTHX_ Nullsv,
514612b7 815 classname,
816 function_to_call,
817 stack_size,
9d9ff5b1 818 context,
514612b7 819 newRV_noinc((SV*)params)));
f4cc38af 820 /* XSRETURN(1); - implied */
821
8222d950 822
68795e93 823void
f4cc38af 824ithread_list(...)
825 PREINIT:
826 char *classname;
fc04eb16 827 ithread *thread;
f4cc38af 828 int list_context;
829 IV count = 0;
ead32952 830 int want_running;
f4cc38af 831 PPCODE:
832 /* Class method only */
833 if (SvROK(ST(0)))
ead32952 834 Perl_croak(aTHX_ "Usage: threads->list(...)");
f4cc38af 835 classname = (char *)SvPV_nolen(ST(0));
836
837 /* Calling context */
838 list_context = (GIMME_V == G_ARRAY);
839
ead32952 840 /* Running or joinable parameter */
841 if (items > 1) {
842 want_running = SvTRUE(ST(1));
843 }
844
f4cc38af 845 /* Walk through threads list */
846 MUTEX_LOCK(&create_destruct_mutex);
fc04eb16 847 for (thread = threads->next;
848 thread != threads;
849 thread = thread->next)
f4cc38af 850 {
851 /* Ignore detached or joined threads */
fc04eb16 852 if (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)) {
f4cc38af 853 continue;
854 }
ead32952 855
856 /* Filter per parameter */
857 if (items > 1) {
858 if (want_running) {
859 if (thread->state & PERL_ITHR_FINISHED) {
860 continue; /* Not running */
861 }
862 } else {
863 if (! (thread->state & PERL_ITHR_FINISHED)) {
864 continue; /* Still running - not joinable yet */
865 }
866 }
867 }
868
f4cc38af 869 /* Push object on stack if list context */
870 if (list_context) {
fc04eb16 871 XPUSHs(sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
f4cc38af 872 }
873 count++;
874 }
875 MUTEX_UNLOCK(&create_destruct_mutex);
876 /* If scalar context, send back count */
877 if (! list_context) {
878 XSRETURN_IV(count);
879 }
678a9b6c 880
881
882void
f4cc38af 883ithread_self(...)
884 PREINIT:
885 char *classname;
fcea4b7c 886 ithread *thread;
f4cc38af 887 CODE:
888 /* Class method only */
889 if (SvROK(ST(0)))
890 Perl_croak(aTHX_ "Usage: threads->self()");
891 classname = (char *)SvPV_nolen(ST(0));
892
fcea4b7c 893 thread = S_ithread_get(aTHX);
894
895 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
f4cc38af 896 /* XSRETURN(1); - implied */
47ba8780 897
47ba8780 898
899void
f4cc38af 900ithread_tid(...)
901 PREINIT:
902 ithread *thread;
903 CODE:
904 thread = SV_to_ithread(aTHX_ ST(0));
905 XST_mUV(0, thread->tid);
906 /* XSRETURN(1); - implied */
907
e1c44605 908
f9dff5f5 909void
f4cc38af 910ithread_join(...)
911 PREINIT:
fcea4b7c 912 ithread *thread;
913 int join_err;
f4cc38af 914 AV *params;
915 int len;
916 int ii;
fcea4b7c 917#ifdef WIN32
918 DWORD waitcode;
919#else
920 void *retval;
921#endif
f4cc38af 922 PPCODE:
923 /* Object method only */
924 if (! sv_isobject(ST(0)))
925 Perl_croak(aTHX_ "Usage: $thr->join()");
926
fcea4b7c 927 /* Check if the thread is joinable */
928 thread = SV_to_ithread(aTHX_ ST(0));
929 MUTEX_LOCK(&thread->mutex);
930 join_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
931 MUTEX_UNLOCK(&thread->mutex);
932 if (join_err) {
933 if (join_err & PERL_ITHR_DETACHED) {
934 Perl_croak(aTHX_ "Cannot join a detached thread");
935 } else {
936 Perl_croak(aTHX_ "Thread already joined");
937 }
938 }
939
940 /* Join the thread */
941#ifdef WIN32
942 waitcode = WaitForSingleObject(thread->handle, INFINITE);
943#else
944 pthread_join(thread->thr, &retval);
945#endif
946
947 MUTEX_LOCK(&thread->mutex);
948 /* Mark as joined */
949 thread->state |= PERL_ITHR_JOINED;
950
951 /* Get the return value from the call_sv */
952 {
953 AV *params_copy;
954 PerlInterpreter *other_perl;
955 CLONE_PARAMS clone_params;
956 ithread *current_thread;
957
958 params_copy = (AV *)SvRV(thread->params);
959 other_perl = thread->interp;
960 clone_params.stashes = newAV();
961 clone_params.flags = CLONEf_JOIN_IN;
962 PL_ptr_table = ptr_table_new();
963 current_thread = S_ithread_get(aTHX);
964 S_ithread_set(aTHX_ thread);
965 /* Ensure 'meaningful' addresses retain their meaning */
966 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
967 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
968 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
969 params = (AV *)sv_dup((SV*)params_copy, &clone_params);
970 S_ithread_set(aTHX_ current_thread);
971 SvREFCNT_dec(clone_params.stashes);
972 SvREFCNT_inc(params);
973 ptr_table_free(PL_ptr_table);
974 PL_ptr_table = NULL;
975 }
976
977 /* We are finished with the thread */
978 S_ithread_clear(aTHX_ thread);
979 MUTEX_UNLOCK(&thread->mutex);
980
4dcb9e53 981 MUTEX_LOCK(&create_destruct_mutex);
982 joinable_threads--;
983 MUTEX_UNLOCK(&create_destruct_mutex);
984
fcea4b7c 985 /* If no return values, then just return */
f4cc38af 986 if (! params) {
987 XSRETURN_UNDEF;
988 }
989
990 /* Put return values on stack */
991 len = (int)AvFILL(params);
992 for (ii=0; ii <= len; ii++) {
993 SV* param = av_shift(params);
994 XPUSHs(sv_2mortal(param));
995 }
996
997 /* Free return value array */
998 SvREFCNT_dec(params);
999
1000
1001void
1002ithread_yield(...)
1003 CODE:
1004 YIELD;
1005
1006
1007void
1008ithread_detach(...)
1009 PREINIT:
1010 ithread *thread;
fcea4b7c 1011 int detach_err;
1012 int cleanup;
f4cc38af 1013 CODE:
1014 thread = SV_to_ithread(aTHX_ ST(0));
fcea4b7c 1015 MUTEX_LOCK(&thread->mutex);
1016
1017 /* Check if the thread is detachable */
1018 if ((detach_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))) {
1019 MUTEX_UNLOCK(&thread->mutex);
1020 if (detach_err & PERL_ITHR_DETACHED) {
1021 Perl_croak(aTHX_ "Thread already detached");
1022 } else {
1023 Perl_croak(aTHX_ "Cannot detach a joined thread");
1024 }
1025 }
1026
1027 /* Detach the thread */
1028 thread->state |= PERL_ITHR_DETACHED;
1029#ifdef WIN32
1030 /* Windows has no 'detach thread' function */
1031#else
1032 PERL_THREAD_DETACH(thread->thr);
1033#endif
1034 /* Cleanup if finished */
1035 cleanup = (thread->state & PERL_ITHR_FINISHED);
1036 MUTEX_UNLOCK(&thread->mutex);
1037
4dcb9e53 1038 MUTEX_LOCK(&create_destruct_mutex);
1039 if (cleanup) {
1040 joinable_threads--;
1041 } else {
1042 running_threads--;
1043 detached_threads++;
1044 }
1045 MUTEX_UNLOCK(&create_destruct_mutex);
1046
1047 if (cleanup) {
fcea4b7c 1048 S_ithread_destruct(aTHX_ thread);
4dcb9e53 1049 }
f4cc38af 1050
47ba8780 1051
1052void
c0003851 1053ithread_kill(...)
1054 PREINIT:
1055 ithread *thread;
1056 char *sig_name;
1057 IV signal;
1058 CODE:
1059 /* Must have safe signals */
1060 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4dcb9e53 1061 Perl_croak(aTHX_ "Cannot signal threads without safe signals");
c0003851 1062
1063 /* Object method only */
1064 if (! sv_isobject(ST(0)))
1065 Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')");
1066
c0003851 1067 /* Get signal */
1068 sig_name = SvPV_nolen(ST(1));
1069 if (isALPHA(*sig_name)) {
1070 if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G')
1071 sig_name += 3;
404aaa48 1072 if ((signal = whichsig(sig_name)) < 0)
c0003851 1073 Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name);
1074 } else
1075 signal = SvIV(ST(1));
1076
1077 /* Set the signal for the thread */
4dcb9e53 1078 thread = SV_to_ithread(aTHX_ ST(0));
1079 MUTEX_LOCK(&thread->mutex);
c0003851 1080 {
1081 dTHXa(thread->interp);
1082 PL_psig_pend[signal]++;
1083 PL_sig_pending = 1;
1084 }
4dcb9e53 1085 MUTEX_UNLOCK(&thread->mutex);
c0003851 1086
1087 /* Return the thread to allow for method chaining */
1088 ST(0) = ST(0);
1089 /* XSRETURN(1); - implied */
1090
1091
1092void
f4cc38af 1093ithread_DESTROY(...)
1094 CODE:
fcea4b7c 1095 sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar);
f4cc38af 1096
1097
1098void
1099ithread_equal(...)
fc04eb16 1100 PREINIT:
1101 int are_equal = 0;
f4cc38af 1102 CODE:
fc04eb16 1103 /* Compares TIDs to determine thread equality */
f4cc38af 1104 if (sv_isobject(ST(0)) && sv_isobject(ST(1))) {
1105 ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1106 ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
fc04eb16 1107 are_equal = (thr1->tid == thr2->tid);
1108 }
1109 if (are_equal) {
1110 XST_mYES(0);
f4cc38af 1111 } else {
fc04eb16 1112 /* Return 0 on false for backward compatibility */
f4cc38af 1113 XST_mIV(0, 0);
1114 }
1115 /* XSRETURN(1); - implied */
1116
47ba8780 1117
47ba8780 1118void
f4cc38af 1119ithread_object(...)
1120 PREINIT:
1121 char *classname;
1122 UV tid;
fc04eb16 1123 ithread *thread;
f4cc38af 1124 int found = 0;
1125 CODE:
1126 /* Class method only */
1127 if (SvROK(ST(0)))
1128 Perl_croak(aTHX_ "Usage: threads->object($tid)");
1129 classname = (char *)SvPV_nolen(ST(0));
1130
1131 if ((items < 2) || ! SvOK(ST(1))) {
1132 XSRETURN_UNDEF;
1133 }
1134
fc04eb16 1135 /* threads->object($tid) */
f4cc38af 1136 tid = SvUV(ST(1));
1137
1138 /* Walk through threads list */
1139 MUTEX_LOCK(&create_destruct_mutex);
fc04eb16 1140 for (thread = threads->next;
1141 thread != threads;
1142 thread = thread->next)
f4cc38af 1143 {
1144 /* Look for TID, but ignore detached or joined threads */
fc04eb16 1145 if ((thread->tid != tid) ||
1146 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))
f4cc38af 1147 {
1148 continue;
1149 }
1150 /* Put object on stack */
fc04eb16 1151 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
f4cc38af 1152 found = 1;
1153 break;
1154 }
1155 MUTEX_UNLOCK(&create_destruct_mutex);
1156 if (! found) {
1157 XSRETURN_UNDEF;
1158 }
1159 /* XSRETURN(1); - implied */
1160
1161
1162void
1163ithread__handle(...);
1164 PREINIT:
1165 ithread *thread;
1166 CODE:
1167 thread = SV_to_ithread(aTHX_ ST(0));
1168#ifdef WIN32
fcea4b7c 1169 XST_mUV(0, PTR2UV(&thread->handle));
f4cc38af 1170#else
75ba4ae2 1171 XST_mUV(0, PTR2UV(&thread->thr));
f4cc38af 1172#endif
1173 /* XSRETURN(1); - implied */
68795e93 1174
514612b7 1175
1176void
1177ithread_get_stack_size(...)
1178 PREINIT:
1179 IV stack_size;
1180 CODE:
1181 if (sv_isobject(ST(0))) {
1182 /* $thr->get_stack_size() */
1183 ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1184 stack_size = thread->stack_size;
1185 } else {
1186 /* threads->get_stack_size() */
1187 stack_size = default_stack_size;
1188 }
1189 XST_mIV(0, stack_size);
1190 /* XSRETURN(1); - implied */
1191
1192
1193void
1194ithread_set_stack_size(...)
1195 PREINIT:
1196 IV old_size;
1197 CODE:
1198 if (items != 2)
1199 Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
1200 if (sv_isobject(ST(0)))
1201 Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
1202
1203 old_size = default_stack_size;
1204 default_stack_size = good_stack_size(aTHX_ SvIV(ST(1)));
1205 XST_mIV(0, old_size);
1206 /* XSRETURN(1); - implied */
1207
ead32952 1208
1209void
1210ithread_is_running(...)
1211 PREINIT:
1212 ithread *thread;
1213 CODE:
1214 /* Object method only */
1215 if (! sv_isobject(ST(0)))
1216 Perl_croak(aTHX_ "Usage: $thr->is_running()");
1217
1218 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1219 MUTEX_LOCK(&thread->mutex);
1220 ST(0) = (thread->state & PERL_ITHR_FINISHED) ? &PL_sv_no : &PL_sv_yes;
1221 MUTEX_UNLOCK(&thread->mutex);
1222 /* XSRETURN(1); - implied */
1223
1224
1225void
1226ithread_is_detached(...)
1227 PREINIT:
1228 ithread *thread;
1229 CODE:
1230 thread = SV_to_ithread(aTHX_ ST(0));
1231 MUTEX_LOCK(&thread->mutex);
1232 ST(0) = (thread->state & PERL_ITHR_DETACHED) ? &PL_sv_yes : &PL_sv_no;
1233 MUTEX_UNLOCK(&thread->mutex);
1234 /* XSRETURN(1); - implied */
1235
1236
1237void
1238ithread_is_joinable(...)
1239 PREINIT:
1240 ithread *thread;
1241 CODE:
1242 /* Object method only */
1243 if (! sv_isobject(ST(0)))
1244 Perl_croak(aTHX_ "Usage: $thr->is_joinable()");
1245
1246 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1247 MUTEX_LOCK(&thread->mutex);
1248 ST(0) = ((thread->state & PERL_ITHR_FINISHED) &&
1249 ! (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))
1250 ? &PL_sv_yes : &PL_sv_no;
1251 MUTEX_UNLOCK(&thread->mutex);
1252 /* XSRETURN(1); - implied */
1253
1254
1255void
1256ithread_wantarray(...)
1257 PREINIT:
1258 ithread *thread;
1259 CODE:
1260 thread = SV_to_ithread(aTHX_ ST(0));
1261 MUTEX_LOCK(&thread->mutex);
1262 ST(0) = (thread->gimme & G_ARRAY) ? &PL_sv_yes :
1263 (thread->gimme & G_VOID) ? &PL_sv_undef
1264 /* G_SCALAR */ : &PL_sv_no;
1265 MUTEX_UNLOCK(&thread->mutex);
1266 /* XSRETURN(1); - implied */
1267
73e09c8f 1268#endif /* USE_ITHREADS */
1269
fc04eb16 1270
68795e93 1271BOOT:
1272{
73e09c8f 1273#ifdef USE_ITHREADS
fc04eb16 1274 /* The 'main' thread is thread 0.
1275 * It is detached (unjoinable) and immortal.
1276 */
62375a60 1277
fc04eb16 1278 ithread *thread;
1279 MY_CXT_INIT;
1280
1281 PL_perl_destruct_level = 2;
1282 MUTEX_INIT(&create_destruct_mutex);
1283 MUTEX_LOCK(&create_destruct_mutex);
1284
1285 PL_threadhook = &Perl_ithread_hook;
1286
1287 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
1288 if (! thread) {
1289 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
1290 my_exit(1);
1291 }
1292 Zero(thread, 1, ithread);
1293
fc04eb16 1294 MUTEX_INIT(&thread->mutex);
1295
1296 thread->tid = tid_counter++; /* Thread 0 */
1297
1298 /* Head of the threads list */
1299 threads = thread;
1300 thread->next = thread;
1301 thread->prev = thread;
1302
1303 thread->count = 1; /* Immortal */
1304
1305 thread->interp = aTHX;
1306 thread->state = PERL_ITHR_DETACHED; /* Detached */
514612b7 1307 thread->stack_size = default_stack_size;
fc04eb16 1308# ifdef WIN32
1309 thread->thr = GetCurrentThreadId();
1310# else
1311 thread->thr = pthread_self();
1312# endif
1313
fc04eb16 1314 S_ithread_set(aTHX_ thread);
1315 MUTEX_UNLOCK(&create_destruct_mutex);
73e09c8f 1316#endif /* USE_ITHREADS */
68795e93 1317}