threads.xs doesn't check the return value of the thread creation
[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
c05ae023 8
68795e93 9#ifdef WIN32
10#include <windows.h>
11#include <win32thread.h>
68795e93 12#else
5c728af0 13#ifdef OS2
14typedef perl_os_thread pthread_t;
15#else
68795e93 16#include <pthread.h>
5c728af0 17#endif
68795e93 18#include <thread.h>
68795e93 19#define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
20#ifdef OLD_PTHREADS_API
21#define PERL_THREAD_DETACH(t) pthread_detach(&(t))
68795e93 22#else
23#define PERL_THREAD_DETACH(t) pthread_detach((t))
c05ae023 24#endif /* OLD_PTHREADS_API */
467f3f08 25#endif
68795e93 26
c05ae023 27
28
29
62375a60 30/* Values for 'state' member */
31#define PERL_ITHR_JOINABLE 0
32#define PERL_ITHR_DETACHED 1
33#define PERL_ITHR_FINISHED 4
34#define PERL_ITHR_JOINED 2
35
68795e93 36typedef struct ithread_s {
6dfd2d05 37 struct ithread_s *next; /* Next thread in the list */
38 struct ithread_s *prev; /* Prev thread in the list */
68795e93 39 PerlInterpreter *interp; /* The threads interpreter */
6dfd2d05 40 I32 tid; /* Threads module's thread id */
41 perl_mutex mutex; /* Mutex for updating things in this struct */
42 I32 count; /* How many SVs have a reference to us */
43 signed char state; /* Are we detached ? */
a446a88f 44 int gimme; /* Context of create */
68795e93 45 SV* init_function; /* Code to run */
6dfd2d05 46 SV* params; /* Args to pass function */
68795e93 47#ifdef WIN32
48 DWORD thr; /* OS's idea if thread id */
49 HANDLE handle; /* OS's waitable handle */
50#else
51 pthread_t thr; /* OS's handle for the thread */
52#endif
53} ithread;
54
55ithread *threads;
56
57/* Macros to supply the aTHX_ in an embed.h like manner */
58#define ithread_join(thread) Perl_ithread_join(aTHX_ thread)
59#define ithread_DESTROY(thread) Perl_ithread_DESTROY(aTHX_ thread)
60#define ithread_CLONE(thread) Perl_ithread_CLONE(aTHX_ thread)
61#define ithread_detach(thread) Perl_ithread_detach(aTHX_ thread)
62#define ithread_tid(thread) ((thread)->tid)
f9dff5f5 63#define ithread_yield(thread) (YIELD);
68795e93 64
58c2ef19 65static perl_mutex create_destruct_mutex; /* protects the creation and destruction of threads*/
68795e93 66
67I32 tid_counter = 0;
62375a60 68I32 known_threads = 0;
58c2ef19 69I32 active_threads = 0;
c05ae023 70
71
72void Perl_ithread_set (pTHX_ ithread* thread)
73{
08ef4a90 74 SV* thread_sv = newSViv(PTR2IV(thread));
c05ae023 75 if(!hv_store(PL_modglobal, "threads::self", 12, thread_sv,0)) {
76 croak("%s\n","Internal error, couldn't set TLS");
77 }
78}
79
80ithread* Perl_ithread_get (pTHX) {
81 SV** thread_sv = hv_fetch(PL_modglobal, "threads::self",12,0);
82 if(!thread_sv) {
83 croak("%s\n","Internal error, couldn't get TLS");
84 }
08ef4a90 85 return INT2PTR(ithread*,SvIV(*thread_sv));
c05ae023 86}
87
88
68795e93 89
90/*
91 * Clear up after thread is done with
92 */
93void
62375a60 94Perl_ithread_destruct (pTHX_ ithread* thread, const char *why)
68795e93 95{
669f4df4 96 PerlInterpreter *freeperl = NULL;
68795e93 97 MUTEX_LOCK(&thread->mutex);
62375a60 98 if (!thread->next) {
99 Perl_croak(aTHX_ "panic: destruct destroyed thread %p (%s)",thread, why);
100 }
68795e93 101 if (thread->count != 0) {
102 MUTEX_UNLOCK(&thread->mutex);
d1400e48 103 return;
68795e93 104 }
58c2ef19 105 MUTEX_LOCK(&create_destruct_mutex);
68795e93 106 /* Remove from circular list of threads */
107 if (thread->next == thread) {
108 /* last one should never get here ? */
109 threads = NULL;
110 }
111 else {
f42ad631 112 thread->next->prev = thread->prev;
113 thread->prev->next = thread->next;
68795e93 114 if (threads == thread) {
115 threads = thread->next;
116 }
62375a60 117 thread->next = NULL;
118 thread->prev = NULL;
68795e93 119 }
62375a60 120 known_threads--;
121 assert( known_threads >= 0 );
ba14dd9a 122#if 0
62375a60 123 Perl_warn(aTHX_ "destruct %d @ %p by %p now %d",
124 thread->tid,thread->interp,aTHX, known_threads);
ba14dd9a 125#endif
62375a60 126 MUTEX_UNLOCK(&create_destruct_mutex);
127 /* Thread is now disowned */
c2f2a82b 128
129 if(thread->interp) {
1c3adb19 130 dTHXa(thread->interp);
c2f2a82b 131 ithread* current_thread;
3e79ab2c 132#ifdef OEMVS
133 void *ptr;
134#endif
68795e93 135 PERL_SET_CONTEXT(thread->interp);
c05ae023 136 current_thread = Perl_ithread_get(aTHX);
137 Perl_ithread_set(aTHX_ thread);
138
3b1c3273 139
140
141
1c3adb19 142 SvREFCNT_dec(thread->params);
3b1c3273 143
144
145
1c3adb19 146 thread->params = Nullsv;
c2f2a82b 147 perl_destruct(thread->interp);
669f4df4 148 freeperl = thread->interp;
68795e93 149 thread->interp = NULL;
150 }
d1400e48 151 MUTEX_UNLOCK(&thread->mutex);
1c3adb19 152 MUTEX_DESTROY(&thread->mutex);
c7667023 153#ifdef WIN32
154 if (thread->handle)
155 CloseHandle(thread->handle);
156 thread->handle = 0;
157#endif
1c3adb19 158 PerlMemShared_free(thread);
669f4df4 159 if (freeperl)
160 perl_free(freeperl);
1d784c90 161
64821230 162 PERL_SET_CONTEXT(aTHX);
68795e93 163}
164
62375a60 165int
166Perl_ithread_hook(pTHX)
167{
168 int veto_cleanup = 0;
169 MUTEX_LOCK(&create_destruct_mutex);
170 if (aTHX == PL_curinterp && active_threads != 1) {
3c42a367 171 Perl_warn(aTHX_ "A thread exited while %" IVdf " threads were running",
436c6dd3 172 (IV)active_threads);
62375a60 173 veto_cleanup = 1;
174 }
175 MUTEX_UNLOCK(&create_destruct_mutex);
176 return veto_cleanup;
177}
178
179void
180Perl_ithread_detach(pTHX_ ithread *thread)
181{
182 MUTEX_LOCK(&thread->mutex);
183 if (!(thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED))) {
184 thread->state |= PERL_ITHR_DETACHED;
185#ifdef WIN32
186 CloseHandle(thread->handle);
187 thread->handle = 0;
188#else
189 PERL_THREAD_DETACH(thread->thr);
190#endif
191 }
192 if ((thread->state & PERL_ITHR_FINISHED) &&
193 (thread->state & PERL_ITHR_DETACHED)) {
194 MUTEX_UNLOCK(&thread->mutex);
195 Perl_ithread_destruct(aTHX_ thread, "detach");
196 }
197 else {
198 MUTEX_UNLOCK(&thread->mutex);
199 }
200}
68795e93 201
202/* MAGIC (in mg.h sense) hooks */
203
204int
205ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
206{
207 ithread *thread = (ithread *) mg->mg_ptr;
208 SvIVX(sv) = PTR2IV(thread);
209 SvIOK_on(sv);
210 return 0;
211}
212
213int
214ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
215{
216 ithread *thread = (ithread *) mg->mg_ptr;
217 MUTEX_LOCK(&thread->mutex);
68795e93 218 thread->count--;
62375a60 219 if (thread->count == 0) {
1c3adb19 220 if(thread->state & PERL_ITHR_FINISHED &&
221 (thread->state & PERL_ITHR_DETACHED ||
222 thread->state & PERL_ITHR_JOINED))
223 {
224 MUTEX_UNLOCK(&thread->mutex);
225 Perl_ithread_destruct(aTHX_ thread, "no reference");
226 }
1ea20f42 227 else {
228 MUTEX_UNLOCK(&thread->mutex);
229 }
62375a60 230 }
231 else {
232 MUTEX_UNLOCK(&thread->mutex);
233 }
68795e93 234 return 0;
235}
236
237int
238ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
239{
240 ithread *thread = (ithread *) mg->mg_ptr;
241 MUTEX_LOCK(&thread->mutex);
68795e93 242 thread->count++;
243 MUTEX_UNLOCK(&thread->mutex);
244 return 0;
245}
246
247MGVTBL ithread_vtbl = {
248 ithread_mg_get, /* get */
249 0, /* set */
250 0, /* len */
251 0, /* clear */
252 ithread_mg_free, /* free */
253 0, /* copy */
254 ithread_mg_dup /* dup */
255};
256
47ba8780 257
47ba8780 258/*
b1edfb69 259 * Starts executing the thread. Needs to clean up memory a tad better.
68795e93 260 * Passed as the C level function to run in the new thread
b1edfb69 261 */
47ba8780 262
263#ifdef WIN32
68795e93 264THREAD_RET_TYPE
265Perl_ithread_run(LPVOID arg) {
47ba8780 266#else
68795e93 267void*
268Perl_ithread_run(void * arg) {
47ba8780 269#endif
5b414d21 270 ithread* thread = (ithread*) arg;
47ba8780 271 dTHXa(thread->interp);
47ba8780 272 PERL_SET_CONTEXT(thread->interp);
c05ae023 273 Perl_ithread_set(aTHX_ thread);
47ba8780 274
68795e93 275#if 0
276 /* Far from clear messing with ->thr child-side is a good idea */
277 MUTEX_LOCK(&thread->mutex);
47ba8780 278#ifdef WIN32
279 thread->thr = GetCurrentThreadId();
280#else
281 thread->thr = pthread_self();
282#endif
68795e93 283 MUTEX_UNLOCK(&thread->mutex);
284#endif
47ba8780 285
47ba8780 286 PL_perl_destruct_level = 2;
4f896ddc 287
47ba8780 288 {
68795e93 289 AV* params = (AV*) SvRV(thread->params);
290 I32 len = av_len(params)+1;
47ba8780 291 int i;
292 dSP;
47ba8780 293 ENTER;
294 SAVETMPS;
295 PUSHMARK(SP);
68795e93 296 for(i = 0; i < len; i++) {
297 XPUSHs(av_shift(params));
47ba8780 298 }
299 PUTBACK;
a446a88f 300 len = call_sv(thread->init_function, thread->gimme|G_EVAL);
0405e91e 301
68795e93 302 SPAGAIN;
a446a88f 303 for (i=len-1; i >= 0; i--) {
e1c44605 304 SV *sv = POPs;
305 av_store(params, i, SvREFCNT_inc(sv));
a446a88f 306 }
a446a88f 307 if (SvTRUE(ERRSV)) {
6b3c7930 308 Perl_warn(aTHX_ "thread failed to start: %" SVf, ERRSV);
a446a88f 309 }
47ba8780 310 FREETMPS;
311 LEAVE;
68795e93 312 SvREFCNT_dec(thread->init_function);
47ba8780 313 }
314
fd58862f 315 PerlIO_flush((PerlIO*)NULL);
68795e93 316 MUTEX_LOCK(&thread->mutex);
62375a60 317 thread->state |= PERL_ITHR_FINISHED;
318
319 if (thread->state & PERL_ITHR_DETACHED) {
47ba8780 320 MUTEX_UNLOCK(&thread->mutex);
62375a60 321 Perl_ithread_destruct(aTHX_ thread, "detached finish");
47ba8780 322 } else {
62375a60 323 MUTEX_UNLOCK(&thread->mutex);
324 }
91604d21 325 MUTEX_LOCK(&create_destruct_mutex);
326 active_threads--;
327 assert( active_threads >= 0 );
328 MUTEX_UNLOCK(&create_destruct_mutex);
329
47ba8780 330#ifdef WIN32
331 return (DWORD)0;
e8f2bb9a 332#else
333 return 0;
47ba8780 334#endif
68795e93 335}
336
337SV *
338ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
339{
340 SV *sv;
341 MAGIC *mg;
342 if (inc) {
343 MUTEX_LOCK(&thread->mutex);
344 thread->count++;
68795e93 345 MUTEX_UNLOCK(&thread->mutex);
346 }
347 if (!obj)
348 obj = newSV(0);
349 sv = newSVrv(obj,classname);
350 sv_setiv(sv,PTR2IV(thread));
351 mg = sv_magicext(sv,Nullsv,PERL_MAGIC_shared_scalar,&ithread_vtbl,(char *)thread,0);
352 mg->mg_flags |= MGf_DUP;
353 SvREADONLY_on(sv);
354 return obj;
355}
47ba8780 356
68795e93 357ithread *
358SV_to_ithread(pTHX_ SV *sv)
359{
68795e93 360 if (SvROK(sv))
361 {
c05ae023 362 return INT2PTR(ithread*, SvIV(SvRV(sv)));
68795e93 363 }
364 else
365 {
c05ae023 366 return Perl_ithread_get(aTHX);
68795e93 367 }
47ba8780 368}
369
47ba8780 370/*
6dfd2d05 371 * ithread->create(); ( aka ithread->new() )
68795e93 372 * Called in context of parent thread
b1edfb69 373 */
47ba8780 374
68795e93 375SV *
376Perl_ithread_create(pTHX_ SV *obj, char* classname, SV* init_function, SV* params)
377{
378 ithread* thread;
379 CLONE_PARAMS clone_param;
c05ae023 380 ithread* current_thread = Perl_ithread_get(aTHX);
3b1c3273 381
382 SV** tmps_tmp = PL_tmps_stack;
383 I32 tmps_ix = PL_tmps_ix;
d94006e8 384#ifndef WIN32
385 int failure;
386 const char* panic = NULL;
387#endif
3b1c3273 388
c05ae023 389
58c2ef19 390 MUTEX_LOCK(&create_destruct_mutex);
68795e93 391 thread = PerlMemShared_malloc(sizeof(ithread));
392 Zero(thread,1,ithread);
393 thread->next = threads;
394 thread->prev = threads->prev;
f42ad631 395 threads->prev = thread;
68795e93 396 thread->prev->next = thread;
397 /* Set count to 1 immediately in case thread exits before
398 * we return to caller !
399 */
400 thread->count = 1;
401 MUTEX_INIT(&thread->mutex);
402 thread->tid = tid_counter++;
a446a88f 403 thread->gimme = GIMME_V;
4f896ddc 404
68795e93 405 /* "Clone" our interpreter into the thread's interpreter
406 * This gives thread access to "static data" and code.
407 */
47ba8780 408
68795e93 409 PerlIO_flush((PerlIO*)NULL);
c05ae023 410 Perl_ithread_set(aTHX_ thread);
3b1c3273 411
9c98058e 412 SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct
413 value */
414 PL_srand_called = FALSE; /* Set it to false so we can detect
415 if it gets set during the clone */
3b1c3273 416
47ba8780 417#ifdef WIN32
68795e93 418 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
47ba8780 419#else
68795e93 420 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
47ba8780 421#endif
ba14dd9a 422 /* perl_clone leaves us in new interpreter's context.
c8dae523 423 As it is tricky to spot an implicit aTHX, create a new scope
a446a88f 424 with aTHX matching the context for the duration of
ba14dd9a 425 our work for new interpreter.
426 */
427 {
428 dTHXa(thread->interp);
9c98058e 429
58c2ef19 430 /* Here we remove END blocks since they should only run
62375a60 431 in the thread they are created
58c2ef19 432 */
433 SvREFCNT_dec(PL_endav);
434 PL_endav = newAV();
d1400e48 435 clone_param.flags = 0;
ba14dd9a 436 thread->init_function = sv_dup(init_function, &clone_param);
437 if (SvREFCNT(thread->init_function) == 0) {
438 SvREFCNT_inc(thread->init_function);
d1400e48 439 }
3b1c3273 440
441
ba14dd9a 442
443 thread->params = sv_dup(params, &clone_param);
444 SvREFCNT_inc(thread->params);
3b1c3273 445
446
447 /* The code below checks that anything living on
448 the tmps stack and has been cloned (so it lives in the
449 ptr_table) has a refcount higher than 0
450
451 If the refcount is 0 it means that a something on the
452 stack/context was holding a reference to it and
453 since we init_stacks() in perl_clone that won't get
454 cleaned and we will get a leaked scalar.
455 The reason it was cloned was that it lived on the
456 @_ stack.
457
458 Example of this can be found in bugreport 15837
459 where calls in the parameter list end up as a temp
460
461 One could argue that this fix should be in perl_clone
462 */
463
464
465 while (tmps_ix > 0) {
466 SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
467 tmps_ix--;
468 if (sv && SvREFCNT(sv) == 0) {
469 SvREFCNT_inc(sv);
470 SvREFCNT_dec(sv);
471 }
472 }
473
474
475
ba14dd9a 476 SvTEMP_off(thread->init_function);
477 ptr_table_free(PL_ptr_table);
478 PL_ptr_table = NULL;
ffb29f90 479 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
ba14dd9a 480 }
c05ae023 481 Perl_ithread_set(aTHX_ current_thread);
68795e93 482 PERL_SET_CONTEXT(aTHX);
47ba8780 483
68795e93 484 /* Start the thread */
47ba8780 485
486#ifdef WIN32
68795e93 487 thread->handle = CreateThread(NULL, 0, Perl_ithread_run,
47ba8780 488 (LPVOID)thread, 0, &thread->thr);
82c40bf6 489#else
fa26028c 490 {
491 static pthread_attr_t attr;
492 static int attr_inited = 0;
fa26028c 493 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
494 if (!attr_inited) {
495 attr_inited = 1;
496 pthread_attr_init(&attr);
497 }
498# ifdef PTHREAD_ATTR_SETDETACHSTATE
499 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
500# endif
3eb37d38 501# ifdef THREAD_CREATE_NEEDS_STACK
502 if(pthread_attr_setstacksize(&attr, THREAD_CREATE_NEEDS_STACK))
d94006e8 503 panic = "panic: pthread_attr_setstacksize failed";
3eb37d38 504# endif
505
3ad0b7d6 506#ifdef OLD_PTHREADS_API
d94006e8 507 failure
508 = panic ? 1 : pthread_create( &thread->thr, attr,
509 Perl_ithread_run, (void *)thread);
47ba8780 510#else
58d975c3 511# if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
47cb5ff9 512 pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
19a077f6 513# endif
d94006e8 514 failure
515 = panic ? 1 : pthread_create( &thread->thr, &attr,
516 Perl_ithread_run, (void *)thread);
47ba8780 517#endif
3ad0b7d6 518 }
82c40bf6 519#endif
62375a60 520 known_threads++;
d94006e8 521 if (
522#ifdef WIN32
523 thread->handle == NULL
524#else
525 failure
526#endif
527 ) {
528 MUTEX_UNLOCK(&create_destruct_mutex);
529 sv_2mortal(params);
530 Perl_ithread_destruct(aTHX_ thread, "create failed");
531#ifndef WIN32
532 if (panic)
533 Perl_croak(aTHX_ panic);
534#endif
535 return &PL_sv_undef;
536 }
58c2ef19 537 active_threads++;
538 MUTEX_UNLOCK(&create_destruct_mutex);
95393226 539 sv_2mortal(params);
3b1c3273 540
68795e93 541 return ithread_to_SV(aTHX_ obj, thread, classname, FALSE);
542}
47ba8780 543
68795e93 544SV*
545Perl_ithread_self (pTHX_ SV *obj, char* Class)
546{
c05ae023 547 ithread *thread = Perl_ithread_get(aTHX);
fe53aa5b 548 if (thread)
549 return ithread_to_SV(aTHX_ obj, thread, Class, TRUE);
550 else
551 Perl_croak(aTHX_ "panic: cannot find thread data");
c5661c80 552 return NULL; /* silence compiler warning */
47ba8780 553}
554
555/*
e1c44605 556 * Joins the thread this code needs to take the returnvalue from the
68795e93 557 * call_sv and send it back
b1edfb69 558 */
47ba8780 559
68795e93 560void
561Perl_ithread_CLONE(pTHX_ SV *obj)
562{
563 if (SvROK(obj))
564 {
565 ithread *thread = SV_to_ithread(aTHX_ obj);
566 }
567 else
568 {
436c6dd3 569 Perl_warn(aTHX_ "CLONE %" SVf,obj);
68795e93 570 }
47ba8780 571}
572
62375a60 573AV*
68795e93 574Perl_ithread_join(pTHX_ SV *obj)
575{
576 ithread *thread = SV_to_ithread(aTHX_ obj);
577 MUTEX_LOCK(&thread->mutex);
62375a60 578 if (thread->state & PERL_ITHR_DETACHED) {
a446a88f 579 MUTEX_UNLOCK(&thread->mutex);
580 Perl_croak(aTHX_ "Cannot join a detached thread");
581 }
62375a60 582 else if (thread->state & PERL_ITHR_JOINED) {
a446a88f 583 MUTEX_UNLOCK(&thread->mutex);
584 Perl_croak(aTHX_ "Thread already joined");
585 }
586 else {
e1c44605 587 AV* retparam;
47ba8780 588#ifdef WIN32
589 DWORD waitcode;
47ba8780 590#else
591 void *retval;
47ba8780 592#endif
47ba8780 593 MUTEX_UNLOCK(&thread->mutex);
68795e93 594#ifdef WIN32
595 waitcode = WaitForSingleObject(thread->handle, INFINITE);
c7667023 596 CloseHandle(thread->handle);
597 thread->handle = 0;
68795e93 598#else
599 pthread_join(thread->thr,&retval);
600#endif
47ba8780 601 MUTEX_LOCK(&thread->mutex);
e1c44605 602
62375a60 603 /* sv_dup over the args */
e1c44605 604 {
1d784c90 605 ithread* current_thread;
62375a60 606 AV* params = (AV*) SvRV(thread->params);
b23f1a86 607 PerlInterpreter *other_perl = thread->interp;
e1c44605 608 CLONE_PARAMS clone_params;
3275ba96 609 clone_params.stashes = newAV();
0405e91e 610 clone_params.flags |= CLONEf_JOIN_IN;
e1c44605 611 PL_ptr_table = ptr_table_new();
c05ae023 612 current_thread = Perl_ithread_get(aTHX);
613 Perl_ithread_set(aTHX_ thread);
b23f1a86 614 /* ensure 'meaningful' addresses retain their meaning */
615 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
616 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
617 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
0405e91e 618
767c1403 619#if 0
0405e91e 620 {
621 I32 len = av_len(params)+1;
622 I32 i;
623 for(i = 0; i < len; i++) {
767c1403 624 sv_dump(SvRV(AvARRAY(params)[i]));
0405e91e 625 }
626 }
767c1403 627#endif
e1c44605 628 retparam = (AV*) sv_dup((SV*)params, &clone_params);
b4cb676b 629#if 0
0405e91e 630 {
631 I32 len = av_len(retparam)+1;
632 I32 i;
633 for(i = 0; i < len; i++) {
b4cb676b 634 sv_dump(SvRV(AvARRAY(retparam)[i]));
0405e91e 635 }
636 }
b4cb676b 637#endif
c05ae023 638 Perl_ithread_set(aTHX_ current_thread);
3275ba96 639 SvREFCNT_dec(clone_params.stashes);
e1c44605 640 SvREFCNT_inc(retparam);
641 ptr_table_free(PL_ptr_table);
642 PL_ptr_table = NULL;
643
644 }
6dfd2d05 645 /* We are finished with it */
62375a60 646 thread->state |= PERL_ITHR_JOINED;
47ba8780 647 MUTEX_UNLOCK(&thread->mutex);
57b48062 648
e1c44605 649 return retparam;
68795e93 650 }
e1c44605 651 return (AV*)NULL;
47ba8780 652}
653
68795e93 654void
68795e93 655Perl_ithread_DESTROY(pTHX_ SV *sv)
656{
657 ithread *thread = SV_to_ithread(aTHX_ sv);
68795e93 658 sv_unmagic(SvRV(sv),PERL_MAGIC_shared_scalar);
659}
8222d950 660
73e09c8f 661#endif /* USE_ITHREADS */
e1c44605 662
68795e93 663MODULE = threads PACKAGE = threads PREFIX = ithread_
664PROTOTYPES: DISABLE
8222d950 665
73e09c8f 666#ifdef USE_ITHREADS
667
68795e93 668void
669ithread_new (classname, function_to_call, ...)
670char * classname
671SV * function_to_call
672CODE:
673{
674 AV* params = newAV();
675 if (items > 2) {
676 int i;
677 for(i = 2; i < items ; i++) {
95393226 678 av_push(params, SvREFCNT_inc(ST(i)));
68795e93 679 }
680 }
681 ST(0) = sv_2mortal(Perl_ithread_create(aTHX_ Nullsv, classname, function_to_call, newRV_noinc((SV*) params)));
682 XSRETURN(1);
683}
8222d950 684
68795e93 685void
678a9b6c 686ithread_list(char *classname)
687PPCODE:
688{
689 ithread *curr_thread;
690 MUTEX_LOCK(&create_destruct_mutex);
691 curr_thread = threads;
5eb9fe8f 692 if(curr_thread->tid != 0)
2379b307 693 XPUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE)));
678a9b6c 694 while(curr_thread) {
678a9b6c 695 curr_thread = curr_thread->next;
696 if(curr_thread == threads)
697 break;
6794f985 698 if(curr_thread->state & PERL_ITHR_DETACHED ||
5eb9fe8f 699 curr_thread->state & PERL_ITHR_JOINED)
700 continue;
2379b307 701 XPUSHs( sv_2mortal(ithread_to_SV(aTHX_ NULL, curr_thread, classname, TRUE)));
678a9b6c 702 }
703 MUTEX_UNLOCK(&create_destruct_mutex);
704}
705
706
707void
68795e93 708ithread_self(char *classname)
709CODE:
710{
711 ST(0) = sv_2mortal(Perl_ithread_self(aTHX_ Nullsv,classname));
712 XSRETURN(1);
713}
47ba8780 714
715int
68795e93 716ithread_tid(ithread *thread)
47ba8780 717
718void
68795e93 719ithread_join(SV *obj)
e1c44605 720PPCODE:
721{
722 AV* params = Perl_ithread_join(aTHX_ obj);
723 int i;
724 I32 len = AvFILL(params);
725 for (i = 0; i <= len; i++) {
1c3adb19 726 SV* tmp = av_shift(params);
727 XPUSHs(tmp);
728 sv_2mortal(tmp);
e1c44605 729 }
730 SvREFCNT_dec(params);
731}
732
f9dff5f5 733void
9d7debe1 734yield(...)
70f2e746 735CODE:
736{
737 YIELD;
738}
739
47ba8780 740
741void
68795e93 742ithread_detach(ithread *thread)
47ba8780 743
47ba8780 744void
68795e93 745ithread_DESTROY(SV *thread)
746
73e09c8f 747#endif /* USE_ITHREADS */
748
68795e93 749BOOT:
750{
73e09c8f 751#ifdef USE_ITHREADS
68795e93 752 ithread* thread;
e1c44605 753 PL_perl_destruct_level = 2;
58c2ef19 754 MUTEX_INIT(&create_destruct_mutex);
755 MUTEX_LOCK(&create_destruct_mutex);
62375a60 756 PL_threadhook = &Perl_ithread_hook;
68795e93 757 thread = PerlMemShared_malloc(sizeof(ithread));
758 Zero(thread,1,ithread);
759 PL_perl_destruct_level = 2;
760 MUTEX_INIT(&thread->mutex);
761 threads = thread;
762 thread->next = thread;
763 thread->prev = thread;
764 thread->interp = aTHX;
6dfd2d05 765 thread->count = 1; /* Immortal. */
68795e93 766 thread->tid = tid_counter++;
62375a60 767 known_threads++;
58c2ef19 768 active_threads++;
1fea7ed3 769 thread->state = PERL_ITHR_DETACHED;
68795e93 770#ifdef WIN32
771 thread->thr = GetCurrentThreadId();
772#else
773 thread->thr = pthread_self();
774#endif
62375a60 775
c05ae023 776 Perl_ithread_set(aTHX_ thread);
58c2ef19 777 MUTEX_UNLOCK(&create_destruct_mutex);
73e09c8f 778#endif /* USE_ITHREADS */
68795e93 779}
780