1 #define PERL_NO_GET_CONTEXT
10 #include <win32thread.h>
11 #define PERL_THREAD_SETSPECIFIC(k,v) TlsSetValue(k,v)
12 #define PERL_THREAD_GETSPECIFIC(k,v) v = TlsGetValue(k)
13 #define PERL_THREAD_ALLOC_SPECIFIC(k) \
15 if((k = TlsAlloc()) == TLS_OUT_OF_INDEXES) {\
16 PerlIO_printf(PerlIO_stderr(),"panic threads.h: TlsAlloc");\
24 #define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
25 #ifdef OLD_PTHREADS_API
26 #define PERL_THREAD_DETACH(t) pthread_detach(&(t))
27 #define PERL_THREAD_GETSPECIFIC(k,v) pthread_getspecific(k,&v)
28 #define PERL_THREAD_ALLOC_SPECIFIC(k) STMT_START {\
29 if(pthread_keycreate(&(k),0)) {\
30 PerlIO_printf(PerlIO_stderr(), "panic threads.h: pthread_key_create");\
35 #define PERL_THREAD_DETACH(t) pthread_detach((t))
36 #define PERL_THREAD_GETSPECIFIC(k,v) v = pthread_getspecific(k)
37 #define PERL_THREAD_ALLOC_SPECIFIC(k) STMT_START {\
38 if(pthread_key_create(&(k),0)) {\
39 PerlIO_printf(PerlIO_stderr(), "panic threads.h: pthread_key_create");\
46 /* Values for 'state' member */
47 #define PERL_ITHR_JOINABLE 0
48 #define PERL_ITHR_DETACHED 1
49 #define PERL_ITHR_FINISHED 4
50 #define PERL_ITHR_JOINED 2
52 typedef struct ithread_s {
53 struct ithread_s *next; /* next thread in the list */
54 struct ithread_s *prev; /* prev thread in the list */
55 PerlInterpreter *interp; /* The threads interpreter */
56 I32 tid; /* threads module's thread id */
57 perl_mutex mutex; /* mutex for updating things in this struct */
58 I32 count; /* how many SVs have a reference to us */
59 signed char state; /* are we detached ? */
60 int gimme; /* Context of create */
61 SV* init_function; /* Code to run */
62 SV* params; /* args to pass function */
64 DWORD thr; /* OS's idea if thread id */
65 HANDLE handle; /* OS's waitable handle */
67 pthread_t thr; /* OS's handle for the thread */
73 /* Macros to supply the aTHX_ in an embed.h like manner */
74 #define ithread_join(thread) Perl_ithread_join(aTHX_ thread)
75 #define ithread_DESTROY(thread) Perl_ithread_DESTROY(aTHX_ thread)
76 #define ithread_CLONE(thread) Perl_ithread_CLONE(aTHX_ thread)
77 #define ithread_detach(thread) Perl_ithread_detach(aTHX_ thread)
78 #define ithread_tid(thread) ((thread)->tid)
79 #define ithread_yield(thread) (YIELD);
81 static perl_mutex create_destruct_mutex; /* protects the creation and destruction of threads*/
84 I32 known_threads = 0;
85 I32 active_threads = 0;
89 * Clear up after thread is done with
92 Perl_ithread_destruct (pTHX_ ithread* thread, const char *why)
94 PerlInterpreter* destroyperl = NULL;
95 MUTEX_LOCK(&thread->mutex);
97 Perl_croak(aTHX_ "panic: destruct destroyed thread %p (%s)",thread, why);
99 if (thread->count != 0) {
100 MUTEX_UNLOCK(&thread->mutex);
103 MUTEX_LOCK(&create_destruct_mutex);
104 /* Remove from circular list of threads */
105 if (thread->next == thread) {
106 /* last one should never get here ? */
110 thread->next->prev = thread->prev;
111 thread->prev->next = thread->next;
112 if (threads == thread) {
113 threads = thread->next;
119 assert( known_threads >= 0 );
121 Perl_warn(aTHX_ "destruct %d @ %p by %p now %d",
122 thread->tid,thread->interp,aTHX, known_threads);
124 MUTEX_UNLOCK(&create_destruct_mutex);
125 /* Thread is now disowned */
126 if (thread->interp) {
127 dTHXa(thread->interp);
128 PERL_SET_CONTEXT(thread->interp);
129 SvREFCNT_dec(thread->params);
130 thread->params = Nullsv;
131 destroyperl = thread->interp;
132 thread->interp = NULL;
134 MUTEX_UNLOCK(&thread->mutex);
135 MUTEX_DESTROY(&thread->mutex);
136 PerlMemShared_free(thread);
138 ithread* current_thread;
139 PERL_THREAD_GETSPECIFIC(self_key,current_thread);
140 PERL_THREAD_SETSPECIFIC(self_key,thread);
141 perl_destruct(destroyperl);
142 perl_free(destroyperl);
143 PERL_THREAD_SETSPECIFIC(self_key,current_thread);
146 PERL_SET_CONTEXT(aTHX);
150 Perl_ithread_hook(pTHX)
152 int veto_cleanup = 0;
153 MUTEX_LOCK(&create_destruct_mutex);
154 if (aTHX == PL_curinterp && active_threads != 1) {
155 Perl_warn(aTHX_ "A thread exited while %" IVdf " other threads were still running",
159 MUTEX_UNLOCK(&create_destruct_mutex);
164 Perl_ithread_detach(pTHX_ ithread *thread)
166 MUTEX_LOCK(&thread->mutex);
167 if (!(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) {
168 thread->state |= PERL_ITHR_DETACHED;
170 CloseHandle(thread->handle);
173 PERL_THREAD_DETACH(thread->thr);
176 if ((thread->state & PERL_ITHR_FINISHED) &&
177 (thread->state & PERL_ITHR_DETACHED)) {
178 MUTEX_UNLOCK(&thread->mutex);
179 Perl_ithread_destruct(aTHX_ thread, "detach");
182 MUTEX_UNLOCK(&thread->mutex);
186 /* MAGIC (in mg.h sense) hooks */
189 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
191 ithread *thread = (ithread *) mg->mg_ptr;
192 SvIVX(sv) = PTR2IV(thread);
198 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
200 ithread *thread = (ithread *) mg->mg_ptr;
201 MUTEX_LOCK(&thread->mutex);
203 if (thread->count == 0) {
204 if(thread->state & PERL_ITHR_FINISHED &&
205 (thread->state & PERL_ITHR_DETACHED ||
206 thread->state & PERL_ITHR_JOINED))
208 MUTEX_UNLOCK(&thread->mutex);
209 Perl_ithread_destruct(aTHX_ thread, "no reference");
212 MUTEX_UNLOCK(&thread->mutex);
216 MUTEX_UNLOCK(&thread->mutex);
222 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
224 ithread *thread = (ithread *) mg->mg_ptr;
225 MUTEX_LOCK(&thread->mutex);
227 MUTEX_UNLOCK(&thread->mutex);
231 MGVTBL ithread_vtbl = {
232 ithread_mg_get, /* get */
236 ithread_mg_free, /* free */
238 ithread_mg_dup /* dup */
243 * Starts executing the thread. Needs to clean up memory a tad better.
244 * Passed as the C level function to run in the new thread
249 Perl_ithread_run(LPVOID arg) {
252 Perl_ithread_run(void * arg) {
254 ithread* thread = (ithread*) arg;
255 dTHXa(thread->interp);
256 PERL_SET_CONTEXT(thread->interp);
257 PERL_THREAD_SETSPECIFIC(self_key,thread);
260 /* Far from clear messing with ->thr child-side is a good idea */
261 MUTEX_LOCK(&thread->mutex);
263 thread->thr = GetCurrentThreadId();
265 thread->thr = pthread_self();
267 MUTEX_UNLOCK(&thread->mutex);
270 PL_perl_destruct_level = 2;
273 AV* params = (AV*) SvRV(thread->params);
274 I32 len = av_len(params)+1;
280 for(i = 0; i < len; i++) {
281 XPUSHs(av_shift(params));
284 len = call_sv(thread->init_function, thread->gimme|G_EVAL);
286 for (i=len-1; i >= 0; i--) {
288 av_store(params, i, SvREFCNT_inc(sv));
292 Perl_warn(aTHX_ "thread failed to start: %" SVf, ERRSV);
296 SvREFCNT_dec(thread->init_function);
299 PerlIO_flush((PerlIO*)NULL);
300 MUTEX_LOCK(&thread->mutex);
301 thread->state |= PERL_ITHR_FINISHED;
303 if (thread->state & PERL_ITHR_DETACHED) {
304 MUTEX_UNLOCK(&thread->mutex);
305 Perl_ithread_destruct(aTHX_ thread, "detached finish");
307 MUTEX_UNLOCK(&thread->mutex);
309 MUTEX_LOCK(&create_destruct_mutex);
311 assert( active_threads >= 0 );
312 MUTEX_UNLOCK(&create_destruct_mutex);
322 ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
327 MUTEX_LOCK(&thread->mutex);
329 MUTEX_UNLOCK(&thread->mutex);
333 sv = newSVrv(obj,classname);
334 sv_setiv(sv,PTR2IV(thread));
335 mg = sv_magicext(sv,Nullsv,PERL_MAGIC_shared_scalar,&ithread_vtbl,(char *)thread,0);
336 mg->mg_flags |= MGf_DUP;
342 SV_to_ithread(pTHX_ SV *sv)
347 thread = INT2PTR(ithread*, SvIV(SvRV(sv)));
351 PERL_THREAD_GETSPECIFIC(self_key,thread);
357 * iThread->create(); ( aka iThread->new() )
358 * Called in context of parent thread
362 Perl_ithread_create(pTHX_ SV *obj, char* classname, SV* init_function, SV* params)
365 CLONE_PARAMS clone_param;
366 ithread* current_thread;
367 PERL_THREAD_GETSPECIFIC(self_key,current_thread);
368 MUTEX_LOCK(&create_destruct_mutex);
369 thread = PerlMemShared_malloc(sizeof(ithread));
370 Zero(thread,1,ithread);
371 thread->next = threads;
372 thread->prev = threads->prev;
373 threads->prev = thread;
374 thread->prev->next = thread;
375 /* Set count to 1 immediately in case thread exits before
376 * we return to caller !
379 MUTEX_INIT(&thread->mutex);
380 thread->tid = tid_counter++;
381 thread->gimme = GIMME_V;
383 /* "Clone" our interpreter into the thread's interpreter
384 * This gives thread access to "static data" and code.
387 PerlIO_flush((PerlIO*)NULL);
388 PERL_THREAD_SETSPECIFIC(self_key,thread);
390 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
392 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
394 /* perl_clone leaves us in new interpreter's context.
395 As it is tricky to spot an implicit aTHX, create a new scope
396 with aTHX matching the context for the duration of
397 our work for new interpreter.
400 dTHXa(thread->interp);
401 /* Here we remove END blocks since they should only run
402 in the thread they are created
404 SvREFCNT_dec(PL_endav);
406 clone_param.flags = 0;
407 thread->init_function = sv_dup(init_function, &clone_param);
408 if (SvREFCNT(thread->init_function) == 0) {
409 SvREFCNT_inc(thread->init_function);
412 thread->params = sv_dup(params, &clone_param);
413 SvREFCNT_inc(thread->params);
414 SvTEMP_off(thread->init_function);
415 ptr_table_free(PL_ptr_table);
417 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
419 PERL_THREAD_SETSPECIFIC(self_key,current_thread);
420 PERL_SET_CONTEXT(aTHX);
422 /* Start the thread */
426 thread->handle = CreateThread(NULL, 0, Perl_ithread_run,
427 (LPVOID)thread, 0, &thread->thr);
431 static pthread_attr_t attr;
432 static int attr_inited = 0;
433 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
436 pthread_attr_init(&attr);
438 # ifdef PTHREAD_ATTR_SETDETACHSTATE
439 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
441 # ifdef THREAD_CREATE_NEEDS_STACK
442 if(pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK))
443 croak("panic: pthread_attr_setstacksize failed");
446 #ifdef OLD_PTHREADS_API
447 pthread_create( &thread->thr, attr, Perl_ithread_run, (void *)thread);
449 pthread_create( &thread->thr, &attr, Perl_ithread_run, (void *)thread);
455 MUTEX_UNLOCK(&create_destruct_mutex);
457 return ithread_to_SV(aTHX_ obj, thread, classname, FALSE);
461 Perl_ithread_self (pTHX_ SV *obj, char* Class)
464 PERL_THREAD_GETSPECIFIC(self_key,thread);
465 return ithread_to_SV(aTHX_ obj, thread, Class, TRUE);
469 * Joins the thread this code needs to take the returnvalue from the
470 * call_sv and send it back
474 Perl_ithread_CLONE(pTHX_ SV *obj)
478 ithread *thread = SV_to_ithread(aTHX_ obj);
482 Perl_warn(aTHX_ "CLONE %" SVf,obj);
487 Perl_ithread_join(pTHX_ SV *obj)
489 ithread *thread = SV_to_ithread(aTHX_ obj);
490 MUTEX_LOCK(&thread->mutex);
491 if (thread->state & PERL_ITHR_DETACHED) {
492 MUTEX_UNLOCK(&thread->mutex);
493 Perl_croak(aTHX_ "Cannot join a detached thread");
495 else if (thread->state & PERL_ITHR_JOINED) {
496 MUTEX_UNLOCK(&thread->mutex);
497 Perl_croak(aTHX_ "Thread already joined");
506 MUTEX_UNLOCK(&thread->mutex);
508 waitcode = WaitForSingleObject(thread->handle, INFINITE);
510 pthread_join(thread->thr,&retval);
512 MUTEX_LOCK(&thread->mutex);
514 /* sv_dup over the args */
516 ithread* current_thread;
517 AV* params = (AV*) SvRV(thread->params);
518 CLONE_PARAMS clone_params;
519 clone_params.stashes = newAV();
520 PL_ptr_table = ptr_table_new();
521 PERL_THREAD_GETSPECIFIC(self_key,current_thread);
522 PERL_THREAD_SETSPECIFIC(self_key,thread);
523 retparam = (AV*) sv_dup((SV*)params, &clone_params);
524 PERL_THREAD_SETSPECIFIC(self_key,current_thread);
525 SvREFCNT_dec(clone_params.stashes);
526 SvREFCNT_inc(retparam);
527 ptr_table_free(PL_ptr_table);
531 /* We have finished with it */
532 thread->state |= PERL_ITHR_JOINED;
533 MUTEX_UNLOCK(&thread->mutex);
534 sv_unmagic(SvRV(obj),PERL_MAGIC_shared_scalar);
541 Perl_ithread_DESTROY(pTHX_ SV *sv)
543 ithread *thread = SV_to_ithread(aTHX_ sv);
544 sv_unmagic(SvRV(sv),PERL_MAGIC_shared_scalar);
547 #endif /* USE_ITHREADS */
549 MODULE = threads PACKAGE = threads PREFIX = ithread_
555 ithread_new (classname, function_to_call, ...)
557 SV * function_to_call
560 AV* params = newAV();
563 for(i = 2; i < items ; i++) {
564 av_push(params, SvREFCNT_inc(ST(i)));
567 ST(0) = sv_2mortal(Perl_ithread_create(aTHX_ Nullsv, classname, function_to_call, newRV_noinc((SV*) params)));
572 ithread_list(char *classname)
575 ithread *curr_thread;
576 MUTEX_LOCK(&create_destruct_mutex);
577 curr_thread = threads;
578 if(curr_thread->tid != 0)
579 PUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE)));
581 curr_thread = curr_thread->next;
582 if(curr_thread == threads)
584 if(curr_thread->state & PERL_ITHR_DETACHED ||
585 curr_thread->state & PERL_ITHR_JOINED)
587 PUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE)));
589 MUTEX_UNLOCK(&create_destruct_mutex);
594 ithread_self(char *classname)
597 ST(0) = sv_2mortal(Perl_ithread_self(aTHX_ Nullsv,classname));
602 ithread_tid(ithread *thread)
605 ithread_join(SV *obj)
608 AV* params = Perl_ithread_join(aTHX_ obj);
610 I32 len = AvFILL(params);
611 for (i = 0; i <= len; i++) {
612 SV* tmp = av_shift(params);
616 SvREFCNT_dec(params);
628 ithread_detach(ithread *thread)
631 ithread_DESTROY(SV *thread)
633 #endif /* USE_ITHREADS */
639 PL_perl_destruct_level = 2;
640 PERL_THREAD_ALLOC_SPECIFIC(self_key);
641 MUTEX_INIT(&create_destruct_mutex);
642 MUTEX_LOCK(&create_destruct_mutex);
643 PL_threadhook = &Perl_ithread_hook;
644 thread = PerlMemShared_malloc(sizeof(ithread));
645 Zero(thread,1,ithread);
646 PL_perl_destruct_level = 2;
647 MUTEX_INIT(&thread->mutex);
649 thread->next = thread;
650 thread->prev = thread;
651 thread->interp = aTHX;
652 thread->count = 1; /* imortal */
653 thread->tid = tid_counter++;
658 thread->thr = GetCurrentThreadId();
660 thread->thr = pthread_self();
663 PERL_THREAD_SETSPECIFIC(self_key,thread);
664 MUTEX_UNLOCK(&create_destruct_mutex);
665 #endif /* USE_ITHREADS */