1 #define PERL_NO_GET_CONTEXT
2 /* Workaround for mingw 32-bit compiler by mingw-w64.sf.net - has to come before any #include.
3 * It also defines USE_NO_MINGW_SETJMP_TWO_ARGS for the mingw.org 32-bit compilers ... but
4 * that's ok as that compiler makes no use of that symbol anyway */
5 #if defined(WIN32) && defined(__MINGW32__) && !defined(__MINGW64__)
6 # define USE_NO_MINGW_SETJMP_TWO_ARGS 1
11 /* Workaround for XSUB.h bug under WIN32 */
14 # if defined(USE_NO_MINGW_SETJMP_TWO_ARGS) || (!defined(__BORLANDC__) && !defined(__MINGW64__))
15 # define setjmp(x) _setjmp(x)
17 # if defined(__MINGW64__)
18 # define setjmp(x) _setjmpex((x), mingw_getsp())
22 # define NEED_PL_signals
23 # define NEED_newRV_noinc
24 # define NEED_sv_2pv_flags
29 # define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t))
36 /* Supposed to be in Winbase.h */
37 # ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
38 # define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
40 # include <win32thread.h>
43 typedef perl_os_thread pthread_t;
48 # define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
49 # ifdef OLD_PTHREADS_API
50 # define PERL_THREAD_DETACH(t) pthread_detach(&(t))
52 # define PERL_THREAD_DETACH(t) pthread_detach((t))
55 #if !defined(HAS_GETPAGESIZE) && defined(I_SYS_PARAM)
56 # include <sys/param.h>
59 /* Values for 'state' member */
60 #define PERL_ITHR_DETACHED 1 /* Thread has been detached */
61 #define PERL_ITHR_JOINED 2 /* Thread is being / has been joined */
62 #define PERL_ITHR_FINISHED 4 /* Thread has finished execution */
63 #define PERL_ITHR_THREAD_EXIT_ONLY 8 /* exit() only exits current thread */
64 #define PERL_ITHR_NONVIABLE 16 /* Thread creation failed */
65 #define PERL_ITHR_DIED 32 /* Thread finished by dying */
67 #define PERL_ITHR_UNCALLABLE (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)
70 typedef struct _ithread {
71 struct _ithread *next; /* Next thread in the list */
72 struct _ithread *prev; /* Prev thread in the list */
73 PerlInterpreter *interp; /* The threads interpreter */
74 UV tid; /* Threads module's thread id */
75 perl_mutex mutex; /* Mutex for updating things in this struct */
76 int count; /* Reference count. See S_ithread_create. */
77 int state; /* Detached, joined, finished, etc. */
78 int gimme; /* Context of create */
79 SV *init_function; /* Code to run */
80 AV *params; /* Args to pass function */
82 DWORD thr; /* OS's idea if thread id */
83 HANDLE handle; /* OS's waitable handle */
85 pthread_t thr; /* OS's handle for the thread */
88 SV *err; /* Error from abnormally terminated thread */
89 char *err_class; /* Error object's classname if applicable */
91 sigset_t initial_sigmask; /* Thread wakes up with signals blocked */
96 #define MY_CXT_KEY "threads::_cxt" XS_VERSION
99 /* Used by Perl interpreter for thread context switching */
106 #define MY_POOL_KEY "threads::_pool" XS_VERSION
109 /* Structure for 'main' thread
110 * Also forms the 'base' for the doubly-linked list of threads */
113 /* Protects the creation and destruction of threads*/
114 perl_mutex create_destruct_mutex;
121 IV default_stack_size;
126 SV *my_pool_sv = *hv_fetch(PL_modglobal, MY_POOL_KEY, \
127 sizeof(MY_POOL_KEY)-1, TRUE); \
128 my_pool_t *my_poolp = INT2PTR(my_pool_t*, SvUV(my_pool_sv))
130 #define MY_POOL (*my_poolp)
133 /* Block most signals for calling thread, setting the old signal mask to
134 * oldmask, if it is not NULL */
136 S_block_most_signals(sigset_t *oldmask)
140 sigfillset(&newmask);
141 /* Don't block certain "important" signals (stolen from mg.c) */
143 sigdelset(&newmask, SIGILL);
146 sigdelset(&newmask, SIGBUS);
149 sigdelset(&newmask, SIGSEGV);
153 /* no per-thread blocking available */
154 return sigprocmask(SIG_BLOCK, &newmask, oldmask);
156 return pthread_sigmask(SIG_BLOCK, &newmask, oldmask);
160 /* Set the signal mask for this thread to newmask */
162 S_set_sigmask(sigset_t *newmask)
165 return sigprocmask(SIG_SETMASK, newmask, NULL);
167 return pthread_sigmask(SIG_SETMASK, newmask, NULL);
172 /* Used by Perl interpreter for thread context switching */
174 S_ithread_set(pTHX_ ithread *thread)
177 MY_CXT.context = thread;
184 return (MY_CXT.context);
188 /* Free any data (such as the Perl interpreter) attached to an ithread
189 * structure. This is a bit like undef on SVs, where the SV isn't freed,
190 * but the PVX is. Must be called with thread->mutex already locked. Also,
191 * must be called with MY_POOL.create_destruct_mutex unlocked as destruction
192 * of the interpreter can lead to recursive destruction calls that could
193 * lead to a deadlock on that mutex.
196 S_ithread_clear(pTHX_ ithread *thread)
198 PerlInterpreter *interp;
203 assert(((thread->state & PERL_ITHR_FINISHED) &&
204 (thread->state & PERL_ITHR_UNCALLABLE))
206 (thread->state & PERL_ITHR_NONVIABLE));
209 /* We temporarily set the interpreter context to the interpreter being
210 * destroyed. It's in no condition to handle signals while it's being
213 S_block_most_signals(&origmask);
216 interp = thread->interp;
220 PERL_SET_CONTEXT(interp);
221 S_ithread_set(aTHX_ thread);
223 SvREFCNT_dec(thread->params);
224 thread->params = NULL;
227 SvREFCNT_dec(thread->err);
228 thread->err = Nullsv;
231 perl_destruct(interp);
233 thread->interp = NULL;
236 PERL_SET_CONTEXT(aTHX);
238 S_set_sigmask(&origmask);
243 /* Decrement the refcount of an ithread, and if it reaches zero, free it.
244 * Must be called with the mutex held.
245 * On return, mutex is released (or destroyed).
248 S_ithread_free(pTHX_ ithread *thread)
255 if (! (thread->state & PERL_ITHR_NONVIABLE)) {
256 assert(thread->count > 0);
257 if (--thread->count > 0) {
258 MUTEX_UNLOCK(&thread->mutex);
261 assert((thread->state & PERL_ITHR_FINISHED) &&
262 (thread->state & PERL_ITHR_UNCALLABLE));
264 MUTEX_UNLOCK(&thread->mutex);
266 /* Main thread (0) is immortal and should never get here */
267 assert(thread->tid != 0);
269 /* Remove from circular list of threads */
270 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
271 assert(thread->prev && thread->next);
272 thread->next->prev = thread->prev;
273 thread->prev->next = thread->next;
276 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
278 /* Thread is now disowned */
279 MUTEX_LOCK(&thread->mutex);
280 S_ithread_clear(aTHX_ thread);
283 handle = thread->handle;
284 thread->handle = NULL;
286 MUTEX_UNLOCK(&thread->mutex);
287 MUTEX_DESTROY(&thread->mutex);
295 PerlMemShared_free(thread);
297 /* total_threads >= 1 is used to veto cleanup by the main thread,
298 * should it happen to exit while other threads still exist.
299 * Decrement this as the very last thing in the thread's existence.
300 * Otherwise, MY_POOL and global state such as PL_op_mutex may get
301 * freed while we're still using it.
303 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
304 MY_POOL.total_threads--;
305 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
310 S_ithread_count_inc(pTHX_ ithread *thread)
312 MUTEX_LOCK(&thread->mutex);
314 MUTEX_UNLOCK(&thread->mutex);
318 /* Warn if exiting with any unjoined threads */
322 int veto_cleanup, warn;
325 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
326 veto_cleanup = (MY_POOL.total_threads > 0);
327 warn = (MY_POOL.running_threads || MY_POOL.joinable_threads);
328 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
331 if (ckWARN_d(WARN_THREADS)) {
332 Perl_warn(aTHX_ "Perl exited with active threads:\n\t%"
333 IVdf " running and unjoined\n\t%"
334 IVdf " finished and unjoined\n\t%"
335 IVdf " running and detached\n",
336 MY_POOL.running_threads,
337 MY_POOL.joinable_threads,
338 MY_POOL.detached_threads);
342 return (veto_cleanup);
346 /* Called from perl_destruct() in each thread. If it's the main thread,
347 * stop it from freeing everything if there are other threads still running.
350 Perl_ithread_hook(pTHX)
353 return ((aTHX == MY_POOL.main_thread.interp) ? S_exit_warning(aTHX) : 0);
357 /* MAGIC (in mg.h sense) hooks */
360 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
362 ithread *thread = (ithread *)mg->mg_ptr;
363 SvIV_set(sv, PTR2IV(thread));
369 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
371 ithread *thread = (ithread *)mg->mg_ptr;
373 MUTEX_LOCK(&thread->mutex);
374 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
379 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
381 PERL_UNUSED_ARG(param);
382 S_ithread_count_inc(aTHX_ (ithread *)mg->mg_ptr);
386 MGVTBL ithread_vtbl = {
387 ithread_mg_get, /* get */
391 ithread_mg_free, /* free */
393 ithread_mg_dup /* dup */
397 /* Provided default, minimum and rational stack sizes */
399 S_good_stack_size(pTHX_ IV stack_size)
403 /* Use default stack size if no stack size specified */
405 return (MY_POOL.default_stack_size);
408 #ifdef PTHREAD_STACK_MIN
409 /* Can't use less than minimum */
410 if (stack_size < PTHREAD_STACK_MIN) {
411 if (ckWARN(WARN_THREADS)) {
412 Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
414 return (PTHREAD_STACK_MIN);
418 /* Round up to page size boundary */
419 if (MY_POOL.page_size <= 0) {
420 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
421 SETERRNO(0, SS_NORMAL);
423 MY_POOL.page_size = sysconf(_SC_PAGESIZE);
425 MY_POOL.page_size = sysconf(_SC_MMAP_PAGE_SIZE);
427 if ((long)MY_POOL.page_size < 0) {
429 SV * const error = get_sv("@", 0);
430 (void)SvUPGRADE(error, SVt_PV);
431 Perl_croak(aTHX_ "PANIC: sysconf: %s", SvPV_nolen(error));
433 Perl_croak(aTHX_ "PANIC: sysconf: pagesize unknown");
437 # ifdef HAS_GETPAGESIZE
438 MY_POOL.page_size = getpagesize();
440 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
441 MY_POOL.page_size = PAGESIZE;
443 MY_POOL.page_size = 8192; /* A conservative default */
446 if (MY_POOL.page_size <= 0) {
447 Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)MY_POOL.page_size);
451 stack_size = ((stack_size + (MY_POOL.page_size - 1)) / MY_POOL.page_size) * MY_POOL.page_size;
457 /* Starts executing the thread.
458 * Passed as the C level function to run in the new thread.
461 STATIC THREAD_RET_TYPE
462 S_ithread_run(LPVOID arg)
465 S_ithread_run(void * arg)
468 ithread *thread = (ithread *)arg;
471 int exit_app = 0; /* Thread terminated using 'exit' */
473 int died = 0; /* Thread terminated abnormally */
477 dTHXa(thread->interp);
481 /* Blocked until ->create() call finishes */
482 MUTEX_LOCK(&thread->mutex);
483 MUTEX_UNLOCK(&thread->mutex);
485 PERL_SET_CONTEXT(thread->interp);
486 S_ithread_set(aTHX_ thread);
489 /* Thread starts with most signals blocked - restore the signal mask from
490 * the ithread struct.
492 S_set_sigmask(&thread->initial_sigmask);
495 PL_perl_destruct_level = 2;
498 AV *params = thread->params;
499 int len = (int)av_len(params)+1;
506 /* Put args on the stack */
508 for (ii=0; ii < len; ii++) {
509 XPUSHs(av_shift(params));
513 oldscope = PL_scopestack_ix;
516 /* Run the specified function */
517 len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
518 } else if (jmp_rc == 2) {
521 exit_code = STATUS_CURRENT;
522 while (PL_scopestack_ix > oldscope) {
529 /* The interpreter is finished, so this thread can stop receiving
530 * signals. This way, our signal handler doesn't get called in the
531 * middle of our parent thread calling perl_destruct()...
533 S_block_most_signals(NULL);
536 /* Remove args from stack and put back in params array */
538 for (ii=len-1; ii >= 0; ii--) {
540 if (jmp_rc == 0 && (thread->gimme & G_WANT) != G_VOID) {
541 av_store(params, ii, SvREFCNT_inc(sv));
548 /* Check for abnormal termination */
550 died = PERL_ITHR_DIED;
551 thread->err = newSVsv(ERRSV);
552 /* If ERRSV is an object, remember the classname and then
553 * rebless into 'main' so it will survive 'cloning'
555 if (sv_isobject(thread->err)) {
556 thread->err_class = HvNAME(SvSTASH(SvRV(thread->err)));
557 sv_bless(thread->err, gv_stashpv("main", 0));
560 if (ckWARN_d(WARN_THREADS)) {
561 oldscope = PL_scopestack_ix;
564 /* Warn that thread died */
565 Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV);
566 } else if (jmp_rc == 2) {
567 /* Warn handler exited */
569 exit_code = STATUS_CURRENT;
570 while (PL_scopestack_ix > oldscope) {
578 /* Release function ref */
579 SvREFCNT_dec(thread->init_function);
580 thread->init_function = Nullsv;
583 PerlIO_flush((PerlIO *)NULL);
585 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
586 MUTEX_LOCK(&thread->mutex);
587 /* Mark as finished */
588 thread->state |= (PERL_ITHR_FINISHED | died);
589 /* Clear exit flag if required */
590 if (thread->state & PERL_ITHR_THREAD_EXIT_ONLY) {
594 /* Adjust thread status counts */
595 if (thread->state & PERL_ITHR_DETACHED) {
596 MY_POOL.detached_threads--;
598 MY_POOL.running_threads--;
599 MY_POOL.joinable_threads++;
601 MUTEX_UNLOCK(&thread->mutex);
602 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
604 /* Exit application if required */
606 oldscope = PL_scopestack_ix;
609 /* Warn if there are unjoined threads */
610 S_exit_warning(aTHX);
611 } else if (jmp_rc == 2) {
612 /* Warn handler exited */
613 exit_code = STATUS_CURRENT;
614 while (PL_scopestack_ix > oldscope) {
623 /* At this point, the interpreter may have been freed, so call
624 * free in the the context of of the 'main' interpreter which
625 * can't have been freed due to the veto_cleanup mechanism.
627 aTHX = MY_POOL.main_thread.interp;
629 MUTEX_LOCK(&thread->mutex);
630 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
640 /* Type conversion helper functions */
643 S_ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
649 S_ithread_count_inc(aTHX_ thread);
655 sv = newSVrv(obj, classname);
656 sv_setiv(sv, PTR2IV(thread));
657 mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0);
658 mg->mg_flags |= MGf_DUP;
665 S_SV_to_ithread(pTHX_ SV *sv)
667 /* Argument is a thread */
669 return (INT2PTR(ithread *, SvIV(SvRV(sv))));
671 /* Argument is classname, therefore return current thread */
672 return (S_ithread_get(aTHX));
677 * Called in context of parent thread.
678 * Called with MY_POOL.create_destruct_mutex locked. (Unlocked on error.)
682 PerlInterpreter *parent_perl,
692 ithread *current_thread = S_ithread_get(aTHX);
696 #if PERL_VERSION <= 8 && PERL_SUBVERSION <= 7
697 SV **tmps_tmp = PL_tmps_stack;
698 IV tmps_ix = PL_tmps_ix;
701 int rc_stack_size = 0;
702 int rc_thread_create = 0;
706 /* Allocate thread structure in context of the main thread's interpreter */
708 PERL_SET_CONTEXT(MY_POOL.main_thread.interp);
709 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
711 PERL_SET_CONTEXT(aTHX);
713 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
714 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
717 Zero(thread, 1, ithread);
719 /* Add to threads list */
720 thread->next = &MY_POOL.main_thread;
721 thread->prev = MY_POOL.main_thread.prev;
722 MY_POOL.main_thread.prev = thread;
723 thread->prev->next = thread;
724 MY_POOL.total_threads++;
726 /* 1 ref to be held by the local var 'thread' in S_ithread_run().
727 * 1 ref to be held by the threads object that we assume we will
728 * be embedded in upon our return.
729 * 1 ref to be the responsibility of join/detach, so we don't get
730 * freed until join/detach, even if no thread objects remain.
731 * This allows the following to work:
732 * { threads->create(sub{...}); } threads->object(1)->join;
736 /* Block new thread until ->create() call finishes */
737 MUTEX_INIT(&thread->mutex);
738 MUTEX_LOCK(&thread->mutex);
740 thread->tid = MY_POOL.tid_counter++;
741 thread->stack_size = S_good_stack_size(aTHX_ stack_size);
742 thread->gimme = gimme;
743 thread->state = exit_opt;
745 /* "Clone" our interpreter into the thread's interpreter.
746 * This gives thread access to "static data" and code.
748 PerlIO_flush((PerlIO *)NULL);
749 S_ithread_set(aTHX_ thread);
751 SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct value */
752 PL_srand_called = FALSE; /* Set it to false so we can detect if it gets
753 set during the clone */
756 /* perl_clone() will leave us the new interpreter's context. This poses
757 * two problems for our signal handler. First, it sets the new context
758 * before the new interpreter struct is fully initialized, so our signal
759 * handler might find bogus data in the interpreter struct it gets.
760 * Second, even if the interpreter is initialized before a signal comes in,
761 * we would like to avoid that interpreter receiving notifications for
762 * signals (especially when they ought to be for the one running in this
763 * thread), until it is running in its own thread. Another problem is that
764 * the new thread will not have set the context until some time after it
765 * has started, so it won't be safe for our signal handler to run until
768 * So we block most signals here, so the new thread will inherit the signal
769 * mask, and unblock them right after the thread creation. The original
770 * mask is saved in the thread struct so that the new thread can restore
773 S_block_most_signals(&thread->initial_sigmask);
777 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
779 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
782 /* perl_clone() leaves us in new interpreter's context. As it is tricky
783 * to spot an implicit aTHX, create a new scope with aTHX matching the
784 * context for the duration of our work for new interpreter.
787 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION > 1)
788 CLONE_PARAMS *clone_param = Perl_clone_params_new(aTHX, thread->interp);
790 CLONE_PARAMS clone_param_s;
791 CLONE_PARAMS *clone_param = &clone_param_s;
793 dTHXa(thread->interp);
797 #if (PERL_VERSION < 13) || (PERL_VERSION == 13 && PERL_SUBVERSION <= 1)
798 clone_param->flags = 0;
801 /* Here we remove END blocks since they should only run in the thread
804 SvREFCNT_dec(PL_endav);
807 if (SvPOK(init_function)) {
808 thread->init_function = newSV(0);
809 sv_copypv(thread->init_function, init_function);
811 thread->init_function = sv_dup_inc(init_function, clone_param);
814 thread->params = params = newAV();
815 av_extend(params, num_params - 1);
816 AvFILLp(params) = num_params - 1;
817 array = AvARRAY(params);
819 /* params_start is an offset onto the Perl stack. This can be
820 reallocated (and hence move) as a side effect of calls to
821 perl_clone() and sv_dup_inc(). Hence copy the parameters
822 somewhere under our control first, before duplicating. */
823 #if (PERL_VERSION > 8)
824 Copy(parent_perl->Istack_base + params_start, array, num_params, SV *);
826 Copy(parent_perl->Tstack_base + params_start, array, num_params, SV *);
828 while (num_params--) {
829 *array = sv_dup_inc(*array, clone_param);
832 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION > 1)
833 Perl_clone_params_del(clone_param);
836 #if PERL_VERSION <= 8 && PERL_SUBVERSION <= 7
837 /* The code below checks that anything living on the tmps stack and
838 * has been cloned (so it lives in the ptr_table) has a refcount
841 * If the refcount is 0 it means that a something on the stack/context
842 * was holding a reference to it and since we init_stacks() in
843 * perl_clone that won't get cleaned and we will get a leaked scalar.
844 * The reason it was cloned was that it lived on the @_ stack.
846 * Example of this can be found in bugreport 15837 where calls in the
847 * parameter list end up as a temp.
849 * As of 5.8.8 this is done in perl_clone.
851 while (tmps_ix > 0) {
852 SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
854 if (sv && SvREFCNT(sv) == 0) {
855 SvREFCNT_inc_void(sv);
861 SvTEMP_off(thread->init_function);
862 ptr_table_free(PL_ptr_table);
864 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
866 S_ithread_set(aTHX_ current_thread);
867 PERL_SET_CONTEXT(aTHX);
869 /* Create/start the thread */
871 thread->handle = CreateThread(NULL,
872 (DWORD)thread->stack_size,
875 STACK_SIZE_PARAM_IS_A_RESERVATION,
879 STATIC pthread_attr_t attr;
880 STATIC int attr_inited = 0;
881 STATIC int attr_joinable = PTHREAD_CREATE_JOINABLE;
883 pthread_attr_init(&attr);
887 # ifdef PTHREAD_ATTR_SETDETACHSTATE
888 /* Threads start out joinable */
889 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
892 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
893 /* Set thread's stack size */
894 if (thread->stack_size > 0) {
895 rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
899 /* Create the thread */
900 if (! rc_stack_size) {
901 # ifdef OLD_PTHREADS_API
902 rc_thread_create = pthread_create(&thread->thr,
907 # if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
908 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
910 rc_thread_create = pthread_create(&thread->thr,
918 /* Now it's safe to accept signals, since we're in our own interpreter's
919 * context and we have created the thread.
921 S_set_sigmask(&thread->initial_sigmask);
924 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
925 /* Try to get thread's actual stack size */
929 stacksize = pthread_attr_getstacksize(attr);
931 if (! pthread_attr_getstacksize(&attr, &stacksize))
934 thread->stack_size = (IV)stacksize;
941 /* Check for errors */
943 if (thread->handle == NULL) {
945 if (rc_stack_size || rc_thread_create) {
947 /* Must unlock mutex for destruct call */
948 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
949 thread->state |= PERL_ITHR_NONVIABLE;
950 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
952 if (ckWARN_d(WARN_THREADS)) {
954 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
956 Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
963 MY_POOL.running_threads++;
967 #endif /* USE_ITHREADS */
970 MODULE = threads PACKAGE = threads PREFIX = ithread_
980 SV *function_to_call;
985 SV *thread_exit_only;
990 if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
992 Perl_croak(aTHX_ "Usage: threads->create(\\%%specs, function, ...)");
994 specs = (HV*)SvRV(ST(1));
998 Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
1004 if (sv_isobject(ST(0))) {
1005 /* $thr->create() */
1006 classname = HvNAME(SvSTASH(SvRV(ST(0))));
1007 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1008 MUTEX_LOCK(&thread->mutex);
1009 stack_size = thread->stack_size;
1010 exit_opt = thread->state & PERL_ITHR_THREAD_EXIT_ONLY;
1011 MUTEX_UNLOCK(&thread->mutex);
1013 /* threads->create() */
1014 classname = (char *)SvPV_nolen(ST(0));
1015 stack_size = MY_POOL.default_stack_size;
1016 thread_exit_only = get_sv("threads::thread_exit_only", GV_ADD);
1017 exit_opt = (SvTRUE(thread_exit_only))
1018 ? PERL_ITHR_THREAD_EXIT_ONLY : 0;
1021 function_to_call = ST(idx+1);
1027 if ((svp = hv_fetch(specs, "stack", 5, 0))) {
1028 stack_size = SvIV(*svp);
1029 } else if ((svp = hv_fetch(specs, "stacksize", 9, 0))) {
1030 stack_size = SvIV(*svp);
1031 } else if ((svp = hv_fetch(specs, "stack_size", 10, 0))) {
1032 stack_size = SvIV(*svp);
1036 if ((svp = hv_fetch(specs, "context", 7, 0))) {
1037 str = (char *)SvPV_nolen(*svp);
1054 Perl_croak(aTHX_ "Invalid context: %s", str);
1056 } else if ((svp = hv_fetch(specs, "array", 5, 0))) {
1060 } else if ((svp = hv_fetch(specs, "list", 4, 0))) {
1064 } else if ((svp = hv_fetch(specs, "scalar", 6, 0))) {
1068 } else if ((svp = hv_fetch(specs, "void", 4, 0))) {
1074 /* exit => thread_only */
1075 if ((svp = hv_fetch(specs, "exit", 4, 0))) {
1076 str = (char *)SvPV_nolen(*svp);
1077 exit_opt = (*str == 't' || *str == 'T')
1078 ? PERL_ITHR_THREAD_EXIT_ONLY : 0;
1081 if (context == -1) {
1082 context = GIMME_V; /* Implicit context */
1084 context |= (GIMME_V & (~(G_ARRAY|G_SCALAR|G_VOID)));
1088 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1089 thread = S_ithread_create(aTHX_ function_to_call,
1094 items > 2 ? items - 2 : 0);
1096 XSRETURN_UNDEF; /* Mutex already unlocked */
1098 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, FALSE));
1099 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1101 /* Let thread run */
1102 MUTEX_UNLOCK(&thread->mutex);
1104 /* XSRETURN(1); - implied */
1114 int want_running = 0;
1118 /* Class method only */
1120 Perl_croak(aTHX_ "Usage: threads->list(...)");
1122 classname = (char *)SvPV_nolen(ST(0));
1124 /* Calling context */
1125 list_context = (GIMME_V == G_ARRAY);
1127 /* Running or joinable parameter */
1129 want_running = SvTRUE(ST(1));
1132 /* Walk through threads list */
1133 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1134 for (thread = MY_POOL.main_thread.next;
1135 thread != &MY_POOL.main_thread;
1136 thread = thread->next)
1138 MUTEX_LOCK(&thread->mutex);
1139 state = thread->state;
1140 MUTEX_UNLOCK(&thread->mutex);
1142 /* Ignore detached or joined threads */
1143 if (state & PERL_ITHR_UNCALLABLE) {
1147 /* Filter per parameter */
1150 if (state & PERL_ITHR_FINISHED) {
1151 continue; /* Not running */
1154 if (! (state & PERL_ITHR_FINISHED)) {
1155 continue; /* Still running - not joinable yet */
1160 /* Push object on stack if list context */
1162 XPUSHs(sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
1166 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1167 /* If scalar context, send back count */
1168 if (! list_context) {
1179 /* Class method only */
1180 if ((items != 1) || SvROK(ST(0))) {
1181 Perl_croak(aTHX_ "Usage: threads->self()");
1183 classname = (char *)SvPV_nolen(ST(0));
1185 thread = S_ithread_get(aTHX);
1187 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1188 /* XSRETURN(1); - implied */
1196 PERL_UNUSED_VAR(items);
1197 thread = S_SV_to_ithread(aTHX_ ST(0));
1198 XST_mUV(0, thread->tid);
1199 /* XSRETURN(1); - implied */
1206 ithread *current_thread;
1217 /* Object method only */
1218 if ((items != 1) || ! sv_isobject(ST(0))) {
1219 Perl_croak(aTHX_ "Usage: $thr->join()");
1222 /* Check if the thread is joinable and not ourselves */
1223 thread = S_SV_to_ithread(aTHX_ ST(0));
1224 current_thread = S_ithread_get(aTHX);
1226 MUTEX_LOCK(&thread->mutex);
1227 if ((join_err = (thread->state & PERL_ITHR_UNCALLABLE))) {
1228 MUTEX_UNLOCK(&thread->mutex);
1229 Perl_croak(aTHX_ (join_err & PERL_ITHR_DETACHED)
1230 ? "Cannot join a detached thread"
1231 : "Thread already joined");
1232 } else if (thread->tid == current_thread->tid) {
1233 MUTEX_UNLOCK(&thread->mutex);
1234 Perl_croak(aTHX_ "Cannot join self");
1237 /* Mark as joined */
1238 thread->state |= PERL_ITHR_JOINED;
1239 MUTEX_UNLOCK(&thread->mutex);
1241 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1242 MY_POOL.joinable_threads--;
1243 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1245 /* Join the thread */
1247 if (WaitForSingleObject(thread->handle, INFINITE) != WAIT_OBJECT_0) {
1248 /* Timeout/abandonment unexpected here; check $^E */
1249 Perl_croak(aTHX_ "PANIC: underlying join failed");
1252 if ((rc_join = pthread_join(thread->thr, &retval)) != 0) {
1253 /* In progress/deadlock/unknown unexpected here; check $! */
1255 Perl_croak(aTHX_ "PANIC: underlying join failed");
1259 MUTEX_LOCK(&thread->mutex);
1260 /* Get the return value from the call_sv */
1261 /* Objects do not survive this process - FIXME */
1262 if ((thread->gimme & G_WANT) != G_VOID) {
1263 #if (PERL_VERSION < 13) || (PERL_VERSION == 13 && PERL_SUBVERSION <= 1)
1265 PerlInterpreter *other_perl;
1266 CLONE_PARAMS clone_params;
1268 params_copy = thread->params;
1269 other_perl = thread->interp;
1270 clone_params.stashes = newAV();
1271 clone_params.flags = CLONEf_JOIN_IN;
1272 PL_ptr_table = ptr_table_new();
1273 S_ithread_set(aTHX_ thread);
1274 /* Ensure 'meaningful' addresses retain their meaning */
1275 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1276 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1277 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1278 params = (AV *)sv_dup((SV*)params_copy, &clone_params);
1279 S_ithread_set(aTHX_ current_thread);
1280 SvREFCNT_dec(clone_params.stashes);
1281 SvREFCNT_inc_void(params);
1282 ptr_table_free(PL_ptr_table);
1283 PL_ptr_table = NULL;
1286 PerlInterpreter *other_perl = thread->interp;
1287 CLONE_PARAMS *clone_params = Perl_clone_params_new(other_perl, aTHX);
1289 params_copy = thread->params;
1290 clone_params->flags |= CLONEf_JOIN_IN;
1291 PL_ptr_table = ptr_table_new();
1292 S_ithread_set(aTHX_ thread);
1293 /* Ensure 'meaningful' addresses retain their meaning */
1294 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1295 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1296 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1297 params = (AV *)sv_dup((SV*)params_copy, clone_params);
1298 S_ithread_set(aTHX_ current_thread);
1299 Perl_clone_params_del(clone_params);
1300 SvREFCNT_inc_void(params);
1301 ptr_table_free(PL_ptr_table);
1302 PL_ptr_table = NULL;
1306 /* If thread didn't die, then we can free its interpreter */
1307 if (! (thread->state & PERL_ITHR_DIED)) {
1308 S_ithread_clear(aTHX_ thread);
1310 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
1312 /* If no return values, then just return */
1317 /* Put return values on stack */
1318 len = (int)AvFILL(params);
1319 for (ii=0; ii <= len; ii++) {
1320 SV* param = av_shift(params);
1321 XPUSHs(sv_2mortal(param));
1324 /* Free return value array */
1325 SvREFCNT_dec(params);
1331 PERL_UNUSED_VAR(items);
1342 PERL_UNUSED_VAR(items);
1344 /* Detach the thread */
1345 thread = S_SV_to_ithread(aTHX_ ST(0));
1346 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1347 MUTEX_LOCK(&thread->mutex);
1348 if (! (detach_err = (thread->state & PERL_ITHR_UNCALLABLE))) {
1349 /* Thread is detachable */
1350 thread->state |= PERL_ITHR_DETACHED;
1352 /* Windows has no 'detach thread' function */
1354 PERL_THREAD_DETACH(thread->thr);
1356 if (thread->state & PERL_ITHR_FINISHED) {
1357 MY_POOL.joinable_threads--;
1359 MY_POOL.running_threads--;
1360 MY_POOL.detached_threads++;
1363 MUTEX_UNLOCK(&thread->mutex);
1364 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1367 Perl_croak(aTHX_ (detach_err & PERL_ITHR_DETACHED)
1368 ? "Thread already detached"
1369 : "Cannot detach a joined thread");
1372 /* If thread is finished and didn't die,
1373 * then we can free its interpreter */
1374 MUTEX_LOCK(&thread->mutex);
1375 if ((thread->state & PERL_ITHR_FINISHED) &&
1376 ! (thread->state & PERL_ITHR_DIED))
1378 S_ithread_clear(aTHX_ thread);
1380 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
1391 /* Must have safe signals */
1392 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) {
1393 Perl_croak(aTHX_ "Cannot signal threads without safe signals");
1396 /* Object method only */
1397 if ((items != 2) || ! sv_isobject(ST(0))) {
1398 Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')");
1402 sig_name = SvPV_nolen(ST(1));
1403 if (isALPHA(*sig_name)) {
1404 if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G') {
1407 if ((signal = whichsig(sig_name)) < 0) {
1408 Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name);
1411 signal = SvIV(ST(1));
1414 /* Set the signal for the thread */
1415 thread = S_SV_to_ithread(aTHX_ ST(0));
1416 MUTEX_LOCK(&thread->mutex);
1417 if (thread->interp) {
1418 dTHXa(thread->interp);
1419 if (PL_psig_pend && PL_psig_ptr[signal]) {
1420 PL_psig_pend[signal]++;
1425 /* Ignore signal to terminated thread */
1428 MUTEX_UNLOCK(&thread->mutex);
1431 Perl_croak(aTHX_ "Signal %s received in thread %"UVuf", but no signal handler set.", sig_name, thread->tid);
1434 /* Return the thread to allow for method chaining */
1436 /* XSRETURN(1); - implied */
1440 ithread_DESTROY(...)
1442 PERL_UNUSED_VAR(items);
1443 sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar);
1451 PERL_UNUSED_VAR(items);
1453 /* Compares TIDs to determine thread equality */
1454 if (sv_isobject(ST(0)) && sv_isobject(ST(1))) {
1455 ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1456 ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
1457 are_equal = (thr1->tid == thr2->tid);
1462 /* Return 0 on false for backward compatibility */
1465 /* XSRETURN(1); - implied */
1479 /* Class method only */
1481 Perl_croak(aTHX_ "Usage: threads->object($tid)");
1483 classname = (char *)SvPV_nolen(ST(0));
1485 /* Turn $tid from PVLV to SV if needed (bug #73330) */
1489 if ((items < 2) || ! SvOK(arg)) {
1493 /* threads->object($tid) */
1496 /* If current thread wants its own object, then behave the same as
1498 thread = S_ithread_get(aTHX);
1499 if (thread->tid == tid) {
1500 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1504 /* Walk through threads list */
1505 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1506 for (thread = MY_POOL.main_thread.next;
1507 thread != &MY_POOL.main_thread;
1508 thread = thread->next)
1511 if (thread->tid == tid) {
1512 /* Ignore if detached or joined */
1513 MUTEX_LOCK(&thread->mutex);
1514 state = thread->state;
1515 MUTEX_UNLOCK(&thread->mutex);
1516 if (! (state & PERL_ITHR_UNCALLABLE)) {
1517 /* Put object on stack */
1518 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1524 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1530 /* XSRETURN(1); - implied */
1534 ithread__handle(...);
1538 PERL_UNUSED_VAR(items);
1539 thread = S_SV_to_ithread(aTHX_ ST(0));
1541 XST_mUV(0, PTR2UV(&thread->handle));
1543 XST_mUV(0, PTR2UV(&thread->thr));
1545 /* XSRETURN(1); - implied */
1549 ithread_get_stack_size(...)
1554 PERL_UNUSED_VAR(items);
1555 if (sv_isobject(ST(0))) {
1556 /* $thr->get_stack_size() */
1557 ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1558 stack_size = thread->stack_size;
1560 /* threads->get_stack_size() */
1561 stack_size = MY_POOL.default_stack_size;
1563 XST_mIV(0, stack_size);
1564 /* XSRETURN(1); - implied */
1568 ithread_set_stack_size(...)
1574 Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
1576 if (sv_isobject(ST(0))) {
1577 Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
1579 if (! looks_like_number(ST(1))) {
1580 Perl_croak(aTHX_ "Stack size must be numeric");
1583 old_size = MY_POOL.default_stack_size;
1584 MY_POOL.default_stack_size = S_good_stack_size(aTHX_ SvIV(ST(1)));
1585 XST_mIV(0, old_size);
1586 /* XSRETURN(1); - implied */
1590 ithread_is_running(...)
1594 /* Object method only */
1595 if ((items != 1) || ! sv_isobject(ST(0))) {
1596 Perl_croak(aTHX_ "Usage: $thr->is_running()");
1599 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1600 MUTEX_LOCK(&thread->mutex);
1601 ST(0) = (thread->state & PERL_ITHR_FINISHED) ? &PL_sv_no : &PL_sv_yes;
1602 MUTEX_UNLOCK(&thread->mutex);
1603 /* XSRETURN(1); - implied */
1607 ithread_is_detached(...)
1611 PERL_UNUSED_VAR(items);
1612 thread = S_SV_to_ithread(aTHX_ ST(0));
1613 MUTEX_LOCK(&thread->mutex);
1614 ST(0) = (thread->state & PERL_ITHR_DETACHED) ? &PL_sv_yes : &PL_sv_no;
1615 MUTEX_UNLOCK(&thread->mutex);
1616 /* XSRETURN(1); - implied */
1620 ithread_is_joinable(...)
1624 /* Object method only */
1625 if ((items != 1) || ! sv_isobject(ST(0))) {
1626 Perl_croak(aTHX_ "Usage: $thr->is_joinable()");
1629 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1630 MUTEX_LOCK(&thread->mutex);
1631 ST(0) = ((thread->state & PERL_ITHR_FINISHED) &&
1632 ! (thread->state & PERL_ITHR_UNCALLABLE))
1633 ? &PL_sv_yes : &PL_sv_no;
1634 MUTEX_UNLOCK(&thread->mutex);
1635 /* XSRETURN(1); - implied */
1639 ithread_wantarray(...)
1643 PERL_UNUSED_VAR(items);
1644 thread = S_SV_to_ithread(aTHX_ ST(0));
1645 ST(0) = ((thread->gimme & G_WANT) == G_ARRAY) ? &PL_sv_yes :
1646 ((thread->gimme & G_WANT) == G_VOID) ? &PL_sv_undef
1647 /* G_SCALAR */ : &PL_sv_no;
1648 /* XSRETURN(1); - implied */
1652 ithread_set_thread_exit_only(...)
1657 Perl_croak(aTHX_ "Usage: ->set_thread_exit_only(boolean)");
1659 thread = S_SV_to_ithread(aTHX_ ST(0));
1660 MUTEX_LOCK(&thread->mutex);
1661 if (SvTRUE(ST(1))) {
1662 thread->state |= PERL_ITHR_THREAD_EXIT_ONLY;
1664 thread->state &= ~PERL_ITHR_THREAD_EXIT_ONLY;
1666 MUTEX_UNLOCK(&thread->mutex);
1675 /* Object method only */
1676 if ((items != 1) || ! sv_isobject(ST(0))) {
1677 Perl_croak(aTHX_ "Usage: $thr->err()");
1680 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1681 MUTEX_LOCK(&thread->mutex);
1683 /* If thread died, then clone the error into the calling thread */
1684 if (thread->state & PERL_ITHR_DIED) {
1685 #if (PERL_VERSION < 13) || (PERL_VERSION == 13 && PERL_SUBVERSION <= 1)
1686 PerlInterpreter *other_perl;
1687 CLONE_PARAMS clone_params;
1688 ithread *current_thread;
1690 other_perl = thread->interp;
1691 clone_params.stashes = newAV();
1692 clone_params.flags = CLONEf_JOIN_IN;
1693 PL_ptr_table = ptr_table_new();
1694 current_thread = S_ithread_get(aTHX);
1695 S_ithread_set(aTHX_ thread);
1696 /* Ensure 'meaningful' addresses retain their meaning */
1697 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1698 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1699 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1700 err = sv_dup(thread->err, &clone_params);
1701 S_ithread_set(aTHX_ current_thread);
1702 SvREFCNT_dec(clone_params.stashes);
1703 SvREFCNT_inc_void(err);
1704 /* If error was an object, bless it into the correct class */
1705 if (thread->err_class) {
1706 sv_bless(err, gv_stashpv(thread->err_class, 1));
1708 ptr_table_free(PL_ptr_table);
1709 PL_ptr_table = NULL;
1711 PerlInterpreter *other_perl = thread->interp;
1712 CLONE_PARAMS *clone_params = Perl_clone_params_new(other_perl, aTHX);
1713 ithread *current_thread;
1715 clone_params->flags |= CLONEf_JOIN_IN;
1716 PL_ptr_table = ptr_table_new();
1717 current_thread = S_ithread_get(aTHX);
1718 S_ithread_set(aTHX_ thread);
1719 /* Ensure 'meaningful' addresses retain their meaning */
1720 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1721 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1722 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1723 err = sv_dup(thread->err, clone_params);
1724 S_ithread_set(aTHX_ current_thread);
1725 Perl_clone_params_del(clone_params);
1726 SvREFCNT_inc_void(err);
1727 /* If error was an object, bless it into the correct class */
1728 if (thread->err_class) {
1729 sv_bless(err, gv_stashpv(thread->err_class, 1));
1731 ptr_table_free(PL_ptr_table);
1732 PL_ptr_table = NULL;
1736 MUTEX_UNLOCK(&thread->mutex);
1742 ST(0) = sv_2mortal(err);
1743 /* XSRETURN(1); - implied */
1746 #endif /* USE_ITHREADS */
1752 SV *my_pool_sv = *hv_fetch(PL_modglobal, MY_POOL_KEY,
1753 sizeof(MY_POOL_KEY)-1, TRUE);
1754 my_pool_t *my_poolp = (my_pool_t*)SvPVX(newSV(sizeof(my_pool_t)-1));
1758 Zero(my_poolp, 1, my_pool_t);
1759 sv_setuv(my_pool_sv, PTR2UV(my_poolp));
1761 PL_perl_destruct_level = 2;
1762 MUTEX_INIT(&MY_POOL.create_destruct_mutex);
1763 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1765 PL_threadhook = &Perl_ithread_hook;
1767 MY_POOL.tid_counter = 1;
1768 # ifdef THREAD_CREATE_NEEDS_STACK
1769 MY_POOL.default_stack_size = THREAD_CREATE_NEEDS_STACK;
1772 /* The 'main' thread is thread 0.
1773 * It is detached (unjoinable) and immortal.
1776 MUTEX_INIT(&MY_POOL.main_thread.mutex);
1778 /* Head of the threads list */
1779 MY_POOL.main_thread.next = &MY_POOL.main_thread;
1780 MY_POOL.main_thread.prev = &MY_POOL.main_thread;
1782 MY_POOL.main_thread.count = 1; /* Immortal */
1784 MY_POOL.main_thread.interp = aTHX;
1785 MY_POOL.main_thread.state = PERL_ITHR_DETACHED; /* Detached */
1786 MY_POOL.main_thread.stack_size = MY_POOL.default_stack_size;
1788 MY_POOL.main_thread.thr = GetCurrentThreadId();
1790 MY_POOL.main_thread.thr = pthread_self();
1793 S_ithread_set(aTHX_ &MY_POOL.main_thread);
1794 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1795 #endif /* USE_ITHREADS */