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))
35 #if !defined(HAS_GETPAGESIZE) && defined(I_SYS_PARAM)
36 # include <sys/param.h>
39 /* Values for 'state' member */
40 #define PERL_ITHR_JOINABLE 0
41 #define PERL_ITHR_DETACHED 1
42 #define PERL_ITHR_JOINED 2
43 #define PERL_ITHR_FINISHED 4
45 typedef struct _ithread {
46 struct _ithread *next; /* Next thread in the list */
47 struct _ithread *prev; /* Prev thread in the list */
48 PerlInterpreter *interp; /* The threads interpreter */
49 UV tid; /* Threads module's thread id */
50 perl_mutex mutex; /* Mutex for updating things in this struct */
51 int count; /* How many SVs have a reference to us */
52 int state; /* Detached, joined, finished, etc. */
53 int gimme; /* Context of create */
54 SV *init_function; /* Code to run */
55 SV *params; /* Args to pass function */
57 DWORD thr; /* OS's idea if thread id */
58 HANDLE handle; /* OS's waitable handle */
60 pthread_t thr; /* OS's handle for the thread */
66 /* Used by Perl interpreter for thread context switching */
67 #define MY_CXT_KEY "threads::_guts" XS_VERSION
76 /* Linked list of all threads */
77 static ithread *threads;
79 /* Protects the creation and destruction of threads*/
80 static perl_mutex create_destruct_mutex;
82 static UV tid_counter = 0;
83 static IV active_threads = 0;
84 #ifdef THREAD_CREATE_NEEDS_STACK
85 static IV default_stack_size = THREAD_CREATE_NEEDS_STACK;
87 static IV default_stack_size = 0;
89 static IV page_size = 0;
92 /* Used by Perl interpreter for thread context switching */
94 S_ithread_set(pTHX_ ithread *thread)
97 MY_CXT.thread = thread;
104 return (MY_CXT.thread);
108 /* Free any data (such as the Perl interpreter) attached to an ithread
109 * structure. This is a bit like undef on SVs, where the SV isn't freed,
110 * but the PVX is. Must be called with thread->mutex already held.
113 S_ithread_clear(pTHX_ ithread *thread)
115 PerlInterpreter *interp;
117 assert(thread->state & PERL_ITHR_FINISHED &&
118 thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
120 interp = thread->interp;
124 PERL_SET_CONTEXT(interp);
125 S_ithread_set(aTHX_ thread);
127 SvREFCNT_dec(thread->params);
128 thread->params = Nullsv;
130 perl_destruct(interp);
131 thread->interp = NULL;
136 PERL_SET_CONTEXT(aTHX);
140 /* Free an ithread structure and any attached data if its count == 0 */
142 S_ithread_destruct(pTHX_ ithread *thread)
148 MUTEX_LOCK(&thread->mutex);
150 /* Thread is still in use */
151 if (thread->count != 0) {
152 MUTEX_UNLOCK(&thread->mutex);
156 MUTEX_LOCK(&create_destruct_mutex);
157 /* Main thread (0) is immortal and should never get here */
158 assert(thread->tid != 0);
160 /* Remove from circular list of threads */
161 thread->next->prev = thread->prev;
162 thread->prev->next = thread->next;
165 MUTEX_UNLOCK(&create_destruct_mutex);
167 /* Thread is now disowned */
168 S_ithread_clear(aTHX_ thread);
171 handle = thread->handle;
172 thread->handle = NULL;
174 MUTEX_UNLOCK(&thread->mutex);
175 MUTEX_DESTROY(&thread->mutex);
182 /* Call PerlMemShared_free() in the context of the "first" interpreter
183 * per http://www.nntp.perl.org/group/perl.perl5.porters/110772
186 PerlMemShared_free(thread);
192 Perl_ithread_hook(pTHX)
194 int veto_cleanup = 0;
195 MUTEX_LOCK(&create_destruct_mutex);
196 if ((aTHX == PL_curinterp) && (active_threads != 1)) {
197 if (ckWARN_d(WARN_THREADS)) {
198 Perl_warn(aTHX_ "A thread exited while %" IVdf " threads were running", active_threads);
202 MUTEX_UNLOCK(&create_destruct_mutex);
203 return (veto_cleanup);
207 /* MAGIC (in mg.h sense) hooks */
210 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
212 ithread *thread = (ithread *)mg->mg_ptr;
213 SvIV_set(sv, PTR2IV(thread));
219 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
221 ithread *thread = (ithread *)mg->mg_ptr;
224 MUTEX_LOCK(&thread->mutex);
225 cleanup = ((--thread->count == 0) &&
226 (thread->state & PERL_ITHR_FINISHED) &&
227 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
228 MUTEX_UNLOCK(&thread->mutex);
231 S_ithread_destruct(aTHX_ thread);
236 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
238 ithread *thread = (ithread *)mg->mg_ptr;
239 MUTEX_LOCK(&thread->mutex);
241 MUTEX_UNLOCK(&thread->mutex);
245 MGVTBL ithread_vtbl = {
246 ithread_mg_get, /* get */
250 ithread_mg_free, /* free */
252 ithread_mg_dup /* dup */
256 /* Provided default, minimum and rational stack sizes */
258 good_stack_size(pTHX_ IV stack_size)
260 /* Use default stack size if no stack size specified */
262 return (default_stack_size);
264 #ifdef PTHREAD_STACK_MIN
265 /* Can't use less than minimum */
266 if (stack_size < PTHREAD_STACK_MIN) {
267 if (ckWARN_d(WARN_THREADS)) {
268 Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
270 return (PTHREAD_STACK_MIN);
274 /* Round up to page size boundary */
275 if (page_size <= 0) {
276 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
277 SETERRNO(0, SS_NORMAL);
279 page_size = sysconf(_SC_PAGESIZE);
281 page_size = sysconf(_SC_MMAP_PAGE_SIZE);
283 if ((long)page_size < 0) {
285 SV * const error = get_sv("@", FALSE);
286 (void)SvUPGRADE(error, SVt_PV);
287 Perl_croak(aTHX_ "PANIC: sysconf: %s", SvPV_nolen(error));
289 Perl_croak(aTHX_ "PANIC: sysconf: pagesize unknown");
293 # ifdef HAS_GETPAGESIZE
294 page_size = getpagesize();
296 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
297 page_size = PAGESIZE;
299 page_size = 8192; /* A conservative default */
303 Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)page_size);
306 stack_size = ((stack_size + (page_size - 1)) / page_size) * page_size;
312 /* Starts executing the thread.
313 * Passed as the C level function to run in the new thread.
316 static THREAD_RET_TYPE
317 S_ithread_run(LPVOID arg)
320 S_ithread_run(void * arg)
323 ithread *thread = (ithread *)arg;
326 dTHXa(thread->interp);
327 PERL_SET_CONTEXT(thread->interp);
328 S_ithread_set(aTHX_ thread);
331 /* Far from clear messing with ->thr child-side is a good idea */
332 MUTEX_LOCK(&thread->mutex);
334 thread->thr = GetCurrentThreadId();
336 thread->thr = pthread_self();
338 MUTEX_UNLOCK(&thread->mutex);
341 PL_perl_destruct_level = 2;
344 AV *params = (AV *)SvRV(thread->params);
345 int len = (int)av_len(params)+1;
352 /* Put args on the stack */
354 for (ii=0; ii < len; ii++) {
355 XPUSHs(av_shift(params));
359 /* Run the specified function */
360 len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
362 /* Remove args from stack and put back in params array */
364 for (ii=len-1; ii >= 0; ii--) {
366 av_store(params, ii, SvREFCNT_inc(sv));
369 /* Check for failure */
370 if (SvTRUE(ERRSV) && ckWARN_d(WARN_THREADS)) {
371 Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV);
377 /* Release function ref */
378 SvREFCNT_dec(thread->init_function);
379 thread->init_function = Nullsv;
382 PerlIO_flush((PerlIO *)NULL);
384 MUTEX_LOCK(&thread->mutex);
385 /* Mark as finished */
386 thread->state |= PERL_ITHR_FINISHED;
387 /* Cleanup if detached */
388 cleanup = (thread->state & PERL_ITHR_DETACHED);
389 MUTEX_UNLOCK(&thread->mutex);
392 S_ithread_destruct(aTHX_ thread);
394 MUTEX_LOCK(&create_destruct_mutex);
396 MUTEX_UNLOCK(&create_destruct_mutex);
406 /* Type conversion helper functions */
408 ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
414 MUTEX_LOCK(&thread->mutex);
416 MUTEX_UNLOCK(&thread->mutex);
423 sv = newSVrv(obj, classname);
424 sv_setiv(sv, PTR2IV(thread));
425 mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0);
426 mg->mg_flags |= MGf_DUP;
433 SV_to_ithread(pTHX_ SV *sv)
435 /* Argument is a thread */
437 return (INT2PTR(ithread *, SvIV(SvRV(sv))));
439 /* Argument is classname, therefore return current thread */
440 return (S_ithread_get(aTHX));
445 * Called in context of parent thread.
456 CLONE_PARAMS clone_param;
457 ithread *current_thread = S_ithread_get(aTHX);
459 SV **tmps_tmp = PL_tmps_stack;
460 IV tmps_ix = PL_tmps_ix;
462 int rc_stack_size = 0;
463 int rc_thread_create = 0;
466 MUTEX_LOCK(&create_destruct_mutex);
468 /* Allocate thread structure */
469 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
471 MUTEX_UNLOCK(&create_destruct_mutex);
472 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
475 Zero(thread, 1, ithread);
477 /* Add to threads list */
478 thread->next = threads;
479 thread->prev = threads->prev;
480 threads->prev = thread;
481 thread->prev->next = thread;
483 /* Set count to 1 immediately in case thread exits before
484 * we return to caller!
488 MUTEX_INIT(&thread->mutex);
489 thread->tid = tid_counter++;
490 thread->stack_size = good_stack_size(aTHX_ stack_size);
491 thread->gimme = GIMME_V;
493 /* "Clone" our interpreter into the thread's interpreter.
494 * This gives thread access to "static data" and code.
496 PerlIO_flush((PerlIO *)NULL);
497 S_ithread_set(aTHX_ thread);
499 SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct value */
500 PL_srand_called = FALSE; /* Set it to false so we can detect if it gets
501 set during the clone */
504 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
506 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
509 /* perl_clone() leaves us in new interpreter's context. As it is tricky
510 * to spot an implicit aTHX, create a new scope with aTHX matching the
511 * context for the duration of our work for new interpreter.
514 dTHXa(thread->interp);
518 /* Here we remove END blocks since they should only run in the thread
521 SvREFCNT_dec(PL_endav);
523 clone_param.flags = 0;
524 thread->init_function = sv_dup(init_function, &clone_param);
525 if (SvREFCNT(thread->init_function) == 0) {
526 SvREFCNT_inc(thread->init_function);
529 thread->params = sv_dup(params, &clone_param);
530 SvREFCNT_inc(thread->params);
532 /* The code below checks that anything living on the tmps stack and
533 * has been cloned (so it lives in the ptr_table) has a refcount
536 * If the refcount is 0 it means that a something on the stack/context
537 * was holding a reference to it and since we init_stacks() in
538 * perl_clone that won't get cleaned and we will get a leaked scalar.
539 * The reason it was cloned was that it lived on the @_ stack.
541 * Example of this can be found in bugreport 15837 where calls in the
542 * parameter list end up as a temp.
544 * One could argue that this fix should be in perl_clone.
546 while (tmps_ix > 0) {
547 SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
549 if (sv && SvREFCNT(sv) == 0) {
555 SvTEMP_off(thread->init_function);
556 ptr_table_free(PL_ptr_table);
558 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
560 S_ithread_set(aTHX_ current_thread);
561 PERL_SET_CONTEXT(aTHX);
563 /* Create/start the thread */
565 thread->handle = CreateThread(NULL,
566 (DWORD)thread->stack_size,
569 STACK_SIZE_PARAM_IS_A_RESERVATION,
573 static pthread_attr_t attr;
574 static int attr_inited = 0;
575 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
577 pthread_attr_init(&attr);
581 # ifdef PTHREAD_ATTR_SETDETACHSTATE
582 /* Threads start out joinable */
583 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
586 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
587 /* Set thread's stack size */
588 if (thread->stack_size > 0) {
589 rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
593 /* Create the thread */
594 if (! rc_stack_size) {
595 # ifdef OLD_PTHREADS_API
596 rc_thread_create = pthread_create(&thread->thr,
601 # if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
602 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
604 rc_thread_create = pthread_create(&thread->thr,
611 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
612 /* Try to get thread's actual stack size */
615 if (! pthread_attr_getstacksize(&attr, &stacksize)) {
617 thread->stack_size = (IV)stacksize;
625 /* Check for errors */
627 if (thread->handle == NULL) {
629 if (rc_stack_size || rc_thread_create) {
631 MUTEX_UNLOCK(&create_destruct_mutex);
633 S_ithread_destruct(aTHX_ thread);
635 if (ckWARN_d(WARN_THREADS)) {
637 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
639 Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
642 return (&PL_sv_undef);
646 MUTEX_UNLOCK(&create_destruct_mutex);
650 return (ithread_to_SV(aTHX_ obj, thread, classname, FALSE));
653 #endif /* USE_ITHREADS */
656 MODULE = threads PACKAGE = threads PREFIX = ithread_
666 SV *function_to_call;
673 if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
675 Perl_croak(aTHX_ "Usage: threads->create(\\%specs, function, ...)");
676 specs = (HV*)SvRV(ST(1));
680 Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
685 if (sv_isobject(ST(0))) {
687 classname = HvNAME(SvSTASH(SvRV(ST(0))));
688 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
689 stack_size = thread->stack_size;
691 /* threads->create() */
692 classname = (char *)SvPV_nolen(ST(0));
693 stack_size = default_stack_size;
696 function_to_call = ST(idx+1);
700 if (hv_exists(specs, "stack", 5)) {
701 stack_size = SvIV(*hv_fetch(specs, "stack", 5, 0));
702 } else if (hv_exists(specs, "stacksize", 9)) {
703 stack_size = SvIV(*hv_fetch(specs, "stacksize", 9, 0));
704 } else if (hv_exists(specs, "stack_size", 10)) {
705 stack_size = SvIV(*hv_fetch(specs, "stack_size", 10, 0));
712 for (ii=2; ii < items ; ii++) {
713 av_push(params, SvREFCNT_inc(ST(idx+ii)));
718 ST(0) = sv_2mortal(S_ithread_create(aTHX_ Nullsv,
722 newRV_noinc((SV*)params)));
723 /* XSRETURN(1); - implied */
734 /* Class method only */
736 Perl_croak(aTHX_ "Usage: threads->list()");
737 classname = (char *)SvPV_nolen(ST(0));
739 /* Calling context */
740 list_context = (GIMME_V == G_ARRAY);
742 /* Walk through threads list */
743 MUTEX_LOCK(&create_destruct_mutex);
744 for (thread = threads->next;
746 thread = thread->next)
748 /* Ignore detached or joined threads */
749 if (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)) {
752 /* Push object on stack if list context */
754 XPUSHs(sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
758 MUTEX_UNLOCK(&create_destruct_mutex);
759 /* If scalar context, send back count */
760 if (! list_context) {
771 /* Class method only */
773 Perl_croak(aTHX_ "Usage: threads->self()");
774 classname = (char *)SvPV_nolen(ST(0));
776 thread = S_ithread_get(aTHX);
778 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
779 /* XSRETURN(1); - implied */
787 thread = SV_to_ithread(aTHX_ ST(0));
788 XST_mUV(0, thread->tid);
789 /* XSRETURN(1); - implied */
806 /* Object method only */
807 if (! sv_isobject(ST(0)))
808 Perl_croak(aTHX_ "Usage: $thr->join()");
810 /* Check if the thread is joinable */
811 thread = SV_to_ithread(aTHX_ ST(0));
812 MUTEX_LOCK(&thread->mutex);
813 join_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
814 MUTEX_UNLOCK(&thread->mutex);
816 if (join_err & PERL_ITHR_DETACHED) {
817 Perl_croak(aTHX_ "Cannot join a detached thread");
819 Perl_croak(aTHX_ "Thread already joined");
823 /* Join the thread */
825 waitcode = WaitForSingleObject(thread->handle, INFINITE);
827 pthread_join(thread->thr, &retval);
830 MUTEX_LOCK(&thread->mutex);
832 thread->state |= PERL_ITHR_JOINED;
834 /* Get the return value from the call_sv */
837 PerlInterpreter *other_perl;
838 CLONE_PARAMS clone_params;
839 ithread *current_thread;
841 params_copy = (AV *)SvRV(thread->params);
842 other_perl = thread->interp;
843 clone_params.stashes = newAV();
844 clone_params.flags = CLONEf_JOIN_IN;
845 PL_ptr_table = ptr_table_new();
846 current_thread = S_ithread_get(aTHX);
847 S_ithread_set(aTHX_ thread);
848 /* Ensure 'meaningful' addresses retain their meaning */
849 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
850 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
851 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
852 params = (AV *)sv_dup((SV*)params_copy, &clone_params);
853 S_ithread_set(aTHX_ current_thread);
854 SvREFCNT_dec(clone_params.stashes);
855 SvREFCNT_inc(params);
856 ptr_table_free(PL_ptr_table);
860 /* We are finished with the thread */
861 S_ithread_clear(aTHX_ thread);
862 MUTEX_UNLOCK(&thread->mutex);
864 /* If no return values, then just return */
869 /* Put return values on stack */
870 len = (int)AvFILL(params);
871 for (ii=0; ii <= len; ii++) {
872 SV* param = av_shift(params);
873 XPUSHs(sv_2mortal(param));
876 /* Free return value array */
877 SvREFCNT_dec(params);
893 thread = SV_to_ithread(aTHX_ ST(0));
894 MUTEX_LOCK(&thread->mutex);
896 /* Check if the thread is detachable */
897 if ((detach_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))) {
898 MUTEX_UNLOCK(&thread->mutex);
899 if (detach_err & PERL_ITHR_DETACHED) {
900 Perl_croak(aTHX_ "Thread already detached");
902 Perl_croak(aTHX_ "Cannot detach a joined thread");
906 /* Detach the thread */
907 thread->state |= PERL_ITHR_DETACHED;
909 /* Windows has no 'detach thread' function */
911 PERL_THREAD_DETACH(thread->thr);
913 /* Cleanup if finished */
914 cleanup = (thread->state & PERL_ITHR_FINISHED);
915 MUTEX_UNLOCK(&thread->mutex);
918 S_ithread_destruct(aTHX_ thread);
928 /* Must have safe signals */
929 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
930 Perl_croak(aTHX_ "Cannot signal other threads without safe signals");
932 /* Object method only */
933 if (! sv_isobject(ST(0)))
934 Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')");
937 thread = SV_to_ithread(aTHX_ ST(0));
940 sig_name = SvPV_nolen(ST(1));
941 if (isALPHA(*sig_name)) {
942 if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G')
944 if ((signal = Perl_whichsig(aTHX_ sig_name)) < 0)
945 Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name);
947 signal = SvIV(ST(1));
949 /* Set the signal for the thread */
951 dTHXa(thread->interp);
952 PL_psig_pend[signal]++;
956 /* Return the thread to allow for method chaining */
958 /* XSRETURN(1); - implied */
964 sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar);
972 /* Compares TIDs to determine thread equality */
973 if (sv_isobject(ST(0)) && sv_isobject(ST(1))) {
974 ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
975 ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
976 are_equal = (thr1->tid == thr2->tid);
981 /* Return 0 on false for backward compatibility */
984 /* XSRETURN(1); - implied */
995 /* Class method only */
997 Perl_croak(aTHX_ "Usage: threads->object($tid)");
998 classname = (char *)SvPV_nolen(ST(0));
1000 if ((items < 2) || ! SvOK(ST(1))) {
1004 /* threads->object($tid) */
1007 /* Walk through threads list */
1008 MUTEX_LOCK(&create_destruct_mutex);
1009 for (thread = threads->next;
1011 thread = thread->next)
1013 /* Look for TID, but ignore detached or joined threads */
1014 if ((thread->tid != tid) ||
1015 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))
1019 /* Put object on stack */
1020 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1024 MUTEX_UNLOCK(&create_destruct_mutex);
1028 /* XSRETURN(1); - implied */
1032 ithread__handle(...);
1036 thread = SV_to_ithread(aTHX_ ST(0));
1038 XST_mUV(0, PTR2UV(&thread->handle));
1040 XST_mUV(0, PTR2UV(&thread->thr));
1042 /* XSRETURN(1); - implied */
1046 ithread_get_stack_size(...)
1050 if (sv_isobject(ST(0))) {
1051 /* $thr->get_stack_size() */
1052 ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1053 stack_size = thread->stack_size;
1055 /* threads->get_stack_size() */
1056 stack_size = default_stack_size;
1058 XST_mIV(0, stack_size);
1059 /* XSRETURN(1); - implied */
1063 ithread_set_stack_size(...)
1068 Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
1069 if (sv_isobject(ST(0)))
1070 Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
1072 old_size = default_stack_size;
1073 default_stack_size = good_stack_size(aTHX_ SvIV(ST(1)));
1074 XST_mIV(0, old_size);
1075 /* XSRETURN(1); - implied */
1077 #endif /* USE_ITHREADS */
1083 /* The 'main' thread is thread 0.
1084 * It is detached (unjoinable) and immortal.
1090 PL_perl_destruct_level = 2;
1091 MUTEX_INIT(&create_destruct_mutex);
1092 MUTEX_LOCK(&create_destruct_mutex);
1094 PL_threadhook = &Perl_ithread_hook;
1096 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
1098 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
1101 Zero(thread, 1, ithread);
1103 PL_perl_destruct_level = 2;
1104 MUTEX_INIT(&thread->mutex);
1106 thread->tid = tid_counter++; /* Thread 0 */
1108 /* Head of the threads list */
1110 thread->next = thread;
1111 thread->prev = thread;
1113 thread->count = 1; /* Immortal */
1115 thread->interp = aTHX;
1116 thread->state = PERL_ITHR_DETACHED; /* Detached */
1117 thread->stack_size = default_stack_size;
1119 thread->thr = GetCurrentThreadId();
1121 thread->thr = pthread_self();
1126 S_ithread_set(aTHX_ thread);
1127 MUTEX_UNLOCK(&create_destruct_mutex);
1128 #endif /* USE_ITHREADS */