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 MUTEX_LOCK(&thread->mutex);
96 Perl_croak(aTHX_ "panic: destruct destroyed thread %p (%s)",thread, why);
98 if (thread->count != 0) {
99 MUTEX_UNLOCK(&thread->mutex);
102 MUTEX_LOCK(&create_destruct_mutex);
103 /* Remove from circular list of threads */
104 if (thread->next == thread) {
105 /* last one should never get here ? */
109 thread->next->prev = thread->prev;
110 thread->prev->next = thread->next;
111 if (threads == thread) {
112 threads = thread->next;
118 assert( known_threads >= 0 );
120 Perl_warn(aTHX_ "destruct %d @ %p by %p now %d",
121 thread->tid,thread->interp,aTHX, known_threads);
123 MUTEX_UNLOCK(&create_destruct_mutex);
124 /* Thread is now disowned */
125 if (thread->interp) {
126 dTHXa(thread->interp);
127 PERL_SET_CONTEXT(thread->interp);
128 SvREFCNT_dec(thread->params);
129 thread->params = Nullsv;
130 perl_destruct(thread->interp);
131 perl_free(thread->interp);
132 thread->interp = NULL;
134 PERL_SET_CONTEXT(aTHX);
135 MUTEX_UNLOCK(&thread->mutex);
136 MUTEX_DESTROY(&thread->mutex);
137 PerlMemShared_free(thread);
141 Perl_ithread_hook(pTHX)
143 int veto_cleanup = 0;
144 MUTEX_LOCK(&create_destruct_mutex);
145 if (aTHX == PL_curinterp && active_threads != 1) {
146 Perl_warn(aTHX_ "A thread exited while %" IVdf " other threads were still running",
150 MUTEX_UNLOCK(&create_destruct_mutex);
155 Perl_ithread_detach(pTHX_ ithread *thread)
157 MUTEX_LOCK(&thread->mutex);
158 if (!(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) {
159 thread->state |= PERL_ITHR_DETACHED;
161 CloseHandle(thread->handle);
164 PERL_THREAD_DETACH(thread->thr);
167 if ((thread->state & PERL_ITHR_FINISHED) &&
168 (thread->state & PERL_ITHR_DETACHED)) {
169 MUTEX_UNLOCK(&thread->mutex);
170 Perl_ithread_destruct(aTHX_ thread, "detach");
173 MUTEX_UNLOCK(&thread->mutex);
177 /* MAGIC (in mg.h sense) hooks */
180 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
182 ithread *thread = (ithread *) mg->mg_ptr;
183 SvIVX(sv) = PTR2IV(thread);
189 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
191 ithread *thread = (ithread *) mg->mg_ptr;
192 MUTEX_LOCK(&thread->mutex);
194 if (thread->count == 0) {
195 if(thread->state & PERL_ITHR_FINISHED &&
196 (thread->state & PERL_ITHR_DETACHED ||
197 thread->state & PERL_ITHR_JOINED))
199 MUTEX_UNLOCK(&thread->mutex);
200 Perl_ithread_destruct(aTHX_ thread, "no reference");
203 MUTEX_UNLOCK(&thread->mutex);
207 MUTEX_UNLOCK(&thread->mutex);
213 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
215 ithread *thread = (ithread *) mg->mg_ptr;
216 MUTEX_LOCK(&thread->mutex);
218 MUTEX_UNLOCK(&thread->mutex);
222 MGVTBL ithread_vtbl = {
223 ithread_mg_get, /* get */
227 ithread_mg_free, /* free */
229 ithread_mg_dup /* dup */
234 * Starts executing the thread. Needs to clean up memory a tad better.
235 * Passed as the C level function to run in the new thread
240 Perl_ithread_run(LPVOID arg) {
243 Perl_ithread_run(void * arg) {
245 ithread* thread = (ithread*) arg;
246 dTHXa(thread->interp);
247 PERL_SET_CONTEXT(thread->interp);
248 PERL_THREAD_SETSPECIFIC(self_key,thread);
251 /* Far from clear messing with ->thr child-side is a good idea */
252 MUTEX_LOCK(&thread->mutex);
254 thread->thr = GetCurrentThreadId();
256 thread->thr = pthread_self();
258 MUTEX_UNLOCK(&thread->mutex);
261 PL_perl_destruct_level = 2;
264 AV* params = (AV*) SvRV(thread->params);
265 I32 len = av_len(params)+1;
271 for(i = 0; i < len; i++) {
272 XPUSHs(av_shift(params));
275 len = call_sv(thread->init_function, thread->gimme|G_EVAL);
277 for (i=len-1; i >= 0; i--) {
279 av_store(params, i, SvREFCNT_inc(sv));
283 Perl_warn(aTHX_ "thread failed to start: %" SVf, ERRSV);
287 SvREFCNT_dec(thread->init_function);
290 PerlIO_flush((PerlIO*)NULL);
291 MUTEX_LOCK(&thread->mutex);
292 thread->state |= PERL_ITHR_FINISHED;
294 if (thread->state & PERL_ITHR_DETACHED) {
295 MUTEX_UNLOCK(&thread->mutex);
296 Perl_ithread_destruct(aTHX_ thread, "detached finish");
298 MUTEX_UNLOCK(&thread->mutex);
300 MUTEX_LOCK(&create_destruct_mutex);
302 assert( active_threads >= 0 );
303 MUTEX_UNLOCK(&create_destruct_mutex);
313 ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
318 MUTEX_LOCK(&thread->mutex);
320 MUTEX_UNLOCK(&thread->mutex);
324 sv = newSVrv(obj,classname);
325 sv_setiv(sv,PTR2IV(thread));
326 mg = sv_magicext(sv,Nullsv,PERL_MAGIC_shared_scalar,&ithread_vtbl,(char *)thread,0);
327 mg->mg_flags |= MGf_DUP;
333 SV_to_ithread(pTHX_ SV *sv)
338 thread = INT2PTR(ithread*, SvIV(SvRV(sv)));
342 PERL_THREAD_GETSPECIFIC(self_key,thread);
348 * iThread->create(); ( aka iThread->new() )
349 * Called in context of parent thread
353 Perl_ithread_create(pTHX_ SV *obj, char* classname, SV* init_function, SV* params)
356 CLONE_PARAMS clone_param;
358 MUTEX_LOCK(&create_destruct_mutex);
359 thread = PerlMemShared_malloc(sizeof(ithread));
360 Zero(thread,1,ithread);
361 thread->next = threads;
362 thread->prev = threads->prev;
363 threads->prev = thread;
364 thread->prev->next = thread;
365 /* Set count to 1 immediately in case thread exits before
366 * we return to caller !
369 MUTEX_INIT(&thread->mutex);
370 thread->tid = tid_counter++;
371 thread->gimme = GIMME_V;
373 /* "Clone" our interpreter into the thread's interpreter
374 * This gives thread access to "static data" and code.
377 PerlIO_flush((PerlIO*)NULL);
380 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
382 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
384 /* perl_clone leaves us in new interpreter's context.
385 As it is tricky to spot implcit aTHX create a new scope
386 with aTHX matching the context for the duration of
387 our work for new interpreter.
390 dTHXa(thread->interp);
391 /* Here we remove END blocks since they should only run
392 in the thread they are created
394 SvREFCNT_dec(PL_endav);
396 clone_param.flags = 0;
397 thread->init_function = sv_dup(init_function, &clone_param);
398 if (SvREFCNT(thread->init_function) == 0) {
399 SvREFCNT_inc(thread->init_function);
402 thread->params = sv_dup(params, &clone_param);
403 SvREFCNT_inc(thread->params);
404 SvTEMP_off(thread->init_function);
405 ptr_table_free(PL_ptr_table);
407 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
410 PERL_SET_CONTEXT(aTHX);
412 /* Start the thread */
416 thread->handle = CreateThread(NULL, 0, Perl_ithread_run,
417 (LPVOID)thread, 0, &thread->thr);
421 static pthread_attr_t attr;
422 static int attr_inited = 0;
423 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
426 pthread_attr_init(&attr);
428 # ifdef PTHREAD_ATTR_SETDETACHSTATE
429 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
431 # ifdef THREAD_CREATE_NEEDS_STACK
432 if(pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK))
433 croak("panic: pthread_attr_setstacksize failed");
436 #ifdef OLD_PTHREADS_API
437 pthread_create( &thread->thr, attr, Perl_ithread_run, (void *)thread);
439 pthread_create( &thread->thr, &attr, Perl_ithread_run, (void *)thread);
445 MUTEX_UNLOCK(&create_destruct_mutex);
447 return ithread_to_SV(aTHX_ obj, thread, classname, FALSE);
451 Perl_ithread_self (pTHX_ SV *obj, char* Class)
454 PERL_THREAD_GETSPECIFIC(self_key,thread);
455 return ithread_to_SV(aTHX_ obj, thread, Class, TRUE);
459 * Joins the thread this code needs to take the returnvalue from the
460 * call_sv and send it back
464 Perl_ithread_CLONE(pTHX_ SV *obj)
468 ithread *thread = SV_to_ithread(aTHX_ obj);
472 Perl_warn(aTHX_ "CLONE %" SVf,obj);
477 Perl_ithread_join(pTHX_ SV *obj)
479 ithread *thread = SV_to_ithread(aTHX_ obj);
480 MUTEX_LOCK(&thread->mutex);
481 if (thread->state & PERL_ITHR_DETACHED) {
482 MUTEX_UNLOCK(&thread->mutex);
483 Perl_croak(aTHX_ "Cannot join a detached thread");
485 else if (thread->state & PERL_ITHR_JOINED) {
486 MUTEX_UNLOCK(&thread->mutex);
487 Perl_croak(aTHX_ "Thread already joined");
496 MUTEX_UNLOCK(&thread->mutex);
498 waitcode = WaitForSingleObject(thread->handle, INFINITE);
500 pthread_join(thread->thr,&retval);
502 MUTEX_LOCK(&thread->mutex);
504 /* sv_dup over the args */
506 AV* params = (AV*) SvRV(thread->params);
507 CLONE_PARAMS clone_params;
508 clone_params.stashes = newAV();
509 PL_ptr_table = ptr_table_new();
510 retparam = (AV*) sv_dup((SV*)params, &clone_params);
511 SvREFCNT_dec(clone_params.stashes);
512 SvREFCNT_inc(retparam);
513 ptr_table_free(PL_ptr_table);
517 /* We have finished with it */
518 thread->state |= PERL_ITHR_JOINED;
519 MUTEX_UNLOCK(&thread->mutex);
520 sv_unmagic(SvRV(obj),PERL_MAGIC_shared_scalar);
527 Perl_ithread_DESTROY(pTHX_ SV *sv)
529 ithread *thread = SV_to_ithread(aTHX_ sv);
530 sv_unmagic(SvRV(sv),PERL_MAGIC_shared_scalar);
533 #endif /* USE_ITHREADS */
535 MODULE = threads PACKAGE = threads PREFIX = ithread_
541 ithread_new (classname, function_to_call, ...)
543 SV * function_to_call
546 AV* params = newAV();
549 for(i = 2; i < items ; i++) {
550 av_push(params, SvREFCNT_inc(ST(i)));
553 ST(0) = sv_2mortal(Perl_ithread_create(aTHX_ Nullsv, classname, function_to_call, newRV_noinc((SV*) params)));
558 ithread_list(char *classname)
561 ithread *curr_thread;
562 MUTEX_LOCK(&create_destruct_mutex);
563 curr_thread = threads;
564 if(curr_thread->tid != 0)
565 PUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE)));
567 curr_thread = curr_thread->next;
568 if(curr_thread == threads)
570 if(curr_thread->state & PERL_ITHR_DETACHED ||
571 curr_thread->state & PERL_ITHR_JOINED)
573 PUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE)));
575 MUTEX_UNLOCK(&create_destruct_mutex);
580 ithread_self(char *classname)
583 ST(0) = sv_2mortal(Perl_ithread_self(aTHX_ Nullsv,classname));
588 ithread_tid(ithread *thread)
591 ithread_join(SV *obj)
594 AV* params = Perl_ithread_join(aTHX_ obj);
596 I32 len = AvFILL(params);
597 for (i = 0; i <= len; i++) {
598 SV* tmp = av_shift(params);
602 SvREFCNT_dec(params);
614 ithread_detach(ithread *thread)
617 ithread_DESTROY(SV *thread)
619 #endif /* USE_ITHREADS */
625 PL_perl_destruct_level = 2;
626 PERL_THREAD_ALLOC_SPECIFIC(self_key);
627 MUTEX_INIT(&create_destruct_mutex);
628 MUTEX_LOCK(&create_destruct_mutex);
629 PL_threadhook = &Perl_ithread_hook;
630 thread = PerlMemShared_malloc(sizeof(ithread));
631 Zero(thread,1,ithread);
632 PL_perl_destruct_level = 2;
633 MUTEX_INIT(&thread->mutex);
635 thread->next = thread;
636 thread->prev = thread;
637 thread->interp = aTHX;
638 thread->count = 1; /* imortal */
639 thread->tid = tid_counter++;
644 thread->thr = GetCurrentThreadId();
646 thread->thr = pthread_self();
649 PERL_THREAD_SETSPECIFIC(self_key,thread);
650 MUTEX_UNLOCK(&create_destruct_mutex);
651 #endif /* USE_ITHREADS */