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