1 #define PERL_NO_GET_CONTEXT
5 /* Workaround for XSUB.h bug under WIN32 */
8 # define setjmp(x) _setjmp(x)
11 # define NEED_PL_signals
12 # define NEED_newRV_noinc
13 # define NEED_sv_2pv_nolen
22 /* Supposed to be in Winbase.h */
23 # ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
24 # define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
26 # include <win32thread.h>
29 typedef perl_os_thread pthread_t;
34 # define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
35 # ifdef OLD_PTHREADS_API
36 # define PERL_THREAD_DETACH(t) pthread_detach(&(t))
38 # define PERL_THREAD_DETACH(t) pthread_detach((t))
41 #if !defined(HAS_GETPAGESIZE) && defined(I_SYS_PARAM)
42 # include <sys/param.h>
45 /* Values for 'state' member */
46 #define PERL_ITHR_JOINABLE 0
47 #define PERL_ITHR_DETACHED 1
48 #define PERL_ITHR_JOINED 2
49 #define PERL_ITHR_FINISHED 4
51 typedef struct _ithread {
52 struct _ithread *next; /* Next thread in the list */
53 struct _ithread *prev; /* Prev thread in the list */
54 PerlInterpreter *interp; /* The threads interpreter */
55 UV tid; /* Threads module's thread id */
56 perl_mutex mutex; /* Mutex for updating things in this struct */
57 int count; /* How many SVs have a reference to us */
58 int state; /* Detached, joined, finished, etc. */
59 int gimme; /* Context of create */
60 SV *init_function; /* Code to run */
61 SV *params; /* Args to pass function */
63 DWORD thr; /* OS's idea if thread id */
64 HANDLE handle; /* OS's waitable handle */
66 pthread_t thr; /* OS's handle for the thread */
72 /* Used by Perl interpreter for thread context switching */
73 #define MY_CXT_KEY "threads::_guts" XS_VERSION
82 /* Linked list of all threads */
83 static ithread *threads;
85 /* Protects the creation and destruction of threads*/
86 static perl_mutex create_destruct_mutex;
88 static UV tid_counter = 0;
89 static IV joinable_threads = 0;
90 static IV running_threads = 0;
91 static IV detached_threads = 0;
92 #ifdef THREAD_CREATE_NEEDS_STACK
93 static IV default_stack_size = THREAD_CREATE_NEEDS_STACK;
95 static IV default_stack_size = 0;
97 static IV page_size = 0;
100 /* Used by Perl interpreter for thread context switching */
102 S_ithread_set(pTHX_ ithread *thread)
105 MY_CXT.thread = thread;
112 return (MY_CXT.thread);
116 /* Free any data (such as the Perl interpreter) attached to an ithread
117 * structure. This is a bit like undef on SVs, where the SV isn't freed,
118 * but the PVX is. Must be called with thread->mutex already held.
121 S_ithread_clear(pTHX_ ithread *thread)
123 PerlInterpreter *interp;
125 assert(thread->state & PERL_ITHR_FINISHED &&
126 thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
128 interp = thread->interp;
132 PERL_SET_CONTEXT(interp);
133 S_ithread_set(aTHX_ thread);
135 SvREFCNT_dec(thread->params);
136 thread->params = Nullsv;
138 perl_destruct(interp);
139 thread->interp = NULL;
144 PERL_SET_CONTEXT(aTHX);
148 /* Free an ithread structure and any attached data if its count == 0 */
150 S_ithread_destruct(pTHX_ ithread *thread)
156 MUTEX_LOCK(&thread->mutex);
158 /* Thread is still in use */
159 if (thread->count != 0) {
160 MUTEX_UNLOCK(&thread->mutex);
164 /* Main thread (0) is immortal and should never get here */
165 assert(thread->tid != 0);
167 /* Remove from circular list of threads */
168 MUTEX_LOCK(&create_destruct_mutex);
169 thread->next->prev = thread->prev;
170 thread->prev->next = thread->next;
173 MUTEX_UNLOCK(&create_destruct_mutex);
175 /* Thread is now disowned */
176 S_ithread_clear(aTHX_ thread);
179 handle = thread->handle;
180 thread->handle = NULL;
182 MUTEX_UNLOCK(&thread->mutex);
183 MUTEX_DESTROY(&thread->mutex);
190 /* Call PerlMemShared_free() in the context of the "first" interpreter
191 * per http://www.nntp.perl.org/group/perl.perl5.porters/110772
194 PerlMemShared_free(thread);
200 Perl_ithread_hook(pTHX)
202 int veto_cleanup = 0;
203 MUTEX_LOCK(&create_destruct_mutex);
204 if ((aTHX == PL_curinterp) &&
205 (running_threads || joinable_threads))
207 if (ckWARN_d(WARN_THREADS)) {
208 Perl_warn(aTHX_ "Perl exited with active threads:\n\t%"
209 IVdf " running and unjoined\n\t%"
210 IVdf " finished and unjoined\n\t%"
211 IVdf " running and detached\n",
218 MUTEX_UNLOCK(&create_destruct_mutex);
219 return (veto_cleanup);
223 /* MAGIC (in mg.h sense) hooks */
226 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
228 ithread *thread = (ithread *)mg->mg_ptr;
229 SvIV_set(sv, PTR2IV(thread));
235 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
237 ithread *thread = (ithread *)mg->mg_ptr;
240 MUTEX_LOCK(&thread->mutex);
241 cleanup = ((--thread->count == 0) &&
242 (thread->state & PERL_ITHR_FINISHED) &&
243 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
244 MUTEX_UNLOCK(&thread->mutex);
247 S_ithread_destruct(aTHX_ thread);
252 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
254 ithread *thread = (ithread *)mg->mg_ptr;
255 MUTEX_LOCK(&thread->mutex);
257 MUTEX_UNLOCK(&thread->mutex);
261 MGVTBL ithread_vtbl = {
262 ithread_mg_get, /* get */
266 ithread_mg_free, /* free */
268 ithread_mg_dup /* dup */
272 /* Provided default, minimum and rational stack sizes */
274 good_stack_size(pTHX_ IV stack_size)
276 /* Use default stack size if no stack size specified */
278 return (default_stack_size);
280 #ifdef PTHREAD_STACK_MIN
281 /* Can't use less than minimum */
282 if (stack_size < PTHREAD_STACK_MIN) {
283 if (ckWARN(WARN_THREADS)) {
284 Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
286 return (PTHREAD_STACK_MIN);
290 /* Round up to page size boundary */
291 if (page_size <= 0) {
292 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
293 SETERRNO(0, SS_NORMAL);
295 page_size = sysconf(_SC_PAGESIZE);
297 page_size = sysconf(_SC_MMAP_PAGE_SIZE);
299 if ((long)page_size < 0) {
301 SV * const error = get_sv("@", FALSE);
302 (void)SvUPGRADE(error, SVt_PV);
303 Perl_croak(aTHX_ "PANIC: sysconf: %s", SvPV_nolen(error));
305 Perl_croak(aTHX_ "PANIC: sysconf: pagesize unknown");
309 # ifdef HAS_GETPAGESIZE
310 page_size = getpagesize();
312 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
313 page_size = PAGESIZE;
315 page_size = 8192; /* A conservative default */
319 Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)page_size);
322 stack_size = ((stack_size + (page_size - 1)) / page_size) * page_size;
328 /* Starts executing the thread.
329 * Passed as the C level function to run in the new thread.
332 static THREAD_RET_TYPE
333 S_ithread_run(LPVOID arg)
336 S_ithread_run(void * arg)
339 ithread *thread = (ithread *)arg;
342 dTHXa(thread->interp);
343 PERL_SET_CONTEXT(thread->interp);
344 S_ithread_set(aTHX_ thread);
347 /* Far from clear messing with ->thr child-side is a good idea */
348 MUTEX_LOCK(&thread->mutex);
350 thread->thr = GetCurrentThreadId();
352 thread->thr = pthread_self();
354 MUTEX_UNLOCK(&thread->mutex);
357 PL_perl_destruct_level = 2;
360 AV *params = (AV *)SvRV(thread->params);
361 int len = (int)av_len(params)+1;
372 /* Put args on the stack */
374 for (ii=0; ii < len; ii++) {
375 XPUSHs(av_shift(params));
379 oldscope = PL_scopestack_ix;
382 /* Run the specified function */
383 len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
384 } else if (jmp_rc == 2) {
385 while (PL_scopestack_ix > oldscope) {
391 /* Remove args from stack and put back in params array */
393 for (ii=len-1; ii >= 0; ii--) {
396 av_store(params, ii, SvREFCNT_inc(sv));
403 /* Check for failure */
404 if (SvTRUE(ERRSV) && ckWARN_d(WARN_THREADS)) {
405 oldscope = PL_scopestack_ix;
408 Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV);
409 } else if (jmp_rc == 2) {
410 while (PL_scopestack_ix > oldscope) {
417 /* Release function ref */
418 SvREFCNT_dec(thread->init_function);
419 thread->init_function = Nullsv;
422 PerlIO_flush((PerlIO *)NULL);
424 MUTEX_LOCK(&thread->mutex);
425 /* Mark as finished */
426 thread->state |= PERL_ITHR_FINISHED;
427 /* Cleanup if detached */
428 cleanup = (thread->state & PERL_ITHR_DETACHED);
429 MUTEX_UNLOCK(&thread->mutex);
432 MUTEX_LOCK(&create_destruct_mutex);
434 MUTEX_UNLOCK(&create_destruct_mutex);
435 S_ithread_destruct(aTHX_ thread);
437 MUTEX_LOCK(&create_destruct_mutex);
440 MUTEX_UNLOCK(&create_destruct_mutex);
451 /* Type conversion helper functions */
453 ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
459 MUTEX_LOCK(&thread->mutex);
461 MUTEX_UNLOCK(&thread->mutex);
468 sv = newSVrv(obj, classname);
469 sv_setiv(sv, PTR2IV(thread));
470 mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0);
471 mg->mg_flags |= MGf_DUP;
478 SV_to_ithread(pTHX_ SV *sv)
480 /* Argument is a thread */
482 return (INT2PTR(ithread *, SvIV(SvRV(sv))));
484 /* Argument is classname, therefore return current thread */
485 return (S_ithread_get(aTHX));
490 * Called in context of parent thread.
502 CLONE_PARAMS clone_param;
503 ithread *current_thread = S_ithread_get(aTHX);
505 SV **tmps_tmp = PL_tmps_stack;
506 IV tmps_ix = PL_tmps_ix;
508 int rc_stack_size = 0;
509 int rc_thread_create = 0;
512 MUTEX_LOCK(&create_destruct_mutex);
514 /* Allocate thread structure */
515 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
517 MUTEX_UNLOCK(&create_destruct_mutex);
518 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
521 Zero(thread, 1, ithread);
523 /* Add to threads list */
524 thread->next = threads;
525 thread->prev = threads->prev;
526 threads->prev = thread;
527 thread->prev->next = thread;
529 /* Set count to 1 immediately in case thread exits before
530 * we return to caller!
534 MUTEX_INIT(&thread->mutex);
535 thread->tid = tid_counter++;
536 thread->stack_size = good_stack_size(aTHX_ stack_size);
537 thread->gimme = gimme;
539 /* "Clone" our interpreter into the thread's interpreter.
540 * This gives thread access to "static data" and code.
542 PerlIO_flush((PerlIO *)NULL);
543 S_ithread_set(aTHX_ thread);
545 SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct value */
546 PL_srand_called = FALSE; /* Set it to false so we can detect if it gets
547 set during the clone */
550 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
552 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
555 /* perl_clone() leaves us in new interpreter's context. As it is tricky
556 * to spot an implicit aTHX, create a new scope with aTHX matching the
557 * context for the duration of our work for new interpreter.
560 dTHXa(thread->interp);
564 /* Here we remove END blocks since they should only run in the thread
567 SvREFCNT_dec(PL_endav);
570 if (SvPOK(init_function)) {
571 thread->init_function = newSV(0);
572 sv_copypv(thread->init_function, init_function);
574 clone_param.flags = 0;
575 thread->init_function = sv_dup(init_function, &clone_param);
576 if (SvREFCNT(thread->init_function) == 0) {
577 SvREFCNT_inc(thread->init_function);
581 thread->params = sv_dup(params, &clone_param);
582 SvREFCNT_inc(thread->params);
584 /* The code below checks that anything living on the tmps stack and
585 * has been cloned (so it lives in the ptr_table) has a refcount
588 * If the refcount is 0 it means that a something on the stack/context
589 * was holding a reference to it and since we init_stacks() in
590 * perl_clone that won't get cleaned and we will get a leaked scalar.
591 * The reason it was cloned was that it lived on the @_ stack.
593 * Example of this can be found in bugreport 15837 where calls in the
594 * parameter list end up as a temp.
596 * One could argue that this fix should be in perl_clone.
598 while (tmps_ix > 0) {
599 SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
601 if (sv && SvREFCNT(sv) == 0) {
607 SvTEMP_off(thread->init_function);
608 ptr_table_free(PL_ptr_table);
610 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
612 S_ithread_set(aTHX_ current_thread);
613 PERL_SET_CONTEXT(aTHX);
615 /* Create/start the thread */
617 thread->handle = CreateThread(NULL,
618 (DWORD)thread->stack_size,
621 STACK_SIZE_PARAM_IS_A_RESERVATION,
625 static pthread_attr_t attr;
626 static int attr_inited = 0;
627 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
629 pthread_attr_init(&attr);
633 # ifdef PTHREAD_ATTR_SETDETACHSTATE
634 /* Threads start out joinable */
635 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
638 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
639 /* Set thread's stack size */
640 if (thread->stack_size > 0) {
641 rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
645 /* Create the thread */
646 if (! rc_stack_size) {
647 # ifdef OLD_PTHREADS_API
648 rc_thread_create = pthread_create(&thread->thr,
653 # if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
654 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
656 rc_thread_create = pthread_create(&thread->thr,
663 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
664 /* Try to get thread's actual stack size */
668 stacksize = pthread_attr_getstacksize(attr);
670 if (! pthread_attr_getstacksize(&attr, &stacksize))
673 thread->stack_size = (IV)stacksize;
680 /* Check for errors */
682 if (thread->handle == NULL) {
684 if (rc_stack_size || rc_thread_create) {
686 MUTEX_UNLOCK(&create_destruct_mutex);
688 S_ithread_destruct(aTHX_ thread);
690 if (ckWARN_d(WARN_THREADS)) {
692 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
694 Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
697 return (&PL_sv_undef);
701 MUTEX_UNLOCK(&create_destruct_mutex);
705 return (ithread_to_SV(aTHX_ obj, thread, classname, FALSE));
708 #endif /* USE_ITHREADS */
711 MODULE = threads PACKAGE = threads PREFIX = ithread_
721 SV *function_to_call;
730 if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
732 Perl_croak(aTHX_ "Usage: threads->create(\\%specs, function, ...)");
733 specs = (HV*)SvRV(ST(1));
737 Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
742 if (sv_isobject(ST(0))) {
744 classname = HvNAME(SvSTASH(SvRV(ST(0))));
745 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
746 stack_size = thread->stack_size;
748 /* threads->create() */
749 classname = (char *)SvPV_nolen(ST(0));
750 stack_size = default_stack_size;
753 function_to_call = ST(idx+1);
758 if (hv_exists(specs, "stack", 5)) {
759 stack_size = SvIV(*hv_fetch(specs, "stack", 5, 0));
760 } else if (hv_exists(specs, "stacksize", 9)) {
761 stack_size = SvIV(*hv_fetch(specs, "stacksize", 9, 0));
762 } else if (hv_exists(specs, "stack_size", 10)) {
763 stack_size = SvIV(*hv_fetch(specs, "stack_size", 10, 0));
767 if (hv_exists(specs, "context", 7)) {
768 str = (char *)SvPV_nolen(*hv_fetch(specs, "context", 7, 0));
783 Perl_croak(aTHX_ "Invalid context: %s", str);
785 } else if (hv_exists(specs, "array", 5)) {
786 if (SvTRUE(*hv_fetch(specs, "array", 5, 0))) {
789 } else if (hv_exists(specs, "scalar", 6)) {
790 if (SvTRUE(*hv_fetch(specs, "scalar", 6, 0))) {
793 } else if (hv_exists(specs, "void", 4)) {
794 if (SvTRUE(*hv_fetch(specs, "void", 4, 0))) {
800 context = GIMME_V; /* Implicit context */
802 context |= (GIMME_V & (~(G_ARRAY|G_SCALAR|G_VOID)));
808 for (ii=2; ii < items ; ii++) {
809 av_push(params, SvREFCNT_inc(ST(idx+ii)));
814 ST(0) = sv_2mortal(S_ithread_create(aTHX_ Nullsv,
819 newRV_noinc((SV*)params)));
820 /* XSRETURN(1); - implied */
831 /* Class method only */
833 Perl_croak(aTHX_ "Usage: threads->list()");
834 classname = (char *)SvPV_nolen(ST(0));
836 /* Calling context */
837 list_context = (GIMME_V == G_ARRAY);
839 /* Walk through threads list */
840 MUTEX_LOCK(&create_destruct_mutex);
841 for (thread = threads->next;
843 thread = thread->next)
845 /* Ignore detached or joined threads */
846 if (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)) {
849 /* Push object on stack if list context */
851 XPUSHs(sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
855 MUTEX_UNLOCK(&create_destruct_mutex);
856 /* If scalar context, send back count */
857 if (! list_context) {
868 /* Class method only */
870 Perl_croak(aTHX_ "Usage: threads->self()");
871 classname = (char *)SvPV_nolen(ST(0));
873 thread = S_ithread_get(aTHX);
875 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
876 /* XSRETURN(1); - implied */
884 thread = SV_to_ithread(aTHX_ ST(0));
885 XST_mUV(0, thread->tid);
886 /* XSRETURN(1); - implied */
903 /* Object method only */
904 if (! sv_isobject(ST(0)))
905 Perl_croak(aTHX_ "Usage: $thr->join()");
907 /* Check if the thread is joinable */
908 thread = SV_to_ithread(aTHX_ ST(0));
909 MUTEX_LOCK(&thread->mutex);
910 join_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
911 MUTEX_UNLOCK(&thread->mutex);
913 if (join_err & PERL_ITHR_DETACHED) {
914 Perl_croak(aTHX_ "Cannot join a detached thread");
916 Perl_croak(aTHX_ "Thread already joined");
920 /* Join the thread */
922 waitcode = WaitForSingleObject(thread->handle, INFINITE);
924 pthread_join(thread->thr, &retval);
927 MUTEX_LOCK(&thread->mutex);
929 thread->state |= PERL_ITHR_JOINED;
931 /* Get the return value from the call_sv */
934 PerlInterpreter *other_perl;
935 CLONE_PARAMS clone_params;
936 ithread *current_thread;
938 params_copy = (AV *)SvRV(thread->params);
939 other_perl = thread->interp;
940 clone_params.stashes = newAV();
941 clone_params.flags = CLONEf_JOIN_IN;
942 PL_ptr_table = ptr_table_new();
943 current_thread = S_ithread_get(aTHX);
944 S_ithread_set(aTHX_ thread);
945 /* Ensure 'meaningful' addresses retain their meaning */
946 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
947 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
948 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
949 params = (AV *)sv_dup((SV*)params_copy, &clone_params);
950 S_ithread_set(aTHX_ current_thread);
951 SvREFCNT_dec(clone_params.stashes);
952 SvREFCNT_inc(params);
953 ptr_table_free(PL_ptr_table);
957 /* We are finished with the thread */
958 S_ithread_clear(aTHX_ thread);
959 MUTEX_UNLOCK(&thread->mutex);
961 MUTEX_LOCK(&create_destruct_mutex);
963 MUTEX_UNLOCK(&create_destruct_mutex);
965 /* If no return values, then just return */
970 /* Put return values on stack */
971 len = (int)AvFILL(params);
972 for (ii=0; ii <= len; ii++) {
973 SV* param = av_shift(params);
974 XPUSHs(sv_2mortal(param));
977 /* Free return value array */
978 SvREFCNT_dec(params);
994 thread = SV_to_ithread(aTHX_ ST(0));
995 MUTEX_LOCK(&thread->mutex);
997 /* Check if the thread is detachable */
998 if ((detach_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))) {
999 MUTEX_UNLOCK(&thread->mutex);
1000 if (detach_err & PERL_ITHR_DETACHED) {
1001 Perl_croak(aTHX_ "Thread already detached");
1003 Perl_croak(aTHX_ "Cannot detach a joined thread");
1007 /* Detach the thread */
1008 thread->state |= PERL_ITHR_DETACHED;
1010 /* Windows has no 'detach thread' function */
1012 PERL_THREAD_DETACH(thread->thr);
1014 /* Cleanup if finished */
1015 cleanup = (thread->state & PERL_ITHR_FINISHED);
1016 MUTEX_UNLOCK(&thread->mutex);
1018 MUTEX_LOCK(&create_destruct_mutex);
1025 MUTEX_UNLOCK(&create_destruct_mutex);
1028 S_ithread_destruct(aTHX_ thread);
1039 /* Must have safe signals */
1040 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
1041 Perl_croak(aTHX_ "Cannot signal threads without safe signals");
1043 /* Object method only */
1044 if (! sv_isobject(ST(0)))
1045 Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')");
1048 sig_name = SvPV_nolen(ST(1));
1049 if (isALPHA(*sig_name)) {
1050 if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G')
1052 if ((signal = whichsig(sig_name)) < 0)
1053 Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name);
1055 signal = SvIV(ST(1));
1057 /* Set the signal for the thread */
1058 thread = SV_to_ithread(aTHX_ ST(0));
1059 MUTEX_LOCK(&thread->mutex);
1061 dTHXa(thread->interp);
1062 PL_psig_pend[signal]++;
1065 MUTEX_UNLOCK(&thread->mutex);
1067 /* Return the thread to allow for method chaining */
1069 /* XSRETURN(1); - implied */
1073 ithread_DESTROY(...)
1075 sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar);
1083 /* Compares TIDs to determine thread equality */
1084 if (sv_isobject(ST(0)) && sv_isobject(ST(1))) {
1085 ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1086 ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
1087 are_equal = (thr1->tid == thr2->tid);
1092 /* Return 0 on false for backward compatibility */
1095 /* XSRETURN(1); - implied */
1106 /* Class method only */
1108 Perl_croak(aTHX_ "Usage: threads->object($tid)");
1109 classname = (char *)SvPV_nolen(ST(0));
1111 if ((items < 2) || ! SvOK(ST(1))) {
1115 /* threads->object($tid) */
1118 /* Walk through threads list */
1119 MUTEX_LOCK(&create_destruct_mutex);
1120 for (thread = threads->next;
1122 thread = thread->next)
1124 /* Look for TID, but ignore detached or joined threads */
1125 if ((thread->tid != tid) ||
1126 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))
1130 /* Put object on stack */
1131 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1135 MUTEX_UNLOCK(&create_destruct_mutex);
1139 /* XSRETURN(1); - implied */
1143 ithread__handle(...);
1147 thread = SV_to_ithread(aTHX_ ST(0));
1149 XST_mUV(0, PTR2UV(&thread->handle));
1151 XST_mUV(0, PTR2UV(&thread->thr));
1153 /* XSRETURN(1); - implied */
1157 ithread_get_stack_size(...)
1161 if (sv_isobject(ST(0))) {
1162 /* $thr->get_stack_size() */
1163 ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1164 stack_size = thread->stack_size;
1166 /* threads->get_stack_size() */
1167 stack_size = default_stack_size;
1169 XST_mIV(0, stack_size);
1170 /* XSRETURN(1); - implied */
1174 ithread_set_stack_size(...)
1179 Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
1180 if (sv_isobject(ST(0)))
1181 Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
1183 old_size = default_stack_size;
1184 default_stack_size = good_stack_size(aTHX_ SvIV(ST(1)));
1185 XST_mIV(0, old_size);
1186 /* XSRETURN(1); - implied */
1188 #endif /* USE_ITHREADS */
1194 /* The 'main' thread is thread 0.
1195 * It is detached (unjoinable) and immortal.
1201 PL_perl_destruct_level = 2;
1202 MUTEX_INIT(&create_destruct_mutex);
1203 MUTEX_LOCK(&create_destruct_mutex);
1205 PL_threadhook = &Perl_ithread_hook;
1207 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
1209 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
1212 Zero(thread, 1, ithread);
1214 MUTEX_INIT(&thread->mutex);
1216 thread->tid = tid_counter++; /* Thread 0 */
1218 /* Head of the threads list */
1220 thread->next = thread;
1221 thread->prev = thread;
1223 thread->count = 1; /* Immortal */
1225 thread->interp = aTHX;
1226 thread->state = PERL_ITHR_DETACHED; /* Detached */
1227 thread->stack_size = default_stack_size;
1229 thread->thr = GetCurrentThreadId();
1231 thread->thr = pthread_self();
1234 S_ithread_set(aTHX_ thread);
1235 MUTEX_UNLOCK(&create_destruct_mutex);
1236 #endif /* USE_ITHREADS */