1 #define PERL_NO_GET_CONTEXT
8 #include <win32thread.h>
9 #define PERL_THREAD_SETSPECIFIC(k,v) TlsSetValue(k,v)
10 #define PERL_THREAD_GETSPECIFIC(k,v) v = TlsGetValue(k)
11 #define PERL_THREAD_ALLOC_SPECIFIC(k) \
13 if((k = TlsAlloc()) == TLS_OUT_OF_INDEXES) {\
14 PerlIO_printf(PerlIO_stderr(),"panic threads.h: TlsAlloc");\
22 #define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
23 #ifdef OLD_PTHREADS_API
24 #define PERL_THREAD_DETACH(t) pthread_detach(&(t))
25 #define PERL_THREAD_GETSPECIFIC(k,v) pthread_getspecific(k,&v)
26 #define PERL_THREAD_ALLOC_SPECIFIC(k) STMT_START {\
27 if(pthread_keycreate(&(k),0)) {\
28 PerlIO_printf(PerlIO_stderr(), "panic threads.h: pthread_key_create");\
33 #define PERL_THREAD_DETACH(t) pthread_detach((t))
34 #define PERL_THREAD_GETSPECIFIC(k,v) v = pthread_getspecific(k)
35 #define PERL_THREAD_ALLOC_SPECIFIC(k) STMT_START {\
36 if(pthread_key_create(&(k),0)) {\
37 PerlIO_printf(PerlIO_stderr(), "panic threads.h: pthread_key_create");\
44 /* Values for 'state' member */
45 #define PERL_ITHR_JOINABLE 0
46 #define PERL_ITHR_DETACHED 1
47 #define PERL_ITHR_FINISHED 4
48 #define PERL_ITHR_JOINED 2
50 typedef struct ithread_s {
51 struct ithread_s *next; /* next thread in the list */
52 struct ithread_s *prev; /* prev thread in the list */
53 PerlInterpreter *interp; /* The threads interpreter */
54 I32 tid; /* threads module's thread id */
55 perl_mutex mutex; /* mutex for updating things in this struct */
56 I32 count; /* how many SVs have a reference to us */
57 signed char state; /* are we detached ? */
58 int gimme; /* Context of create */
59 SV* init_function; /* Code to run */
60 SV* params; /* args to pass function */
62 DWORD thr; /* OS's idea if thread id */
63 HANDLE handle; /* OS's waitable handle */
65 pthread_t thr; /* OS's handle for the thread */
71 /* Macros to supply the aTHX_ in an embed.h like manner */
72 #define ithread_join(thread) Perl_ithread_join(aTHX_ thread)
73 #define ithread_DESTROY(thread) Perl_ithread_DESTROY(aTHX_ thread)
74 #define ithread_CLONE(thread) Perl_ithread_CLONE(aTHX_ thread)
75 #define ithread_detach(thread) Perl_ithread_detach(aTHX_ thread)
76 #define ithread_tid(thread) ((thread)->tid)
78 static perl_mutex create_destruct_mutex; /* protects the creation and destruction of threads*/
81 I32 known_threads = 0;
82 I32 active_threads = 0;
86 * Clear up after thread is done with
89 Perl_ithread_destruct (pTHX_ ithread* thread, const char *why)
91 MUTEX_LOCK(&thread->mutex);
93 Perl_croak(aTHX_ "panic: destruct destroyed thread %p (%s)",thread, why);
95 if (thread->count != 0) {
96 MUTEX_UNLOCK(&thread->mutex);
99 MUTEX_LOCK(&create_destruct_mutex);
100 /* Remove from circular list of threads */
101 if (thread->next == thread) {
102 /* last one should never get here ? */
106 thread->next->prev = thread->prev->next;
107 thread->prev->next = thread->next->prev;
108 if (threads == thread) {
109 threads = thread->next;
115 assert( known_threads >= 0 );
117 Perl_warn(aTHX_ "destruct %d @ %p by %p now %d",
118 thread->tid,thread->interp,aTHX, known_threads);
120 MUTEX_UNLOCK(&create_destruct_mutex);
121 /* Thread is now disowned */
122 if (thread->interp) {
123 PERL_SET_CONTEXT(thread->interp);
124 perl_destruct(thread->interp);
125 perl_free(thread->interp);
126 thread->interp = NULL;
128 PERL_SET_CONTEXT(aTHX);
129 MUTEX_UNLOCK(&thread->mutex);
133 Perl_ithread_hook(pTHX)
135 int veto_cleanup = 0;
136 MUTEX_LOCK(&create_destruct_mutex);
137 if (aTHX == PL_curinterp && active_threads != 1) {
138 Perl_warn(aTHX_ "Cleanup skipped %d active threads", active_threads);
141 MUTEX_UNLOCK(&create_destruct_mutex);
146 Perl_ithread_detach(pTHX_ ithread *thread)
148 MUTEX_LOCK(&thread->mutex);
149 if (!(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) {
150 thread->state |= PERL_ITHR_DETACHED;
152 CloseHandle(thread->handle);
155 PERL_THREAD_DETACH(thread->thr);
158 if ((thread->state & PERL_ITHR_FINISHED) &&
159 (thread->state & PERL_ITHR_DETACHED)) {
160 MUTEX_UNLOCK(&thread->mutex);
161 Perl_ithread_destruct(aTHX_ thread, "detach");
164 MUTEX_UNLOCK(&thread->mutex);
168 /* MAGIC (in mg.h sense) hooks */
171 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
173 ithread *thread = (ithread *) mg->mg_ptr;
174 SvIVX(sv) = PTR2IV(thread);
180 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
182 ithread *thread = (ithread *) mg->mg_ptr;
183 MUTEX_LOCK(&thread->mutex);
185 if (thread->count == 0) {
186 if (!(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) {
187 Perl_warn(aTHX_ "Implicit detach");
189 MUTEX_UNLOCK(&thread->mutex);
190 Perl_ithread_detach(aTHX_ thread);
193 MUTEX_UNLOCK(&thread->mutex);
199 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
201 ithread *thread = (ithread *) mg->mg_ptr;
202 MUTEX_LOCK(&thread->mutex);
204 MUTEX_UNLOCK(&thread->mutex);
208 MGVTBL ithread_vtbl = {
209 ithread_mg_get, /* get */
213 ithread_mg_free, /* free */
215 ithread_mg_dup /* dup */
220 * Starts executing the thread. Needs to clean up memory a tad better.
221 * Passed as the C level function to run in the new thread
226 Perl_ithread_run(LPVOID arg) {
229 Perl_ithread_run(void * arg) {
231 ithread* thread = (ithread*) arg;
232 dTHXa(thread->interp);
233 PERL_SET_CONTEXT(thread->interp);
234 PERL_THREAD_SETSPECIFIC(self_key,thread);
237 /* Far from clear messing with ->thr child-side is a good idea */
238 MUTEX_LOCK(&thread->mutex);
240 thread->thr = GetCurrentThreadId();
242 thread->thr = pthread_self();
244 MUTEX_UNLOCK(&thread->mutex);
247 PL_perl_destruct_level = 2;
250 AV* params = (AV*) SvRV(thread->params);
251 I32 len = av_len(params)+1;
257 for(i = 0; i < len; i++) {
258 XPUSHs(av_shift(params));
261 len = call_sv(thread->init_function, thread->gimme|G_EVAL);
263 for (i=len-1; i >= 0; i--) {
265 av_store(params, i, SvREFCNT_inc(sv));
269 Perl_warn(aTHX_ "Died:%_",ERRSV);
273 SvREFCNT_dec(thread->init_function);
276 PerlIO_flush((PerlIO*)NULL);
277 MUTEX_LOCK(&create_destruct_mutex);
279 assert( active_threads >= 0 );
280 MUTEX_UNLOCK(&create_destruct_mutex);
281 MUTEX_LOCK(&thread->mutex);
282 thread->state |= PERL_ITHR_FINISHED;
284 if (thread->state & PERL_ITHR_DETACHED) {
285 MUTEX_UNLOCK(&thread->mutex);
286 SvREFCNT_dec(thread->params);
287 thread->params = Nullsv;
288 Perl_ithread_destruct(aTHX_ thread, "detached finish");
290 MUTEX_UNLOCK(&thread->mutex);
300 ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
305 MUTEX_LOCK(&thread->mutex);
307 MUTEX_UNLOCK(&thread->mutex);
311 sv = newSVrv(obj,classname);
312 sv_setiv(sv,PTR2IV(thread));
313 mg = sv_magicext(sv,Nullsv,PERL_MAGIC_shared_scalar,&ithread_vtbl,(char *)thread,0);
314 mg->mg_flags |= MGf_DUP;
320 SV_to_ithread(pTHX_ SV *sv)
325 thread = INT2PTR(ithread*, SvIV(SvRV(sv)));
329 PERL_THREAD_GETSPECIFIC(self_key,thread);
335 * iThread->create(); ( aka iThread->new() )
336 * Called in context of parent thread
340 Perl_ithread_create(pTHX_ SV *obj, char* classname, SV* init_function, SV* params)
343 CLONE_PARAMS clone_param;
345 MUTEX_LOCK(&create_destruct_mutex);
346 thread = PerlMemShared_malloc(sizeof(ithread));
347 Zero(thread,1,ithread);
348 thread->next = threads;
349 thread->prev = threads->prev;
350 thread->prev->next = thread;
351 /* Set count to 1 immediately in case thread exits before
352 * we return to caller !
355 MUTEX_INIT(&thread->mutex);
356 thread->tid = tid_counter++;
357 thread->gimme = GIMME_V;
358 thread->state = (thread->gimme == G_VOID) ? 1 : 0;
360 /* "Clone" our interpreter into the thread's interpreter
361 * This gives thread access to "static data" and code.
364 PerlIO_flush((PerlIO*)NULL);
367 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
369 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
371 /* perl_clone leaves us in new interpreter's context.
372 As it is tricky to spot implcit aTHX create a new scope
373 with aTHX matching the context for the duration of
374 our work for new interpreter.
377 dTHXa(thread->interp);
378 /* Here we remove END blocks since they should only run
379 in the thread they are created
381 SvREFCNT_dec(PL_endav);
383 clone_param.flags = 0;
384 thread->init_function = sv_dup(init_function, &clone_param);
385 if (SvREFCNT(thread->init_function) == 0) {
386 SvREFCNT_inc(thread->init_function);
389 thread->params = sv_dup(params, &clone_param);
390 SvREFCNT_inc(thread->params);
391 SvTEMP_off(thread->init_function);
392 ptr_table_free(PL_ptr_table);
396 PERL_SET_CONTEXT(aTHX);
398 /* Start the thread */
402 thread->handle = CreateThread(NULL, 0, Perl_ithread_run,
403 (LPVOID)thread, 0, &thread->thr);
407 static pthread_attr_t attr;
408 static int attr_inited = 0;
409 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
412 pthread_attr_init(&attr);
414 # ifdef PTHREAD_ATTR_SETDETACHSTATE
415 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
417 # ifdef THREAD_CREATE_NEEDS_STACK
418 if(pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK))
419 croak("panic: pthread_attr_setstacksize failed");
422 #ifdef OLD_PTHREADS_API
423 pthread_create( &thread->thr, attr, Perl_ithread_run, (void *)thread);
425 pthread_create( &thread->thr, &attr, Perl_ithread_run, (void *)thread);
431 MUTEX_UNLOCK(&create_destruct_mutex);
432 return ithread_to_SV(aTHX_ obj, thread, classname, FALSE);
436 Perl_ithread_self (pTHX_ SV *obj, char* Class)
439 PERL_THREAD_GETSPECIFIC(self_key,thread);
440 return ithread_to_SV(aTHX_ obj, thread, Class, TRUE);
444 * Joins the thread this code needs to take the returnvalue from the
445 * call_sv and send it back
449 Perl_ithread_CLONE(pTHX_ SV *obj)
453 ithread *thread = SV_to_ithread(aTHX_ obj);
457 Perl_warn(aTHX_ "CLONE %_",obj);
462 Perl_ithread_join(pTHX_ SV *obj)
464 ithread *thread = SV_to_ithread(aTHX_ obj);
465 MUTEX_LOCK(&thread->mutex);
466 if (thread->state & PERL_ITHR_DETACHED) {
467 MUTEX_UNLOCK(&thread->mutex);
468 Perl_croak(aTHX_ "Cannot join a detached thread");
470 else if (thread->state & PERL_ITHR_JOINED) {
471 MUTEX_UNLOCK(&thread->mutex);
472 Perl_croak(aTHX_ "Thread already joined");
481 MUTEX_UNLOCK(&thread->mutex);
483 waitcode = WaitForSingleObject(thread->handle, INFINITE);
485 pthread_join(thread->thr,&retval);
487 MUTEX_LOCK(&thread->mutex);
489 /* sv_dup over the args */
491 AV* params = (AV*) SvRV(thread->params);
492 CLONE_PARAMS clone_params;
493 clone_params.stashes = newAV();
494 PL_ptr_table = ptr_table_new();
495 retparam = (AV*) sv_dup((SV*)params, &clone_params);
496 SvREFCNT_dec(clone_params.stashes);
497 SvREFCNT_inc(retparam);
498 ptr_table_free(PL_ptr_table);
502 /* We have finished with it */
503 thread->state |= PERL_ITHR_JOINED;
504 MUTEX_UNLOCK(&thread->mutex);
505 sv_unmagic(SvRV(obj),PERL_MAGIC_shared_scalar);
506 Perl_ithread_destruct(aTHX_ thread, "joined");
513 Perl_ithread_DESTROY(pTHX_ SV *sv)
515 ithread *thread = SV_to_ithread(aTHX_ sv);
516 sv_unmagic(SvRV(sv),PERL_MAGIC_shared_scalar);
521 MODULE = threads PACKAGE = threads PREFIX = ithread_
525 ithread_new (classname, function_to_call, ...)
527 SV * function_to_call
530 AV* params = newAV();
533 for(i = 2; i < items ; i++) {
534 av_push(params, ST(i));
537 ST(0) = sv_2mortal(Perl_ithread_create(aTHX_ Nullsv, classname, function_to_call, newRV_noinc((SV*) params)));
542 ithread_self(char *classname)
545 ST(0) = sv_2mortal(Perl_ithread_self(aTHX_ Nullsv,classname));
550 ithread_tid(ithread *thread)
553 ithread_join(SV *obj)
556 AV* params = Perl_ithread_join(aTHX_ obj);
558 I32 len = AvFILL(params);
559 for (i = 0; i <= len; i++) {
560 XPUSHs(av_shift(params));
562 SvREFCNT_dec(params);
567 ithread_detach(ithread *thread)
570 ithread_DESTROY(SV *thread)
575 PL_perl_destruct_level = 2;
576 PERL_THREAD_ALLOC_SPECIFIC(self_key);
577 MUTEX_INIT(&create_destruct_mutex);
578 MUTEX_LOCK(&create_destruct_mutex);
579 PL_threadhook = &Perl_ithread_hook;
580 thread = PerlMemShared_malloc(sizeof(ithread));
581 Zero(thread,1,ithread);
582 PL_perl_destruct_level = 2;
583 MUTEX_INIT(&thread->mutex);
585 thread->next = thread;
586 thread->prev = thread;
587 thread->interp = aTHX;
588 thread->count = 1; /* imortal */
589 thread->tid = tid_counter++;
594 thread->thr = GetCurrentThreadId();
596 thread->thr = pthread_self();
599 PERL_THREAD_SETSPECIFIC(self_key,thread);
600 MUTEX_UNLOCK(&create_destruct_mutex);