1 #define PERL_NO_GET_CONTEXT
5 /* Workaround for XSUB.h bug under WIN32 */
8 # if !defined(__BORLANDC__)
9 # define setjmp(x) _setjmp(x)
13 # define NEED_PL_signals
14 # define NEED_newRV_noinc
15 # define NEED_sv_2pv_nolen
24 /* Supposed to be in Winbase.h */
25 # ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
26 # define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
28 # include <win32thread.h>
31 typedef perl_os_thread pthread_t;
36 # define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
37 # ifdef OLD_PTHREADS_API
38 # define PERL_THREAD_DETACH(t) pthread_detach(&(t))
40 # define PERL_THREAD_DETACH(t) pthread_detach((t))
43 #if !defined(HAS_GETPAGESIZE) && defined(I_SYS_PARAM)
44 # include <sys/param.h>
47 /* Values for 'state' member */
48 #define PERL_ITHR_DETACHED 1 /* thread has been detached */
49 #define PERL_ITHR_JOINED 2 /* thread has been joined */
50 #define PERL_ITHR_FINISHED 4 /* thread has finished execution */
51 #define PERL_ITHR_THREAD_EXIT_ONLY 8 /* exit() only exits current thread */
52 #define PERL_ITHR_NONVIABLE 16 /* thread creation failed */
53 #define PERL_ITHR_DIED 32 /* thread finished by dying */
55 #define PERL_ITHR_UNCALLABLE (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)
58 typedef struct _ithread {
59 struct _ithread *next; /* Next thread in the list */
60 struct _ithread *prev; /* Prev thread in the list */
61 PerlInterpreter *interp; /* The threads interpreter */
62 UV tid; /* Threads module's thread id */
63 perl_mutex mutex; /* Mutex for updating things in this struct */
64 int count; /* reference count. See S_ithread_create */
65 int state; /* Detached, joined, finished, etc. */
66 int gimme; /* Context of create */
67 SV *init_function; /* Code to run */
68 SV *params; /* Args to pass function */
70 DWORD thr; /* OS's idea if thread id */
71 HANDLE handle; /* OS's waitable handle */
73 pthread_t thr; /* OS's handle for the thread */
76 SV *err; /* Error from abnormally terminated thread */
77 char *err_class; /* Error object's classname if applicable */
81 #define MY_CXT_KEY "threads::_cxt" XS_VERSION
84 /* Used by Perl interpreter for thread context switching */
91 #define MY_POOL_KEY "threads::_pool" XS_VERSION
94 /* Structure for 'main' thread
95 * Also forms the 'base' for the doubly-linked list of threads */
98 /* Protects the creation and destruction of threads*/
99 perl_mutex create_destruct_mutex;
105 IV default_stack_size;
110 SV *my_pool_sv = *hv_fetch(PL_modglobal, MY_POOL_KEY, \
111 sizeof(MY_POOL_KEY)-1, TRUE); \
112 my_pool_t *my_poolp = INT2PTR(my_pool_t*, SvUV(my_pool_sv))
114 #define MY_POOL (*my_poolp)
117 /* Used by Perl interpreter for thread context switching */
119 S_ithread_set(pTHX_ ithread *thread)
122 MY_CXT.context = thread;
129 return (MY_CXT.context);
133 /* Free any data (such as the Perl interpreter) attached to an ithread
134 * structure. This is a bit like undef on SVs, where the SV isn't freed,
135 * but the PVX is. Must be called with thread->mutex already held.
138 S_ithread_clear(pTHX_ ithread *thread)
140 PerlInterpreter *interp;
142 assert(((thread->state & PERL_ITHR_FINISHED) &&
143 (thread->state & PERL_ITHR_UNCALLABLE))
145 (thread->state & PERL_ITHR_NONVIABLE));
147 interp = thread->interp;
151 PERL_SET_CONTEXT(interp);
152 S_ithread_set(aTHX_ thread);
154 SvREFCNT_dec(thread->params);
155 thread->params = Nullsv;
158 SvREFCNT_dec(thread->err);
159 thread->err = Nullsv;
162 perl_destruct(interp);
164 thread->interp = NULL;
167 PERL_SET_CONTEXT(aTHX);
171 /* Decrement the refcount of an ithread, and if it reaches zero, free it.
172 * Must be called with the mutex held.
173 * On return, mutex is released (or destroyed) */
176 S_ithread_free(pTHX_ ithread *thread)
183 if (! (thread->state & PERL_ITHR_NONVIABLE)) {
184 assert(thread->count > 0);
185 if (--thread->count > 0) {
186 MUTEX_UNLOCK(&thread->mutex);
189 assert((thread->state & PERL_ITHR_FINISHED)
190 && (thread->state & PERL_ITHR_UNCALLABLE));
192 MUTEX_UNLOCK(&thread->mutex);
194 /* Main thread (0) is immortal and should never get here */
195 assert(thread->tid != 0);
197 /* Remove from circular list of threads */
198 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
199 assert(thread->prev && thread->next);
200 thread->next->prev = thread->prev;
201 thread->prev->next = thread->next;
204 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
206 /* Thread is now disowned */
207 MUTEX_LOCK(&thread->mutex);
208 S_ithread_clear(aTHX_ thread);
211 handle = thread->handle;
212 thread->handle = NULL;
214 MUTEX_UNLOCK(&thread->mutex);
215 MUTEX_DESTROY(&thread->mutex);
223 /* Call PerlMemShared_free() in the context of the "first" interpreter
224 * per http://www.nntp.perl.org/group/perl.perl5.porters/110772
226 aTHX = MY_POOL.main_thread.interp;
227 PerlMemShared_free(thread);
233 S_ithread_count_inc(pTHX_ ithread *thread)
235 MUTEX_LOCK(&thread->mutex);
237 MUTEX_UNLOCK(&thread->mutex);
242 /* Warn if exiting with any unjoined threads */
249 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
250 veto_cleanup = (MY_POOL.running_threads || MY_POOL.joinable_threads);
251 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
254 if (ckWARN_d(WARN_THREADS)) {
255 Perl_warn(aTHX_ "Perl exited with active threads:\n\t%"
256 IVdf " running and unjoined\n\t%"
257 IVdf " finished and unjoined\n\t%"
258 IVdf " running and detached\n",
259 MY_POOL.running_threads,
260 MY_POOL.joinable_threads,
261 MY_POOL.detached_threads);
265 return (veto_cleanup);
268 /* Called on exit from main thread */
270 Perl_ithread_hook(pTHX)
273 return ((aTHX == MY_POOL.main_thread.interp) ? S_exit_warning(aTHX) : 0);
277 /* MAGIC (in mg.h sense) hooks */
280 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
282 ithread *thread = (ithread *)mg->mg_ptr;
283 SvIV_set(sv, PTR2IV(thread));
289 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
291 ithread *thread = (ithread *)mg->mg_ptr;
292 MUTEX_LOCK(&thread->mutex);
293 S_ithread_free(aTHX_ thread); /* releases MUTEX */
299 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
301 S_ithread_count_inc(aTHX_ (ithread *)mg->mg_ptr);
305 MGVTBL ithread_vtbl = {
306 ithread_mg_get, /* get */
310 ithread_mg_free, /* free */
312 ithread_mg_dup /* dup */
316 /* Provided default, minimum and rational stack sizes */
318 S_good_stack_size(pTHX_ IV stack_size)
322 /* Use default stack size if no stack size specified */
324 return (MY_POOL.default_stack_size);
327 #ifdef PTHREAD_STACK_MIN
328 /* Can't use less than minimum */
329 if (stack_size < PTHREAD_STACK_MIN) {
330 if (ckWARN(WARN_THREADS)) {
331 Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
333 return (PTHREAD_STACK_MIN);
337 /* Round up to page size boundary */
338 if (MY_POOL.page_size <= 0) {
339 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
340 SETERRNO(0, SS_NORMAL);
342 MY_POOL.page_size = sysconf(_SC_PAGESIZE);
344 MY_POOL.page_size = sysconf(_SC_MMAP_PAGE_SIZE);
346 if ((long)MY_POOL.page_size < 0) {
348 SV * const error = get_sv("@", FALSE);
349 (void)SvUPGRADE(error, SVt_PV);
350 Perl_croak(aTHX_ "PANIC: sysconf: %s", SvPV_nolen(error));
352 Perl_croak(aTHX_ "PANIC: sysconf: pagesize unknown");
356 # ifdef HAS_GETPAGESIZE
357 MY_POOL.page_size = getpagesize();
359 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
360 MY_POOL.page_size = PAGESIZE;
362 MY_POOL.page_size = 8192; /* A conservative default */
365 if (MY_POOL.page_size <= 0) {
366 Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)MY_POOL.page_size);
370 stack_size = ((stack_size + (MY_POOL.page_size - 1)) / MY_POOL.page_size) * MY_POOL.page_size;
376 /* Starts executing the thread.
377 * Passed as the C level function to run in the new thread.
380 STATIC THREAD_RET_TYPE
381 S_ithread_run(LPVOID arg)
384 S_ithread_run(void * arg)
387 ithread *thread = (ithread *)arg;
390 int exit_app = 0; /* Thread terminated using 'exit' */
392 int died = 0; /* Thread terminated abnormally */
396 dTHXa(thread->interp);
400 /* Blocked until ->create() call finishes */
401 MUTEX_LOCK(&thread->mutex);
402 MUTEX_UNLOCK(&thread->mutex);
404 PERL_SET_CONTEXT(thread->interp);
405 S_ithread_set(aTHX_ thread);
407 PL_perl_destruct_level = 2;
410 AV *params = (AV *)SvRV(thread->params);
411 int len = (int)av_len(params)+1;
418 /* Put args on the stack */
420 for (ii=0; ii < len; ii++) {
421 XPUSHs(av_shift(params));
425 oldscope = PL_scopestack_ix;
428 /* Run the specified function */
429 len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
430 } else if (jmp_rc == 2) {
433 exit_code = STATUS_CURRENT;
434 while (PL_scopestack_ix > oldscope) {
440 /* Remove args from stack and put back in params array */
442 for (ii=len-1; ii >= 0; ii--) {
445 av_store(params, ii, SvREFCNT_inc(sv));
452 /* Check for abnormal termination */
454 died = PERL_ITHR_DIED;
455 thread->err = newSVsv(ERRSV);
456 /* If ERRSV is an object, remember the classname and then
457 * rebless into 'main' so it will survive 'cloning'
459 if (sv_isobject(thread->err)) {
460 thread->err_class = HvNAME(SvSTASH(SvRV(thread->err)));
461 sv_bless(thread->err, gv_stashpv("main", 0));
464 if (ckWARN_d(WARN_THREADS)) {
465 oldscope = PL_scopestack_ix;
468 /* Warn that thread died */
469 Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV);
470 } else if (jmp_rc == 2) {
471 /* Warn handler exited */
473 exit_code = STATUS_CURRENT;
474 while (PL_scopestack_ix > oldscope) {
482 /* Release function ref */
483 SvREFCNT_dec(thread->init_function);
484 thread->init_function = Nullsv;
487 PerlIO_flush((PerlIO *)NULL);
489 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
490 MUTEX_LOCK(&thread->mutex);
491 /* Mark as finished */
492 thread->state |= (PERL_ITHR_FINISHED | died);
493 /* Clear exit flag if required */
494 if (thread->state & PERL_ITHR_THREAD_EXIT_ONLY) {
498 /* Adjust thread status counts */
499 if (thread->state & PERL_ITHR_DETACHED) {
500 MY_POOL.detached_threads--;
502 MY_POOL.running_threads--;
503 MY_POOL.joinable_threads++;
505 MUTEX_UNLOCK(&thread->mutex);
506 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
508 /* Exit application if required */
510 oldscope = PL_scopestack_ix;
513 /* Warn if there are unjoined threads */
514 S_exit_warning(aTHX);
515 } else if (jmp_rc == 2) {
516 /* Warn handler exited */
517 exit_code = STATUS_CURRENT;
518 while (PL_scopestack_ix > oldscope) {
527 MUTEX_LOCK(&thread->mutex);
528 S_ithread_free(aTHX_ thread); /* releases MUTEX */
538 /* Type conversion helper functions */
541 S_ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
547 S_ithread_count_inc(aTHX_ thread);
553 sv = newSVrv(obj, classname);
554 sv_setiv(sv, PTR2IV(thread));
555 mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0);
556 mg->mg_flags |= MGf_DUP;
563 S_SV_to_ithread(pTHX_ SV *sv)
565 /* Argument is a thread */
567 return (INT2PTR(ithread *, SvIV(SvRV(sv))));
569 /* Argument is classname, therefore return current thread */
570 return (S_ithread_get(aTHX));
575 * Called in context of parent thread.
576 * Called with MY_POOL.create_destruct_mutex locked. (Unlocked on error.)
580 pTHX_ SV *init_function,
587 ithread *current_thread = S_ithread_get(aTHX);
589 SV **tmps_tmp = PL_tmps_stack;
590 IV tmps_ix = PL_tmps_ix;
592 int rc_stack_size = 0;
593 int rc_thread_create = 0;
597 /* Allocate thread structure in context of the main thread's interpreter */
599 PERL_SET_CONTEXT(MY_POOL.main_thread.interp);
600 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
602 PERL_SET_CONTEXT(aTHX);
604 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
605 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
608 Zero(thread, 1, ithread);
610 /* Add to threads list */
611 thread->next = &MY_POOL.main_thread;
612 thread->prev = MY_POOL.main_thread.prev;
613 MY_POOL.main_thread.prev = thread;
614 thread->prev->next = thread;
616 /* 1 ref to be held by the local var 'thread' in S_ithread_run()
617 * 1 ref to be held by the threads object that we assume we will
618 * be embedded in upon our return
619 * 1 ref to be the responsibility of join/detach, so we don't get freed
620 until join/detach, even if no thread objects remain. This
621 allows the following to work:
622 { threads->new(sub{...}); } threads->object(1)->join;
626 /* Block new thread until ->create() call finishes */
627 MUTEX_INIT(&thread->mutex);
628 MUTEX_LOCK(&thread->mutex);
630 thread->tid = MY_POOL.tid_counter++;
631 thread->stack_size = S_good_stack_size(aTHX_ stack_size);
632 thread->gimme = gimme;
633 thread->state = exit_opt;
635 /* "Clone" our interpreter into the thread's interpreter.
636 * This gives thread access to "static data" and code.
638 PerlIO_flush((PerlIO *)NULL);
639 S_ithread_set(aTHX_ thread);
641 SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct value */
642 PL_srand_called = FALSE; /* Set it to false so we can detect if it gets
643 set during the clone */
646 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
648 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
651 /* perl_clone() leaves us in new interpreter's context. As it is tricky
652 * to spot an implicit aTHX, create a new scope with aTHX matching the
653 * context for the duration of our work for new interpreter.
656 CLONE_PARAMS clone_param;
658 dTHXa(thread->interp);
662 /* Here we remove END blocks since they should only run in the thread
665 SvREFCNT_dec(PL_endav);
668 clone_param.flags = 0;
669 if (SvPOK(init_function)) {
670 thread->init_function = newSV(0);
671 sv_copypv(thread->init_function, init_function);
673 thread->init_function = sv_dup(init_function, &clone_param);
674 if (SvREFCNT(thread->init_function) == 0) {
675 SvREFCNT_inc_void(thread->init_function);
679 thread->params = sv_dup(params, &clone_param);
680 SvREFCNT_inc_void(thread->params);
682 /* The code below checks that anything living on the tmps stack and
683 * has been cloned (so it lives in the ptr_table) has a refcount
686 * If the refcount is 0 it means that a something on the stack/context
687 * was holding a reference to it and since we init_stacks() in
688 * perl_clone that won't get cleaned and we will get a leaked scalar.
689 * The reason it was cloned was that it lived on the @_ stack.
691 * Example of this can be found in bugreport 15837 where calls in the
692 * parameter list end up as a temp.
694 * One could argue that this fix should be in perl_clone.
696 while (tmps_ix > 0) {
697 SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
699 if (sv && SvREFCNT(sv) == 0) {
700 SvREFCNT_inc_void(sv);
705 SvTEMP_off(thread->init_function);
706 ptr_table_free(PL_ptr_table);
708 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
710 S_ithread_set(aTHX_ current_thread);
711 PERL_SET_CONTEXT(aTHX);
713 /* Create/start the thread */
715 thread->handle = CreateThread(NULL,
716 (DWORD)thread->stack_size,
719 STACK_SIZE_PARAM_IS_A_RESERVATION,
723 STATIC pthread_attr_t attr;
724 STATIC int attr_inited = 0;
725 STATIC int attr_joinable = PTHREAD_CREATE_JOINABLE;
727 pthread_attr_init(&attr);
731 # ifdef PTHREAD_ATTR_SETDETACHSTATE
732 /* Threads start out joinable */
733 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
736 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
737 /* Set thread's stack size */
738 if (thread->stack_size > 0) {
739 rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
743 /* Create the thread */
744 if (! rc_stack_size) {
745 # ifdef OLD_PTHREADS_API
746 rc_thread_create = pthread_create(&thread->thr,
751 # if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
752 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
754 rc_thread_create = pthread_create(&thread->thr,
761 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
762 /* Try to get thread's actual stack size */
766 stacksize = pthread_attr_getstacksize(attr);
768 if (! pthread_attr_getstacksize(&attr, &stacksize))
771 thread->stack_size = (IV)stacksize;
778 /* Check for errors */
780 if (thread->handle == NULL) {
782 if (rc_stack_size || rc_thread_create) {
784 /* Must unlock mutex for destruct call */
785 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
787 thread->state |= PERL_ITHR_NONVIABLE;
788 S_ithread_free(aTHX_ thread); /* releases MUTEX */
790 if (ckWARN_d(WARN_THREADS)) {
792 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
794 Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
801 MY_POOL.running_threads++;
806 #endif /* USE_ITHREADS */
809 MODULE = threads PACKAGE = threads PREFIX = ithread_
819 SV *function_to_call;
825 SV *thread_exit_only;
831 if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
833 Perl_croak(aTHX_ "Usage: threads->create(\\%specs, function, ...)");
835 specs = (HV*)SvRV(ST(1));
839 Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
845 if (sv_isobject(ST(0))) {
847 classname = HvNAME(SvSTASH(SvRV(ST(0))));
848 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
849 MUTEX_LOCK(&thread->mutex);
850 stack_size = thread->stack_size;
851 exit_opt = thread->state & PERL_ITHR_THREAD_EXIT_ONLY;
852 MUTEX_UNLOCK(&thread->mutex);
854 /* threads->create() */
855 classname = (char *)SvPV_nolen(ST(0));
856 stack_size = MY_POOL.default_stack_size;
857 thread_exit_only = get_sv("threads::thread_exit_only", TRUE);
858 exit_opt = (SvTRUE(thread_exit_only))
859 ? PERL_ITHR_THREAD_EXIT_ONLY : 0;
862 function_to_call = ST(idx+1);
867 if (hv_exists(specs, "stack", 5)) {
868 stack_size = SvIV(*hv_fetch(specs, "stack", 5, 0));
869 } else if (hv_exists(specs, "stacksize", 9)) {
870 stack_size = SvIV(*hv_fetch(specs, "stacksize", 9, 0));
871 } else if (hv_exists(specs, "stack_size", 10)) {
872 stack_size = SvIV(*hv_fetch(specs, "stack_size", 10, 0));
876 if (hv_exists(specs, "context", 7)) {
877 str = (char *)SvPV_nolen(*hv_fetch(specs, "context", 7, 0));
892 Perl_croak(aTHX_ "Invalid context: %s", str);
894 } else if (hv_exists(specs, "array", 5)) {
895 if (SvTRUE(*hv_fetch(specs, "array", 5, 0))) {
898 } else if (hv_exists(specs, "scalar", 6)) {
899 if (SvTRUE(*hv_fetch(specs, "scalar", 6, 0))) {
902 } else if (hv_exists(specs, "void", 4)) {
903 if (SvTRUE(*hv_fetch(specs, "void", 4, 0))) {
908 /* exit => thread_only */
909 if (hv_exists(specs, "exit", 4)) {
910 str = (char *)SvPV_nolen(*hv_fetch(specs, "exit", 4, 0));
911 exit_opt = (*str == 't' || *str == 'T')
912 ? PERL_ITHR_THREAD_EXIT_ONLY : 0;
916 context = GIMME_V; /* Implicit context */
918 context |= (GIMME_V & (~(G_ARRAY|G_SCALAR|G_VOID)));
924 for (ii=2; ii < items ; ii++) {
925 av_push(params, SvREFCNT_inc(ST(idx+ii)));
930 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
931 thread = S_ithread_create(aTHX_ function_to_call,
935 newRV_noinc((SV*)params));
937 XSRETURN_UNDEF; /* Mutex already unlocked */
939 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, FALSE));
940 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
943 MUTEX_UNLOCK(&thread->mutex);
945 /* XSRETURN(1); - implied */
955 int want_running = 0;
959 /* Class method only */
961 Perl_croak(aTHX_ "Usage: threads->list(...)");
963 classname = (char *)SvPV_nolen(ST(0));
965 /* Calling context */
966 list_context = (GIMME_V == G_ARRAY);
968 /* Running or joinable parameter */
970 want_running = SvTRUE(ST(1));
973 /* Walk through threads list */
974 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
975 for (thread = MY_POOL.main_thread.next;
976 thread != &MY_POOL.main_thread;
977 thread = thread->next)
979 MUTEX_LOCK(&thread->mutex);
980 state = thread->state;
981 MUTEX_UNLOCK(&thread->mutex);
983 /* Ignore detached or joined threads */
984 if (state & PERL_ITHR_UNCALLABLE) {
988 /* Filter per parameter */
991 if (state & PERL_ITHR_FINISHED) {
992 continue; /* Not running */
995 if (! (state & PERL_ITHR_FINISHED)) {
996 continue; /* Still running - not joinable yet */
1001 /* Push object on stack if list context */
1003 XPUSHs(sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
1007 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1008 /* If scalar context, send back count */
1009 if (! list_context) {
1020 /* Class method only */
1021 if ((items != 1) || SvROK(ST(0))) {
1022 Perl_croak(aTHX_ "Usage: threads->self()");
1024 classname = (char *)SvPV_nolen(ST(0));
1026 thread = S_ithread_get(aTHX);
1028 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1029 /* XSRETURN(1); - implied */
1037 PERL_UNUSED_VAR(items);
1038 thread = S_SV_to_ithread(aTHX_ ST(0));
1039 XST_mUV(0, thread->tid);
1040 /* XSRETURN(1); - implied */
1047 ithread *current_thread;
1060 /* Object method only */
1061 if ((items != 1) || ! sv_isobject(ST(0))) {
1062 Perl_croak(aTHX_ "Usage: $thr->join()");
1065 /* Check if the thread is joinable and not ourselves */
1066 thread = S_SV_to_ithread(aTHX_ ST(0));
1067 current_thread = S_ithread_get(aTHX);
1069 MUTEX_LOCK(&thread->mutex);
1070 if ((join_err = (thread->state & PERL_ITHR_UNCALLABLE))) {
1071 MUTEX_UNLOCK(&thread->mutex);
1072 Perl_croak(aTHX_ (join_err & PERL_ITHR_DETACHED)
1073 ? "Cannot join a detached thread"
1074 : "Thread already joined");
1075 } else if (thread->tid == current_thread->tid) {
1076 MUTEX_UNLOCK(&thread->mutex);
1077 Perl_croak(aTHX_ "Cannot join self");
1080 /* Mark as joined */
1081 thread->state |= PERL_ITHR_JOINED;
1082 MUTEX_UNLOCK(&thread->mutex);
1084 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1085 MY_POOL.joinable_threads--;
1086 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1088 /* Join the thread */
1090 if (WaitForSingleObject(thread->handle, INFINITE) != WAIT_OBJECT_0) {
1091 /* Timeout/abandonment unexpected here; check $^E */
1092 Perl_croak(aTHX_ "PANIC: underlying join failed");
1095 if ((rc_join = pthread_join(thread->thr, &retval)) != 0) {
1096 /* In progress/deadlock/unknown unexpected here; check $! */
1098 Perl_croak(aTHX_ "PANIC: underlying join failed");
1102 MUTEX_LOCK(&thread->mutex);
1103 /* Get the return value from the call_sv */
1104 /* Objects do not survive this process - FIXME */
1107 PerlInterpreter *other_perl;
1108 CLONE_PARAMS clone_params;
1110 params_copy = (AV *)SvRV(thread->params);
1111 other_perl = thread->interp;
1112 clone_params.stashes = newAV();
1113 clone_params.flags = CLONEf_JOIN_IN;
1114 PL_ptr_table = ptr_table_new();
1115 S_ithread_set(aTHX_ thread);
1116 /* Ensure 'meaningful' addresses retain their meaning */
1117 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1118 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1119 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1120 params = (AV *)sv_dup((SV*)params_copy, &clone_params);
1121 S_ithread_set(aTHX_ current_thread);
1122 SvREFCNT_dec(clone_params.stashes);
1123 SvREFCNT_inc_void(params);
1124 ptr_table_free(PL_ptr_table);
1125 PL_ptr_table = NULL;
1128 /* If thread didn't die, then we can free its interpreter */
1129 if (! (thread->state & PERL_ITHR_DIED)) {
1130 S_ithread_clear(aTHX_ thread);
1132 S_ithread_free(aTHX_ thread); /* releases MUTEX */
1134 /* If no return values, then just return */
1139 /* Put return values on stack */
1140 len = (int)AvFILL(params);
1141 for (ii=0; ii <= len; ii++) {
1142 SV* param = av_shift(params);
1143 XPUSHs(sv_2mortal(param));
1146 /* Free return value array */
1147 SvREFCNT_dec(params);
1153 PERL_UNUSED_VAR(items);
1164 PERL_UNUSED_VAR(items);
1166 /* Detach the thread */
1167 thread = S_SV_to_ithread(aTHX_ ST(0));
1168 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1169 MUTEX_LOCK(&thread->mutex);
1170 if (! (detach_err = (thread->state & PERL_ITHR_UNCALLABLE))) {
1171 /* Thread is detachable */
1172 thread->state |= PERL_ITHR_DETACHED;
1174 /* Windows has no 'detach thread' function */
1176 PERL_THREAD_DETACH(thread->thr);
1178 if (thread->state & PERL_ITHR_FINISHED) {
1179 MY_POOL.joinable_threads--;
1181 MY_POOL.running_threads--;
1182 MY_POOL.detached_threads++;
1185 MUTEX_UNLOCK(&thread->mutex);
1186 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1189 Perl_croak(aTHX_ (detach_err & PERL_ITHR_DETACHED)
1190 ? "Thread already detached"
1191 : "Cannot detach a joined thread");
1194 /* If thread is finished and didn't die,
1195 * then we can free its interpreter */
1196 MUTEX_LOCK(&thread->mutex);
1197 if ((thread->state & PERL_ITHR_FINISHED) &&
1198 ! (thread->state & PERL_ITHR_DIED))
1200 S_ithread_clear(aTHX_ thread);
1202 S_ithread_free(aTHX_ thread); /* releases MUTEX */
1213 /* Must have safe signals */
1214 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) {
1215 Perl_croak(aTHX_ "Cannot signal threads without safe signals");
1218 /* Object method only */
1219 if ((items != 2) || ! sv_isobject(ST(0))) {
1220 Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')");
1224 sig_name = SvPV_nolen(ST(1));
1225 if (isALPHA(*sig_name)) {
1226 if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G') {
1229 if ((signal = whichsig(sig_name)) < 0) {
1230 Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name);
1233 signal = SvIV(ST(1));
1236 /* Set the signal for the thread */
1237 thread = S_SV_to_ithread(aTHX_ ST(0));
1238 MUTEX_LOCK(&thread->mutex);
1239 if (thread->interp) {
1240 dTHXa(thread->interp);
1241 PL_psig_pend[signal]++;
1244 MUTEX_UNLOCK(&thread->mutex);
1246 /* Return the thread to allow for method chaining */
1248 /* XSRETURN(1); - implied */
1252 ithread_DESTROY(...)
1254 PERL_UNUSED_VAR(items);
1255 sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar);
1263 PERL_UNUSED_VAR(items);
1265 /* Compares TIDs to determine thread equality */
1266 if (sv_isobject(ST(0)) && sv_isobject(ST(1))) {
1267 ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1268 ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
1269 are_equal = (thr1->tid == thr2->tid);
1274 /* Return 0 on false for backward compatibility */
1277 /* XSRETURN(1); - implied */
1290 /* Class method only */
1292 Perl_croak(aTHX_ "Usage: threads->object($tid)");
1294 classname = (char *)SvPV_nolen(ST(0));
1296 if ((items < 2) || ! SvOK(ST(1))) {
1300 /* threads->object($tid) */
1303 /* Walk through threads list */
1304 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1305 for (thread = MY_POOL.main_thread.next;
1306 thread != &MY_POOL.main_thread;
1307 thread = thread->next)
1310 if (thread->tid == tid) {
1311 /* Ignore if detached or joined */
1312 MUTEX_LOCK(&thread->mutex);
1313 state = thread->state;
1314 MUTEX_UNLOCK(&thread->mutex);
1315 if (! (state & PERL_ITHR_UNCALLABLE)) {
1316 /* Put object on stack */
1317 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1323 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1328 /* XSRETURN(1); - implied */
1332 ithread__handle(...);
1336 PERL_UNUSED_VAR(items);
1337 thread = S_SV_to_ithread(aTHX_ ST(0));
1339 XST_mUV(0, PTR2UV(&thread->handle));
1341 XST_mUV(0, PTR2UV(&thread->thr));
1343 /* XSRETURN(1); - implied */
1347 ithread_get_stack_size(...)
1352 PERL_UNUSED_VAR(items);
1353 if (sv_isobject(ST(0))) {
1354 /* $thr->get_stack_size() */
1355 ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1356 stack_size = thread->stack_size;
1358 /* threads->get_stack_size() */
1359 stack_size = MY_POOL.default_stack_size;
1361 XST_mIV(0, stack_size);
1362 /* XSRETURN(1); - implied */
1366 ithread_set_stack_size(...)
1372 Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
1374 if (sv_isobject(ST(0))) {
1375 Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
1378 old_size = MY_POOL.default_stack_size;
1379 MY_POOL.default_stack_size = S_good_stack_size(aTHX_ SvIV(ST(1)));
1380 XST_mIV(0, old_size);
1381 /* XSRETURN(1); - implied */
1385 ithread_is_running(...)
1389 /* Object method only */
1390 if ((items != 1) || ! sv_isobject(ST(0))) {
1391 Perl_croak(aTHX_ "Usage: $thr->is_running()");
1394 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1395 MUTEX_LOCK(&thread->mutex);
1396 ST(0) = (thread->state & PERL_ITHR_FINISHED) ? &PL_sv_no : &PL_sv_yes;
1397 MUTEX_UNLOCK(&thread->mutex);
1398 /* XSRETURN(1); - implied */
1402 ithread_is_detached(...)
1406 PERL_UNUSED_VAR(items);
1407 thread = S_SV_to_ithread(aTHX_ ST(0));
1408 MUTEX_LOCK(&thread->mutex);
1409 ST(0) = (thread->state & PERL_ITHR_DETACHED) ? &PL_sv_yes : &PL_sv_no;
1410 MUTEX_UNLOCK(&thread->mutex);
1411 /* XSRETURN(1); - implied */
1415 ithread_is_joinable(...)
1419 /* Object method only */
1420 if ((items != 1) || ! sv_isobject(ST(0))) {
1421 Perl_croak(aTHX_ "Usage: $thr->is_joinable()");
1424 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1425 MUTEX_LOCK(&thread->mutex);
1426 ST(0) = ((thread->state & PERL_ITHR_FINISHED) &&
1427 ! (thread->state & PERL_ITHR_UNCALLABLE))
1428 ? &PL_sv_yes : &PL_sv_no;
1429 MUTEX_UNLOCK(&thread->mutex);
1430 /* XSRETURN(1); - implied */
1434 ithread_wantarray(...)
1438 PERL_UNUSED_VAR(items);
1439 thread = S_SV_to_ithread(aTHX_ ST(0));
1440 ST(0) = (thread->gimme & G_ARRAY) ? &PL_sv_yes :
1441 (thread->gimme & G_VOID) ? &PL_sv_undef
1442 /* G_SCALAR */ : &PL_sv_no;
1443 /* XSRETURN(1); - implied */
1447 ithread_set_thread_exit_only(...)
1452 Perl_croak(aTHX_ "Usage: ->set_thread_exit_only(boolean)");
1454 thread = S_SV_to_ithread(aTHX_ ST(0));
1455 MUTEX_LOCK(&thread->mutex);
1456 if (SvTRUE(ST(1))) {
1457 thread->state |= PERL_ITHR_THREAD_EXIT_ONLY;
1459 thread->state &= ~PERL_ITHR_THREAD_EXIT_ONLY;
1461 MUTEX_UNLOCK(&thread->mutex);
1470 /* Object method only */
1471 if ((items != 1) || ! sv_isobject(ST(0))) {
1472 Perl_croak(aTHX_ "Usage: $thr->err()");
1475 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1476 MUTEX_LOCK(&thread->mutex);
1478 /* If thread died, then clone the error into the calling thread */
1479 if (thread->state & PERL_ITHR_DIED) {
1480 PerlInterpreter *other_perl;
1481 CLONE_PARAMS clone_params;
1482 ithread *current_thread;
1484 other_perl = thread->interp;
1485 clone_params.stashes = newAV();
1486 clone_params.flags = CLONEf_JOIN_IN;
1487 PL_ptr_table = ptr_table_new();
1488 current_thread = S_ithread_get(aTHX);
1489 S_ithread_set(aTHX_ thread);
1490 /* Ensure 'meaningful' addresses retain their meaning */
1491 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1492 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1493 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1494 err = sv_dup(thread->err, &clone_params);
1495 S_ithread_set(aTHX_ current_thread);
1496 SvREFCNT_dec(clone_params.stashes);
1497 SvREFCNT_inc_void(err);
1498 /* If error was an object, bless it into the correct class */
1499 if (thread->err_class) {
1500 sv_bless(err, gv_stashpv(thread->err_class, 1));
1502 ptr_table_free(PL_ptr_table);
1503 PL_ptr_table = NULL;
1506 MUTEX_UNLOCK(&thread->mutex);
1512 ST(0) = sv_2mortal(err);
1513 /* XSRETURN(1); - implied */
1516 #endif /* USE_ITHREADS */
1522 SV *my_pool_sv = *hv_fetch(PL_modglobal, MY_POOL_KEY,
1523 sizeof(MY_POOL_KEY)-1, TRUE);
1524 my_pool_t *my_poolp = (my_pool_t*)SvPVX(newSV(sizeof(my_pool_t)-1));
1528 Zero(my_poolp, 1, my_pool_t);
1529 sv_setuv(my_pool_sv, PTR2UV(my_poolp));
1531 PL_perl_destruct_level = 2;
1532 MUTEX_INIT(&MY_POOL.create_destruct_mutex);
1533 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1535 PL_threadhook = &Perl_ithread_hook;
1537 MY_POOL.tid_counter = 1;
1538 # ifdef THREAD_CREATE_NEEDS_STACK
1539 MY_POOL.default_stack_size = THREAD_CREATE_NEEDS_STACK;
1542 /* The 'main' thread is thread 0.
1543 * It is detached (unjoinable) and immortal.
1546 MUTEX_INIT(&MY_POOL.main_thread.mutex);
1548 /* Head of the threads list */
1549 MY_POOL.main_thread.next = &MY_POOL.main_thread;
1550 MY_POOL.main_thread.prev = &MY_POOL.main_thread;
1552 MY_POOL.main_thread.count = 1; /* Immortal */
1554 MY_POOL.main_thread.interp = aTHX;
1555 MY_POOL.main_thread.state = PERL_ITHR_DETACHED; /* Detached */
1556 MY_POOL.main_thread.stack_size = MY_POOL.default_stack_size;
1558 MY_POOL.main_thread.thr = GetCurrentThreadId();
1560 MY_POOL.main_thread.thr = pthread_self();
1563 S_ithread_set(aTHX_ &MY_POOL.main_thread);
1564 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1565 #endif /* USE_ITHREADS */