1 #define PERL_NO_GET_CONTEXT
6 # define NEED_newRV_noinc
7 # define NEED_sv_2pv_nolen
16 /* Supposed to be in Winbase.h */
17 # ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
18 # define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
20 # include <win32thread.h>
23 typedef perl_os_thread pthread_t;
28 # define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
29 # ifdef OLD_PTHREADS_API
30 # define PERL_THREAD_DETACH(t) pthread_detach(&(t))
32 # define PERL_THREAD_DETACH(t) pthread_detach((t))
36 /* Values for 'state' member */
37 #define PERL_ITHR_JOINABLE 0
38 #define PERL_ITHR_DETACHED 1
39 #define PERL_ITHR_JOINED 2
40 #define PERL_ITHR_FINISHED 4
42 typedef struct _ithread {
43 struct _ithread *next; /* Next thread in the list */
44 struct _ithread *prev; /* Prev thread in the list */
45 PerlInterpreter *interp; /* The threads interpreter */
46 UV tid; /* Threads module's thread id */
47 perl_mutex mutex; /* Mutex for updating things in this struct */
48 int count; /* How many SVs have a reference to us */
49 int state; /* Detached, joined, finished, etc. */
50 int gimme; /* Context of create */
51 SV *init_function; /* Code to run */
52 SV *params; /* Args to pass function */
54 DWORD thr; /* OS's idea if thread id */
55 HANDLE handle; /* OS's waitable handle */
57 pthread_t thr; /* OS's handle for the thread */
63 /* Used by Perl interpreter for thread context switching */
64 #define MY_CXT_KEY "threads::_guts" XS_VERSION
73 /* Linked list of all threads */
74 static ithread *threads;
76 /* Protects the creation and destruction of threads*/
77 static perl_mutex create_destruct_mutex;
79 static UV tid_counter = 0;
80 static IV active_threads = 0;
81 #ifdef THREAD_CREATE_NEEDS_STACK
82 static IV default_stack_size = THREAD_CREATE_NEEDS_STACK;
84 static IV default_stack_size = 0;
86 static IV page_size = 0;
89 /* Used by Perl interpreter for thread context switching */
91 S_ithread_set(pTHX_ ithread *thread)
94 MY_CXT.thread = thread;
101 return (MY_CXT.thread);
105 /* Free any data (such as the Perl interpreter) attached to an ithread
106 * structure. This is a bit like undef on SVs, where the SV isn't freed,
107 * but the PVX is. Must be called with thread->mutex already held.
110 S_ithread_clear(pTHX_ ithread *thread)
112 PerlInterpreter *interp;
114 assert(thread->state & PERL_ITHR_FINISHED &&
115 thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
117 interp = thread->interp;
121 PERL_SET_CONTEXT(interp);
122 S_ithread_set(aTHX_ thread);
124 SvREFCNT_dec(thread->params);
125 thread->params = Nullsv;
127 perl_destruct(interp);
128 thread->interp = NULL;
133 PERL_SET_CONTEXT(aTHX);
137 /* Free an ithread structure and any attached data if its count == 0 */
139 S_ithread_destruct(pTHX_ ithread *thread)
145 MUTEX_LOCK(&thread->mutex);
147 /* Thread is still in use */
148 if (thread->count != 0) {
149 MUTEX_UNLOCK(&thread->mutex);
153 MUTEX_LOCK(&create_destruct_mutex);
154 /* Main thread (0) is immortal and should never get here */
155 assert(thread->tid != 0);
157 /* Remove from circular list of threads */
158 thread->next->prev = thread->prev;
159 thread->prev->next = thread->next;
162 MUTEX_UNLOCK(&create_destruct_mutex);
164 /* Thread is now disowned */
165 S_ithread_clear(aTHX_ thread);
168 handle = thread->handle;
169 thread->handle = NULL;
171 MUTEX_UNLOCK(&thread->mutex);
172 MUTEX_DESTROY(&thread->mutex);
179 /* Call PerlMemShared_free() in the context of the "first" interpreter
180 * per http://www.nntp.perl.org/group/perl.perl5.porters/110772
183 PerlMemShared_free(thread);
189 Perl_ithread_hook(pTHX)
191 int veto_cleanup = 0;
192 MUTEX_LOCK(&create_destruct_mutex);
193 if ((aTHX == PL_curinterp) && (active_threads != 1)) {
194 if (ckWARN_d(WARN_THREADS)) {
195 Perl_warn(aTHX_ "A thread exited while %" IVdf " threads were running", active_threads);
199 MUTEX_UNLOCK(&create_destruct_mutex);
200 return (veto_cleanup);
204 /* MAGIC (in mg.h sense) hooks */
207 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
209 ithread *thread = (ithread *)mg->mg_ptr;
210 SvIV_set(sv, PTR2IV(thread));
216 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
218 ithread *thread = (ithread *)mg->mg_ptr;
221 MUTEX_LOCK(&thread->mutex);
222 cleanup = ((--thread->count == 0) &&
223 (thread->state & PERL_ITHR_FINISHED) &&
224 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
225 MUTEX_UNLOCK(&thread->mutex);
228 S_ithread_destruct(aTHX_ thread);
233 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
235 ithread *thread = (ithread *)mg->mg_ptr;
236 MUTEX_LOCK(&thread->mutex);
238 MUTEX_UNLOCK(&thread->mutex);
242 MGVTBL ithread_vtbl = {
243 ithread_mg_get, /* get */
247 ithread_mg_free, /* free */
249 ithread_mg_dup /* dup */
253 /* Provided default, minimum and rational stack sizes */
255 good_stack_size(pTHX_ IV stack_size)
257 /* Use default stack size if no stack size specified */
259 return (default_stack_size);
261 #ifdef PTHREAD_STACK_MIN
262 /* Can't use less than minimum */
263 if (stack_size < PTHREAD_STACK_MIN) {
264 if (ckWARN_d(WARN_THREADS)) {
265 Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
267 return (PTHREAD_STACK_MIN);
271 /* Round up to page size boundary */
272 if (page_size <= 0) {
273 #ifdef PL_mmap_page_size
274 page_size = PL_mmap_page_size;
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;
302 Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)page_size);
305 page_size = 8192; /* A conservative default */
309 stack_size = ((stack_size + (page_size - 1)) / page_size) * page_size;
315 /* Starts executing the thread.
316 * Passed as the C level function to run in the new thread.
319 static THREAD_RET_TYPE
320 S_ithread_run(LPVOID arg)
323 S_ithread_run(void * arg)
326 ithread *thread = (ithread *)arg;
329 dTHXa(thread->interp);
330 PERL_SET_CONTEXT(thread->interp);
331 S_ithread_set(aTHX_ thread);
334 /* Far from clear messing with ->thr child-side is a good idea */
335 MUTEX_LOCK(&thread->mutex);
337 thread->thr = GetCurrentThreadId();
339 thread->thr = pthread_self();
341 MUTEX_UNLOCK(&thread->mutex);
344 PL_perl_destruct_level = 2;
347 AV *params = (AV *)SvRV(thread->params);
348 int len = (int)av_len(params)+1;
355 /* Put args on the stack */
357 for (ii=0; ii < len; ii++) {
358 XPUSHs(av_shift(params));
362 /* Run the specified function */
363 len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
365 /* Remove args from stack and put back in params array */
367 for (ii=len-1; ii >= 0; ii--) {
369 av_store(params, ii, SvREFCNT_inc(sv));
372 /* Check for failure */
373 if (SvTRUE(ERRSV) && ckWARN_d(WARN_THREADS)) {
374 Perl_warn(aTHX_ "Thread failed to start: %" SVf, ERRSV);
380 /* Release function ref */
381 SvREFCNT_dec(thread->init_function);
382 thread->init_function = Nullsv;
385 PerlIO_flush((PerlIO *)NULL);
387 MUTEX_LOCK(&thread->mutex);
388 /* Mark as finished */
389 thread->state |= PERL_ITHR_FINISHED;
390 /* Cleanup if detached */
391 cleanup = (thread->state & PERL_ITHR_DETACHED);
392 MUTEX_UNLOCK(&thread->mutex);
395 S_ithread_destruct(aTHX_ thread);
397 MUTEX_LOCK(&create_destruct_mutex);
399 MUTEX_UNLOCK(&create_destruct_mutex);
409 /* Type conversion helper functions */
411 ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
417 MUTEX_LOCK(&thread->mutex);
419 MUTEX_UNLOCK(&thread->mutex);
426 sv = newSVrv(obj, classname);
427 sv_setiv(sv, PTR2IV(thread));
428 mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0);
429 mg->mg_flags |= MGf_DUP;
436 SV_to_ithread(pTHX_ SV *sv)
438 /* Argument is a thread */
440 return (INT2PTR(ithread *, SvIV(SvRV(sv))));
442 /* Argument is classname, therefore return current thread */
443 return (S_ithread_get(aTHX));
448 * Called in context of parent thread.
459 CLONE_PARAMS clone_param;
460 ithread *current_thread = S_ithread_get(aTHX);
462 SV **tmps_tmp = PL_tmps_stack;
463 IV tmps_ix = PL_tmps_ix;
465 int rc_stack_size = 0;
466 int rc_thread_create = 0;
469 MUTEX_LOCK(&create_destruct_mutex);
471 /* Allocate thread structure */
472 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
474 MUTEX_UNLOCK(&create_destruct_mutex);
475 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
478 Zero(thread, 1, ithread);
480 /* Add to threads list */
481 thread->next = threads;
482 thread->prev = threads->prev;
483 threads->prev = thread;
484 thread->prev->next = thread;
486 /* Set count to 1 immediately in case thread exits before
487 * we return to caller!
491 MUTEX_INIT(&thread->mutex);
492 thread->tid = tid_counter++;
493 thread->stack_size = good_stack_size(aTHX_ stack_size);
494 thread->gimme = GIMME_V;
496 /* "Clone" our interpreter into the thread's interpreter.
497 * This gives thread access to "static data" and code.
499 PerlIO_flush((PerlIO *)NULL);
500 S_ithread_set(aTHX_ thread);
502 SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct value */
503 PL_srand_called = FALSE; /* Set it to false so we can detect if it gets
504 set during the clone */
507 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
509 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
512 /* perl_clone() leaves us in new interpreter's context. As it is tricky
513 * to spot an implicit aTHX, create a new scope with aTHX matching the
514 * context for the duration of our work for new interpreter.
517 dTHXa(thread->interp);
521 /* Here we remove END blocks since they should only run in the thread
524 SvREFCNT_dec(PL_endav);
526 clone_param.flags = 0;
527 thread->init_function = sv_dup(init_function, &clone_param);
528 if (SvREFCNT(thread->init_function) == 0) {
529 SvREFCNT_inc(thread->init_function);
532 thread->params = sv_dup(params, &clone_param);
533 SvREFCNT_inc(thread->params);
535 /* The code below checks that anything living on the tmps stack and
536 * has been cloned (so it lives in the ptr_table) has a refcount
539 * If the refcount is 0 it means that a something on the stack/context
540 * was holding a reference to it and since we init_stacks() in
541 * perl_clone that won't get cleaned and we will get a leaked scalar.
542 * The reason it was cloned was that it lived on the @_ stack.
544 * Example of this can be found in bugreport 15837 where calls in the
545 * parameter list end up as a temp.
547 * One could argue that this fix should be in perl_clone.
549 while (tmps_ix > 0) {
550 SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
552 if (sv && SvREFCNT(sv) == 0) {
558 SvTEMP_off(thread->init_function);
559 ptr_table_free(PL_ptr_table);
561 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
563 S_ithread_set(aTHX_ current_thread);
564 PERL_SET_CONTEXT(aTHX);
566 /* Create/start the thread */
568 thread->handle = CreateThread(NULL,
569 (DWORD)thread->stack_size,
572 STACK_SIZE_PARAM_IS_A_RESERVATION,
576 static pthread_attr_t attr;
577 static int attr_inited = 0;
578 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
580 pthread_attr_init(&attr);
584 # ifdef PTHREAD_ATTR_SETDETACHSTATE
585 /* Threads start out joinable */
586 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
589 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
590 /* Set thread's stack size */
591 if (thread->stack_size > 0) {
592 rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
596 /* Create the thread */
597 if (! rc_stack_size) {
598 # ifdef OLD_PTHREADS_API
599 rc_thread_create = pthread_create(&thread->thr,
604 # if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
605 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
607 rc_thread_create = pthread_create(&thread->thr,
614 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
615 /* Try to get thread's actual stack size */
618 if (! pthread_attr_getstacksize(&attr, &stacksize)) {
620 thread->stack_size = (IV)stacksize;
628 /* Check for errors */
630 if (thread->handle == NULL) {
632 if (rc_stack_size || rc_thread_create) {
634 MUTEX_UNLOCK(&create_destruct_mutex);
636 S_ithread_destruct(aTHX_ thread);
638 if (ckWARN_d(WARN_THREADS)) {
640 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
642 Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
645 return (&PL_sv_undef);
649 MUTEX_UNLOCK(&create_destruct_mutex);
653 return (ithread_to_SV(aTHX_ obj, thread, classname, FALSE));
656 #endif /* USE_ITHREADS */
659 MODULE = threads PACKAGE = threads PREFIX = ithread_
669 SV *function_to_call;
676 if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
678 Perl_croak(aTHX_ "Usage: threads->create(\\%specs, function, ...)");
679 specs = (HV*)SvRV(ST(1));
683 Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
688 if (sv_isobject(ST(0))) {
690 classname = HvNAME(SvSTASH(SvRV(ST(0))));
691 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
692 stack_size = thread->stack_size;
694 /* threads->create() */
695 classname = (char *)SvPV_nolen(ST(0));
696 stack_size = default_stack_size;
699 function_to_call = ST(idx+1);
703 if (hv_exists(specs, "stack", 5)) {
704 stack_size = SvIV(*hv_fetch(specs, "stack", 5, 0));
705 } else if (hv_exists(specs, "stacksize", 9)) {
706 stack_size = SvIV(*hv_fetch(specs, "stacksize", 9, 0));
707 } else if (hv_exists(specs, "stack_size", 10)) {
708 stack_size = SvIV(*hv_fetch(specs, "stack_size", 10, 0));
715 for (ii=2; ii < items ; ii++) {
716 av_push(params, SvREFCNT_inc(ST(idx+ii)));
721 ST(0) = sv_2mortal(S_ithread_create(aTHX_ Nullsv,
725 newRV_noinc((SV*)params)));
726 /* XSRETURN(1); - implied */
737 /* Class method only */
739 Perl_croak(aTHX_ "Usage: threads->list()");
740 classname = (char *)SvPV_nolen(ST(0));
742 /* Calling context */
743 list_context = (GIMME_V == G_ARRAY);
745 /* Walk through threads list */
746 MUTEX_LOCK(&create_destruct_mutex);
747 for (thread = threads->next;
749 thread = thread->next)
751 /* Ignore detached or joined threads */
752 if (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)) {
755 /* Push object on stack if list context */
757 XPUSHs(sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
761 MUTEX_UNLOCK(&create_destruct_mutex);
762 /* If scalar context, send back count */
763 if (! list_context) {
774 /* Class method only */
776 Perl_croak(aTHX_ "Usage: threads->self()");
777 classname = (char *)SvPV_nolen(ST(0));
779 thread = S_ithread_get(aTHX);
781 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
782 /* XSRETURN(1); - implied */
790 thread = SV_to_ithread(aTHX_ ST(0));
791 XST_mUV(0, thread->tid);
792 /* XSRETURN(1); - implied */
809 /* Object method only */
810 if (! sv_isobject(ST(0)))
811 Perl_croak(aTHX_ "Usage: $thr->join()");
813 /* Check if the thread is joinable */
814 thread = SV_to_ithread(aTHX_ ST(0));
815 MUTEX_LOCK(&thread->mutex);
816 join_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
817 MUTEX_UNLOCK(&thread->mutex);
819 if (join_err & PERL_ITHR_DETACHED) {
820 Perl_croak(aTHX_ "Cannot join a detached thread");
822 Perl_croak(aTHX_ "Thread already joined");
826 /* Join the thread */
828 waitcode = WaitForSingleObject(thread->handle, INFINITE);
830 pthread_join(thread->thr, &retval);
833 MUTEX_LOCK(&thread->mutex);
835 thread->state |= PERL_ITHR_JOINED;
837 /* Get the return value from the call_sv */
840 PerlInterpreter *other_perl;
841 CLONE_PARAMS clone_params;
842 ithread *current_thread;
844 params_copy = (AV *)SvRV(thread->params);
845 other_perl = thread->interp;
846 clone_params.stashes = newAV();
847 clone_params.flags = CLONEf_JOIN_IN;
848 PL_ptr_table = ptr_table_new();
849 current_thread = S_ithread_get(aTHX);
850 S_ithread_set(aTHX_ thread);
851 /* Ensure 'meaningful' addresses retain their meaning */
852 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
853 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
854 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
855 params = (AV *)sv_dup((SV*)params_copy, &clone_params);
856 S_ithread_set(aTHX_ current_thread);
857 SvREFCNT_dec(clone_params.stashes);
858 SvREFCNT_inc(params);
859 ptr_table_free(PL_ptr_table);
863 /* We are finished with the thread */
864 S_ithread_clear(aTHX_ thread);
865 MUTEX_UNLOCK(&thread->mutex);
867 /* If no return values, then just return */
872 /* Put return values on stack */
873 len = (int)AvFILL(params);
874 for (ii=0; ii <= len; ii++) {
875 SV* param = av_shift(params);
876 XPUSHs(sv_2mortal(param));
879 /* Free return value array */
880 SvREFCNT_dec(params);
896 thread = SV_to_ithread(aTHX_ ST(0));
897 MUTEX_LOCK(&thread->mutex);
899 /* Check if the thread is detachable */
900 if ((detach_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))) {
901 MUTEX_UNLOCK(&thread->mutex);
902 if (detach_err & PERL_ITHR_DETACHED) {
903 Perl_croak(aTHX_ "Thread already detached");
905 Perl_croak(aTHX_ "Cannot detach a joined thread");
909 /* Detach the thread */
910 thread->state |= PERL_ITHR_DETACHED;
912 /* Windows has no 'detach thread' function */
914 PERL_THREAD_DETACH(thread->thr);
916 /* Cleanup if finished */
917 cleanup = (thread->state & PERL_ITHR_FINISHED);
918 MUTEX_UNLOCK(&thread->mutex);
921 S_ithread_destruct(aTHX_ thread);
927 sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar);
935 /* Compares TIDs to determine thread equality */
936 if (sv_isobject(ST(0)) && sv_isobject(ST(1))) {
937 ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
938 ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
939 are_equal = (thr1->tid == thr2->tid);
944 /* Return 0 on false for backward compatibility */
947 /* XSRETURN(1); - implied */
958 /* Class method only */
960 Perl_croak(aTHX_ "Usage: threads->object($tid)");
961 classname = (char *)SvPV_nolen(ST(0));
963 if ((items < 2) || ! SvOK(ST(1))) {
967 /* threads->object($tid) */
970 /* Walk through threads list */
971 MUTEX_LOCK(&create_destruct_mutex);
972 for (thread = threads->next;
974 thread = thread->next)
976 /* Look for TID, but ignore detached or joined threads */
977 if ((thread->tid != tid) ||
978 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))
982 /* Put object on stack */
983 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
987 MUTEX_UNLOCK(&create_destruct_mutex);
991 /* XSRETURN(1); - implied */
995 ithread__handle(...);
999 thread = SV_to_ithread(aTHX_ ST(0));
1001 XST_mUV(0, PTR2UV(&thread->handle));
1003 XST_mUV(0, PTR2UV(&thread->thr));
1005 /* XSRETURN(1); - implied */
1009 ithread_get_stack_size(...)
1013 if (sv_isobject(ST(0))) {
1014 /* $thr->get_stack_size() */
1015 ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1016 stack_size = thread->stack_size;
1018 /* threads->get_stack_size() */
1019 stack_size = default_stack_size;
1021 XST_mIV(0, stack_size);
1022 /* XSRETURN(1); - implied */
1026 ithread_set_stack_size(...)
1031 Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
1032 if (sv_isobject(ST(0)))
1033 Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
1035 old_size = default_stack_size;
1036 default_stack_size = good_stack_size(aTHX_ SvIV(ST(1)));
1037 XST_mIV(0, old_size);
1038 /* XSRETURN(1); - implied */
1040 #endif /* USE_ITHREADS */
1046 /* The 'main' thread is thread 0.
1047 * It is detached (unjoinable) and immortal.
1053 PL_perl_destruct_level = 2;
1054 MUTEX_INIT(&create_destruct_mutex);
1055 MUTEX_LOCK(&create_destruct_mutex);
1057 PL_threadhook = &Perl_ithread_hook;
1059 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
1061 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
1064 Zero(thread, 1, ithread);
1066 PL_perl_destruct_level = 2;
1067 MUTEX_INIT(&thread->mutex);
1069 thread->tid = tid_counter++; /* Thread 0 */
1071 /* Head of the threads list */
1073 thread->next = thread;
1074 thread->prev = thread;
1076 thread->count = 1; /* Immortal */
1078 thread->interp = aTHX;
1079 thread->state = PERL_ITHR_DETACHED; /* Detached */
1080 thread->stack_size = default_stack_size;
1082 thread->thr = GetCurrentThreadId();
1084 thread->thr = pthread_self();
1089 S_ithread_set(aTHX_ thread);
1090 MUTEX_UNLOCK(&create_destruct_mutex);
1091 #endif /* USE_ITHREADS */