1 #define PERL_NO_GET_CONTEXT
6 # define NEED_PL_signals
7 # define NEED_newRV_noinc
8 # define NEED_sv_2pv_nolen
17 /* Supposed to be in Winbase.h */
18 # ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
19 # define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
21 # include <win32thread.h>
24 typedef perl_os_thread pthread_t;
29 # define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
30 # ifdef OLD_PTHREADS_API
31 # define PERL_THREAD_DETACH(t) pthread_detach(&(t))
33 # define PERL_THREAD_DETACH(t) pthread_detach((t))
36 #if !defined(HAS_GETPAGESIZE) && defined(I_SYS_PARAM)
37 # include <sys/param.h>
40 /* Values for 'state' member */
41 #define PERL_ITHR_JOINABLE 0
42 #define PERL_ITHR_DETACHED 1
43 #define PERL_ITHR_JOINED 2
44 #define PERL_ITHR_FINISHED 4
46 typedef struct _ithread {
47 struct _ithread *next; /* Next thread in the list */
48 struct _ithread *prev; /* Prev thread in the list */
49 PerlInterpreter *interp; /* The threads interpreter */
50 UV tid; /* Threads module's thread id */
51 perl_mutex mutex; /* Mutex for updating things in this struct */
52 int count; /* How many SVs have a reference to us */
53 int state; /* Detached, joined, finished, etc. */
54 int gimme; /* Context of create */
55 SV *init_function; /* Code to run */
56 SV *params; /* Args to pass function */
58 DWORD thr; /* OS's idea if thread id */
59 HANDLE handle; /* OS's waitable handle */
61 pthread_t thr; /* OS's handle for the thread */
67 /* Used by Perl interpreter for thread context switching */
68 #define MY_CXT_KEY "threads::_guts" XS_VERSION
77 /* Linked list of all threads */
78 static ithread *threads;
80 /* Protects the creation and destruction of threads*/
81 static perl_mutex create_destruct_mutex;
83 static UV tid_counter = 0;
84 static IV active_threads = 0;
85 static IV joinable_threads = 0;
86 #ifdef THREAD_CREATE_NEEDS_STACK
87 static IV default_stack_size = THREAD_CREATE_NEEDS_STACK;
89 static IV default_stack_size = 0;
91 static IV page_size = 0;
94 /* Used by Perl interpreter for thread context switching */
96 S_ithread_set(pTHX_ ithread *thread)
99 MY_CXT.thread = thread;
106 return (MY_CXT.thread);
110 /* Free any data (such as the Perl interpreter) attached to an ithread
111 * structure. This is a bit like undef on SVs, where the SV isn't freed,
112 * but the PVX is. Must be called with thread->mutex already held.
115 S_ithread_clear(pTHX_ ithread *thread)
117 PerlInterpreter *interp;
119 assert(thread->state & PERL_ITHR_FINISHED &&
120 thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
122 interp = thread->interp;
126 PERL_SET_CONTEXT(interp);
127 S_ithread_set(aTHX_ thread);
129 SvREFCNT_dec(thread->params);
130 thread->params = Nullsv;
132 perl_destruct(interp);
133 thread->interp = NULL;
138 PERL_SET_CONTEXT(aTHX);
142 /* Free an ithread structure and any attached data if its count == 0 */
144 S_ithread_destruct(pTHX_ ithread *thread)
150 MUTEX_LOCK(&thread->mutex);
152 /* Thread is still in use */
153 if (thread->count != 0) {
154 MUTEX_UNLOCK(&thread->mutex);
158 MUTEX_LOCK(&create_destruct_mutex);
159 /* Main thread (0) is immortal and should never get here */
160 assert(thread->tid != 0);
162 /* Remove from circular list of threads */
163 thread->next->prev = thread->prev;
164 thread->prev->next = thread->next;
167 MUTEX_UNLOCK(&create_destruct_mutex);
169 /* Thread is now disowned */
170 S_ithread_clear(aTHX_ thread);
173 handle = thread->handle;
174 thread->handle = NULL;
176 MUTEX_UNLOCK(&thread->mutex);
177 MUTEX_DESTROY(&thread->mutex);
184 /* Call PerlMemShared_free() in the context of the "first" interpreter
185 * per http://www.nntp.perl.org/group/perl.perl5.porters/110772
188 PerlMemShared_free(thread);
194 Perl_ithread_hook(pTHX)
196 int veto_cleanup = 0;
197 MUTEX_LOCK(&create_destruct_mutex);
198 if ((aTHX == PL_curinterp) && (joinable_threads != 1)) {
199 if (ckWARN_d(WARN_THREADS)) {
200 Perl_warn(aTHX_ "A thread exited while %" IVdf " threads were running", joinable_threads);
204 MUTEX_UNLOCK(&create_destruct_mutex);
205 return (veto_cleanup);
209 /* MAGIC (in mg.h sense) hooks */
212 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
214 ithread *thread = (ithread *)mg->mg_ptr;
215 SvIV_set(sv, PTR2IV(thread));
221 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
223 ithread *thread = (ithread *)mg->mg_ptr;
226 MUTEX_LOCK(&thread->mutex);
227 cleanup = ((--thread->count == 0) &&
228 (thread->state & PERL_ITHR_FINISHED) &&
229 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
230 MUTEX_UNLOCK(&thread->mutex);
233 S_ithread_destruct(aTHX_ thread);
238 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
240 ithread *thread = (ithread *)mg->mg_ptr;
241 MUTEX_LOCK(&thread->mutex);
243 MUTEX_UNLOCK(&thread->mutex);
247 MGVTBL ithread_vtbl = {
248 ithread_mg_get, /* get */
252 ithread_mg_free, /* free */
254 ithread_mg_dup /* dup */
258 /* Provided default, minimum and rational stack sizes */
260 good_stack_size(pTHX_ IV stack_size)
262 /* Use default stack size if no stack size specified */
264 return (default_stack_size);
266 #ifdef PTHREAD_STACK_MIN
267 /* Can't use less than minimum */
268 if (stack_size < PTHREAD_STACK_MIN) {
269 if (ckWARN_d(WARN_THREADS)) {
270 Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
272 return (PTHREAD_STACK_MIN);
276 /* Round up to page size boundary */
277 if (page_size <= 0) {
278 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
279 SETERRNO(0, SS_NORMAL);
281 page_size = sysconf(_SC_PAGESIZE);
283 page_size = sysconf(_SC_MMAP_PAGE_SIZE);
285 if ((long)page_size < 0) {
287 SV * const error = get_sv("@", FALSE);
288 (void)SvUPGRADE(error, SVt_PV);
289 Perl_croak(aTHX_ "PANIC: sysconf: %s", SvPV_nolen(error));
291 Perl_croak(aTHX_ "PANIC: sysconf: pagesize unknown");
295 # ifdef HAS_GETPAGESIZE
296 page_size = getpagesize();
298 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
299 page_size = PAGESIZE;
301 page_size = 8192; /* A conservative default */
305 Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)page_size);
308 stack_size = ((stack_size + (page_size - 1)) / page_size) * page_size;
314 /* Starts executing the thread.
315 * Passed as the C level function to run in the new thread.
318 static THREAD_RET_TYPE
319 S_ithread_run(LPVOID arg)
322 S_ithread_run(void * arg)
325 ithread *thread = (ithread *)arg;
328 dTHXa(thread->interp);
329 PERL_SET_CONTEXT(thread->interp);
330 S_ithread_set(aTHX_ thread);
333 /* Far from clear messing with ->thr child-side is a good idea */
334 MUTEX_LOCK(&thread->mutex);
336 thread->thr = GetCurrentThreadId();
338 thread->thr = pthread_self();
340 MUTEX_UNLOCK(&thread->mutex);
343 PL_perl_destruct_level = 2;
346 AV *params = (AV *)SvRV(thread->params);
347 int len = (int)av_len(params)+1;
354 /* Put args on the stack */
356 for (ii=0; ii < len; ii++) {
357 XPUSHs(av_shift(params));
361 /* Run the specified function */
362 len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
364 /* Remove args from stack and put back in params array */
366 for (ii=len-1; ii >= 0; ii--) {
368 av_store(params, ii, SvREFCNT_inc(sv));
371 /* Check for failure */
372 if (SvTRUE(ERRSV) && ckWARN_d(WARN_THREADS)) {
373 Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV);
379 /* Release function ref */
380 SvREFCNT_dec(thread->init_function);
381 thread->init_function = Nullsv;
384 PerlIO_flush((PerlIO *)NULL);
386 MUTEX_LOCK(&thread->mutex);
387 /* Mark as finished */
388 thread->state |= PERL_ITHR_FINISHED;
389 /* Cleanup if detached */
390 cleanup = (thread->state & PERL_ITHR_DETACHED);
391 MUTEX_UNLOCK(&thread->mutex);
394 S_ithread_destruct(aTHX_ thread);
396 MUTEX_LOCK(&create_destruct_mutex);
401 MUTEX_UNLOCK(&create_destruct_mutex);
411 /* Type conversion helper functions */
413 ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
419 MUTEX_LOCK(&thread->mutex);
421 MUTEX_UNLOCK(&thread->mutex);
428 sv = newSVrv(obj, classname);
429 sv_setiv(sv, PTR2IV(thread));
430 mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0);
431 mg->mg_flags |= MGf_DUP;
438 SV_to_ithread(pTHX_ SV *sv)
440 /* Argument is a thread */
442 return (INT2PTR(ithread *, SvIV(SvRV(sv))));
444 /* Argument is classname, therefore return current thread */
445 return (S_ithread_get(aTHX));
450 * Called in context of parent thread.
462 CLONE_PARAMS clone_param;
463 ithread *current_thread = S_ithread_get(aTHX);
465 SV **tmps_tmp = PL_tmps_stack;
466 IV tmps_ix = PL_tmps_ix;
468 int rc_stack_size = 0;
469 int rc_thread_create = 0;
472 MUTEX_LOCK(&create_destruct_mutex);
474 /* Allocate thread structure */
475 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
477 MUTEX_UNLOCK(&create_destruct_mutex);
478 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
481 Zero(thread, 1, ithread);
483 /* Add to threads list */
484 thread->next = threads;
485 thread->prev = threads->prev;
486 threads->prev = thread;
487 thread->prev->next = thread;
489 /* Set count to 1 immediately in case thread exits before
490 * we return to caller!
494 MUTEX_INIT(&thread->mutex);
495 thread->tid = tid_counter++;
496 thread->stack_size = good_stack_size(aTHX_ stack_size);
497 thread->gimme = gimme;
499 /* "Clone" our interpreter into the thread's interpreter.
500 * This gives thread access to "static data" and code.
502 PerlIO_flush((PerlIO *)NULL);
503 S_ithread_set(aTHX_ thread);
505 SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct value */
506 PL_srand_called = FALSE; /* Set it to false so we can detect if it gets
507 set during the clone */
510 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
512 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
515 /* perl_clone() leaves us in new interpreter's context. As it is tricky
516 * to spot an implicit aTHX, create a new scope with aTHX matching the
517 * context for the duration of our work for new interpreter.
520 dTHXa(thread->interp);
524 /* Here we remove END blocks since they should only run in the thread
527 SvREFCNT_dec(PL_endav);
530 if (SvPOK(init_function)) {
531 thread->init_function = newSV(0);
532 sv_copypv(thread->init_function, init_function);
534 clone_param.flags = 0;
535 thread->init_function = sv_dup(init_function, &clone_param);
536 if (SvREFCNT(thread->init_function) == 0) {
537 SvREFCNT_inc(thread->init_function);
541 thread->params = sv_dup(params, &clone_param);
542 SvREFCNT_inc(thread->params);
544 /* The code below checks that anything living on the tmps stack and
545 * has been cloned (so it lives in the ptr_table) has a refcount
548 * If the refcount is 0 it means that a something on the stack/context
549 * was holding a reference to it and since we init_stacks() in
550 * perl_clone that won't get cleaned and we will get a leaked scalar.
551 * The reason it was cloned was that it lived on the @_ stack.
553 * Example of this can be found in bugreport 15837 where calls in the
554 * parameter list end up as a temp.
556 * One could argue that this fix should be in perl_clone.
558 while (tmps_ix > 0) {
559 SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
561 if (sv && SvREFCNT(sv) == 0) {
567 SvTEMP_off(thread->init_function);
568 ptr_table_free(PL_ptr_table);
570 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
572 S_ithread_set(aTHX_ current_thread);
573 PERL_SET_CONTEXT(aTHX);
575 /* Create/start the thread */
577 thread->handle = CreateThread(NULL,
578 (DWORD)thread->stack_size,
581 STACK_SIZE_PARAM_IS_A_RESERVATION,
585 static pthread_attr_t attr;
586 static int attr_inited = 0;
587 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
589 pthread_attr_init(&attr);
593 # ifdef PTHREAD_ATTR_SETDETACHSTATE
594 /* Threads start out joinable */
595 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
598 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
599 /* Set thread's stack size */
600 if (thread->stack_size > 0) {
601 rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
605 /* Create the thread */
606 if (! rc_stack_size) {
607 # ifdef OLD_PTHREADS_API
608 rc_thread_create = pthread_create(&thread->thr,
613 # if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
614 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
616 rc_thread_create = pthread_create(&thread->thr,
623 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
624 /* Try to get thread's actual stack size */
628 stacksize = pthread_attr_getstacksize(attr);
630 if (! pthread_attr_getstacksize(&attr, &stacksize))
633 thread->stack_size = (IV)stacksize;
640 /* Check for errors */
642 if (thread->handle == NULL) {
644 if (rc_stack_size || rc_thread_create) {
646 MUTEX_UNLOCK(&create_destruct_mutex);
648 S_ithread_destruct(aTHX_ thread);
650 if (ckWARN_d(WARN_THREADS)) {
652 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
654 Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
657 return (&PL_sv_undef);
662 MUTEX_UNLOCK(&create_destruct_mutex);
666 return (ithread_to_SV(aTHX_ obj, thread, classname, FALSE));
669 #endif /* USE_ITHREADS */
672 MODULE = threads PACKAGE = threads PREFIX = ithread_
682 SV *function_to_call;
692 if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
694 Perl_croak(aTHX_ "Usage: threads->create(\\%specs, function, ...)");
695 specs = (HV*)SvRV(ST(1));
699 Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
704 if (sv_isobject(ST(0))) {
706 classname = HvNAME(SvSTASH(SvRV(ST(0))));
707 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
708 stack_size = thread->stack_size;
710 /* threads->create() */
711 classname = (char *)SvPV_nolen(ST(0));
712 stack_size = default_stack_size;
715 function_to_call = ST(idx+1);
720 if (hv_exists(specs, "stack", 5)) {
721 stack_size = SvIV(*hv_fetch(specs, "stack", 5, 0));
722 } else if (hv_exists(specs, "stacksize", 9)) {
723 stack_size = SvIV(*hv_fetch(specs, "stacksize", 9, 0));
724 } else if (hv_exists(specs, "stack_size", 10)) {
725 stack_size = SvIV(*hv_fetch(specs, "stack_size", 10, 0));
729 if (hv_exists(specs, "context", 7)) {
730 str = (char *)SvPV_nolen(*hv_fetch(specs, "context", 7, 0));
745 Perl_croak(aTHX_ "Invalid context: %s", str);
747 } else if (hv_exists(specs, "array", 5)) {
748 if (SvTRUE(*hv_fetch(specs, "array", 5, 0))) {
751 } else if (hv_exists(specs, "scalar", 6)) {
752 if (SvTRUE(*hv_fetch(specs, "scalar", 6, 0))) {
755 } else if (hv_exists(specs, "void", 4)) {
756 if (SvTRUE(*hv_fetch(specs, "void", 4, 0))) {
762 context = GIMME_V; /* Implicit context */
764 context |= (GIMME_V & (~(G_ARRAY|G_SCALAR|G_VOID)));
770 for (ii=2; ii < items ; ii++) {
771 av_push(params, SvREFCNT_inc(ST(idx+ii)));
776 ST(0) = sv_2mortal(S_ithread_create(aTHX_ Nullsv,
781 newRV_noinc((SV*)params)));
782 /* XSRETURN(1); - implied */
793 /* Class method only */
795 Perl_croak(aTHX_ "Usage: threads->list()");
796 classname = (char *)SvPV_nolen(ST(0));
798 /* Calling context */
799 list_context = (GIMME_V == G_ARRAY);
801 /* Walk through threads list */
802 MUTEX_LOCK(&create_destruct_mutex);
803 for (thread = threads->next;
805 thread = thread->next)
807 /* Ignore detached or joined threads */
808 if (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)) {
811 /* Push object on stack if list context */
813 XPUSHs(sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
817 MUTEX_UNLOCK(&create_destruct_mutex);
818 /* If scalar context, send back count */
819 if (! list_context) {
830 /* Class method only */
832 Perl_croak(aTHX_ "Usage: threads->self()");
833 classname = (char *)SvPV_nolen(ST(0));
835 thread = S_ithread_get(aTHX);
837 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
838 /* XSRETURN(1); - implied */
846 thread = SV_to_ithread(aTHX_ ST(0));
847 XST_mUV(0, thread->tid);
848 /* XSRETURN(1); - implied */
865 /* Object method only */
866 if (! sv_isobject(ST(0)))
867 Perl_croak(aTHX_ "Usage: $thr->join()");
869 /* Check if the thread is joinable */
870 thread = SV_to_ithread(aTHX_ ST(0));
871 MUTEX_LOCK(&thread->mutex);
872 join_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
873 MUTEX_UNLOCK(&thread->mutex);
875 if (join_err & PERL_ITHR_DETACHED) {
876 Perl_croak(aTHX_ "Cannot join a detached thread");
878 Perl_croak(aTHX_ "Thread already joined");
882 /* Join the thread */
884 waitcode = WaitForSingleObject(thread->handle, INFINITE);
886 pthread_join(thread->thr, &retval);
889 MUTEX_LOCK(&thread->mutex);
891 thread->state |= PERL_ITHR_JOINED;
893 /* Get the return value from the call_sv */
896 PerlInterpreter *other_perl;
897 CLONE_PARAMS clone_params;
898 ithread *current_thread;
900 params_copy = (AV *)SvRV(thread->params);
901 other_perl = thread->interp;
902 clone_params.stashes = newAV();
903 clone_params.flags = CLONEf_JOIN_IN;
904 PL_ptr_table = ptr_table_new();
905 current_thread = S_ithread_get(aTHX);
906 S_ithread_set(aTHX_ thread);
907 /* Ensure 'meaningful' addresses retain their meaning */
908 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
909 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
910 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
911 params = (AV *)sv_dup((SV*)params_copy, &clone_params);
912 S_ithread_set(aTHX_ current_thread);
913 SvREFCNT_dec(clone_params.stashes);
914 SvREFCNT_inc(params);
915 ptr_table_free(PL_ptr_table);
919 /* We are finished with the thread */
920 S_ithread_clear(aTHX_ thread);
921 MUTEX_UNLOCK(&thread->mutex);
923 /* If no return values, then just return */
928 /* Put return values on stack */
929 len = (int)AvFILL(params);
930 for (ii=0; ii <= len; ii++) {
931 SV* param = av_shift(params);
932 XPUSHs(sv_2mortal(param));
935 /* Free return value array */
936 SvREFCNT_dec(params);
952 thread = SV_to_ithread(aTHX_ ST(0));
953 MUTEX_LOCK(&thread->mutex);
955 /* Check if the thread is detachable */
956 if ((detach_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))) {
957 MUTEX_UNLOCK(&thread->mutex);
958 if (detach_err & PERL_ITHR_DETACHED) {
959 Perl_croak(aTHX_ "Thread already detached");
961 Perl_croak(aTHX_ "Cannot detach a joined thread");
965 /* Detach the thread */
966 thread->state |= PERL_ITHR_DETACHED;
968 /* Windows has no 'detach thread' function */
970 PERL_THREAD_DETACH(thread->thr);
972 /* Cleanup if finished */
973 cleanup = (thread->state & PERL_ITHR_FINISHED);
974 MUTEX_UNLOCK(&thread->mutex);
977 S_ithread_destruct(aTHX_ thread);
979 MUTEX_LOCK(&create_destruct_mutex);
981 MUTEX_UNLOCK(&create_destruct_mutex);
992 /* Must have safe signals */
993 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
994 Perl_croak(aTHX_ "Cannot signal other threads without safe signals");
996 /* Object method only */
997 if (! sv_isobject(ST(0)))
998 Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')");
1001 thread = SV_to_ithread(aTHX_ ST(0));
1004 sig_name = SvPV_nolen(ST(1));
1005 if (isALPHA(*sig_name)) {
1006 if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G')
1008 if ((signal = whichsig(sig_name)) < 0)
1009 Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name);
1011 signal = SvIV(ST(1));
1013 /* Set the signal for the thread */
1015 dTHXa(thread->interp);
1016 PL_psig_pend[signal]++;
1020 /* Return the thread to allow for method chaining */
1022 /* XSRETURN(1); - implied */
1026 ithread_DESTROY(...)
1028 sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar);
1036 /* Compares TIDs to determine thread equality */
1037 if (sv_isobject(ST(0)) && sv_isobject(ST(1))) {
1038 ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1039 ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
1040 are_equal = (thr1->tid == thr2->tid);
1045 /* Return 0 on false for backward compatibility */
1048 /* XSRETURN(1); - implied */
1059 /* Class method only */
1061 Perl_croak(aTHX_ "Usage: threads->object($tid)");
1062 classname = (char *)SvPV_nolen(ST(0));
1064 if ((items < 2) || ! SvOK(ST(1))) {
1068 /* threads->object($tid) */
1071 /* Walk through threads list */
1072 MUTEX_LOCK(&create_destruct_mutex);
1073 for (thread = threads->next;
1075 thread = thread->next)
1077 /* Look for TID, but ignore detached or joined threads */
1078 if ((thread->tid != tid) ||
1079 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))
1083 /* Put object on stack */
1084 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1088 MUTEX_UNLOCK(&create_destruct_mutex);
1092 /* XSRETURN(1); - implied */
1096 ithread__handle(...);
1100 thread = SV_to_ithread(aTHX_ ST(0));
1102 XST_mUV(0, PTR2UV(&thread->handle));
1104 XST_mUV(0, PTR2UV(&thread->thr));
1106 /* XSRETURN(1); - implied */
1110 ithread_get_stack_size(...)
1114 if (sv_isobject(ST(0))) {
1115 /* $thr->get_stack_size() */
1116 ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1117 stack_size = thread->stack_size;
1119 /* threads->get_stack_size() */
1120 stack_size = default_stack_size;
1122 XST_mIV(0, stack_size);
1123 /* XSRETURN(1); - implied */
1127 ithread_set_stack_size(...)
1132 Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
1133 if (sv_isobject(ST(0)))
1134 Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
1136 old_size = default_stack_size;
1137 default_stack_size = good_stack_size(aTHX_ SvIV(ST(1)));
1138 XST_mIV(0, old_size);
1139 /* XSRETURN(1); - implied */
1141 #endif /* USE_ITHREADS */
1147 /* The 'main' thread is thread 0.
1148 * It is detached (unjoinable) and immortal.
1154 PL_perl_destruct_level = 2;
1155 MUTEX_INIT(&create_destruct_mutex);
1156 MUTEX_LOCK(&create_destruct_mutex);
1158 PL_threadhook = &Perl_ithread_hook;
1160 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
1162 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
1165 Zero(thread, 1, ithread);
1167 PL_perl_destruct_level = 2;
1168 MUTEX_INIT(&thread->mutex);
1170 thread->tid = tid_counter++; /* Thread 0 */
1172 /* Head of the threads list */
1174 thread->next = thread;
1175 thread->prev = thread;
1177 thread->count = 1; /* Immortal */
1179 thread->interp = aTHX;
1180 thread->state = PERL_ITHR_DETACHED; /* Detached */
1181 thread->stack_size = default_stack_size;
1183 thread->thr = GetCurrentThreadId();
1185 thread->thr = pthread_self();
1191 S_ithread_set(aTHX_ thread);
1192 MUTEX_UNLOCK(&create_destruct_mutex);
1193 #endif /* USE_ITHREADS */