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))
31 #ifndef PERL_UNUSED_RESULT
32 # if defined(__GNUC__) && defined(HASATTRIBUTE_WARN_UNUSED_RESULT)
33 # define PERL_UNUSED_RESULT(v) STMT_START { __typeof__(v) z = (v); (void)sizeof(z); } STMT_END
35 # define PERL_UNUSED_RESULT(v) ((void)(v))
43 # define YIELD sleep(0)
47 /* Supposed to be in Winbase.h */
48 # ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
49 # define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
51 # include <win32thread.h>
54 typedef perl_os_thread pthread_t;
59 # define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
60 # ifdef OLD_PTHREADS_API
61 # define PERL_THREAD_DETACH(t) pthread_detach(&(t))
63 # define PERL_THREAD_DETACH(t) pthread_detach((t))
66 #if !defined(HAS_GETPAGESIZE) && defined(I_SYS_PARAM)
67 # include <sys/param.h>
70 /* Values for 'state' member */
71 #define PERL_ITHR_DETACHED 1 /* Thread has been detached */
72 #define PERL_ITHR_JOINED 2 /* Thread is being / has been joined */
73 #define PERL_ITHR_FINISHED 4 /* Thread has finished execution */
74 #define PERL_ITHR_THREAD_EXIT_ONLY 8 /* exit() only exits current thread */
75 #define PERL_ITHR_NONVIABLE 16 /* Thread creation failed */
76 #define PERL_ITHR_DIED 32 /* Thread finished by dying */
78 #define PERL_ITHR_UNCALLABLE (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)
81 typedef struct _ithread {
82 struct _ithread *next; /* Next thread in the list */
83 struct _ithread *prev; /* Prev thread in the list */
84 PerlInterpreter *interp; /* The threads interpreter */
85 UV tid; /* Threads module's thread id */
86 perl_mutex mutex; /* Mutex for updating things in this struct */
87 int count; /* Reference count. See S_ithread_create. */
88 int state; /* Detached, joined, finished, etc. */
89 int gimme; /* Context of create */
90 SV *init_function; /* Code to run */
91 AV *params; /* Args to pass function */
93 DWORD thr; /* OS's idea if thread id */
94 HANDLE handle; /* OS's waitable handle */
96 pthread_t thr; /* OS's handle for the thread */
99 SV *err; /* Error from abnormally terminated thread */
100 char *err_class; /* Error object's classname if applicable */
102 sigset_t initial_sigmask; /* Thread wakes up with signals blocked */
107 #define MY_CXT_KEY "threads::_cxt" XS_VERSION
110 /* Used by Perl interpreter for thread context switching */
117 #define MY_POOL_KEY "threads::_pool" XS_VERSION
120 /* Structure for 'main' thread
121 * Also forms the 'base' for the doubly-linked list of threads */
124 /* Protects the creation and destruction of threads*/
125 perl_mutex create_destruct_mutex;
132 IV default_stack_size;
137 SV *my_pool_sv = *hv_fetch(PL_modglobal, MY_POOL_KEY, \
138 sizeof(MY_POOL_KEY)-1, TRUE); \
139 my_pool_t *my_poolp = INT2PTR(my_pool_t*, SvUV(my_pool_sv))
141 #define MY_POOL (*my_poolp)
143 #if defined(WIN32) || (defined(__amigaos4__) && defined(__NEWLIB__))
144 # undef THREAD_SIGNAL_BLOCKING
146 # define THREAD_SIGNAL_BLOCKING
149 #ifdef THREAD_SIGNAL_BLOCKING
151 /* Block most signals for calling thread, setting the old signal mask to
152 * oldmask, if it is not NULL */
154 S_block_most_signals(sigset_t *oldmask)
158 sigfillset(&newmask);
159 /* Don't block certain "important" signals (stolen from mg.c) */
161 sigdelset(&newmask, SIGILL);
164 sigdelset(&newmask, SIGBUS);
167 sigdelset(&newmask, SIGSEGV);
171 /* no per-thread blocking available */
172 return sigprocmask(SIG_BLOCK, &newmask, oldmask);
174 return pthread_sigmask(SIG_BLOCK, &newmask, oldmask);
178 /* Set the signal mask for this thread to newmask */
180 S_set_sigmask(sigset_t *newmask)
183 return sigprocmask(SIG_SETMASK, newmask, NULL);
185 return pthread_sigmask(SIG_SETMASK, newmask, NULL);
190 /* Used by Perl interpreter for thread context switching */
192 S_ithread_set(pTHX_ ithread *thread)
195 MY_CXT.context = thread;
202 return (MY_CXT.context);
206 /* Free any data (such as the Perl interpreter) attached to an ithread
207 * structure. This is a bit like undef on SVs, where the SV isn't freed,
208 * but the PVX is. Must be called with thread->mutex already locked. Also,
209 * must be called with MY_POOL.create_destruct_mutex unlocked as destruction
210 * of the interpreter can lead to recursive destruction calls that could
211 * lead to a deadlock on that mutex.
214 S_ithread_clear(pTHX_ ithread *thread)
216 PerlInterpreter *interp;
221 assert(((thread->state & PERL_ITHR_FINISHED) &&
222 (thread->state & PERL_ITHR_UNCALLABLE))
224 (thread->state & PERL_ITHR_NONVIABLE));
226 #ifdef THREAD_SIGNAL_BLOCKING
227 /* We temporarily set the interpreter context to the interpreter being
228 * destroyed. It's in no condition to handle signals while it's being
231 S_block_most_signals(&origmask);
234 interp = thread->interp;
238 PERL_SET_CONTEXT(interp);
239 S_ithread_set(aTHX_ thread);
241 SvREFCNT_dec(thread->params);
242 thread->params = NULL;
245 SvREFCNT_dec(thread->err);
246 thread->err = Nullsv;
249 perl_destruct(interp);
251 thread->interp = NULL;
254 PERL_SET_CONTEXT(aTHX);
255 #ifdef THREAD_SIGNAL_BLOCKING
256 S_set_sigmask(&origmask);
261 /* Decrement the refcount of an ithread, and if it reaches zero, free it.
262 * Must be called with the mutex held.
263 * On return, mutex is released (or destroyed).
266 S_ithread_free(pTHX_ ithread *thread)
267 PERL_TSA_RELEASE(thread->mutex)
274 if (! (thread->state & PERL_ITHR_NONVIABLE)) {
275 assert(thread->count > 0);
276 if (--thread->count > 0) {
277 MUTEX_UNLOCK(&thread->mutex);
280 assert((thread->state & PERL_ITHR_FINISHED) &&
281 (thread->state & PERL_ITHR_UNCALLABLE));
283 MUTEX_UNLOCK(&thread->mutex);
285 /* Main thread (0) is immortal and should never get here */
286 assert(thread->tid != 0);
288 /* Remove from circular list of threads */
289 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
290 assert(thread->prev && thread->next);
291 thread->next->prev = thread->prev;
292 thread->prev->next = thread->next;
295 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
297 /* Thread is now disowned */
298 MUTEX_LOCK(&thread->mutex);
299 S_ithread_clear(aTHX_ thread);
302 handle = thread->handle;
303 thread->handle = NULL;
305 MUTEX_UNLOCK(&thread->mutex);
306 MUTEX_DESTROY(&thread->mutex);
314 PerlMemShared_free(thread);
316 /* total_threads >= 1 is used to veto cleanup by the main thread,
317 * should it happen to exit while other threads still exist.
318 * Decrement this as the very last thing in the thread's existence.
319 * Otherwise, MY_POOL and global state such as PL_op_mutex may get
320 * freed while we're still using it.
322 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
323 MY_POOL.total_threads--;
324 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
329 S_ithread_count_inc(pTHX_ ithread *thread)
330 PERL_TSA_EXCLUDES(thread->mutex)
332 MUTEX_LOCK(&thread->mutex);
334 MUTEX_UNLOCK(&thread->mutex);
338 /* Warn if exiting with any unjoined threads */
342 int veto_cleanup, warn;
345 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
346 veto_cleanup = (MY_POOL.total_threads > 0);
347 warn = (MY_POOL.running_threads || MY_POOL.joinable_threads);
348 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
351 if (ckWARN_d(WARN_THREADS)) {
352 Perl_warn(aTHX_ "Perl exited with active threads:\n\t%"
353 IVdf " running and unjoined\n\t%"
354 IVdf " finished and unjoined\n\t%"
355 IVdf " running and detached\n",
356 MY_POOL.running_threads,
357 MY_POOL.joinable_threads,
358 MY_POOL.detached_threads);
362 return (veto_cleanup);
366 /* Called from perl_destruct() in each thread. If it's the main thread,
367 * stop it from freeing everything if there are other threads still running.
370 Perl_ithread_hook(pTHX)
373 return ((aTHX == MY_POOL.main_thread.interp) ? S_exit_warning(aTHX) : 0);
377 /* MAGIC (in mg.h sense) hooks */
380 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
382 ithread *thread = (ithread *)mg->mg_ptr;
383 SvIV_set(sv, PTR2IV(thread));
389 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
391 ithread *thread = (ithread *)mg->mg_ptr;
393 MUTEX_LOCK(&thread->mutex);
394 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
399 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
401 PERL_UNUSED_ARG(param);
402 S_ithread_count_inc(aTHX_ (ithread *)mg->mg_ptr);
406 STATIC const MGVTBL ithread_vtbl = {
407 ithread_mg_get, /* get */
411 ithread_mg_free, /* free */
413 ithread_mg_dup, /* dup */
414 #if (PERL_VERSION > 8) || (PERL_VERSION == 8 && PERL_SUBVERSION > 8)
420 /* Provided default, minimum and rational stack sizes */
422 S_good_stack_size(pTHX_ IV stack_size)
426 /* Use default stack size if no stack size specified */
428 return (MY_POOL.default_stack_size);
431 #ifdef PTHREAD_STACK_MIN
432 /* Can't use less than minimum */
433 if (stack_size < PTHREAD_STACK_MIN) {
434 if (ckWARN(WARN_THREADS)) {
435 Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
437 return (PTHREAD_STACK_MIN);
441 /* Round up to page size boundary */
442 if (MY_POOL.page_size <= 0) {
443 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
444 SETERRNO(0, SS_NORMAL);
446 MY_POOL.page_size = sysconf(_SC_PAGESIZE);
448 MY_POOL.page_size = sysconf(_SC_MMAP_PAGE_SIZE);
450 if ((long)MY_POOL.page_size < 0) {
452 SV * const error = get_sv("@", 0);
453 (void)SvUPGRADE(error, SVt_PV);
454 Perl_croak(aTHX_ "PANIC: sysconf: %s", SvPV_nolen(error));
456 Perl_croak(aTHX_ "PANIC: sysconf: pagesize unknown");
460 # ifdef HAS_GETPAGESIZE
461 MY_POOL.page_size = getpagesize();
463 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
464 MY_POOL.page_size = PAGESIZE;
466 MY_POOL.page_size = 8192; /* A conservative default */
469 if (MY_POOL.page_size <= 0) {
470 Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)MY_POOL.page_size);
474 stack_size = ((stack_size + (MY_POOL.page_size - 1)) / MY_POOL.page_size) * MY_POOL.page_size;
480 /* Starts executing the thread.
481 * Passed as the C level function to run in the new thread.
484 STATIC THREAD_RET_TYPE
485 S_ithread_run(LPVOID arg)
488 S_ithread_run(void * arg)
491 ithread *thread = (ithread *)arg;
493 volatile I32 oldscope;
494 volatile int exit_app = 0; /* Thread terminated using 'exit' */
495 volatile int exit_code = 0;
496 volatile int died = 0; /* Thread terminated abnormally */
500 dTHXa(thread->interp);
504 /* The following mutex lock + mutex unlock pair explained.
507 * - calls ithread_create (and S_ithread_create), which:
508 * - creates the new thread
509 * - does MUTEX_LOCK(&thread->mutex)
510 * - calls pthread_create(..., S_ithread_run,...)
512 * - starts the S_ithread_run (where we are now), which:
513 * - tries to MUTEX_LOCK(&thread->mutex)
516 * - continues doing more createy stuff
517 * - does MUTEX_UNLOCK(&thread->mutex)
520 * - finishes MUTEX_LOCK(&thread->mutex)
521 * - does MUTEX_UNLOCK(&thread->mutex)
524 MUTEX_LOCK(&thread->mutex);
525 MUTEX_UNLOCK(&thread->mutex);
527 PERL_SET_CONTEXT(thread->interp);
528 S_ithread_set(aTHX_ thread);
530 #ifdef THREAD_SIGNAL_BLOCKING
531 /* Thread starts with most signals blocked - restore the signal mask from
532 * the ithread struct.
534 S_set_sigmask(&thread->initial_sigmask);
537 PL_perl_destruct_level = 2;
540 AV *params = thread->params;
541 volatile int len = (int)av_len(params)+1;
548 /* Put args on the stack */
550 for (ii=0; ii < len; ii++) {
551 XPUSHs(av_shift(params));
555 oldscope = PL_scopestack_ix;
558 /* Run the specified function */
559 len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
560 } else if (jmp_rc == 2) {
563 exit_code = STATUS_CURRENT;
564 while (PL_scopestack_ix > oldscope) {
570 #ifdef THREAD_SIGNAL_BLOCKING
571 /* The interpreter is finished, so this thread can stop receiving
572 * signals. This way, our signal handler doesn't get called in the
573 * middle of our parent thread calling perl_destruct()...
575 S_block_most_signals(NULL);
578 /* Remove args from stack and put back in params array */
580 for (ii=len-1; ii >= 0; ii--) {
582 if (jmp_rc == 0 && (thread->gimme & G_WANT) != G_VOID) {
583 av_store(params, ii, SvREFCNT_inc(sv));
590 /* Check for abnormal termination */
592 died = PERL_ITHR_DIED;
593 thread->err = newSVsv(ERRSV);
594 /* If ERRSV is an object, remember the classname and then
595 * rebless into 'main' so it will survive 'cloning'
597 if (sv_isobject(thread->err)) {
598 thread->err_class = HvNAME(SvSTASH(SvRV(thread->err)));
599 sv_bless(thread->err, gv_stashpv("main", 0));
602 if (ckWARN_d(WARN_THREADS)) {
603 oldscope = PL_scopestack_ix;
606 /* Warn that thread died */
607 Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV);
608 } else if (jmp_rc == 2) {
609 /* Warn handler exited */
611 exit_code = STATUS_CURRENT;
612 while (PL_scopestack_ix > oldscope) {
620 /* Release function ref */
621 SvREFCNT_dec(thread->init_function);
622 thread->init_function = Nullsv;
625 PerlIO_flush((PerlIO *)NULL);
627 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
628 MUTEX_LOCK(&thread->mutex);
629 /* Mark as finished */
630 thread->state |= (PERL_ITHR_FINISHED | died);
631 /* Clear exit flag if required */
632 if (thread->state & PERL_ITHR_THREAD_EXIT_ONLY) {
636 /* Adjust thread status counts */
637 if (thread->state & PERL_ITHR_DETACHED) {
638 MY_POOL.detached_threads--;
640 MY_POOL.running_threads--;
641 MY_POOL.joinable_threads++;
643 MUTEX_UNLOCK(&thread->mutex);
644 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
646 /* Exit application if required */
648 oldscope = PL_scopestack_ix;
651 /* Warn if there are unjoined threads */
652 S_exit_warning(aTHX);
653 } else if (jmp_rc == 2) {
654 /* Warn handler exited */
655 exit_code = STATUS_CURRENT;
656 while (PL_scopestack_ix > oldscope) {
665 /* At this point, the interpreter may have been freed, so call
666 * free in the the context of of the 'main' interpreter which
667 * can't have been freed due to the veto_cleanup mechanism.
669 aTHX = MY_POOL.main_thread.interp;
671 MUTEX_LOCK(&thread->mutex);
672 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
682 /* Type conversion helper functions */
685 S_ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
691 S_ithread_count_inc(aTHX_ thread);
697 sv = newSVrv(obj, classname);
698 sv_setiv(sv, PTR2IV(thread));
699 mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0);
700 mg->mg_flags |= MGf_DUP;
707 S_SV_to_ithread(pTHX_ SV *sv)
709 /* Argument is a thread */
711 return (INT2PTR(ithread *, SvIV(SvRV(sv))));
713 /* Argument is classname, therefore return current thread */
714 return (S_ithread_get(aTHX));
719 * Called in context of parent thread.
720 * Called with my_pool->create_destruct_mutex locked.
721 * (Unlocked both on error and on success.)
725 PerlInterpreter *parent_perl,
733 PERL_TSA_RELEASE(my_pool->create_destruct_mutex)
737 ithread *current_thread = S_ithread_get(aTHX);
741 #if PERL_VERSION <= 8 && PERL_SUBVERSION <= 7
742 SV **tmps_tmp = PL_tmps_stack;
743 IV tmps_ix = PL_tmps_ix;
746 int rc_stack_size = 0;
747 int rc_thread_create = 0;
750 /* Allocate thread structure in context of the main thread's interpreter */
752 PERL_SET_CONTEXT(my_pool->main_thread.interp);
753 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
755 PERL_SET_CONTEXT(aTHX);
757 /* This lock was acquired in ithread_create()
758 * prior to calling S_ithread_create(). */
759 MUTEX_UNLOCK(&my_pool->create_destruct_mutex);
761 int fd = PerlIO_fileno(Perl_error_log);
763 /* If there's no error_log, we cannot scream about it missing. */
764 PERL_UNUSED_RESULT(PerlLIO_write(fd, PL_no_mem, strlen(PL_no_mem)));
769 Zero(thread, 1, ithread);
771 /* Add to threads list */
772 thread->next = &my_pool->main_thread;
773 thread->prev = my_pool->main_thread.prev;
774 my_pool->main_thread.prev = thread;
775 thread->prev->next = thread;
776 my_pool->total_threads++;
778 /* 1 ref to be held by the local var 'thread' in S_ithread_run().
779 * 1 ref to be held by the threads object that we assume we will
780 * be embedded in upon our return.
781 * 1 ref to be the responsibility of join/detach, so we don't get
782 * freed until join/detach, even if no thread objects remain.
783 * This allows the following to work:
784 * { threads->create(sub{...}); } threads->object(1)->join;
788 /* Block new thread until ->create() call finishes */
789 MUTEX_INIT(&thread->mutex);
790 MUTEX_LOCK(&thread->mutex); /* See S_ithread_run() for more detail. */
792 thread->tid = my_pool->tid_counter++;
793 thread->stack_size = S_good_stack_size(aTHX_ stack_size);
794 thread->gimme = gimme;
795 thread->state = exit_opt;
797 /* "Clone" our interpreter into the thread's interpreter.
798 * This gives thread access to "static data" and code.
800 PerlIO_flush((PerlIO *)NULL);
801 S_ithread_set(aTHX_ thread);
803 SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct value */
804 PL_srand_called = FALSE; /* Set it to false so we can detect if it gets
805 set during the clone */
807 #ifdef THREAD_SIGNAL_BLOCKING
808 /* perl_clone() will leave us the new interpreter's context. This poses
809 * two problems for our signal handler. First, it sets the new context
810 * before the new interpreter struct is fully initialized, so our signal
811 * handler might find bogus data in the interpreter struct it gets.
812 * Second, even if the interpreter is initialized before a signal comes in,
813 * we would like to avoid that interpreter receiving notifications for
814 * signals (especially when they ought to be for the one running in this
815 * thread), until it is running in its own thread. Another problem is that
816 * the new thread will not have set the context until some time after it
817 * has started, so it won't be safe for our signal handler to run until
820 * So we block most signals here, so the new thread will inherit the signal
821 * mask, and unblock them right after the thread creation. The original
822 * mask is saved in the thread struct so that the new thread can restore
825 S_block_most_signals(&thread->initial_sigmask);
829 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
831 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
834 /* perl_clone() leaves us in new interpreter's context. As it is tricky
835 * to spot an implicit aTHX, create a new scope with aTHX matching the
836 * context for the duration of our work for new interpreter.
839 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION > 1)
840 CLONE_PARAMS *clone_param = Perl_clone_params_new(aTHX, thread->interp);
842 CLONE_PARAMS clone_param_s;
843 CLONE_PARAMS *clone_param = &clone_param_s;
845 dTHXa(thread->interp);
849 #if (PERL_VERSION < 13) || (PERL_VERSION == 13 && PERL_SUBVERSION <= 1)
850 clone_param->flags = 0;
853 /* Here we remove END blocks since they should only run in the thread
856 SvREFCNT_dec(PL_endav);
859 if (SvPOK(init_function)) {
860 thread->init_function = newSV(0);
861 sv_copypv(thread->init_function, init_function);
863 thread->init_function = sv_dup_inc(init_function, clone_param);
866 thread->params = params = newAV();
867 av_extend(params, num_params - 1);
868 AvFILLp(params) = num_params - 1;
869 array = AvARRAY(params);
871 /* params_start is an offset onto the Perl stack. This can be
872 reallocated (and hence move) as a side effect of calls to
873 perl_clone() and sv_dup_inc(). Hence copy the parameters
874 somewhere under our control first, before duplicating. */
875 #if (PERL_VERSION > 8)
876 Copy(parent_perl->Istack_base + params_start, array, num_params, SV *);
878 Copy(parent_perl->Tstack_base + params_start, array, num_params, SV *);
880 while (num_params--) {
881 *array = sv_dup_inc(*array, clone_param);
884 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION > 1)
885 Perl_clone_params_del(clone_param);
888 #if PERL_VERSION <= 8 && PERL_SUBVERSION <= 7
889 /* The code below checks that anything living on the tmps stack and
890 * has been cloned (so it lives in the ptr_table) has a refcount
893 * If the refcount is 0 it means that a something on the stack/context
894 * was holding a reference to it and since we init_stacks() in
895 * perl_clone that won't get cleaned and we will get a leaked scalar.
896 * The reason it was cloned was that it lived on the @_ stack.
898 * Example of this can be found in bugreport 15837 where calls in the
899 * parameter list end up as a temp.
901 * As of 5.8.8 this is done in perl_clone.
903 while (tmps_ix > 0) {
904 SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
906 if (sv && SvREFCNT(sv) == 0) {
907 SvREFCNT_inc_void(sv);
913 SvTEMP_off(thread->init_function);
914 ptr_table_free(PL_ptr_table);
916 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
918 S_ithread_set(aTHX_ current_thread);
919 PERL_SET_CONTEXT(aTHX);
921 /* Create/start the thread */
923 thread->handle = CreateThread(NULL,
924 (DWORD)thread->stack_size,
927 STACK_SIZE_PARAM_IS_A_RESERVATION,
931 STATIC pthread_attr_t attr;
932 STATIC int attr_inited = 0;
933 STATIC int attr_joinable = PTHREAD_CREATE_JOINABLE;
935 pthread_attr_init(&attr);
939 # ifdef PTHREAD_ATTR_SETDETACHSTATE
940 /* Threads start out joinable */
941 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
944 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
945 /* Set thread's stack size */
946 if (thread->stack_size > 0) {
947 rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
951 /* Create the thread */
952 if (! rc_stack_size) {
953 # ifdef OLD_PTHREADS_API
954 rc_thread_create = pthread_create(&thread->thr,
959 # if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
960 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
962 rc_thread_create = pthread_create(&thread->thr,
969 #ifdef THREAD_SIGNAL_BLOCKING
970 /* Now it's safe to accept signals, since we're in our own interpreter's
971 * context and we have created the thread.
973 S_set_sigmask(&thread->initial_sigmask);
976 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
977 /* Try to get thread's actual stack size */
981 stacksize = pthread_attr_getstacksize(attr);
983 if (! pthread_attr_getstacksize(&attr, &stacksize))
986 thread->stack_size = (IV)stacksize;
993 /* Check for errors */
995 if (thread->handle == NULL) {
997 if (rc_stack_size || rc_thread_create) {
999 /* Must unlock mutex for destruct call */
1000 /* This lock was acquired in ithread_create()
1001 * prior to calling S_ithread_create(). */
1002 MUTEX_UNLOCK(&my_pool->create_destruct_mutex);
1003 thread->state |= PERL_ITHR_NONVIABLE;
1004 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
1006 if (ckWARN_d(WARN_THREADS)) {
1007 if (rc_stack_size) {
1008 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
1010 Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
1017 my_pool->running_threads++;
1018 MUTEX_UNLOCK(&my_pool->create_destruct_mutex);
1022 #endif /* USE_ITHREADS */
1025 MODULE = threads PACKAGE = threads PREFIX = ithread_
1035 SV *function_to_call;
1040 SV *thread_exit_only;
1045 if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
1047 Perl_croak(aTHX_ "Usage: threads->create(\\%%specs, function, ...)");
1049 specs = (HV*)SvRV(ST(1));
1053 Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
1059 if (sv_isobject(ST(0))) {
1060 /* $thr->create() */
1061 classname = HvNAME(SvSTASH(SvRV(ST(0))));
1062 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1063 MUTEX_LOCK(&thread->mutex);
1064 stack_size = thread->stack_size;
1065 exit_opt = thread->state & PERL_ITHR_THREAD_EXIT_ONLY;
1066 MUTEX_UNLOCK(&thread->mutex);
1068 /* threads->create() */
1069 classname = (char *)SvPV_nolen(ST(0));
1070 stack_size = MY_POOL.default_stack_size;
1071 thread_exit_only = get_sv("threads::thread_exit_only", GV_ADD);
1072 exit_opt = (SvTRUE(thread_exit_only))
1073 ? PERL_ITHR_THREAD_EXIT_ONLY : 0;
1076 function_to_call = ST(idx+1);
1082 if ((svp = hv_fetch(specs, "stack", 5, 0))) {
1083 stack_size = SvIV(*svp);
1084 } else if ((svp = hv_fetch(specs, "stacksize", 9, 0))) {
1085 stack_size = SvIV(*svp);
1086 } else if ((svp = hv_fetch(specs, "stack_size", 10, 0))) {
1087 stack_size = SvIV(*svp);
1091 if ((svp = hv_fetch(specs, "context", 7, 0))) {
1092 str = (char *)SvPV_nolen(*svp);
1109 Perl_croak(aTHX_ "Invalid context: %s", str);
1111 } else if ((svp = hv_fetch(specs, "array", 5, 0))) {
1115 } else if ((svp = hv_fetch(specs, "list", 4, 0))) {
1119 } else if ((svp = hv_fetch(specs, "scalar", 6, 0))) {
1123 } else if ((svp = hv_fetch(specs, "void", 4, 0))) {
1129 /* exit => thread_only */
1130 if ((svp = hv_fetch(specs, "exit", 4, 0))) {
1131 str = (char *)SvPV_nolen(*svp);
1132 exit_opt = (*str == 't' || *str == 'T')
1133 ? PERL_ITHR_THREAD_EXIT_ONLY : 0;
1136 if (context == -1) {
1137 context = GIMME_V; /* Implicit context */
1139 context |= (GIMME_V & (~(G_ARRAY|G_SCALAR|G_VOID)));
1143 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1144 thread = S_ithread_create(aTHX_ &MY_POOL,
1150 items > 2 ? items - 2 : 0);
1152 XSRETURN_UNDEF; /* Mutex already unlocked */
1154 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, FALSE));
1156 /* Let thread run. */
1157 /* See S_ithread_run() for more detail. */
1158 MUTEX_UNLOCK(&thread->mutex);
1160 /* XSRETURN(1); - implied */
1170 int want_running = 0;
1174 /* Class method only */
1176 Perl_croak(aTHX_ "Usage: threads->list(...)");
1178 classname = (char *)SvPV_nolen(ST(0));
1180 /* Calling context */
1181 list_context = (GIMME_V == G_ARRAY);
1183 /* Running or joinable parameter */
1185 want_running = SvTRUE(ST(1));
1188 /* Walk through threads list */
1189 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1190 for (thread = MY_POOL.main_thread.next;
1191 thread != &MY_POOL.main_thread;
1192 thread = thread->next)
1194 MUTEX_LOCK(&thread->mutex);
1195 state = thread->state;
1196 MUTEX_UNLOCK(&thread->mutex);
1198 /* Ignore detached or joined threads */
1199 if (state & PERL_ITHR_UNCALLABLE) {
1203 /* Filter per parameter */
1206 if (state & PERL_ITHR_FINISHED) {
1207 continue; /* Not running */
1210 if (! (state & PERL_ITHR_FINISHED)) {
1211 continue; /* Still running - not joinable yet */
1216 /* Push object on stack if list context */
1218 XPUSHs(sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
1222 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1223 /* If scalar context, send back count */
1224 if (! list_context) {
1235 /* Class method only */
1236 if ((items != 1) || SvROK(ST(0))) {
1237 Perl_croak(aTHX_ "Usage: threads->self()");
1239 classname = (char *)SvPV_nolen(ST(0));
1241 thread = S_ithread_get(aTHX);
1243 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1244 /* XSRETURN(1); - implied */
1252 PERL_UNUSED_VAR(items);
1253 thread = S_SV_to_ithread(aTHX_ ST(0));
1254 XST_mUV(0, thread->tid);
1255 /* XSRETURN(1); - implied */
1262 ithread *current_thread;
1273 /* Object method only */
1274 if ((items != 1) || ! sv_isobject(ST(0))) {
1275 Perl_croak(aTHX_ "Usage: $thr->join()");
1278 /* Check if the thread is joinable and not ourselves */
1279 thread = S_SV_to_ithread(aTHX_ ST(0));
1280 current_thread = S_ithread_get(aTHX);
1282 MUTEX_LOCK(&thread->mutex);
1283 if ((join_err = (thread->state & PERL_ITHR_UNCALLABLE))) {
1284 MUTEX_UNLOCK(&thread->mutex);
1285 Perl_croak(aTHX_ (join_err & PERL_ITHR_DETACHED)
1286 ? "Cannot join a detached thread"
1287 : "Thread already joined");
1288 } else if (thread->tid == current_thread->tid) {
1289 MUTEX_UNLOCK(&thread->mutex);
1290 Perl_croak(aTHX_ "Cannot join self");
1293 /* Mark as joined */
1294 thread->state |= PERL_ITHR_JOINED;
1295 MUTEX_UNLOCK(&thread->mutex);
1297 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1298 MY_POOL.joinable_threads--;
1299 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1301 /* Join the thread */
1303 if (WaitForSingleObject(thread->handle, INFINITE) != WAIT_OBJECT_0) {
1304 /* Timeout/abandonment unexpected here; check $^E */
1305 Perl_croak(aTHX_ "PANIC: underlying join failed");
1308 if ((rc_join = pthread_join(thread->thr, &retval)) != 0) {
1309 /* In progress/deadlock/unknown unexpected here; check $! */
1311 Perl_croak(aTHX_ "PANIC: underlying join failed");
1315 MUTEX_LOCK(&thread->mutex);
1316 /* Get the return value from the call_sv */
1317 /* Objects do not survive this process - FIXME */
1318 if ((thread->gimme & G_WANT) != G_VOID) {
1319 #if (PERL_VERSION < 13) || (PERL_VERSION == 13 && PERL_SUBVERSION <= 1)
1321 PerlInterpreter *other_perl;
1322 CLONE_PARAMS clone_params;
1324 params_copy = thread->params;
1325 other_perl = thread->interp;
1326 clone_params.stashes = newAV();
1327 clone_params.flags = CLONEf_JOIN_IN;
1328 PL_ptr_table = ptr_table_new();
1329 S_ithread_set(aTHX_ thread);
1330 /* Ensure 'meaningful' addresses retain their meaning */
1331 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1332 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1333 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1334 params = (AV *)sv_dup((SV*)params_copy, &clone_params);
1335 S_ithread_set(aTHX_ current_thread);
1336 SvREFCNT_dec(clone_params.stashes);
1337 SvREFCNT_inc_void(params);
1338 ptr_table_free(PL_ptr_table);
1339 PL_ptr_table = NULL;
1342 PerlInterpreter *other_perl = thread->interp;
1343 CLONE_PARAMS *clone_params = Perl_clone_params_new(other_perl, aTHX);
1345 params_copy = thread->params;
1346 clone_params->flags |= CLONEf_JOIN_IN;
1347 PL_ptr_table = ptr_table_new();
1348 S_ithread_set(aTHX_ thread);
1349 /* Ensure 'meaningful' addresses retain their meaning */
1350 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1351 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1352 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1353 params = (AV *)sv_dup((SV*)params_copy, clone_params);
1354 S_ithread_set(aTHX_ current_thread);
1355 Perl_clone_params_del(clone_params);
1356 SvREFCNT_inc_void(params);
1357 ptr_table_free(PL_ptr_table);
1358 PL_ptr_table = NULL;
1362 /* If thread didn't die, then we can free its interpreter */
1363 if (! (thread->state & PERL_ITHR_DIED)) {
1364 S_ithread_clear(aTHX_ thread);
1366 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
1368 /* If no return values, then just return */
1373 /* Put return values on stack */
1374 len = (int)AvFILL(params);
1375 for (ii=0; ii <= len; ii++) {
1376 SV* param = av_shift(params);
1377 XPUSHs(sv_2mortal(param));
1380 /* Free return value array */
1381 SvREFCNT_dec(params);
1387 PERL_UNUSED_VAR(items);
1398 PERL_UNUSED_VAR(items);
1400 /* Detach the thread */
1401 thread = S_SV_to_ithread(aTHX_ ST(0));
1402 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1403 MUTEX_LOCK(&thread->mutex);
1404 if (! (detach_err = (thread->state & PERL_ITHR_UNCALLABLE))) {
1405 /* Thread is detachable */
1406 thread->state |= PERL_ITHR_DETACHED;
1408 /* Windows has no 'detach thread' function */
1410 PERL_THREAD_DETACH(thread->thr);
1412 if (thread->state & PERL_ITHR_FINISHED) {
1413 MY_POOL.joinable_threads--;
1415 MY_POOL.running_threads--;
1416 MY_POOL.detached_threads++;
1419 MUTEX_UNLOCK(&thread->mutex);
1420 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1423 Perl_croak(aTHX_ (detach_err & PERL_ITHR_DETACHED)
1424 ? "Thread already detached"
1425 : "Cannot detach a joined thread");
1428 /* If thread is finished and didn't die,
1429 * then we can free its interpreter */
1430 MUTEX_LOCK(&thread->mutex);
1431 if ((thread->state & PERL_ITHR_FINISHED) &&
1432 ! (thread->state & PERL_ITHR_DIED))
1434 S_ithread_clear(aTHX_ thread);
1436 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
1447 /* Must have safe signals */
1448 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) {
1449 Perl_croak(aTHX_ "Cannot signal threads without safe signals");
1452 /* Object method only */
1453 if ((items != 2) || ! sv_isobject(ST(0))) {
1454 Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')");
1458 sig_name = SvPV_nolen(ST(1));
1459 if (isALPHA(*sig_name)) {
1460 if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G') {
1463 if ((signal = whichsig(sig_name)) < 0) {
1464 Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name);
1467 signal = SvIV(ST(1));
1470 /* Set the signal for the thread */
1471 thread = S_SV_to_ithread(aTHX_ ST(0));
1472 MUTEX_LOCK(&thread->mutex);
1473 if (thread->interp && ! (thread->state & PERL_ITHR_FINISHED)) {
1474 dTHXa(thread->interp);
1475 if (PL_psig_pend && PL_psig_ptr[signal]) {
1476 PL_psig_pend[signal]++;
1481 /* Ignore signal to terminated/finished thread */
1484 MUTEX_UNLOCK(&thread->mutex);
1487 Perl_croak(aTHX_ "Signal %s received in thread %"UVuf", but no signal handler set.", sig_name, thread->tid);
1490 /* Return the thread to allow for method chaining */
1492 /* XSRETURN(1); - implied */
1496 ithread_DESTROY(...)
1498 PERL_UNUSED_VAR(items);
1499 sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar);
1507 PERL_UNUSED_VAR(items);
1509 /* Compares TIDs to determine thread equality */
1510 if (sv_isobject(ST(0)) && sv_isobject(ST(1))) {
1511 ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1512 ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
1513 are_equal = (thr1->tid == thr2->tid);
1518 /* Return 0 on false for backward compatibility */
1521 /* XSRETURN(1); - implied */
1535 /* Class method only */
1537 Perl_croak(aTHX_ "Usage: threads->object($tid)");
1539 classname = (char *)SvPV_nolen(ST(0));
1541 /* Turn $tid from PVLV to SV if needed (bug #73330) */
1545 if ((items < 2) || ! SvOK(arg)) {
1549 /* threads->object($tid) */
1552 /* If current thread wants its own object, then behave the same as
1554 thread = S_ithread_get(aTHX);
1555 if (thread->tid == tid) {
1556 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1560 /* Walk through threads list */
1561 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1562 for (thread = MY_POOL.main_thread.next;
1563 thread != &MY_POOL.main_thread;
1564 thread = thread->next)
1567 if (thread->tid == tid) {
1568 /* Ignore if detached or joined */
1569 MUTEX_LOCK(&thread->mutex);
1570 state = thread->state;
1571 MUTEX_UNLOCK(&thread->mutex);
1572 if (! (state & PERL_ITHR_UNCALLABLE)) {
1573 /* Put object on stack */
1574 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1580 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1586 /* XSRETURN(1); - implied */
1590 ithread__handle(...);
1594 PERL_UNUSED_VAR(items);
1595 thread = S_SV_to_ithread(aTHX_ ST(0));
1597 XST_mUV(0, PTR2UV(&thread->handle));
1599 XST_mUV(0, PTR2UV(&thread->thr));
1601 /* XSRETURN(1); - implied */
1605 ithread_get_stack_size(...)
1610 PERL_UNUSED_VAR(items);
1611 if (sv_isobject(ST(0))) {
1612 /* $thr->get_stack_size() */
1613 ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1614 stack_size = thread->stack_size;
1616 /* threads->get_stack_size() */
1617 stack_size = MY_POOL.default_stack_size;
1619 XST_mIV(0, stack_size);
1620 /* XSRETURN(1); - implied */
1624 ithread_set_stack_size(...)
1630 Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
1632 if (sv_isobject(ST(0))) {
1633 Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
1635 if (! looks_like_number(ST(1))) {
1636 Perl_croak(aTHX_ "Stack size must be numeric");
1639 old_size = MY_POOL.default_stack_size;
1640 MY_POOL.default_stack_size = S_good_stack_size(aTHX_ SvIV(ST(1)));
1641 XST_mIV(0, old_size);
1642 /* XSRETURN(1); - implied */
1646 ithread_is_running(...)
1650 /* Object method only */
1651 if ((items != 1) || ! sv_isobject(ST(0))) {
1652 Perl_croak(aTHX_ "Usage: $thr->is_running()");
1655 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1656 MUTEX_LOCK(&thread->mutex);
1657 ST(0) = (thread->state & PERL_ITHR_FINISHED) ? &PL_sv_no : &PL_sv_yes;
1658 MUTEX_UNLOCK(&thread->mutex);
1659 /* XSRETURN(1); - implied */
1663 ithread_is_detached(...)
1667 PERL_UNUSED_VAR(items);
1668 thread = S_SV_to_ithread(aTHX_ ST(0));
1669 MUTEX_LOCK(&thread->mutex);
1670 ST(0) = (thread->state & PERL_ITHR_DETACHED) ? &PL_sv_yes : &PL_sv_no;
1671 MUTEX_UNLOCK(&thread->mutex);
1672 /* XSRETURN(1); - implied */
1676 ithread_is_joinable(...)
1680 /* Object method only */
1681 if ((items != 1) || ! sv_isobject(ST(0))) {
1682 Perl_croak(aTHX_ "Usage: $thr->is_joinable()");
1685 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1686 MUTEX_LOCK(&thread->mutex);
1687 ST(0) = ((thread->state & PERL_ITHR_FINISHED) &&
1688 ! (thread->state & PERL_ITHR_UNCALLABLE))
1689 ? &PL_sv_yes : &PL_sv_no;
1690 MUTEX_UNLOCK(&thread->mutex);
1691 /* XSRETURN(1); - implied */
1695 ithread_wantarray(...)
1699 PERL_UNUSED_VAR(items);
1700 thread = S_SV_to_ithread(aTHX_ ST(0));
1701 ST(0) = ((thread->gimme & G_WANT) == G_ARRAY) ? &PL_sv_yes :
1702 ((thread->gimme & G_WANT) == G_VOID) ? &PL_sv_undef
1703 /* G_SCALAR */ : &PL_sv_no;
1704 /* XSRETURN(1); - implied */
1708 ithread_set_thread_exit_only(...)
1713 Perl_croak(aTHX_ "Usage: ->set_thread_exit_only(boolean)");
1715 thread = S_SV_to_ithread(aTHX_ ST(0));
1716 MUTEX_LOCK(&thread->mutex);
1717 if (SvTRUE(ST(1))) {
1718 thread->state |= PERL_ITHR_THREAD_EXIT_ONLY;
1720 thread->state &= ~PERL_ITHR_THREAD_EXIT_ONLY;
1722 MUTEX_UNLOCK(&thread->mutex);
1731 /* Object method only */
1732 if ((items != 1) || ! sv_isobject(ST(0))) {
1733 Perl_croak(aTHX_ "Usage: $thr->err()");
1736 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1737 MUTEX_LOCK(&thread->mutex);
1739 /* If thread died, then clone the error into the calling thread */
1740 if (thread->state & PERL_ITHR_DIED) {
1741 #if (PERL_VERSION < 13) || (PERL_VERSION == 13 && PERL_SUBVERSION <= 1)
1742 PerlInterpreter *other_perl;
1743 CLONE_PARAMS clone_params;
1744 ithread *current_thread;
1746 other_perl = thread->interp;
1747 clone_params.stashes = newAV();
1748 clone_params.flags = CLONEf_JOIN_IN;
1749 PL_ptr_table = ptr_table_new();
1750 current_thread = S_ithread_get(aTHX);
1751 S_ithread_set(aTHX_ thread);
1752 /* Ensure 'meaningful' addresses retain their meaning */
1753 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1754 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1755 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1756 err = sv_dup(thread->err, &clone_params);
1757 S_ithread_set(aTHX_ current_thread);
1758 SvREFCNT_dec(clone_params.stashes);
1759 SvREFCNT_inc_void(err);
1760 /* If error was an object, bless it into the correct class */
1761 if (thread->err_class) {
1762 sv_bless(err, gv_stashpv(thread->err_class, 1));
1764 ptr_table_free(PL_ptr_table);
1765 PL_ptr_table = NULL;
1767 PerlInterpreter *other_perl = thread->interp;
1768 CLONE_PARAMS *clone_params = Perl_clone_params_new(other_perl, aTHX);
1769 ithread *current_thread;
1771 clone_params->flags |= CLONEf_JOIN_IN;
1772 PL_ptr_table = ptr_table_new();
1773 current_thread = S_ithread_get(aTHX);
1774 S_ithread_set(aTHX_ thread);
1775 /* Ensure 'meaningful' addresses retain their meaning */
1776 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1777 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1778 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1779 err = sv_dup(thread->err, clone_params);
1780 S_ithread_set(aTHX_ current_thread);
1781 Perl_clone_params_del(clone_params);
1782 SvREFCNT_inc_void(err);
1783 /* If error was an object, bless it into the correct class */
1784 if (thread->err_class) {
1785 sv_bless(err, gv_stashpv(thread->err_class, 1));
1787 ptr_table_free(PL_ptr_table);
1788 PL_ptr_table = NULL;
1792 MUTEX_UNLOCK(&thread->mutex);
1798 ST(0) = sv_2mortal(err);
1799 /* XSRETURN(1); - implied */
1802 #endif /* USE_ITHREADS */
1808 SV *my_pool_sv = *hv_fetch(PL_modglobal, MY_POOL_KEY,
1809 sizeof(MY_POOL_KEY)-1, TRUE);
1810 my_pool_t *my_poolp = (my_pool_t*)SvPVX(newSV(sizeof(my_pool_t)-1));
1814 Zero(my_poolp, 1, my_pool_t);
1815 sv_setuv(my_pool_sv, PTR2UV(my_poolp));
1817 PL_perl_destruct_level = 2;
1818 MUTEX_INIT(&MY_POOL.create_destruct_mutex);
1819 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1821 PL_threadhook = &Perl_ithread_hook;
1823 MY_POOL.tid_counter = 1;
1824 # ifdef THREAD_CREATE_NEEDS_STACK
1825 MY_POOL.default_stack_size = THREAD_CREATE_NEEDS_STACK;
1828 /* The 'main' thread is thread 0.
1829 * It is detached (unjoinable) and immortal.
1832 MUTEX_INIT(&MY_POOL.main_thread.mutex);
1834 /* Head of the threads list */
1835 MY_POOL.main_thread.next = &MY_POOL.main_thread;
1836 MY_POOL.main_thread.prev = &MY_POOL.main_thread;
1838 MY_POOL.main_thread.count = 1; /* Immortal */
1840 MY_POOL.main_thread.interp = aTHX;
1841 MY_POOL.main_thread.state = PERL_ITHR_DETACHED; /* Detached */
1842 MY_POOL.main_thread.stack_size = MY_POOL.default_stack_size;
1844 MY_POOL.main_thread.thr = GetCurrentThreadId();
1846 MY_POOL.main_thread.thr = pthread_self();
1849 S_ithread_set(aTHX_ &MY_POOL.main_thread);
1850 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1851 #endif /* USE_ITHREADS */