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_ "Cleanup skipped %d active threads", active_threads);
149 MUTEX_UNLOCK(&create_destruct_mutex);
154 Perl_ithread_detach(pTHX_ ithread *thread)
156 MUTEX_LOCK(&thread->mutex);
157 if (!(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) {
158 thread->state |= PERL_ITHR_DETACHED;
160 CloseHandle(thread->handle);
163 PERL_THREAD_DETACH(thread->thr);
166 if ((thread->state & PERL_ITHR_FINISHED) &&
167 (thread->state & PERL_ITHR_DETACHED)) {
168 MUTEX_UNLOCK(&thread->mutex);
169 Perl_ithread_destruct(aTHX_ thread, "detach");
172 MUTEX_UNLOCK(&thread->mutex);
176 /* MAGIC (in mg.h sense) hooks */
179 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
181 ithread *thread = (ithread *) mg->mg_ptr;
182 SvIVX(sv) = PTR2IV(thread);
188 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
190 ithread *thread = (ithread *) mg->mg_ptr;
191 MUTEX_LOCK(&thread->mutex);
193 if (thread->count == 0) {
194 if(thread->state & PERL_ITHR_FINISHED &&
195 (thread->state & PERL_ITHR_DETACHED ||
196 thread->state & PERL_ITHR_JOINED))
198 MUTEX_UNLOCK(&thread->mutex);
199 Perl_ithread_destruct(aTHX_ thread, "no reference");
202 MUTEX_UNLOCK(&thread->mutex);
206 MUTEX_UNLOCK(&thread->mutex);
212 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
214 ithread *thread = (ithread *) mg->mg_ptr;
215 MUTEX_LOCK(&thread->mutex);
217 MUTEX_UNLOCK(&thread->mutex);
221 MGVTBL ithread_vtbl = {
222 ithread_mg_get, /* get */
226 ithread_mg_free, /* free */
228 ithread_mg_dup /* dup */
233 * Starts executing the thread. Needs to clean up memory a tad better.
234 * Passed as the C level function to run in the new thread
239 Perl_ithread_run(LPVOID arg) {
242 Perl_ithread_run(void * arg) {
244 ithread* thread = (ithread*) arg;
245 dTHXa(thread->interp);
246 PERL_SET_CONTEXT(thread->interp);
247 PERL_THREAD_SETSPECIFIC(self_key,thread);
250 /* Far from clear messing with ->thr child-side is a good idea */
251 MUTEX_LOCK(&thread->mutex);
253 thread->thr = GetCurrentThreadId();
255 thread->thr = pthread_self();
257 MUTEX_UNLOCK(&thread->mutex);
260 PL_perl_destruct_level = 2;
263 AV* params = (AV*) SvRV(thread->params);
264 I32 len = av_len(params)+1;
270 for(i = 0; i < len; i++) {
271 XPUSHs(av_shift(params));
274 len = call_sv(thread->init_function, thread->gimme|G_EVAL);
276 for (i=len-1; i >= 0; i--) {
278 av_store(params, i, SvREFCNT_inc(sv));
282 Perl_warn(aTHX_ "Died:%_",ERRSV);
286 SvREFCNT_dec(thread->init_function);
289 PerlIO_flush((PerlIO*)NULL);
290 MUTEX_LOCK(&create_destruct_mutex);
292 assert( active_threads >= 0 );
293 MUTEX_UNLOCK(&create_destruct_mutex);
294 MUTEX_LOCK(&thread->mutex);
295 thread->state |= PERL_ITHR_FINISHED;
297 if (thread->state & PERL_ITHR_DETACHED) {
298 MUTEX_UNLOCK(&thread->mutex);
299 Perl_ithread_destruct(aTHX_ thread, "detached finish");
301 MUTEX_UNLOCK(&thread->mutex);
311 ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
316 MUTEX_LOCK(&thread->mutex);
318 MUTEX_UNLOCK(&thread->mutex);
322 sv = newSVrv(obj,classname);
323 sv_setiv(sv,PTR2IV(thread));
324 mg = sv_magicext(sv,Nullsv,PERL_MAGIC_shared_scalar,&ithread_vtbl,(char *)thread,0);
325 mg->mg_flags |= MGf_DUP;
331 SV_to_ithread(pTHX_ SV *sv)
336 thread = INT2PTR(ithread*, SvIV(SvRV(sv)));
340 PERL_THREAD_GETSPECIFIC(self_key,thread);
346 * iThread->create(); ( aka iThread->new() )
347 * Called in context of parent thread
351 Perl_ithread_create(pTHX_ SV *obj, char* classname, SV* init_function, SV* params)
354 CLONE_PARAMS clone_param;
356 MUTEX_LOCK(&create_destruct_mutex);
357 thread = PerlMemShared_malloc(sizeof(ithread));
358 Zero(thread,1,ithread);
359 thread->next = threads;
360 thread->prev = threads->prev;
361 threads->prev = thread;
362 thread->prev->next = thread;
363 /* Set count to 1 immediately in case thread exits before
364 * we return to caller !
367 MUTEX_INIT(&thread->mutex);
368 thread->tid = tid_counter++;
369 thread->gimme = GIMME_V;
371 /* "Clone" our interpreter into the thread's interpreter
372 * This gives thread access to "static data" and code.
375 PerlIO_flush((PerlIO*)NULL);
378 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
380 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
382 /* perl_clone leaves us in new interpreter's context.
383 As it is tricky to spot implcit aTHX create a new scope
384 with aTHX matching the context for the duration of
385 our work for new interpreter.
388 dTHXa(thread->interp);
389 /* Here we remove END blocks since they should only run
390 in the thread they are created
392 SvREFCNT_dec(PL_endav);
394 clone_param.flags = 0;
395 thread->init_function = sv_dup(init_function, &clone_param);
396 if (SvREFCNT(thread->init_function) == 0) {
397 SvREFCNT_inc(thread->init_function);
400 thread->params = sv_dup(params, &clone_param);
401 SvREFCNT_inc(thread->params);
402 SvTEMP_off(thread->init_function);
403 ptr_table_free(PL_ptr_table);
405 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
408 PERL_SET_CONTEXT(aTHX);
410 /* Start the thread */
414 thread->handle = CreateThread(NULL, 0, Perl_ithread_run,
415 (LPVOID)thread, 0, &thread->thr);
419 static pthread_attr_t attr;
420 static int attr_inited = 0;
421 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
424 pthread_attr_init(&attr);
426 # ifdef PTHREAD_ATTR_SETDETACHSTATE
427 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
429 # ifdef THREAD_CREATE_NEEDS_STACK
430 if(pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK))
431 croak("panic: pthread_attr_setstacksize failed");
434 #ifdef OLD_PTHREADS_API
435 pthread_create( &thread->thr, attr, Perl_ithread_run, (void *)thread);
437 pthread_create( &thread->thr, &attr, Perl_ithread_run, (void *)thread);
443 MUTEX_UNLOCK(&create_destruct_mutex);
445 return ithread_to_SV(aTHX_ obj, thread, classname, FALSE);
449 Perl_ithread_self (pTHX_ SV *obj, char* Class)
452 PERL_THREAD_GETSPECIFIC(self_key,thread);
453 return ithread_to_SV(aTHX_ obj, thread, Class, TRUE);
457 * Joins the thread this code needs to take the returnvalue from the
458 * call_sv and send it back
462 Perl_ithread_CLONE(pTHX_ SV *obj)
466 ithread *thread = SV_to_ithread(aTHX_ obj);
470 Perl_warn(aTHX_ "CLONE %_",obj);
475 Perl_ithread_join(pTHX_ SV *obj)
477 ithread *thread = SV_to_ithread(aTHX_ obj);
478 MUTEX_LOCK(&thread->mutex);
479 if (thread->state & PERL_ITHR_DETACHED) {
480 MUTEX_UNLOCK(&thread->mutex);
481 Perl_croak(aTHX_ "Cannot join a detached thread");
483 else if (thread->state & PERL_ITHR_JOINED) {
484 MUTEX_UNLOCK(&thread->mutex);
485 Perl_croak(aTHX_ "Thread already joined");
494 MUTEX_UNLOCK(&thread->mutex);
496 waitcode = WaitForSingleObject(thread->handle, INFINITE);
498 pthread_join(thread->thr,&retval);
500 MUTEX_LOCK(&thread->mutex);
502 /* sv_dup over the args */
504 AV* params = (AV*) SvRV(thread->params);
505 CLONE_PARAMS clone_params;
506 clone_params.stashes = newAV();
507 PL_ptr_table = ptr_table_new();
508 retparam = (AV*) sv_dup((SV*)params, &clone_params);
509 SvREFCNT_dec(clone_params.stashes);
510 SvREFCNT_inc(retparam);
511 ptr_table_free(PL_ptr_table);
515 /* We have finished with it */
516 thread->state |= PERL_ITHR_JOINED;
517 MUTEX_UNLOCK(&thread->mutex);
518 sv_unmagic(SvRV(obj),PERL_MAGIC_shared_scalar);
525 Perl_ithread_DESTROY(pTHX_ SV *sv)
527 ithread *thread = SV_to_ithread(aTHX_ sv);
528 sv_unmagic(SvRV(sv),PERL_MAGIC_shared_scalar);
531 #endif /* USE_ITHREADS */
533 MODULE = threads PACKAGE = threads PREFIX = ithread_
539 ithread_new (classname, function_to_call, ...)
541 SV * function_to_call
544 AV* params = newAV();
547 for(i = 2; i < items ; i++) {
548 av_push(params, SvREFCNT_inc(ST(i)));
551 ST(0) = sv_2mortal(Perl_ithread_create(aTHX_ Nullsv, classname, function_to_call, newRV_noinc((SV*) params)));
556 ithread_list(char *classname)
559 ithread *curr_thread;
560 MUTEX_LOCK(&create_destruct_mutex);
561 curr_thread = threads;
562 PUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE)));
564 curr_thread = curr_thread->next;
565 if(curr_thread == threads)
567 if(curr_thread->state & PERL_ITHR_DETACHED ||
568 curr_thread->state & PERL_ITHR_JOINED)
570 PUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE)));
572 MUTEX_UNLOCK(&create_destruct_mutex);
577 ithread_self(char *classname)
580 ST(0) = sv_2mortal(Perl_ithread_self(aTHX_ Nullsv,classname));
585 ithread_tid(ithread *thread)
588 ithread_join(SV *obj)
591 AV* params = Perl_ithread_join(aTHX_ obj);
593 I32 len = AvFILL(params);
594 for (i = 0; i <= len; i++) {
595 SV* tmp = av_shift(params);
599 SvREFCNT_dec(params);
603 ithread_yield(ithread *thread)
606 ithread_detach(ithread *thread)
609 ithread_DESTROY(SV *thread)
611 #endif /* USE_ITHREADS */
617 PL_perl_destruct_level = 2;
618 PERL_THREAD_ALLOC_SPECIFIC(self_key);
619 MUTEX_INIT(&create_destruct_mutex);
620 MUTEX_LOCK(&create_destruct_mutex);
621 PL_threadhook = &Perl_ithread_hook;
622 thread = PerlMemShared_malloc(sizeof(ithread));
623 Zero(thread,1,ithread);
624 PL_perl_destruct_level = 2;
625 MUTEX_INIT(&thread->mutex);
627 thread->next = thread;
628 thread->prev = thread;
629 thread->interp = aTHX;
630 thread->count = 1; /* imortal */
631 thread->tid = tid_counter++;
636 thread->thr = GetCurrentThreadId();
638 thread->thr = pthread_self();
641 PERL_THREAD_SETSPECIFIC(self_key,thread);
642 MUTEX_UNLOCK(&create_destruct_mutex);
643 #endif /* USE_ITHREADS */