1 #define PERL_NO_GET_CONTEXT
6 # define NEED_newRV_noinc
7 # define NEED_sv_2pv_nolen
17 #include <win32thread.h>
20 typedef perl_os_thread pthread_t;
25 #define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
26 #ifdef OLD_PTHREADS_API
27 #define PERL_THREAD_DETACH(t) pthread_detach(&(t))
29 #define PERL_THREAD_DETACH(t) pthread_detach((t))
30 #endif /* OLD_PTHREADS_API */
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_s {
43 struct ithread_s *next; /* Next thread in the list */
44 struct ithread_s *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; /* Are we detached ? */
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 */
61 #define MY_CXT_KEY "threads::_guts" XS_VERSION
70 static ithread *threads;
72 static perl_mutex create_destruct_mutex; /* protects the creation and destruction of threads*/
74 static UV tid_counter = 0;
75 static IV active_threads = 0;
79 S_ithread_set (pTHX_ ithread* thread)
82 MY_CXT.thread = thread;
86 S_ithread_get (pTHX) {
92 /* free any data (such as the perl interpreter) attached to an
93 * ithread structure. This is a bit like undef on SVs, where the SV
94 * isn't freed, but the PVX is.
95 * Must be called with thread->mutex already held
99 S_ithread_clear(pTHX_ ithread* thread)
101 PerlInterpreter *interp;
102 assert(thread->state & PERL_ITHR_FINISHED &&
103 (thread->state & PERL_ITHR_DETACHED ||
104 thread->state & PERL_ITHR_JOINED));
106 interp = thread->interp;
109 ithread* current_thread;
113 PERL_SET_CONTEXT(interp);
114 current_thread = S_ithread_get(aTHX);
115 S_ithread_set(aTHX_ thread);
117 SvREFCNT_dec(thread->params);
119 thread->params = Nullsv;
120 perl_destruct(interp);
121 thread->interp = NULL;
125 PERL_SET_CONTEXT(aTHX);
130 * free an ithread structure and any attached data if its count == 0
133 S_ithread_destruct (pTHX_ ithread* thread)
139 MUTEX_LOCK(&thread->mutex);
141 /* Thread is still in use */
142 if (thread->count != 0) {
143 MUTEX_UNLOCK(&thread->mutex);
147 MUTEX_LOCK(&create_destruct_mutex);
148 /* Main thread (0) is immortal and should never get here */
149 assert(thread->tid != 0);
151 /* Remove from circular list of threads */
152 thread->next->prev = thread->prev;
153 thread->prev->next = thread->next;
156 MUTEX_UNLOCK(&create_destruct_mutex);
158 /* Thread is now disowned */
159 S_ithread_clear(aTHX_ thread);
162 handle = thread->handle;
163 thread->handle = NULL;
165 MUTEX_UNLOCK(&thread->mutex);
166 MUTEX_DESTROY(&thread->mutex);
173 /* Call PerlMemShared_free() in the context of the "first" interpreter
174 * per http://www.nntp.perl.org/group/perl.perl5.porters/110772
177 PerlMemShared_free(thread);
181 Perl_ithread_hook(pTHX)
183 int veto_cleanup = 0;
184 MUTEX_LOCK(&create_destruct_mutex);
185 if (aTHX == PL_curinterp && active_threads != 1) {
186 if (ckWARN_d(WARN_THREADS))
187 Perl_warn(aTHX_ "A thread exited while %" IVdf " threads were running",
191 MUTEX_UNLOCK(&create_destruct_mutex);
196 S_ithread_detach(pTHX_ ithread *thread)
198 MUTEX_LOCK(&thread->mutex);
199 if (!(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) {
200 thread->state |= PERL_ITHR_DETACHED;
202 CloseHandle(thread->handle);
205 PERL_THREAD_DETACH(thread->thr);
208 if ((thread->state & PERL_ITHR_FINISHED) &&
209 (thread->state & PERL_ITHR_DETACHED)) {
210 MUTEX_UNLOCK(&thread->mutex);
211 S_ithread_destruct(aTHX_ thread);
214 MUTEX_UNLOCK(&thread->mutex);
218 /* MAGIC (in mg.h sense) hooks */
221 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
223 ithread *thread = (ithread *) mg->mg_ptr;
224 SvIV_set(sv, PTR2IV(thread));
230 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
232 ithread *thread = (ithread *) mg->mg_ptr;
233 MUTEX_LOCK(&thread->mutex);
235 if (thread->count == 0) {
236 if(thread->state & PERL_ITHR_FINISHED &&
237 (thread->state & PERL_ITHR_DETACHED ||
238 thread->state & PERL_ITHR_JOINED))
240 MUTEX_UNLOCK(&thread->mutex);
241 S_ithread_destruct(aTHX_ thread);
244 MUTEX_UNLOCK(&thread->mutex);
248 MUTEX_UNLOCK(&thread->mutex);
254 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
256 ithread *thread = (ithread *) mg->mg_ptr;
257 MUTEX_LOCK(&thread->mutex);
259 MUTEX_UNLOCK(&thread->mutex);
263 MGVTBL ithread_vtbl = {
264 ithread_mg_get, /* get */
268 ithread_mg_free, /* free */
270 ithread_mg_dup /* dup */
275 * Starts executing the thread. Needs to clean up memory a tad better.
276 * Passed as the C level function to run in the new thread
280 static THREAD_RET_TYPE
281 S_ithread_run(LPVOID arg) {
284 S_ithread_run(void * arg) {
286 ithread* thread = (ithread*) arg;
287 dTHXa(thread->interp);
288 PERL_SET_CONTEXT(thread->interp);
289 S_ithread_set(aTHX_ thread);
292 /* Far from clear messing with ->thr child-side is a good idea */
293 MUTEX_LOCK(&thread->mutex);
295 thread->thr = GetCurrentThreadId();
297 thread->thr = pthread_self();
299 MUTEX_UNLOCK(&thread->mutex);
302 PL_perl_destruct_level = 2;
305 AV* params = (AV*) SvRV(thread->params);
306 int len = (int)av_len(params)+1;
312 for(ii = 0; ii < len; ii++) {
313 XPUSHs(av_shift(params));
316 len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
319 for (ii=len-1; ii >= 0; ii--) {
321 av_store(params, ii, SvREFCNT_inc(sv));
323 if (SvTRUE(ERRSV) && ckWARN_d(WARN_THREADS)) {
324 Perl_warn(aTHX_ "thread failed to start: %" SVf, ERRSV);
328 SvREFCNT_dec(thread->init_function);
331 PerlIO_flush((PerlIO*)NULL);
332 MUTEX_LOCK(&thread->mutex);
333 thread->state |= PERL_ITHR_FINISHED;
335 if (thread->state & PERL_ITHR_DETACHED) {
336 MUTEX_UNLOCK(&thread->mutex);
337 S_ithread_destruct(aTHX_ thread);
339 MUTEX_UNLOCK(&thread->mutex);
341 MUTEX_LOCK(&create_destruct_mutex);
343 MUTEX_UNLOCK(&create_destruct_mutex);
353 ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
358 MUTEX_LOCK(&thread->mutex);
360 MUTEX_UNLOCK(&thread->mutex);
364 sv = newSVrv(obj,classname);
365 sv_setiv(sv,PTR2IV(thread));
366 mg = sv_magicext(sv,Nullsv,PERL_MAGIC_shared_scalar,&ithread_vtbl,(char *)thread,0);
367 mg->mg_flags |= MGf_DUP;
373 SV_to_ithread(pTHX_ SV *sv)
377 return INT2PTR(ithread*, SvIV(SvRV(sv)));
381 return S_ithread_get(aTHX);
386 * ithread->create(); ( aka ithread->new() )
387 * Called in context of parent thread
391 S_ithread_create(pTHX_ SV *obj, char* classname, SV* init_function, SV* params)
394 CLONE_PARAMS clone_param;
395 ithread* current_thread = S_ithread_get(aTHX);
397 SV** tmps_tmp = PL_tmps_stack;
398 IV tmps_ix = PL_tmps_ix;
400 int rc_stack_size = 0;
401 int rc_thread_create = 0;
405 MUTEX_LOCK(&create_destruct_mutex);
406 thread = (ithread *) PerlMemShared_malloc(sizeof(ithread));
408 MUTEX_UNLOCK(&create_destruct_mutex);
409 PerlLIO_write(PerlIO_fileno(Perl_error_log),
410 PL_no_mem, strlen(PL_no_mem));
413 Zero(thread,1,ithread);
415 /* Add to threads list */
416 thread->next = threads;
417 thread->prev = threads->prev;
418 threads->prev = thread;
419 thread->prev->next = thread;
421 /* Set count to 1 immediately in case thread exits before
422 * we return to caller !
425 MUTEX_INIT(&thread->mutex);
426 thread->tid = tid_counter++;
427 thread->gimme = GIMME_V;
429 /* "Clone" our interpreter into the thread's interpreter
430 * This gives thread access to "static data" and code.
433 PerlIO_flush((PerlIO*)NULL);
434 S_ithread_set(aTHX_ thread);
436 SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct
438 PL_srand_called = FALSE; /* Set it to false so we can detect
439 if it gets set during the clone */
442 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
444 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
446 /* perl_clone leaves us in new interpreter's context.
447 As it is tricky to spot an implicit aTHX, create a new scope
448 with aTHX matching the context for the duration of
449 our work for new interpreter.
452 dTHXa(thread->interp);
456 /* Here we remove END blocks since they should only run
457 in the thread they are created
459 SvREFCNT_dec(PL_endav);
461 clone_param.flags = 0;
462 thread->init_function = sv_dup(init_function, &clone_param);
463 if (SvREFCNT(thread->init_function) == 0) {
464 SvREFCNT_inc(thread->init_function);
469 thread->params = sv_dup(params, &clone_param);
470 SvREFCNT_inc(thread->params);
473 /* The code below checks that anything living on
474 the tmps stack and has been cloned (so it lives in the
475 ptr_table) has a refcount higher than 0
477 If the refcount is 0 it means that a something on the
478 stack/context was holding a reference to it and
479 since we init_stacks() in perl_clone that won't get
480 cleaned and we will get a leaked scalar.
481 The reason it was cloned was that it lived on the
484 Example of this can be found in bugreport 15837
485 where calls in the parameter list end up as a temp
487 One could argue that this fix should be in perl_clone
491 while (tmps_ix > 0) {
492 SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
494 if (sv && SvREFCNT(sv) == 0) {
502 SvTEMP_off(thread->init_function);
503 ptr_table_free(PL_ptr_table);
505 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
507 S_ithread_set(aTHX_ current_thread);
508 PERL_SET_CONTEXT(aTHX);
510 /* Start the thread */
513 thread->handle = CreateThread(NULL, 0, S_ithread_run,
514 (LPVOID)thread, 0, &thread->thr);
517 static pthread_attr_t attr;
518 static int attr_inited = 0;
519 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
522 pthread_attr_init(&attr);
524 # ifdef PTHREAD_ATTR_SETDETACHSTATE
525 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
527 # ifdef THREAD_CREATE_NEEDS_STACK
528 rc_stack_size = pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK);
531 if (! rc_stack_size) {
532 #ifdef OLD_PTHREADS_API
533 rc_thread_create = pthread_create( &thread->thr, attr,
534 S_ithread_run, (void *)thread);
536 # if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
537 pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
539 rc_thread_create = pthread_create( &thread->thr, &attr,
540 S_ithread_run, (void *)thread);
546 /* Check for errors */
548 if (thread->handle == NULL) {
550 if (rc_stack_size || rc_thread_create) {
552 MUTEX_UNLOCK(&create_destruct_mutex);
554 S_ithread_destruct(aTHX_ thread);
556 if (ckWARN_d(WARN_THREADS)) {
557 # ifdef THREAD_CREATE_NEEDS_STACK
559 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", (IV)THREAD_CREATE_NEEDS_STACK, rc_stack_size);
562 Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
568 MUTEX_UNLOCK(&create_destruct_mutex);
571 return ithread_to_SV(aTHX_ obj, thread, classname, FALSE);
575 S_ithread_self (pTHX_ SV *obj, char* Class)
577 ithread *thread = S_ithread_get(aTHX);
579 return ithread_to_SV(aTHX_ obj, thread, Class, TRUE);
581 Perl_croak(aTHX_ "panic: cannot find thread data");
582 return NULL; /* silence compiler warning */
587 * This code takes the return value from the call_sv and sends it back.
590 S_ithread_join(pTHX_ SV *obj)
592 ithread *thread = SV_to_ithread(aTHX_ obj);
593 MUTEX_LOCK(&thread->mutex);
594 if (thread->state & PERL_ITHR_DETACHED) {
595 MUTEX_UNLOCK(&thread->mutex);
596 Perl_croak(aTHX_ "Cannot join a detached thread");
598 else if (thread->state & PERL_ITHR_JOINED) {
599 MUTEX_UNLOCK(&thread->mutex);
600 Perl_croak(aTHX_ "Thread already joined");
609 MUTEX_UNLOCK(&thread->mutex);
611 waitcode = WaitForSingleObject(thread->handle, INFINITE);
612 CloseHandle(thread->handle);
615 pthread_join(thread->thr,&retval);
617 MUTEX_LOCK(&thread->mutex);
619 /* sv_dup over the args */
621 ithread* current_thread;
622 AV* params = (AV*) SvRV(thread->params);
623 PerlInterpreter *other_perl = thread->interp;
624 CLONE_PARAMS clone_params;
625 clone_params.stashes = newAV();
626 clone_params.flags = CLONEf_JOIN_IN;
627 PL_ptr_table = ptr_table_new();
628 current_thread = S_ithread_get(aTHX);
629 S_ithread_set(aTHX_ thread);
630 /* ensure 'meaningful' addresses retain their meaning */
631 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
632 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
633 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
637 I32 len = av_len(params)+1;
639 for(i = 0; i < len; i++) {
640 sv_dump(SvRV(AvARRAY(params)[i]));
644 retparam = (AV*) sv_dup((SV*)params, &clone_params);
647 I32 len = av_len(retparam)+1;
649 for(i = 0; i < len; i++) {
650 sv_dump(SvRV(AvARRAY(retparam)[i]));
654 S_ithread_set(aTHX_ current_thread);
655 SvREFCNT_dec(clone_params.stashes);
656 SvREFCNT_inc(retparam);
657 ptr_table_free(PL_ptr_table);
661 /* We are finished with it */
662 thread->state |= PERL_ITHR_JOINED;
663 S_ithread_clear(aTHX_ thread);
664 MUTEX_UNLOCK(&thread->mutex);
672 S_ithread_DESTROY(pTHX_ SV *sv)
674 ithread *thread = SV_to_ithread(aTHX_ sv);
675 sv_unmagic(SvRV(sv),PERL_MAGIC_shared_scalar);
678 #endif /* USE_ITHREADS */
680 MODULE = threads PACKAGE = threads PREFIX = ithread_
689 SV *function_to_call;
694 Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
696 classname = (char *)SvPV_nolen(ST(0));
697 function_to_call = ST(1);
702 for (ii=2; ii < items; ii++) {
703 av_push(params, SvREFCNT_inc(ST(ii)));
708 ST(0) = sv_2mortal(S_ithread_create(aTHX_ Nullsv,
711 newRV_noinc((SV*)params)));
712 /* XSRETURN(1); - implied */
723 /* Class method only */
725 Perl_croak(aTHX_ "Usage: threads->list()");
726 classname = (char *)SvPV_nolen(ST(0));
728 /* Calling context */
729 list_context = (GIMME_V == G_ARRAY);
731 /* Walk through threads list */
732 MUTEX_LOCK(&create_destruct_mutex);
733 for (thr = threads->next;
737 /* Ignore detached or joined threads */
738 if (thr->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)) {
741 /* Push object on stack if list context */
743 XPUSHs(sv_2mortal(ithread_to_SV(aTHX_ NULL, thr, classname, TRUE)));
747 MUTEX_UNLOCK(&create_destruct_mutex);
748 /* If scalar context, send back count */
749 if (! list_context) {
759 /* Class method only */
761 Perl_croak(aTHX_ "Usage: threads->self()");
762 classname = (char *)SvPV_nolen(ST(0));
764 ST(0) = sv_2mortal(S_ithread_self(aTHX_ Nullsv, classname));
765 /* XSRETURN(1); - implied */
773 thread = SV_to_ithread(aTHX_ ST(0));
774 XST_mUV(0, thread->tid);
775 /* XSRETURN(1); - implied */
785 /* Object method only */
786 if (! sv_isobject(ST(0)))
787 Perl_croak(aTHX_ "Usage: $thr->join()");
789 /* Join thread and get return values */
790 params = S_ithread_join(aTHX_ ST(0));
795 /* Put return values on stack */
796 len = (int)AvFILL(params);
797 for (ii=0; ii <= len; ii++) {
798 SV* param = av_shift(params);
799 XPUSHs(sv_2mortal(param));
802 /* Free return value array */
803 SvREFCNT_dec(params);
817 thread = SV_to_ithread(aTHX_ ST(0));
818 S_ithread_detach(aTHX_ thread);
824 S_ithread_DESTROY(aTHX_ ST(0));
830 /* Compares TIDs to determine thread equality.
831 * Return 0 on false for backward compatibility.
833 if (sv_isobject(ST(0)) && sv_isobject(ST(1))) {
834 ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
835 ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
836 if (thr1->tid == thr2->tid) {
844 /* XSRETURN(1); - implied */
855 /* Class method only */
857 Perl_croak(aTHX_ "Usage: threads->object($tid)");
858 classname = (char *)SvPV_nolen(ST(0));
860 if ((items < 2) || ! SvOK(ST(1))) {
866 /* Walk through threads list */
867 MUTEX_LOCK(&create_destruct_mutex);
868 for (thr = threads->next;
872 /* Look for TID, but ignore detached or joined threads */
873 if ((thr->tid != tid) ||
874 (thr->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))
878 /* Put object on stack */
879 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ NULL, thr, classname, TRUE));
883 MUTEX_UNLOCK(&create_destruct_mutex);
887 /* XSRETURN(1); - implied */
891 ithread__handle(...);
895 thread = SV_to_ithread(aTHX_ ST(0));
897 XST_mUV(0, PTR2UV(thread->handle));
899 XST_mUV(0, PTR2UV(&thread->thr));
901 /* XSRETURN(1); - implied */
903 #endif /* USE_ITHREADS */
908 /* The 'main' thread is thread 0.
909 * It is detached (unjoinable) and immortal.
914 PL_perl_destruct_level = 2;
915 MUTEX_INIT(&create_destruct_mutex);
916 MUTEX_LOCK(&create_destruct_mutex);
917 PL_threadhook = &Perl_ithread_hook;
918 thread = (ithread *) PerlMemShared_malloc(sizeof(ithread));
920 PerlLIO_write(PerlIO_fileno(Perl_error_log),
921 PL_no_mem, strlen(PL_no_mem));
924 Zero(thread,1,ithread);
925 PL_perl_destruct_level = 2;
926 MUTEX_INIT(&thread->mutex);
928 /* Head of the threads list */
930 thread->next = thread;
931 thread->prev = thread;
933 thread->interp = aTHX;
934 thread->count = 1; /* Immortal. */
935 thread->tid = tid_counter++;
937 thread->state = PERL_ITHR_DETACHED;
939 thread->thr = GetCurrentThreadId();
941 thread->thr = pthread_self();
944 S_ithread_set(aTHX_ thread);
945 MUTEX_UNLOCK(&create_destruct_mutex);
946 #endif /* USE_ITHREADS */