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 PERL_SET_CONTEXT(thread->interp);
127 perl_destruct(thread->interp);
128 perl_free(thread->interp);
129 thread->interp = NULL;
131 PERL_SET_CONTEXT(aTHX);
132 MUTEX_UNLOCK(&thread->mutex);
136 Perl_ithread_hook(pTHX)
138 int veto_cleanup = 0;
139 MUTEX_LOCK(&create_destruct_mutex);
140 if (aTHX == PL_curinterp && active_threads != 1) {
141 Perl_warn(aTHX_ "Cleanup skipped %d active threads", active_threads);
144 MUTEX_UNLOCK(&create_destruct_mutex);
149 Perl_ithread_detach(pTHX_ ithread *thread)
151 MUTEX_LOCK(&thread->mutex);
152 if (!(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) {
153 thread->state |= PERL_ITHR_DETACHED;
155 CloseHandle(thread->handle);
158 PERL_THREAD_DETACH(thread->thr);
161 if ((thread->state & PERL_ITHR_FINISHED) &&
162 (thread->state & PERL_ITHR_DETACHED)) {
163 MUTEX_UNLOCK(&thread->mutex);
164 Perl_ithread_destruct(aTHX_ thread, "detach");
167 MUTEX_UNLOCK(&thread->mutex);
171 /* MAGIC (in mg.h sense) hooks */
174 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
176 ithread *thread = (ithread *) mg->mg_ptr;
177 SvIVX(sv) = PTR2IV(thread);
183 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
185 ithread *thread = (ithread *) mg->mg_ptr;
186 MUTEX_LOCK(&thread->mutex);
188 if (thread->count == 0) {
189 if (!(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) {
190 Perl_warn(aTHX_ "Implicit detach");
192 MUTEX_UNLOCK(&thread->mutex);
193 Perl_ithread_detach(aTHX_ thread);
196 MUTEX_UNLOCK(&thread->mutex);
202 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
204 ithread *thread = (ithread *) mg->mg_ptr;
205 MUTEX_LOCK(&thread->mutex);
207 MUTEX_UNLOCK(&thread->mutex);
211 MGVTBL ithread_vtbl = {
212 ithread_mg_get, /* get */
216 ithread_mg_free, /* free */
218 ithread_mg_dup /* dup */
223 * Starts executing the thread. Needs to clean up memory a tad better.
224 * Passed as the C level function to run in the new thread
229 Perl_ithread_run(LPVOID arg) {
232 Perl_ithread_run(void * arg) {
234 ithread* thread = (ithread*) arg;
235 dTHXa(thread->interp);
236 PERL_SET_CONTEXT(thread->interp);
237 PERL_THREAD_SETSPECIFIC(self_key,thread);
240 /* Far from clear messing with ->thr child-side is a good idea */
241 MUTEX_LOCK(&thread->mutex);
243 thread->thr = GetCurrentThreadId();
245 thread->thr = pthread_self();
247 MUTEX_UNLOCK(&thread->mutex);
250 PL_perl_destruct_level = 2;
253 AV* params = (AV*) SvRV(thread->params);
254 I32 len = av_len(params)+1;
260 for(i = 0; i < len; i++) {
261 XPUSHs(av_shift(params));
264 len = call_sv(thread->init_function, thread->gimme|G_EVAL);
266 for (i=len-1; i >= 0; i--) {
268 av_store(params, i, SvREFCNT_inc(sv));
272 Perl_warn(aTHX_ "Died:%_",ERRSV);
276 SvREFCNT_dec(thread->init_function);
279 PerlIO_flush((PerlIO*)NULL);
280 MUTEX_LOCK(&create_destruct_mutex);
282 assert( active_threads >= 0 );
283 MUTEX_UNLOCK(&create_destruct_mutex);
284 MUTEX_LOCK(&thread->mutex);
285 thread->state |= PERL_ITHR_FINISHED;
287 if (thread->state & PERL_ITHR_DETACHED) {
288 MUTEX_UNLOCK(&thread->mutex);
289 SvREFCNT_dec(thread->params);
290 thread->params = Nullsv;
291 Perl_ithread_destruct(aTHX_ thread, "detached finish");
293 MUTEX_UNLOCK(&thread->mutex);
303 ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
308 MUTEX_LOCK(&thread->mutex);
310 MUTEX_UNLOCK(&thread->mutex);
314 sv = newSVrv(obj,classname);
315 sv_setiv(sv,PTR2IV(thread));
316 mg = sv_magicext(sv,Nullsv,PERL_MAGIC_shared_scalar,&ithread_vtbl,(char *)thread,0);
317 mg->mg_flags |= MGf_DUP;
323 SV_to_ithread(pTHX_ SV *sv)
328 thread = INT2PTR(ithread*, SvIV(SvRV(sv)));
332 PERL_THREAD_GETSPECIFIC(self_key,thread);
338 * iThread->create(); ( aka iThread->new() )
339 * Called in context of parent thread
343 Perl_ithread_create(pTHX_ SV *obj, char* classname, SV* init_function, SV* params)
346 CLONE_PARAMS clone_param;
348 MUTEX_LOCK(&create_destruct_mutex);
349 thread = PerlMemShared_malloc(sizeof(ithread));
350 Zero(thread,1,ithread);
351 thread->next = threads;
352 thread->prev = threads->prev;
353 threads->prev = thread;
354 thread->prev->next = thread;
355 /* Set count to 1 immediately in case thread exits before
356 * we return to caller !
359 MUTEX_INIT(&thread->mutex);
360 thread->tid = tid_counter++;
361 thread->gimme = GIMME_V;
362 thread->state = (thread->gimme == G_VOID) ? 1 : 0;
364 /* "Clone" our interpreter into the thread's interpreter
365 * This gives thread access to "static data" and code.
368 PerlIO_flush((PerlIO*)NULL);
371 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
373 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
375 /* perl_clone leaves us in new interpreter's context.
376 As it is tricky to spot implcit aTHX create a new scope
377 with aTHX matching the context for the duration of
378 our work for new interpreter.
381 dTHXa(thread->interp);
382 /* Here we remove END blocks since they should only run
383 in the thread they are created
385 SvREFCNT_dec(PL_endav);
387 clone_param.flags = 0;
388 thread->init_function = sv_dup(init_function, &clone_param);
389 if (SvREFCNT(thread->init_function) == 0) {
390 SvREFCNT_inc(thread->init_function);
393 thread->params = sv_dup(params, &clone_param);
394 SvREFCNT_inc(thread->params);
395 SvTEMP_off(thread->init_function);
396 ptr_table_free(PL_ptr_table);
400 PERL_SET_CONTEXT(aTHX);
402 /* Start the thread */
406 thread->handle = CreateThread(NULL, 0, Perl_ithread_run,
407 (LPVOID)thread, 0, &thread->thr);
411 static pthread_attr_t attr;
412 static int attr_inited = 0;
413 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
416 pthread_attr_init(&attr);
418 # ifdef PTHREAD_ATTR_SETDETACHSTATE
419 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
421 # ifdef THREAD_CREATE_NEEDS_STACK
422 if(pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK))
423 croak("panic: pthread_attr_setstacksize failed");
426 #ifdef OLD_PTHREADS_API
427 pthread_create( &thread->thr, attr, Perl_ithread_run, (void *)thread);
429 pthread_create( &thread->thr, &attr, Perl_ithread_run, (void *)thread);
435 MUTEX_UNLOCK(&create_destruct_mutex);
436 return ithread_to_SV(aTHX_ obj, thread, classname, FALSE);
440 Perl_ithread_self (pTHX_ SV *obj, char* Class)
443 PERL_THREAD_GETSPECIFIC(self_key,thread);
444 return ithread_to_SV(aTHX_ obj, thread, Class, TRUE);
448 * Joins the thread this code needs to take the returnvalue from the
449 * call_sv and send it back
453 Perl_ithread_CLONE(pTHX_ SV *obj)
457 ithread *thread = SV_to_ithread(aTHX_ obj);
461 Perl_warn(aTHX_ "CLONE %_",obj);
466 Perl_ithread_join(pTHX_ SV *obj)
468 ithread *thread = SV_to_ithread(aTHX_ obj);
469 MUTEX_LOCK(&thread->mutex);
470 if (thread->state & PERL_ITHR_DETACHED) {
471 MUTEX_UNLOCK(&thread->mutex);
472 Perl_croak(aTHX_ "Cannot join a detached thread");
474 else if (thread->state & PERL_ITHR_JOINED) {
475 MUTEX_UNLOCK(&thread->mutex);
476 Perl_croak(aTHX_ "Thread already joined");
485 MUTEX_UNLOCK(&thread->mutex);
487 waitcode = WaitForSingleObject(thread->handle, INFINITE);
489 pthread_join(thread->thr,&retval);
491 MUTEX_LOCK(&thread->mutex);
493 /* sv_dup over the args */
495 AV* params = (AV*) SvRV(thread->params);
496 CLONE_PARAMS clone_params;
497 clone_params.stashes = newAV();
498 PL_ptr_table = ptr_table_new();
499 retparam = (AV*) sv_dup((SV*)params, &clone_params);
500 SvREFCNT_dec(clone_params.stashes);
501 SvREFCNT_inc(retparam);
502 ptr_table_free(PL_ptr_table);
506 /* We have finished with it */
507 thread->state |= PERL_ITHR_JOINED;
508 MUTEX_UNLOCK(&thread->mutex);
509 sv_unmagic(SvRV(obj),PERL_MAGIC_shared_scalar);
510 Perl_ithread_destruct(aTHX_ thread, "joined");
517 Perl_ithread_DESTROY(pTHX_ SV *sv)
519 ithread *thread = SV_to_ithread(aTHX_ sv);
520 sv_unmagic(SvRV(sv),PERL_MAGIC_shared_scalar);
523 #endif /* USE_ITHREADS */
525 MODULE = threads PACKAGE = threads PREFIX = ithread_
531 ithread_new (classname, function_to_call, ...)
533 SV * function_to_call
536 AV* params = newAV();
539 for(i = 2; i < items ; i++) {
540 av_push(params, ST(i));
543 ST(0) = sv_2mortal(Perl_ithread_create(aTHX_ Nullsv, classname, function_to_call, newRV_noinc((SV*) params)));
548 ithread_self(char *classname)
551 ST(0) = sv_2mortal(Perl_ithread_self(aTHX_ Nullsv,classname));
556 ithread_tid(ithread *thread)
559 ithread_join(SV *obj)
562 AV* params = Perl_ithread_join(aTHX_ obj);
564 I32 len = AvFILL(params);
565 for (i = 0; i <= len; i++) {
566 XPUSHs(av_shift(params));
568 SvREFCNT_dec(params);
572 ithread_yield(ithread *thread)
575 ithread_detach(ithread *thread)
578 ithread_DESTROY(SV *thread)
580 #endif /* USE_ITHREADS */
586 PL_perl_destruct_level = 2;
587 PERL_THREAD_ALLOC_SPECIFIC(self_key);
588 MUTEX_INIT(&create_destruct_mutex);
589 MUTEX_LOCK(&create_destruct_mutex);
590 PL_threadhook = &Perl_ithread_hook;
591 thread = PerlMemShared_malloc(sizeof(ithread));
592 Zero(thread,1,ithread);
593 PL_perl_destruct_level = 2;
594 MUTEX_INIT(&thread->mutex);
596 thread->next = thread;
597 thread->prev = thread;
598 thread->interp = aTHX;
599 thread->count = 1; /* imortal */
600 thread->tid = tid_counter++;
605 thread->thr = GetCurrentThreadId();
607 thread->thr = pthread_self();
610 PERL_THREAD_SETSPECIFIC(self_key,thread);
611 MUTEX_UNLOCK(&create_destruct_mutex);
612 #endif /* USE_ITHREADS */