squelch some more type mismatch warnings
[p5sagit/p5-mst-13.2.git] / ext / threads / threads.xs
CommitLineData
68795e93 1#define PERL_NO_GET_CONTEXT
2#include "EXTERN.h"
3#include "perl.h"
4#include "XSUB.h"
5
73e09c8f 6#ifdef USE_ITHREADS
7
68795e93 8#ifdef WIN32
9#include <windows.h>
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) \
14STMT_START {\
15 if((k = TlsAlloc()) == TLS_OUT_OF_INDEXES) {\
16 PerlIO_printf(PerlIO_stderr(),"panic threads.h: TlsAlloc");\
17 exit(1);\
18 }\
19} STMT_END
20#else
21#include <pthread.h>
22#include <thread.h>
23
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");\
31 exit(1);\
32 }\
33} STMT_END
34#else
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");\
40 exit(1);\
41 }\
42} STMT_END
43#endif
44#endif
45
62375a60 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
51
68795e93 52typedef 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 */
62375a60 59 signed char state; /* are we detached ? */
a446a88f 60 int gimme; /* Context of create */
68795e93 61 SV* init_function; /* Code to run */
62 SV* params; /* args to pass function */
63#ifdef WIN32
64 DWORD thr; /* OS's idea if thread id */
65 HANDLE handle; /* OS's waitable handle */
66#else
67 pthread_t thr; /* OS's handle for the thread */
68#endif
69} ithread;
70
71ithread *threads;
72
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)
f9dff5f5 79#define ithread_yield(thread) (YIELD);
68795e93 80
58c2ef19 81static perl_mutex create_destruct_mutex; /* protects the creation and destruction of threads*/
68795e93 82
83I32 tid_counter = 0;
62375a60 84I32 known_threads = 0;
58c2ef19 85I32 active_threads = 0;
68795e93 86perl_key self_key;
87
88/*
89 * Clear up after thread is done with
90 */
91void
62375a60 92Perl_ithread_destruct (pTHX_ ithread* thread, const char *why)
68795e93 93{
94 MUTEX_LOCK(&thread->mutex);
62375a60 95 if (!thread->next) {
96 Perl_croak(aTHX_ "panic: destruct destroyed thread %p (%s)",thread, why);
97 }
68795e93 98 if (thread->count != 0) {
99 MUTEX_UNLOCK(&thread->mutex);
d1400e48 100 return;
68795e93 101 }
58c2ef19 102 MUTEX_LOCK(&create_destruct_mutex);
68795e93 103 /* Remove from circular list of threads */
104 if (thread->next == thread) {
105 /* last one should never get here ? */
106 threads = NULL;
107 }
108 else {
f42ad631 109 thread->next->prev = thread->prev;
110 thread->prev->next = thread->next;
68795e93 111 if (threads == thread) {
112 threads = thread->next;
113 }
62375a60 114 thread->next = NULL;
115 thread->prev = NULL;
68795e93 116 }
62375a60 117 known_threads--;
118 assert( known_threads >= 0 );
ba14dd9a 119#if 0
62375a60 120 Perl_warn(aTHX_ "destruct %d @ %p by %p now %d",
121 thread->tid,thread->interp,aTHX, known_threads);
ba14dd9a 122#endif
62375a60 123 MUTEX_UNLOCK(&create_destruct_mutex);
124 /* Thread is now disowned */
68795e93 125 if (thread->interp) {
1c3adb19 126 dTHXa(thread->interp);
68795e93 127 PERL_SET_CONTEXT(thread->interp);
1c3adb19 128 SvREFCNT_dec(thread->params);
129 thread->params = Nullsv;
68795e93 130 perl_destruct(thread->interp);
131 perl_free(thread->interp);
132 thread->interp = NULL;
133 }
134 PERL_SET_CONTEXT(aTHX);
d1400e48 135 MUTEX_UNLOCK(&thread->mutex);
1c3adb19 136 MUTEX_DESTROY(&thread->mutex);
137 PerlMemShared_free(thread);
68795e93 138}
139
62375a60 140int
141Perl_ithread_hook(pTHX)
142{
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);
147 veto_cleanup = 1;
148 }
149 MUTEX_UNLOCK(&create_destruct_mutex);
150 return veto_cleanup;
151}
152
153void
154Perl_ithread_detach(pTHX_ ithread *thread)
155{
156 MUTEX_LOCK(&thread->mutex);
157 if (!(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) {
158 thread->state |= PERL_ITHR_DETACHED;
159#ifdef WIN32
160 CloseHandle(thread->handle);
161 thread->handle = 0;
162#else
163 PERL_THREAD_DETACH(thread->thr);
164#endif
165 }
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");
170 }
171 else {
172 MUTEX_UNLOCK(&thread->mutex);
173 }
174}
68795e93 175
176/* MAGIC (in mg.h sense) hooks */
177
178int
179ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
180{
181 ithread *thread = (ithread *) mg->mg_ptr;
182 SvIVX(sv) = PTR2IV(thread);
183 SvIOK_on(sv);
184 return 0;
185}
186
187int
188ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
189{
190 ithread *thread = (ithread *) mg->mg_ptr;
191 MUTEX_LOCK(&thread->mutex);
68795e93 192 thread->count--;
62375a60 193 if (thread->count == 0) {
1c3adb19 194 if(thread->state & PERL_ITHR_FINISHED &&
195 (thread->state & PERL_ITHR_DETACHED ||
196 thread->state & PERL_ITHR_JOINED))
197 {
198 MUTEX_UNLOCK(&thread->mutex);
199 Perl_ithread_destruct(aTHX_ thread, "no reference");
200 }
201 MUTEX_UNLOCK(&thread->mutex);
62375a60 202 }
203 else {
204 MUTEX_UNLOCK(&thread->mutex);
205 }
68795e93 206 return 0;
207}
208
209int
210ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
211{
212 ithread *thread = (ithread *) mg->mg_ptr;
213 MUTEX_LOCK(&thread->mutex);
68795e93 214 thread->count++;
215 MUTEX_UNLOCK(&thread->mutex);
216 return 0;
217}
218
219MGVTBL ithread_vtbl = {
220 ithread_mg_get, /* get */
221 0, /* set */
222 0, /* len */
223 0, /* clear */
224 ithread_mg_free, /* free */
225 0, /* copy */
226 ithread_mg_dup /* dup */
227};
228
47ba8780 229
47ba8780 230/*
b1edfb69 231 * Starts executing the thread. Needs to clean up memory a tad better.
68795e93 232 * Passed as the C level function to run in the new thread
b1edfb69 233 */
47ba8780 234
235#ifdef WIN32
68795e93 236THREAD_RET_TYPE
237Perl_ithread_run(LPVOID arg) {
47ba8780 238#else
68795e93 239void*
240Perl_ithread_run(void * arg) {
47ba8780 241#endif
5b414d21 242 ithread* thread = (ithread*) arg;
47ba8780 243 dTHXa(thread->interp);
47ba8780 244 PERL_SET_CONTEXT(thread->interp);
68795e93 245 PERL_THREAD_SETSPECIFIC(self_key,thread);
47ba8780 246
68795e93 247#if 0
248 /* Far from clear messing with ->thr child-side is a good idea */
249 MUTEX_LOCK(&thread->mutex);
47ba8780 250#ifdef WIN32
251 thread->thr = GetCurrentThreadId();
252#else
253 thread->thr = pthread_self();
254#endif
68795e93 255 MUTEX_UNLOCK(&thread->mutex);
256#endif
47ba8780 257
47ba8780 258 PL_perl_destruct_level = 2;
4f896ddc 259
47ba8780 260 {
68795e93 261 AV* params = (AV*) SvRV(thread->params);
262 I32 len = av_len(params)+1;
47ba8780 263 int i;
264 dSP;
47ba8780 265 ENTER;
266 SAVETMPS;
267 PUSHMARK(SP);
68795e93 268 for(i = 0; i < len; i++) {
269 XPUSHs(av_shift(params));
47ba8780 270 }
271 PUTBACK;
a446a88f 272 len = call_sv(thread->init_function, thread->gimme|G_EVAL);
68795e93 273 SPAGAIN;
a446a88f 274 for (i=len-1; i >= 0; i--) {
e1c44605 275 SV *sv = POPs;
276 av_store(params, i, SvREFCNT_inc(sv));
a446a88f 277 }
278 PUTBACK;
279 if (SvTRUE(ERRSV)) {
280 Perl_warn(aTHX_ "Died:%_",ERRSV);
281 }
47ba8780 282 FREETMPS;
283 LEAVE;
68795e93 284 SvREFCNT_dec(thread->init_function);
47ba8780 285 }
286
fd58862f 287 PerlIO_flush((PerlIO*)NULL);
62375a60 288 MUTEX_LOCK(&create_destruct_mutex);
289 active_threads--;
290 assert( active_threads >= 0 );
291 MUTEX_UNLOCK(&create_destruct_mutex);
68795e93 292 MUTEX_LOCK(&thread->mutex);
62375a60 293 thread->state |= PERL_ITHR_FINISHED;
294
295 if (thread->state & PERL_ITHR_DETACHED) {
47ba8780 296 MUTEX_UNLOCK(&thread->mutex);
62375a60 297 Perl_ithread_destruct(aTHX_ thread, "detached finish");
47ba8780 298 } else {
62375a60 299 MUTEX_UNLOCK(&thread->mutex);
300 }
47ba8780 301#ifdef WIN32
302 return (DWORD)0;
e8f2bb9a 303#else
304 return 0;
47ba8780 305#endif
68795e93 306}
307
308SV *
309ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
310{
311 SV *sv;
312 MAGIC *mg;
313 if (inc) {
314 MUTEX_LOCK(&thread->mutex);
315 thread->count++;
68795e93 316 MUTEX_UNLOCK(&thread->mutex);
317 }
318 if (!obj)
319 obj = newSV(0);
320 sv = newSVrv(obj,classname);
321 sv_setiv(sv,PTR2IV(thread));
322 mg = sv_magicext(sv,Nullsv,PERL_MAGIC_shared_scalar,&ithread_vtbl,(char *)thread,0);
323 mg->mg_flags |= MGf_DUP;
324 SvREADONLY_on(sv);
325 return obj;
326}
47ba8780 327
68795e93 328ithread *
329SV_to_ithread(pTHX_ SV *sv)
330{
331 ithread *thread;
332 if (SvROK(sv))
333 {
334 thread = INT2PTR(ithread*, SvIV(SvRV(sv)));
335 }
336 else
337 {
338 PERL_THREAD_GETSPECIFIC(self_key,thread);
339 }
340 return thread;
47ba8780 341}
342
47ba8780 343/*
68795e93 344 * iThread->create(); ( aka iThread->new() )
345 * Called in context of parent thread
b1edfb69 346 */
47ba8780 347
68795e93 348SV *
349Perl_ithread_create(pTHX_ SV *obj, char* classname, SV* init_function, SV* params)
350{
351 ithread* thread;
352 CLONE_PARAMS clone_param;
353
58c2ef19 354 MUTEX_LOCK(&create_destruct_mutex);
68795e93 355 thread = PerlMemShared_malloc(sizeof(ithread));
356 Zero(thread,1,ithread);
357 thread->next = threads;
358 thread->prev = threads->prev;
f42ad631 359 threads->prev = thread;
68795e93 360 thread->prev->next = thread;
361 /* Set count to 1 immediately in case thread exits before
362 * we return to caller !
363 */
364 thread->count = 1;
365 MUTEX_INIT(&thread->mutex);
366 thread->tid = tid_counter++;
a446a88f 367 thread->gimme = GIMME_V;
62375a60 368 thread->state = (thread->gimme == G_VOID) ? 1 : 0;
4f896ddc 369
68795e93 370 /* "Clone" our interpreter into the thread's interpreter
371 * This gives thread access to "static data" and code.
372 */
47ba8780 373
68795e93 374 PerlIO_flush((PerlIO*)NULL);
cd8c9bf8 375
47ba8780 376#ifdef WIN32
68795e93 377 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
47ba8780 378#else
68795e93 379 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
47ba8780 380#endif
ba14dd9a 381 /* perl_clone leaves us in new interpreter's context.
382 As it is tricky to spot implcit aTHX create a new scope
a446a88f 383 with aTHX matching the context for the duration of
ba14dd9a 384 our work for new interpreter.
385 */
386 {
387 dTHXa(thread->interp);
58c2ef19 388 /* Here we remove END blocks since they should only run
62375a60 389 in the thread they are created
58c2ef19 390 */
391 SvREFCNT_dec(PL_endav);
392 PL_endav = newAV();
d1400e48 393 clone_param.flags = 0;
ba14dd9a 394 thread->init_function = sv_dup(init_function, &clone_param);
395 if (SvREFCNT(thread->init_function) == 0) {
396 SvREFCNT_inc(thread->init_function);
d1400e48 397 }
ba14dd9a 398
399 thread->params = sv_dup(params, &clone_param);
400 SvREFCNT_inc(thread->params);
401 SvTEMP_off(thread->init_function);
402 ptr_table_free(PL_ptr_table);
403 PL_ptr_table = NULL;
404 }
d1400e48 405
68795e93 406 PERL_SET_CONTEXT(aTHX);
47ba8780 407
68795e93 408 /* Start the thread */
47ba8780 409
410#ifdef WIN32
411
68795e93 412 thread->handle = CreateThread(NULL, 0, Perl_ithread_run,
47ba8780 413 (LPVOID)thread, 0, &thread->thr);
414
82c40bf6 415#else
fa26028c 416 {
417 static pthread_attr_t attr;
418 static int attr_inited = 0;
fa26028c 419 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
420 if (!attr_inited) {
421 attr_inited = 1;
422 pthread_attr_init(&attr);
423 }
424# ifdef PTHREAD_ATTR_SETDETACHSTATE
425 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
426# endif
3eb37d38 427# ifdef THREAD_CREATE_NEEDS_STACK
428 if(pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK))
429 croak("panic: pthread_attr_setstacksize failed");
430# endif
431
3ad0b7d6 432#ifdef OLD_PTHREADS_API
68795e93 433 pthread_create( &thread->thr, attr, Perl_ithread_run, (void *)thread);
47ba8780 434#else
68795e93 435 pthread_create( &thread->thr, &attr, Perl_ithread_run, (void *)thread);
47ba8780 436#endif
3ad0b7d6 437 }
82c40bf6 438#endif
62375a60 439 known_threads++;
58c2ef19 440 active_threads++;
441 MUTEX_UNLOCK(&create_destruct_mutex);
95393226 442 sv_2mortal(params);
68795e93 443 return ithread_to_SV(aTHX_ obj, thread, classname, FALSE);
444}
47ba8780 445
68795e93 446SV*
447Perl_ithread_self (pTHX_ SV *obj, char* Class)
448{
449 ithread *thread;
450 PERL_THREAD_GETSPECIFIC(self_key,thread);
451 return ithread_to_SV(aTHX_ obj, thread, Class, TRUE);
47ba8780 452}
453
454/*
e1c44605 455 * Joins the thread this code needs to take the returnvalue from the
68795e93 456 * call_sv and send it back
b1edfb69 457 */
47ba8780 458
68795e93 459void
460Perl_ithread_CLONE(pTHX_ SV *obj)
461{
462 if (SvROK(obj))
463 {
464 ithread *thread = SV_to_ithread(aTHX_ obj);
465 }
466 else
467 {
468 Perl_warn(aTHX_ "CLONE %_",obj);
469 }
47ba8780 470}
471
62375a60 472AV*
68795e93 473Perl_ithread_join(pTHX_ SV *obj)
474{
475 ithread *thread = SV_to_ithread(aTHX_ obj);
476 MUTEX_LOCK(&thread->mutex);
62375a60 477 if (thread->state & PERL_ITHR_DETACHED) {
a446a88f 478 MUTEX_UNLOCK(&thread->mutex);
479 Perl_croak(aTHX_ "Cannot join a detached thread");
480 }
62375a60 481 else if (thread->state & PERL_ITHR_JOINED) {
a446a88f 482 MUTEX_UNLOCK(&thread->mutex);
483 Perl_croak(aTHX_ "Thread already joined");
484 }
485 else {
e1c44605 486 AV* retparam;
47ba8780 487#ifdef WIN32
488 DWORD waitcode;
47ba8780 489#else
490 void *retval;
47ba8780 491#endif
47ba8780 492 MUTEX_UNLOCK(&thread->mutex);
68795e93 493#ifdef WIN32
494 waitcode = WaitForSingleObject(thread->handle, INFINITE);
495#else
496 pthread_join(thread->thr,&retval);
497#endif
47ba8780 498 MUTEX_LOCK(&thread->mutex);
e1c44605 499
62375a60 500 /* sv_dup over the args */
e1c44605 501 {
62375a60 502 AV* params = (AV*) SvRV(thread->params);
e1c44605 503 CLONE_PARAMS clone_params;
3275ba96 504 clone_params.stashes = newAV();
e1c44605 505 PL_ptr_table = ptr_table_new();
506 retparam = (AV*) sv_dup((SV*)params, &clone_params);
3275ba96 507 SvREFCNT_dec(clone_params.stashes);
e1c44605 508 SvREFCNT_inc(retparam);
509 ptr_table_free(PL_ptr_table);
510 PL_ptr_table = NULL;
511
512 }
a446a88f 513 /* We have finished with it */
62375a60 514 thread->state |= PERL_ITHR_JOINED;
47ba8780 515 MUTEX_UNLOCK(&thread->mutex);
e1c44605 516 return retparam;
68795e93 517 }
e1c44605 518 return (AV*)NULL;
47ba8780 519}
520
68795e93 521void
68795e93 522Perl_ithread_DESTROY(pTHX_ SV *sv)
523{
524 ithread *thread = SV_to_ithread(aTHX_ sv);
68795e93 525 sv_unmagic(SvRV(sv),PERL_MAGIC_shared_scalar);
526}
8222d950 527
73e09c8f 528#endif /* USE_ITHREADS */
e1c44605 529
68795e93 530MODULE = threads PACKAGE = threads PREFIX = ithread_
531PROTOTYPES: DISABLE
8222d950 532
73e09c8f 533#ifdef USE_ITHREADS
534
68795e93 535void
536ithread_new (classname, function_to_call, ...)
537char * classname
538SV * function_to_call
539CODE:
540{
541 AV* params = newAV();
542 if (items > 2) {
543 int i;
544 for(i = 2; i < items ; i++) {
95393226 545 av_push(params, SvREFCNT_inc(ST(i)));
68795e93 546 }
547 }
548 ST(0) = sv_2mortal(Perl_ithread_create(aTHX_ Nullsv, classname, function_to_call, newRV_noinc((SV*) params)));
549 XSRETURN(1);
550}
8222d950 551
68795e93 552void
553ithread_self(char *classname)
554CODE:
555{
556 ST(0) = sv_2mortal(Perl_ithread_self(aTHX_ Nullsv,classname));
557 XSRETURN(1);
558}
47ba8780 559
560int
68795e93 561ithread_tid(ithread *thread)
47ba8780 562
563void
68795e93 564ithread_join(SV *obj)
e1c44605 565PPCODE:
566{
567 AV* params = Perl_ithread_join(aTHX_ obj);
568 int i;
569 I32 len = AvFILL(params);
570 for (i = 0; i <= len; i++) {
1c3adb19 571 SV* tmp = av_shift(params);
572 XPUSHs(tmp);
573 sv_2mortal(tmp);
e1c44605 574 }
575 SvREFCNT_dec(params);
576}
577
f9dff5f5 578void
579ithread_yield(ithread *thread)
47ba8780 580
581void
68795e93 582ithread_detach(ithread *thread)
47ba8780 583
47ba8780 584void
68795e93 585ithread_DESTROY(SV *thread)
586
73e09c8f 587#endif /* USE_ITHREADS */
588
68795e93 589BOOT:
590{
73e09c8f 591#ifdef USE_ITHREADS
68795e93 592 ithread* thread;
e1c44605 593 PL_perl_destruct_level = 2;
68795e93 594 PERL_THREAD_ALLOC_SPECIFIC(self_key);
58c2ef19 595 MUTEX_INIT(&create_destruct_mutex);
596 MUTEX_LOCK(&create_destruct_mutex);
62375a60 597 PL_threadhook = &Perl_ithread_hook;
68795e93 598 thread = PerlMemShared_malloc(sizeof(ithread));
599 Zero(thread,1,ithread);
600 PL_perl_destruct_level = 2;
601 MUTEX_INIT(&thread->mutex);
602 threads = thread;
603 thread->next = thread;
604 thread->prev = thread;
605 thread->interp = aTHX;
606 thread->count = 1; /* imortal */
607 thread->tid = tid_counter++;
62375a60 608 known_threads++;
58c2ef19 609 active_threads++;
62375a60 610 thread->state = 1;
68795e93 611#ifdef WIN32
612 thread->thr = GetCurrentThreadId();
613#else
614 thread->thr = pthread_self();
615#endif
62375a60 616
68795e93 617 PERL_THREAD_SETSPECIFIC(self_key,thread);
58c2ef19 618 MUTEX_UNLOCK(&create_destruct_mutex);
73e09c8f 619#endif /* USE_ITHREADS */
68795e93 620}
621