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 #ifdef THREAD_CREATE_NEEDS_STACK
86 static IV default_stack_size = THREAD_CREATE_NEEDS_STACK;
88 static IV default_stack_size = 0;
90 static IV page_size = 0;
93 /* Used by Perl interpreter for thread context switching */
95 S_ithread_set(pTHX_ ithread *thread)
98 MY_CXT.thread = thread;
105 return (MY_CXT.thread);
109 /* Free any data (such as the Perl interpreter) attached to an ithread
110 * structure. This is a bit like undef on SVs, where the SV isn't freed,
111 * but the PVX is. Must be called with thread->mutex already held.
114 S_ithread_clear(pTHX_ ithread *thread)
116 PerlInterpreter *interp;
118 assert(thread->state & PERL_ITHR_FINISHED &&
119 thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
121 interp = thread->interp;
125 PERL_SET_CONTEXT(interp);
126 S_ithread_set(aTHX_ thread);
128 SvREFCNT_dec(thread->params);
129 thread->params = Nullsv;
131 perl_destruct(interp);
132 thread->interp = NULL;
137 PERL_SET_CONTEXT(aTHX);
141 /* Free an ithread structure and any attached data if its count == 0 */
143 S_ithread_destruct(pTHX_ ithread *thread)
149 MUTEX_LOCK(&thread->mutex);
151 /* Thread is still in use */
152 if (thread->count != 0) {
153 MUTEX_UNLOCK(&thread->mutex);
157 MUTEX_LOCK(&create_destruct_mutex);
158 /* Main thread (0) is immortal and should never get here */
159 assert(thread->tid != 0);
161 /* Remove from circular list of threads */
162 thread->next->prev = thread->prev;
163 thread->prev->next = thread->next;
166 MUTEX_UNLOCK(&create_destruct_mutex);
168 /* Thread is now disowned */
169 S_ithread_clear(aTHX_ thread);
172 handle = thread->handle;
173 thread->handle = NULL;
175 MUTEX_UNLOCK(&thread->mutex);
176 MUTEX_DESTROY(&thread->mutex);
183 /* Call PerlMemShared_free() in the context of the "first" interpreter
184 * per http://www.nntp.perl.org/group/perl.perl5.porters/110772
187 PerlMemShared_free(thread);
193 Perl_ithread_hook(pTHX)
195 int veto_cleanup = 0;
196 MUTEX_LOCK(&create_destruct_mutex);
197 if ((aTHX == PL_curinterp) && (active_threads != 1)) {
198 if (ckWARN_d(WARN_THREADS)) {
199 Perl_warn(aTHX_ "A thread exited while %" IVdf " threads were running", active_threads);
203 MUTEX_UNLOCK(&create_destruct_mutex);
204 return (veto_cleanup);
208 /* MAGIC (in mg.h sense) hooks */
211 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
213 ithread *thread = (ithread *)mg->mg_ptr;
214 SvIV_set(sv, PTR2IV(thread));
220 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
222 ithread *thread = (ithread *)mg->mg_ptr;
225 MUTEX_LOCK(&thread->mutex);
226 cleanup = ((--thread->count == 0) &&
227 (thread->state & PERL_ITHR_FINISHED) &&
228 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
229 MUTEX_UNLOCK(&thread->mutex);
232 S_ithread_destruct(aTHX_ thread);
237 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
239 ithread *thread = (ithread *)mg->mg_ptr;
240 MUTEX_LOCK(&thread->mutex);
242 MUTEX_UNLOCK(&thread->mutex);
246 MGVTBL ithread_vtbl = {
247 ithread_mg_get, /* get */
251 ithread_mg_free, /* free */
253 ithread_mg_dup /* dup */
257 /* Provided default, minimum and rational stack sizes */
259 good_stack_size(pTHX_ IV stack_size)
261 /* Use default stack size if no stack size specified */
263 return (default_stack_size);
265 #ifdef PTHREAD_STACK_MIN
266 /* Can't use less than minimum */
267 if (stack_size < PTHREAD_STACK_MIN) {
268 if (ckWARN_d(WARN_THREADS)) {
269 Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
271 return (PTHREAD_STACK_MIN);
275 /* Round up to page size boundary */
276 if (page_size <= 0) {
277 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
278 SETERRNO(0, SS_NORMAL);
280 page_size = sysconf(_SC_PAGESIZE);
282 page_size = sysconf(_SC_MMAP_PAGE_SIZE);
284 if ((long)page_size < 0) {
286 SV * const error = get_sv("@", FALSE);
287 (void)SvUPGRADE(error, SVt_PV);
288 Perl_croak(aTHX_ "PANIC: sysconf: %s", SvPV_nolen(error));
290 Perl_croak(aTHX_ "PANIC: sysconf: pagesize unknown");
294 # ifdef HAS_GETPAGESIZE
295 page_size = getpagesize();
297 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
298 page_size = PAGESIZE;
300 page_size = 8192; /* A conservative default */
304 Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)page_size);
307 stack_size = ((stack_size + (page_size - 1)) / page_size) * page_size;
313 /* Starts executing the thread.
314 * Passed as the C level function to run in the new thread.
317 static THREAD_RET_TYPE
318 S_ithread_run(LPVOID arg)
321 S_ithread_run(void * arg)
324 ithread *thread = (ithread *)arg;
327 dTHXa(thread->interp);
328 PERL_SET_CONTEXT(thread->interp);
329 S_ithread_set(aTHX_ thread);
332 /* Far from clear messing with ->thr child-side is a good idea */
333 MUTEX_LOCK(&thread->mutex);
335 thread->thr = GetCurrentThreadId();
337 thread->thr = pthread_self();
339 MUTEX_UNLOCK(&thread->mutex);
342 PL_perl_destruct_level = 2;
345 AV *params = (AV *)SvRV(thread->params);
346 int len = (int)av_len(params)+1;
353 /* Put args on the stack */
355 for (ii=0; ii < len; ii++) {
356 XPUSHs(av_shift(params));
360 /* Run the specified function */
361 len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
363 /* Remove args from stack and put back in params array */
365 for (ii=len-1; ii >= 0; ii--) {
367 av_store(params, ii, SvREFCNT_inc(sv));
370 /* Check for failure */
371 if (SvTRUE(ERRSV) && ckWARN_d(WARN_THREADS)) {
372 Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV);
378 /* Release function ref */
379 SvREFCNT_dec(thread->init_function);
380 thread->init_function = Nullsv;
383 PerlIO_flush((PerlIO *)NULL);
385 MUTEX_LOCK(&thread->mutex);
386 /* Mark as finished */
387 thread->state |= PERL_ITHR_FINISHED;
388 /* Cleanup if detached */
389 cleanup = (thread->state & PERL_ITHR_DETACHED);
390 MUTEX_UNLOCK(&thread->mutex);
393 S_ithread_destruct(aTHX_ thread);
395 MUTEX_LOCK(&create_destruct_mutex);
397 MUTEX_UNLOCK(&create_destruct_mutex);
407 /* Type conversion helper functions */
409 ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
415 MUTEX_LOCK(&thread->mutex);
417 MUTEX_UNLOCK(&thread->mutex);
424 sv = newSVrv(obj, classname);
425 sv_setiv(sv, PTR2IV(thread));
426 mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0);
427 mg->mg_flags |= MGf_DUP;
434 SV_to_ithread(pTHX_ SV *sv)
436 /* Argument is a thread */
438 return (INT2PTR(ithread *, SvIV(SvRV(sv))));
440 /* Argument is classname, therefore return current thread */
441 return (S_ithread_get(aTHX));
446 * Called in context of parent thread.
457 CLONE_PARAMS clone_param;
458 ithread *current_thread = S_ithread_get(aTHX);
460 SV **tmps_tmp = PL_tmps_stack;
461 IV tmps_ix = PL_tmps_ix;
463 int rc_stack_size = 0;
464 int rc_thread_create = 0;
467 MUTEX_LOCK(&create_destruct_mutex);
469 /* Allocate thread structure */
470 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
472 MUTEX_UNLOCK(&create_destruct_mutex);
473 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
476 Zero(thread, 1, ithread);
478 /* Add to threads list */
479 thread->next = threads;
480 thread->prev = threads->prev;
481 threads->prev = thread;
482 thread->prev->next = thread;
484 /* Set count to 1 immediately in case thread exits before
485 * we return to caller!
489 MUTEX_INIT(&thread->mutex);
490 thread->tid = tid_counter++;
491 thread->stack_size = good_stack_size(aTHX_ stack_size);
492 thread->gimme = GIMME_V;
494 /* "Clone" our interpreter into the thread's interpreter.
495 * This gives thread access to "static data" and code.
497 PerlIO_flush((PerlIO *)NULL);
498 S_ithread_set(aTHX_ thread);
500 SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct value */
501 PL_srand_called = FALSE; /* Set it to false so we can detect if it gets
502 set during the clone */
505 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
507 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
510 /* perl_clone() leaves us in new interpreter's context. As it is tricky
511 * to spot an implicit aTHX, create a new scope with aTHX matching the
512 * context for the duration of our work for new interpreter.
515 dTHXa(thread->interp);
519 /* Here we remove END blocks since they should only run in the thread
522 SvREFCNT_dec(PL_endav);
525 clone_param.flags = 0;
526 thread->init_function = sv_dup(init_function, &clone_param);
527 if (SvREFCNT(thread->init_function) == 0) {
528 SvREFCNT_inc(thread->init_function);
531 thread->params = sv_dup(params, &clone_param);
532 SvREFCNT_inc(thread->params);
534 /* The code below checks that anything living on the tmps stack and
535 * has been cloned (so it lives in the ptr_table) has a refcount
538 * If the refcount is 0 it means that a something on the stack/context
539 * was holding a reference to it and since we init_stacks() in
540 * perl_clone that won't get cleaned and we will get a leaked scalar.
541 * The reason it was cloned was that it lived on the @_ stack.
543 * Example of this can be found in bugreport 15837 where calls in the
544 * parameter list end up as a temp.
546 * One could argue that this fix should be in perl_clone.
548 while (tmps_ix > 0) {
549 SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
551 if (sv && SvREFCNT(sv) == 0) {
557 SvTEMP_off(thread->init_function);
558 ptr_table_free(PL_ptr_table);
560 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
562 S_ithread_set(aTHX_ current_thread);
563 PERL_SET_CONTEXT(aTHX);
565 /* Create/start the thread */
567 thread->handle = CreateThread(NULL,
568 (DWORD)thread->stack_size,
571 STACK_SIZE_PARAM_IS_A_RESERVATION,
575 static pthread_attr_t attr;
576 static int attr_inited = 0;
577 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
579 pthread_attr_init(&attr);
583 # ifdef PTHREAD_ATTR_SETDETACHSTATE
584 /* Threads start out joinable */
585 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
588 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
589 /* Set thread's stack size */
590 if (thread->stack_size > 0) {
591 rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
595 /* Create the thread */
596 if (! rc_stack_size) {
597 # ifdef OLD_PTHREADS_API
598 rc_thread_create = pthread_create(&thread->thr,
603 # if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
604 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
606 rc_thread_create = pthread_create(&thread->thr,
613 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
614 /* Try to get thread's actual stack size */
617 if (! pthread_attr_getstacksize(&attr, &stacksize)) {
619 thread->stack_size = (IV)stacksize;
627 /* Check for errors */
629 if (thread->handle == NULL) {
631 if (rc_stack_size || rc_thread_create) {
633 MUTEX_UNLOCK(&create_destruct_mutex);
635 S_ithread_destruct(aTHX_ thread);
637 if (ckWARN_d(WARN_THREADS)) {
639 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
641 Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
644 return (&PL_sv_undef);
648 MUTEX_UNLOCK(&create_destruct_mutex);
652 return (ithread_to_SV(aTHX_ obj, thread, classname, FALSE));
655 #endif /* USE_ITHREADS */
658 MODULE = threads PACKAGE = threads PREFIX = ithread_
668 SV *function_to_call;
675 if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
677 Perl_croak(aTHX_ "Usage: threads->create(\\%specs, function, ...)");
678 specs = (HV*)SvRV(ST(1));
682 Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
687 if (sv_isobject(ST(0))) {
689 classname = HvNAME(SvSTASH(SvRV(ST(0))));
690 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
691 stack_size = thread->stack_size;
693 /* threads->create() */
694 classname = (char *)SvPV_nolen(ST(0));
695 stack_size = default_stack_size;
698 function_to_call = ST(idx+1);
702 if (hv_exists(specs, "stack", 5)) {
703 stack_size = SvIV(*hv_fetch(specs, "stack", 5, 0));
704 } else if (hv_exists(specs, "stacksize", 9)) {
705 stack_size = SvIV(*hv_fetch(specs, "stacksize", 9, 0));
706 } else if (hv_exists(specs, "stack_size", 10)) {
707 stack_size = SvIV(*hv_fetch(specs, "stack_size", 10, 0));
714 for (ii=2; ii < items ; ii++) {
715 av_push(params, SvREFCNT_inc(ST(idx+ii)));
720 ST(0) = sv_2mortal(S_ithread_create(aTHX_ Nullsv,
724 newRV_noinc((SV*)params)));
725 /* XSRETURN(1); - implied */
736 /* Class method only */
738 Perl_croak(aTHX_ "Usage: threads->list()");
739 classname = (char *)SvPV_nolen(ST(0));
741 /* Calling context */
742 list_context = (GIMME_V == G_ARRAY);
744 /* Walk through threads list */
745 MUTEX_LOCK(&create_destruct_mutex);
746 for (thread = threads->next;
748 thread = thread->next)
750 /* Ignore detached or joined threads */
751 if (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)) {
754 /* Push object on stack if list context */
756 XPUSHs(sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
760 MUTEX_UNLOCK(&create_destruct_mutex);
761 /* If scalar context, send back count */
762 if (! list_context) {
773 /* Class method only */
775 Perl_croak(aTHX_ "Usage: threads->self()");
776 classname = (char *)SvPV_nolen(ST(0));
778 thread = S_ithread_get(aTHX);
780 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
781 /* XSRETURN(1); - implied */
789 thread = SV_to_ithread(aTHX_ ST(0));
790 XST_mUV(0, thread->tid);
791 /* XSRETURN(1); - implied */
808 /* Object method only */
809 if (! sv_isobject(ST(0)))
810 Perl_croak(aTHX_ "Usage: $thr->join()");
812 /* Check if the thread is joinable */
813 thread = SV_to_ithread(aTHX_ ST(0));
814 MUTEX_LOCK(&thread->mutex);
815 join_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
816 MUTEX_UNLOCK(&thread->mutex);
818 if (join_err & PERL_ITHR_DETACHED) {
819 Perl_croak(aTHX_ "Cannot join a detached thread");
821 Perl_croak(aTHX_ "Thread already joined");
825 /* Join the thread */
827 waitcode = WaitForSingleObject(thread->handle, INFINITE);
829 pthread_join(thread->thr, &retval);
832 MUTEX_LOCK(&thread->mutex);
834 thread->state |= PERL_ITHR_JOINED;
836 /* Get the return value from the call_sv */
839 PerlInterpreter *other_perl;
840 CLONE_PARAMS clone_params;
841 ithread *current_thread;
843 params_copy = (AV *)SvRV(thread->params);
844 other_perl = thread->interp;
845 clone_params.stashes = newAV();
846 clone_params.flags = CLONEf_JOIN_IN;
847 PL_ptr_table = ptr_table_new();
848 current_thread = S_ithread_get(aTHX);
849 S_ithread_set(aTHX_ thread);
850 /* Ensure 'meaningful' addresses retain their meaning */
851 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
852 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
853 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
854 params = (AV *)sv_dup((SV*)params_copy, &clone_params);
855 S_ithread_set(aTHX_ current_thread);
856 SvREFCNT_dec(clone_params.stashes);
857 SvREFCNT_inc(params);
858 ptr_table_free(PL_ptr_table);
862 /* We are finished with the thread */
863 S_ithread_clear(aTHX_ thread);
864 MUTEX_UNLOCK(&thread->mutex);
866 /* If no return values, then just return */
871 /* Put return values on stack */
872 len = (int)AvFILL(params);
873 for (ii=0; ii <= len; ii++) {
874 SV* param = av_shift(params);
875 XPUSHs(sv_2mortal(param));
878 /* Free return value array */
879 SvREFCNT_dec(params);
895 thread = SV_to_ithread(aTHX_ ST(0));
896 MUTEX_LOCK(&thread->mutex);
898 /* Check if the thread is detachable */
899 if ((detach_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))) {
900 MUTEX_UNLOCK(&thread->mutex);
901 if (detach_err & PERL_ITHR_DETACHED) {
902 Perl_croak(aTHX_ "Thread already detached");
904 Perl_croak(aTHX_ "Cannot detach a joined thread");
908 /* Detach the thread */
909 thread->state |= PERL_ITHR_DETACHED;
911 /* Windows has no 'detach thread' function */
913 PERL_THREAD_DETACH(thread->thr);
915 /* Cleanup if finished */
916 cleanup = (thread->state & PERL_ITHR_FINISHED);
917 MUTEX_UNLOCK(&thread->mutex);
920 S_ithread_destruct(aTHX_ thread);
930 /* Must have safe signals */
931 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
932 Perl_croak(aTHX_ "Cannot signal other threads without safe signals");
934 /* Object method only */
935 if (! sv_isobject(ST(0)))
936 Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')");
939 thread = SV_to_ithread(aTHX_ ST(0));
942 sig_name = SvPV_nolen(ST(1));
943 if (isALPHA(*sig_name)) {
944 if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G')
946 if ((signal = whichsig(sig_name)) < 0)
947 Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name);
949 signal = SvIV(ST(1));
951 /* Set the signal for the thread */
953 dTHXa(thread->interp);
954 PL_psig_pend[signal]++;
958 /* Return the thread to allow for method chaining */
960 /* XSRETURN(1); - implied */
966 sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar);
974 /* Compares TIDs to determine thread equality */
975 if (sv_isobject(ST(0)) && sv_isobject(ST(1))) {
976 ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
977 ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
978 are_equal = (thr1->tid == thr2->tid);
983 /* Return 0 on false for backward compatibility */
986 /* XSRETURN(1); - implied */
997 /* Class method only */
999 Perl_croak(aTHX_ "Usage: threads->object($tid)");
1000 classname = (char *)SvPV_nolen(ST(0));
1002 if ((items < 2) || ! SvOK(ST(1))) {
1006 /* threads->object($tid) */
1009 /* Walk through threads list */
1010 MUTEX_LOCK(&create_destruct_mutex);
1011 for (thread = threads->next;
1013 thread = thread->next)
1015 /* Look for TID, but ignore detached or joined threads */
1016 if ((thread->tid != tid) ||
1017 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))
1021 /* Put object on stack */
1022 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1026 MUTEX_UNLOCK(&create_destruct_mutex);
1030 /* XSRETURN(1); - implied */
1034 ithread__handle(...);
1038 thread = SV_to_ithread(aTHX_ ST(0));
1040 XST_mUV(0, PTR2UV(&thread->handle));
1042 XST_mUV(0, PTR2UV(&thread->thr));
1044 /* XSRETURN(1); - implied */
1048 ithread_get_stack_size(...)
1052 if (sv_isobject(ST(0))) {
1053 /* $thr->get_stack_size() */
1054 ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1055 stack_size = thread->stack_size;
1057 /* threads->get_stack_size() */
1058 stack_size = default_stack_size;
1060 XST_mIV(0, stack_size);
1061 /* XSRETURN(1); - implied */
1065 ithread_set_stack_size(...)
1070 Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
1071 if (sv_isobject(ST(0)))
1072 Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
1074 old_size = default_stack_size;
1075 default_stack_size = good_stack_size(aTHX_ SvIV(ST(1)));
1076 XST_mIV(0, old_size);
1077 /* XSRETURN(1); - implied */
1079 #endif /* USE_ITHREADS */
1085 /* The 'main' thread is thread 0.
1086 * It is detached (unjoinable) and immortal.
1092 PL_perl_destruct_level = 2;
1093 MUTEX_INIT(&create_destruct_mutex);
1094 MUTEX_LOCK(&create_destruct_mutex);
1096 PL_threadhook = &Perl_ithread_hook;
1098 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
1100 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
1103 Zero(thread, 1, ithread);
1105 PL_perl_destruct_level = 2;
1106 MUTEX_INIT(&thread->mutex);
1108 thread->tid = tid_counter++; /* Thread 0 */
1110 /* Head of the threads list */
1112 thread->next = thread;
1113 thread->prev = thread;
1115 thread->count = 1; /* Immortal */
1117 thread->interp = aTHX;
1118 thread->state = PERL_ITHR_DETACHED; /* Detached */
1119 thread->stack_size = default_stack_size;
1121 thread->thr = GetCurrentThreadId();
1123 thread->thr = pthread_self();
1128 S_ithread_set(aTHX_ thread);
1129 MUTEX_UNLOCK(&create_destruct_mutex);
1130 #endif /* USE_ITHREADS */