Remove perlcc and the byteloader
[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"
4dcb9e53 5/* Workaround for XSUB.h bug under WIN32 */
6#ifdef WIN32
7# undef setjmp
c608f8c0 8# if !defined(__BORLANDC__)
9# define setjmp(x) _setjmp(x)
10# endif
4dcb9e53 11#endif
0f1612a7 12#ifdef HAS_PPPORT_H
404aaa48 13# define NEED_PL_signals
0f1612a7 14# define NEED_newRV_noinc
15# define NEED_sv_2pv_nolen
16# include "ppport.h"
17# include "threads.h"
18#endif
68795e93 19
73e09c8f 20#ifdef USE_ITHREADS
21
68795e93 22#ifdef WIN32
fc04eb16 23# include <windows.h>
514612b7 24 /* Supposed to be in Winbase.h */
25# ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
26# define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
27# endif
fc04eb16 28# include <win32thread.h>
68795e93 29#else
fc04eb16 30# ifdef OS2
5c728af0 31typedef perl_os_thread pthread_t;
fc04eb16 32# else
33# include <pthread.h>
34# endif
35# include <thread.h>
36# define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
37# ifdef OLD_PTHREADS_API
38# define PERL_THREAD_DETACH(t) pthread_detach(&(t))
39# else
40# define PERL_THREAD_DETACH(t) pthread_detach((t))
41# endif
467f3f08 42#endif
d305c2c9 43#if !defined(HAS_GETPAGESIZE) && defined(I_SYS_PARAM)
44# include <sys/param.h>
45#endif
68795e93 46
62375a60 47/* Values for 'state' member */
69a9b4b8 48#define PERL_ITHR_JOINABLE 0
49#define PERL_ITHR_DETACHED 1
50#define PERL_ITHR_JOINED 2
51#define PERL_ITHR_FINISHED 4
52#define PERL_ITHR_THREAD_EXIT_ONLY 8
fc04eb16 53
54typedef struct _ithread {
55 struct _ithread *next; /* Next thread in the list */
56 struct _ithread *prev; /* Prev thread in the list */
57 PerlInterpreter *interp; /* The threads interpreter */
58 UV tid; /* Threads module's thread id */
59 perl_mutex mutex; /* Mutex for updating things in this struct */
9feacc09 60 int count; /* How many SVs have a reference to us */
fc04eb16 61 int state; /* Detached, joined, finished, etc. */
62 int gimme; /* Context of create */
63 SV *init_function; /* Code to run */
64 SV *params; /* Args to pass function */
68795e93 65#ifdef WIN32
fc04eb16 66 DWORD thr; /* OS's idea if thread id */
67 HANDLE handle; /* OS's waitable handle */
68795e93 68#else
fc04eb16 69 pthread_t thr; /* OS's handle for the thread */
68795e93 70#endif
514612b7 71 IV stack_size;
68795e93 72} ithread;
73
fc04eb16 74
75/* Used by Perl interpreter for thread context switching */
628ab322 76#define MY_CXT_KEY "threads::_guts" XS_VERSION
77
78typedef struct {
79 ithread *thread;
80} my_cxt_t;
81
82START_MY_CXT
83
84
fc04eb16 85/* Linked list of all threads */
f4cc38af 86static ithread *threads;
68795e93 87
fc04eb16 88/* Protects the creation and destruction of threads*/
89static perl_mutex create_destruct_mutex;
68795e93 90
f4cc38af 91static UV tid_counter = 0;
5168baf3 92static IV joinable_threads = 0;
4dcb9e53 93static IV running_threads = 0;
94static IV detached_threads = 0;
514612b7 95#ifdef THREAD_CREATE_NEEDS_STACK
96static IV default_stack_size = THREAD_CREATE_NEEDS_STACK;
97#else
98static IV default_stack_size = 0;
99#endif
100static IV page_size = 0;
c05ae023 101
102
fc04eb16 103/* Used by Perl interpreter for thread context switching */
f4cc38af 104static void
fc04eb16 105S_ithread_set(pTHX_ ithread *thread)
c05ae023 106{
628ab322 107 dMY_CXT;
108 MY_CXT.thread = thread;
c05ae023 109}
110
fc04eb16 111static ithread *
112S_ithread_get(pTHX)
113{
628ab322 114 dMY_CXT;
fc04eb16 115 return (MY_CXT.thread);
c05ae023 116}
117
118
fc04eb16 119/* Free any data (such as the Perl interpreter) attached to an ithread
120 * structure. This is a bit like undef on SVs, where the SV isn't freed,
121 * but the PVX is. Must be called with thread->mutex already held.
2e676467 122 */
2e676467 123static void
fc04eb16 124S_ithread_clear(pTHX_ ithread *thread)
2e676467 125{
126 PerlInterpreter *interp;
fc04eb16 127
ead32952 128 assert((thread->state & PERL_ITHR_FINISHED) &&
129 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
2e676467 130
131 interp = thread->interp;
132 if (interp) {
fc04eb16 133 dTHXa(interp);
134
135 PERL_SET_CONTEXT(interp);
136 S_ithread_set(aTHX_ thread);
f2cba68d 137
fc04eb16 138 SvREFCNT_dec(thread->params);
139 thread->params = Nullsv;
2e676467 140
fc04eb16 141 perl_destruct(interp);
142 thread->interp = NULL;
2e676467 143 }
144 if (interp)
fc04eb16 145 perl_free(interp);
146
2e676467 147 PERL_SET_CONTEXT(aTHX);
148}
149
68795e93 150
fc04eb16 151/* Free an ithread structure and any attached data if its count == 0 */
bcd9ca9b 152static void
fc04eb16 153S_ithread_destruct(pTHX_ ithread *thread)
68795e93 154{
385d56e4 155#ifdef WIN32
fc04eb16 156 HANDLE handle;
385d56e4 157#endif
158
fc04eb16 159 MUTEX_LOCK(&thread->mutex);
bcd9ca9b 160
fc04eb16 161 /* Thread is still in use */
162 if (thread->count != 0) {
163 MUTEX_UNLOCK(&thread->mutex);
164 return;
165 }
9feacc09 166
fc04eb16 167 /* Main thread (0) is immortal and should never get here */
168 assert(thread->tid != 0);
169
170 /* Remove from circular list of threads */
4dcb9e53 171 MUTEX_LOCK(&create_destruct_mutex);
fc04eb16 172 thread->next->prev = thread->prev;
173 thread->prev->next = thread->next;
174 thread->next = NULL;
175 thread->prev = NULL;
176 MUTEX_UNLOCK(&create_destruct_mutex);
c2f2a82b 177
fc04eb16 178 /* Thread is now disowned */
179 S_ithread_clear(aTHX_ thread);
385d56e4 180
181#ifdef WIN32
fc04eb16 182 handle = thread->handle;
183 thread->handle = NULL;
385d56e4 184#endif
fc04eb16 185 MUTEX_UNLOCK(&thread->mutex);
186 MUTEX_DESTROY(&thread->mutex);
385d56e4 187
c7667023 188#ifdef WIN32
fc04eb16 189 if (handle)
190 CloseHandle(handle);
c7667023 191#endif
385d56e4 192
fc04eb16 193 /* Call PerlMemShared_free() in the context of the "first" interpreter
194 * per http://www.nntp.perl.org/group/perl.perl5.porters/110772
195 */
196 aTHX = PL_curinterp;
197 PerlMemShared_free(thread);
68795e93 198}
199
fc04eb16 200
69a9b4b8 201/* Warn if exiting with any unjoined threads */
60bd5ef6 202static int
69a9b4b8 203S_exit_warning(pTHX)
62375a60 204{
60bd5ef6 205 int veto_cleanup;
69a9b4b8 206
62375a60 207 MUTEX_LOCK(&create_destruct_mutex);
60bd5ef6 208 veto_cleanup = (running_threads || joinable_threads);
209 MUTEX_UNLOCK(&create_destruct_mutex);
210
211 if (veto_cleanup) {
fc04eb16 212 if (ckWARN_d(WARN_THREADS)) {
4dcb9e53 213 Perl_warn(aTHX_ "Perl exited with active threads:\n\t%"
214 IVdf " running and unjoined\n\t%"
215 IVdf " finished and unjoined\n\t%"
216 IVdf " running and detached\n",
217 running_threads,
218 joinable_threads,
219 detached_threads);
fc04eb16 220 }
62375a60 221 }
69a9b4b8 222
fc04eb16 223 return (veto_cleanup);
62375a60 224}
225
69a9b4b8 226/* Called on exit from main thread */
227int
228Perl_ithread_hook(pTHX)
229{
230 return ((aTHX == PL_curinterp) ? S_exit_warning(aTHX) : 0);
231}
232
68795e93 233
234/* MAGIC (in mg.h sense) hooks */
235
236int
237ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
238{
fc04eb16 239 ithread *thread = (ithread *)mg->mg_ptr;
45977657 240 SvIV_set(sv, PTR2IV(thread));
68795e93 241 SvIOK_on(sv);
fc04eb16 242 return (0);
68795e93 243}
244
245int
246ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
247{
f2cba68d 248 ithread *thread = (ithread *)mg->mg_ptr;
249 int cleanup;
250
68795e93 251 MUTEX_LOCK(&thread->mutex);
f2cba68d 252 cleanup = ((--thread->count == 0) &&
253 (thread->state & PERL_ITHR_FINISHED) &&
254 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)));
255 MUTEX_UNLOCK(&thread->mutex);
256
257 if (cleanup)
258 S_ithread_destruct(aTHX_ thread);
fc04eb16 259 return (0);
68795e93 260}
261
262int
263ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
264{
fc04eb16 265 ithread *thread = (ithread *)mg->mg_ptr;
68795e93 266 MUTEX_LOCK(&thread->mutex);
68795e93 267 thread->count++;
268 MUTEX_UNLOCK(&thread->mutex);
fc04eb16 269 return (0);
68795e93 270}
271
272MGVTBL ithread_vtbl = {
fc04eb16 273 ithread_mg_get, /* get */
274 0, /* set */
275 0, /* len */
276 0, /* clear */
277 ithread_mg_free, /* free */
278 0, /* copy */
279 ithread_mg_dup /* dup */
68795e93 280};
281
47ba8780 282
514612b7 283/* Provided default, minimum and rational stack sizes */
284static IV
285good_stack_size(pTHX_ IV stack_size)
286{
287 /* Use default stack size if no stack size specified */
288 if (! stack_size)
289 return (default_stack_size);
290
291#ifdef PTHREAD_STACK_MIN
292 /* Can't use less than minimum */
293 if (stack_size < PTHREAD_STACK_MIN) {
4dcb9e53 294 if (ckWARN(WARN_THREADS)) {
514612b7 295 Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
296 }
297 return (PTHREAD_STACK_MIN);
298 }
299#endif
300
301 /* Round up to page size boundary */
302 if (page_size <= 0) {
d305c2c9 303#if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
514612b7 304 SETERRNO(0, SS_NORMAL);
d305c2c9 305# ifdef _SC_PAGESIZE
514612b7 306 page_size = sysconf(_SC_PAGESIZE);
d305c2c9 307# else
514612b7 308 page_size = sysconf(_SC_MMAP_PAGE_SIZE);
d305c2c9 309# endif
514612b7 310 if ((long)page_size < 0) {
311 if (errno) {
312 SV * const error = get_sv("@", FALSE);
313 (void)SvUPGRADE(error, SVt_PV);
314 Perl_croak(aTHX_ "PANIC: sysconf: %s", SvPV_nolen(error));
315 } else {
316 Perl_croak(aTHX_ "PANIC: sysconf: pagesize unknown");
317 }
318 }
d305c2c9 319#else
320# ifdef HAS_GETPAGESIZE
514612b7 321 page_size = getpagesize();
514612b7 322# else
d305c2c9 323# if defined(I_SYS_PARAM) && defined(PAGESIZE)
324 page_size = PAGESIZE;
325# else
514612b7 326 page_size = 8192; /* A conservative default */
d305c2c9 327# endif
514612b7 328# endif
d305c2c9 329 if (page_size <= 0)
330 Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)page_size);
514612b7 331#endif
332 }
333 stack_size = ((stack_size + (page_size - 1)) / page_size) * page_size;
334
335 return (stack_size);
336}
337
338
fc04eb16 339/* Starts executing the thread.
340 * Passed as the C level function to run in the new thread.
b1edfb69 341 */
47ba8780 342#ifdef WIN32
f4cc38af 343static THREAD_RET_TYPE
fc04eb16 344S_ithread_run(LPVOID arg)
47ba8780 345#else
fc04eb16 346static void *
347S_ithread_run(void * arg)
47ba8780 348#endif
fc04eb16 349{
350 ithread *thread = (ithread *)arg;
69a9b4b8 351 int jmp_rc = 0;
352 I32 oldscope;
353 int exit_app = 0;
354 int exit_code = 0;
fc04eb16 355 int cleanup;
f2cba68d 356
69a9b4b8 357 dJMPENV;
358
fc04eb16 359 dTHXa(thread->interp);
360 PERL_SET_CONTEXT(thread->interp);
361 S_ithread_set(aTHX_ thread);
47ba8780 362
68795e93 363#if 0
fc04eb16 364 /* Far from clear messing with ->thr child-side is a good idea */
365 MUTEX_LOCK(&thread->mutex);
47ba8780 366#ifdef WIN32
fc04eb16 367 thread->thr = GetCurrentThreadId();
47ba8780 368#else
fc04eb16 369 thread->thr = pthread_self();
47ba8780 370#endif
fc04eb16 371 MUTEX_UNLOCK(&thread->mutex);
68795e93 372#endif
47ba8780 373
fc04eb16 374 PL_perl_destruct_level = 2;
f2cba68d 375
fc04eb16 376 {
377 AV *params = (AV *)SvRV(thread->params);
378 int len = (int)av_len(params)+1;
379 int ii;
380
381 dSP;
382 ENTER;
383 SAVETMPS;
384
385 /* Put args on the stack */
386 PUSHMARK(SP);
387 for (ii=0; ii < len; ii++) {
388 XPUSHs(av_shift(params));
389 }
390 PUTBACK;
391
4dcb9e53 392 oldscope = PL_scopestack_ix;
393 JMPENV_PUSH(jmp_rc);
394 if (jmp_rc == 0) {
395 /* Run the specified function */
396 len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
397 } else if (jmp_rc == 2) {
69a9b4b8 398 /* Thread exited */
399 exit_app = 1;
400 exit_code = STATUS_CURRENT;
4dcb9e53 401 while (PL_scopestack_ix > oldscope) {
402 LEAVE;
403 }
404 }
405 JMPENV_POP;
fc04eb16 406
407 /* Remove args from stack and put back in params array */
408 SPAGAIN;
409 for (ii=len-1; ii >= 0; ii--) {
410 SV *sv = POPs;
4dcb9e53 411 if (jmp_rc == 0) {
412 av_store(params, ii, SvREFCNT_inc(sv));
413 }
fc04eb16 414 }
415
4dcb9e53 416 FREETMPS;
417 LEAVE;
418
fc04eb16 419 /* Check for failure */
420 if (SvTRUE(ERRSV) && ckWARN_d(WARN_THREADS)) {
4dcb9e53 421 oldscope = PL_scopestack_ix;
422 JMPENV_PUSH(jmp_rc);
423 if (jmp_rc == 0) {
69a9b4b8 424 /* Warn that thread died */
4dcb9e53 425 Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV);
426 } else if (jmp_rc == 2) {
69a9b4b8 427 /* Warn handler exited */
428 exit_app = 1;
429 exit_code = STATUS_CURRENT;
4dcb9e53 430 while (PL_scopestack_ix > oldscope) {
431 LEAVE;
432 }
433 }
434 JMPENV_POP;
fc04eb16 435 }
436
fc04eb16 437 /* Release function ref */
438 SvREFCNT_dec(thread->init_function);
439 thread->init_function = Nullsv;
440 }
62375a60 441
fc04eb16 442 PerlIO_flush((PerlIO *)NULL);
443
444 MUTEX_LOCK(&thread->mutex);
445 /* Mark as finished */
446 thread->state |= PERL_ITHR_FINISHED;
69a9b4b8 447 /* Clear exit flag if required */
448 if (thread->state & PERL_ITHR_THREAD_EXIT_ONLY)
449 exit_app = 0;
fc04eb16 450 /* Cleanup if detached */
451 cleanup = (thread->state & PERL_ITHR_DETACHED);
452 MUTEX_UNLOCK(&thread->mutex);
453
69a9b4b8 454 /* Adjust thread status counts */
455 MUTEX_LOCK(&create_destruct_mutex);
4dcb9e53 456 if (cleanup) {
4dcb9e53 457 detached_threads--;
4dcb9e53 458 } else {
4dcb9e53 459 running_threads--;
460 joinable_threads++;
5168baf3 461 }
69a9b4b8 462 MUTEX_UNLOCK(&create_destruct_mutex);
463
464 /* Exit application if required */
465 if (exit_app) {
466 oldscope = PL_scopestack_ix;
467 JMPENV_PUSH(jmp_rc);
468 if (jmp_rc == 0) {
469 /* Warn if there are unjoined threads */
470 S_exit_warning(aTHX);
471 } else if (jmp_rc == 2) {
472 /* Warn handler exited */
473 exit_code = STATUS_CURRENT;
474 while (PL_scopestack_ix > oldscope) {
475 LEAVE;
476 }
477 }
478 JMPENV_POP;
479
480 my_exit(exit_code);
481 }
482
483 /* Clean up detached thread */
484 if (cleanup)
485 S_ithread_destruct(aTHX_ thread);
91604d21 486
47ba8780 487#ifdef WIN32
fc04eb16 488 return ((DWORD)0);
e8f2bb9a 489#else
fc04eb16 490 return (0);
47ba8780 491#endif
68795e93 492}
493
fc04eb16 494
495/* Type conversion helper functions */
f4cc38af 496static SV *
68795e93 497ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
498{
499 SV *sv;
500 MAGIC *mg;
fc04eb16 501
68795e93 502 if (inc) {
fc04eb16 503 MUTEX_LOCK(&thread->mutex);
504 thread->count++;
505 MUTEX_UNLOCK(&thread->mutex);
506 }
507
508 if (! obj) {
509 obj = newSV(0);
68795e93 510 }
fc04eb16 511
512 sv = newSVrv(obj, classname);
513 sv_setiv(sv, PTR2IV(thread));
514 mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0);
68795e93 515 mg->mg_flags |= MGf_DUP;
516 SvREADONLY_on(sv);
fc04eb16 517
518 return (obj);
68795e93 519}
47ba8780 520
f4cc38af 521static ithread *
68795e93 522SV_to_ithread(pTHX_ SV *sv)
523{
fc04eb16 524 /* Argument is a thread */
525 if (SvROK(sv)) {
526 return (INT2PTR(ithread *, SvIV(SvRV(sv))));
527 }
528 /* Argument is classname, therefore return current thread */
529 return (S_ithread_get(aTHX));
47ba8780 530}
531
47ba8780 532
fc04eb16 533/* threads->create()
534 * Called in context of parent thread.
535 */
f4cc38af 536static SV *
fc04eb16 537S_ithread_create(
538 pTHX_ SV *obj,
539 char *classname,
540 SV *init_function,
514612b7 541 IV stack_size,
9d9ff5b1 542 int gimme,
69a9b4b8 543 int exit_opt,
fc04eb16 544 SV *params)
68795e93 545{
fc04eb16 546 ithread *thread;
547 CLONE_PARAMS clone_param;
548 ithread *current_thread = S_ithread_get(aTHX);
3b1c3273 549
fc04eb16 550 SV **tmps_tmp = PL_tmps_stack;
551 IV tmps_ix = PL_tmps_ix;
d94006e8 552#ifndef WIN32
fc04eb16 553 int rc_stack_size = 0;
554 int rc_thread_create = 0;
d94006e8 555#endif
3b1c3273 556
fc04eb16 557 MUTEX_LOCK(&create_destruct_mutex);
558
559 /* Allocate thread structure */
560 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
561 if (!thread) {
562 MUTEX_UNLOCK(&create_destruct_mutex);
563 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
564 my_exit(1);
565 }
566 Zero(thread, 1, ithread);
567
568 /* Add to threads list */
569 thread->next = threads;
570 thread->prev = threads->prev;
571 threads->prev = thread;
572 thread->prev->next = thread;
c05ae023 573
fc04eb16 574 /* Set count to 1 immediately in case thread exits before
575 * we return to caller!
576 */
577 thread->count = 1;
578
579 MUTEX_INIT(&thread->mutex);
580 thread->tid = tid_counter++;
514612b7 581 thread->stack_size = good_stack_size(aTHX_ stack_size);
9d9ff5b1 582 thread->gimme = gimme;
69a9b4b8 583 thread->state = exit_opt;
fc04eb16 584
585 /* "Clone" our interpreter into the thread's interpreter.
586 * This gives thread access to "static data" and code.
587 */
588 PerlIO_flush((PerlIO *)NULL);
589 S_ithread_set(aTHX_ thread);
590
591 SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct value */
592 PL_srand_called = FALSE; /* Set it to false so we can detect if it gets
593 set during the clone */
3b1c3273 594
47ba8780 595#ifdef WIN32
fc04eb16 596 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
47ba8780 597#else
fc04eb16 598 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
47ba8780 599#endif
47ba8780 600
fc04eb16 601 /* perl_clone() leaves us in new interpreter's context. As it is tricky
602 * to spot an implicit aTHX, create a new scope with aTHX matching the
603 * context for the duration of our work for new interpreter.
604 */
605 {
606 dTHXa(thread->interp);
607
608 MY_CXT_CLONE;
609
610 /* Here we remove END blocks since they should only run in the thread
611 * they are created
612 */
613 SvREFCNT_dec(PL_endav);
614 PL_endav = newAV();
404aaa48 615
f2e0bb91 616 if (SvPOK(init_function)) {
617 thread->init_function = newSV(0);
618 sv_copypv(thread->init_function, init_function);
619 } else {
620 clone_param.flags = 0;
621 thread->init_function = sv_dup(init_function, &clone_param);
622 if (SvREFCNT(thread->init_function) == 0) {
d4315dd6 623 SvREFCNT_inc_void(thread->init_function);
f2e0bb91 624 }
fc04eb16 625 }
626
627 thread->params = sv_dup(params, &clone_param);
d4315dd6 628 SvREFCNT_inc_void(thread->params);
fc04eb16 629
630 /* The code below checks that anything living on the tmps stack and
631 * has been cloned (so it lives in the ptr_table) has a refcount
632 * higher than 0.
633 *
634 * If the refcount is 0 it means that a something on the stack/context
635 * was holding a reference to it and since we init_stacks() in
636 * perl_clone that won't get cleaned and we will get a leaked scalar.
637 * The reason it was cloned was that it lived on the @_ stack.
638 *
639 * Example of this can be found in bugreport 15837 where calls in the
640 * parameter list end up as a temp.
641 *
642 * One could argue that this fix should be in perl_clone.
643 */
644 while (tmps_ix > 0) {
645 SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
646 tmps_ix--;
647 if (sv && SvREFCNT(sv) == 0) {
d4315dd6 648 SvREFCNT_inc_void(sv);
fc04eb16 649 SvREFCNT_dec(sv);
650 }
651 }
652
653 SvTEMP_off(thread->init_function);
654 ptr_table_free(PL_ptr_table);
655 PL_ptr_table = NULL;
656 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
657 }
658 S_ithread_set(aTHX_ current_thread);
659 PERL_SET_CONTEXT(aTHX);
660
661 /* Create/start the thread */
47ba8780 662#ifdef WIN32
fc04eb16 663 thread->handle = CreateThread(NULL,
514612b7 664 (DWORD)thread->stack_size,
fc04eb16 665 S_ithread_run,
666 (LPVOID)thread,
514612b7 667 STACK_SIZE_PARAM_IS_A_RESERVATION,
fc04eb16 668 &thread->thr);
82c40bf6 669#else
fc04eb16 670 {
671 static pthread_attr_t attr;
672 static int attr_inited = 0;
673 static int attr_joinable = PTHREAD_CREATE_JOINABLE;
674 if (! attr_inited) {
675 pthread_attr_init(&attr);
676 attr_inited = 1;
677 }
678
fa26028c 679# ifdef PTHREAD_ATTR_SETDETACHSTATE
fc04eb16 680 /* Threads start out joinable */
681 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
fa26028c 682# endif
fc04eb16 683
514612b7 684# ifdef _POSIX_THREAD_ATTR_STACKSIZE
fc04eb16 685 /* Set thread's stack size */
514612b7 686 if (thread->stack_size > 0) {
687 rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
688 }
3eb37d38 689# endif
690
fc04eb16 691 /* Create the thread */
692 if (! rc_stack_size) {
693# ifdef OLD_PTHREADS_API
694 rc_thread_create = pthread_create(&thread->thr,
695 attr,
696 S_ithread_run,
697 (void *)thread);
698# else
699# if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
700 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
701# endif
702 rc_thread_create = pthread_create(&thread->thr,
703 &attr,
704 S_ithread_run,
705 (void *)thread);
19a077f6 706# endif
fc04eb16 707 }
514612b7 708
709# ifdef _POSIX_THREAD_ATTR_STACKSIZE
710 /* Try to get thread's actual stack size */
711 {
712 size_t stacksize;
58a3a76c 713#ifdef HPUX1020
714 stacksize = pthread_attr_getstacksize(attr);
715#else
716 if (! pthread_attr_getstacksize(&attr, &stacksize))
717#endif
718 if (stacksize > 0) {
514612b7 719 thread->stack_size = (IV)stacksize;
720 }
514612b7 721 }
722# endif
fc04eb16 723 }
82c40bf6 724#endif
bcd9ca9b 725
fc04eb16 726 /* Check for errors */
d94006e8 727#ifdef WIN32
fc04eb16 728 if (thread->handle == NULL) {
d94006e8 729#else
fc04eb16 730 if (rc_stack_size || rc_thread_create) {
d94006e8 731#endif
fc04eb16 732 MUTEX_UNLOCK(&create_destruct_mutex);
733 sv_2mortal(params);
734 S_ithread_destruct(aTHX_ thread);
d94006e8 735#ifndef WIN32
514612b7 736 if (ckWARN_d(WARN_THREADS)) {
737 if (rc_stack_size)
738 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
739 else
740 Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
741 }
d94006e8 742#endif
fc04eb16 743 return (&PL_sv_undef);
744 }
745
4dcb9e53 746 running_threads++;
fc04eb16 747 MUTEX_UNLOCK(&create_destruct_mutex);
748
749 sv_2mortal(params);
3b1c3273 750
fc04eb16 751 return (ithread_to_SV(aTHX_ obj, thread, classname, FALSE));
68795e93 752}
47ba8780 753
73e09c8f 754#endif /* USE_ITHREADS */
e1c44605 755
fcea4b7c 756
fc04eb16 757MODULE = threads PACKAGE = threads PREFIX = ithread_
68795e93 758PROTOTYPES: DISABLE
8222d950 759
73e09c8f 760#ifdef USE_ITHREADS
761
68795e93 762void
f4cc38af 763ithread_create(...)
764 PREINIT:
765 char *classname;
514612b7 766 ithread *thread;
f4cc38af 767 SV *function_to_call;
768 AV *params;
514612b7 769 HV *specs;
770 IV stack_size;
9d9ff5b1 771 int context;
69a9b4b8 772 int exit_opt;
773 SV *thread_exit_only;
9d9ff5b1 774 char *str;
514612b7 775 int idx;
f4cc38af 776 int ii;
777 CODE:
514612b7 778 if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
779 if (--items < 2)
780 Perl_croak(aTHX_ "Usage: threads->create(\\%specs, function, ...)");
781 specs = (HV*)SvRV(ST(1));
782 idx = 1;
783 } else {
784 if (items < 2)
785 Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
786 specs = NULL;
787 idx = 0;
788 }
f4cc38af 789
514612b7 790 if (sv_isobject(ST(0))) {
791 /* $thr->create() */
792 classname = HvNAME(SvSTASH(SvRV(ST(0))));
793 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
794 stack_size = thread->stack_size;
69a9b4b8 795 exit_opt = thread->state & PERL_ITHR_THREAD_EXIT_ONLY;
514612b7 796 } else {
797 /* threads->create() */
798 classname = (char *)SvPV_nolen(ST(0));
799 stack_size = default_stack_size;
69a9b4b8 800 thread_exit_only = get_sv("threads::thread_exit_only", TRUE);
801 exit_opt = (SvTRUE(thread_exit_only))
802 ? PERL_ITHR_THREAD_EXIT_ONLY : 0;
514612b7 803 }
804
805 function_to_call = ST(idx+1);
806
9d9ff5b1 807 context = -1;
514612b7 808 if (specs) {
809 /* stack_size */
810 if (hv_exists(specs, "stack", 5)) {
811 stack_size = SvIV(*hv_fetch(specs, "stack", 5, 0));
812 } else if (hv_exists(specs, "stacksize", 9)) {
813 stack_size = SvIV(*hv_fetch(specs, "stacksize", 9, 0));
814 } else if (hv_exists(specs, "stack_size", 10)) {
815 stack_size = SvIV(*hv_fetch(specs, "stack_size", 10, 0));
816 }
9d9ff5b1 817
818 /* context */
819 if (hv_exists(specs, "context", 7)) {
820 str = (char *)SvPV_nolen(*hv_fetch(specs, "context", 7, 0));
821 switch (*str) {
822 case 'a':
823 case 'A':
824 context = G_ARRAY;
825 break;
826 case 's':
827 case 'S':
828 context = G_SCALAR;
829 break;
830 case 'v':
831 case 'V':
832 context = G_VOID;
833 break;
834 default:
835 Perl_croak(aTHX_ "Invalid context: %s", str);
836 }
837 } else if (hv_exists(specs, "array", 5)) {
838 if (SvTRUE(*hv_fetch(specs, "array", 5, 0))) {
839 context = G_ARRAY;
840 }
841 } else if (hv_exists(specs, "scalar", 6)) {
842 if (SvTRUE(*hv_fetch(specs, "scalar", 6, 0))) {
843 context = G_SCALAR;
844 }
845 } else if (hv_exists(specs, "void", 4)) {
846 if (SvTRUE(*hv_fetch(specs, "void", 4, 0))) {
847 context = G_VOID;
848 }
849 }
69a9b4b8 850
851 /* exit => thread_only */
852 if (hv_exists(specs, "exit", 4)) {
853 str = (char *)SvPV_nolen(*hv_fetch(specs, "exit", 4, 0));
854 exit_opt = (*str == 't' || *str == 'T')
855 ? PERL_ITHR_THREAD_EXIT_ONLY : 0;
856 }
9d9ff5b1 857 }
858 if (context == -1) {
859 context = GIMME_V; /* Implicit context */
860 } else {
861 context |= (GIMME_V & (~(G_ARRAY|G_SCALAR|G_VOID)));
514612b7 862 }
f4cc38af 863
864 /* Function args */
865 params = newAV();
866 if (items > 2) {
514612b7 867 for (ii=2; ii < items ; ii++) {
868 av_push(params, SvREFCNT_inc(ST(idx+ii)));
f4cc38af 869 }
870 }
871
872 /* Create thread */
bcd9ca9b 873 ST(0) = sv_2mortal(S_ithread_create(aTHX_ Nullsv,
514612b7 874 classname,
875 function_to_call,
876 stack_size,
9d9ff5b1 877 context,
69a9b4b8 878 exit_opt,
514612b7 879 newRV_noinc((SV*)params)));
f4cc38af 880 /* XSRETURN(1); - implied */
881
8222d950 882
68795e93 883void
f4cc38af 884ithread_list(...)
885 PREINIT:
886 char *classname;
fc04eb16 887 ithread *thread;
f4cc38af 888 int list_context;
889 IV count = 0;
ead32952 890 int want_running;
f4cc38af 891 PPCODE:
892 /* Class method only */
893 if (SvROK(ST(0)))
ead32952 894 Perl_croak(aTHX_ "Usage: threads->list(...)");
f4cc38af 895 classname = (char *)SvPV_nolen(ST(0));
896
897 /* Calling context */
898 list_context = (GIMME_V == G_ARRAY);
899
ead32952 900 /* Running or joinable parameter */
901 if (items > 1) {
902 want_running = SvTRUE(ST(1));
903 }
904
f4cc38af 905 /* Walk through threads list */
906 MUTEX_LOCK(&create_destruct_mutex);
fc04eb16 907 for (thread = threads->next;
908 thread != threads;
909 thread = thread->next)
f4cc38af 910 {
911 /* Ignore detached or joined threads */
fc04eb16 912 if (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)) {
f4cc38af 913 continue;
914 }
ead32952 915
916 /* Filter per parameter */
917 if (items > 1) {
918 if (want_running) {
919 if (thread->state & PERL_ITHR_FINISHED) {
920 continue; /* Not running */
921 }
922 } else {
923 if (! (thread->state & PERL_ITHR_FINISHED)) {
924 continue; /* Still running - not joinable yet */
925 }
926 }
927 }
928
f4cc38af 929 /* Push object on stack if list context */
930 if (list_context) {
fc04eb16 931 XPUSHs(sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
f4cc38af 932 }
933 count++;
934 }
935 MUTEX_UNLOCK(&create_destruct_mutex);
936 /* If scalar context, send back count */
937 if (! list_context) {
938 XSRETURN_IV(count);
939 }
678a9b6c 940
941
942void
f4cc38af 943ithread_self(...)
944 PREINIT:
945 char *classname;
fcea4b7c 946 ithread *thread;
f4cc38af 947 CODE:
948 /* Class method only */
949 if (SvROK(ST(0)))
950 Perl_croak(aTHX_ "Usage: threads->self()");
951 classname = (char *)SvPV_nolen(ST(0));
952
fcea4b7c 953 thread = S_ithread_get(aTHX);
954
955 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
f4cc38af 956 /* XSRETURN(1); - implied */
47ba8780 957
47ba8780 958
959void
f4cc38af 960ithread_tid(...)
961 PREINIT:
962 ithread *thread;
963 CODE:
964 thread = SV_to_ithread(aTHX_ ST(0));
965 XST_mUV(0, thread->tid);
966 /* XSRETURN(1); - implied */
967
e1c44605 968
f9dff5f5 969void
f4cc38af 970ithread_join(...)
971 PREINIT:
fcea4b7c 972 ithread *thread;
973 int join_err;
f4cc38af 974 AV *params;
975 int len;
976 int ii;
fcea4b7c 977#ifdef WIN32
978 DWORD waitcode;
979#else
980 void *retval;
981#endif
f4cc38af 982 PPCODE:
983 /* Object method only */
984 if (! sv_isobject(ST(0)))
985 Perl_croak(aTHX_ "Usage: $thr->join()");
986
fcea4b7c 987 /* Check if the thread is joinable */
988 thread = SV_to_ithread(aTHX_ ST(0));
989 MUTEX_LOCK(&thread->mutex);
990 join_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED));
991 MUTEX_UNLOCK(&thread->mutex);
992 if (join_err) {
993 if (join_err & PERL_ITHR_DETACHED) {
994 Perl_croak(aTHX_ "Cannot join a detached thread");
995 } else {
996 Perl_croak(aTHX_ "Thread already joined");
997 }
998 }
999
1000 /* Join the thread */
1001#ifdef WIN32
1002 waitcode = WaitForSingleObject(thread->handle, INFINITE);
1003#else
1004 pthread_join(thread->thr, &retval);
1005#endif
1006
1007 MUTEX_LOCK(&thread->mutex);
1008 /* Mark as joined */
1009 thread->state |= PERL_ITHR_JOINED;
1010
1011 /* Get the return value from the call_sv */
1012 {
1013 AV *params_copy;
1014 PerlInterpreter *other_perl;
1015 CLONE_PARAMS clone_params;
1016 ithread *current_thread;
1017
1018 params_copy = (AV *)SvRV(thread->params);
1019 other_perl = thread->interp;
1020 clone_params.stashes = newAV();
1021 clone_params.flags = CLONEf_JOIN_IN;
1022 PL_ptr_table = ptr_table_new();
1023 current_thread = S_ithread_get(aTHX);
1024 S_ithread_set(aTHX_ thread);
1025 /* Ensure 'meaningful' addresses retain their meaning */
1026 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1027 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1028 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1029 params = (AV *)sv_dup((SV*)params_copy, &clone_params);
1030 S_ithread_set(aTHX_ current_thread);
1031 SvREFCNT_dec(clone_params.stashes);
d4315dd6 1032 SvREFCNT_inc_void(params);
fcea4b7c 1033 ptr_table_free(PL_ptr_table);
1034 PL_ptr_table = NULL;
1035 }
1036
1037 /* We are finished with the thread */
1038 S_ithread_clear(aTHX_ thread);
1039 MUTEX_UNLOCK(&thread->mutex);
1040
4dcb9e53 1041 MUTEX_LOCK(&create_destruct_mutex);
1042 joinable_threads--;
1043 MUTEX_UNLOCK(&create_destruct_mutex);
1044
fcea4b7c 1045 /* If no return values, then just return */
f4cc38af 1046 if (! params) {
1047 XSRETURN_UNDEF;
1048 }
1049
1050 /* Put return values on stack */
1051 len = (int)AvFILL(params);
1052 for (ii=0; ii <= len; ii++) {
1053 SV* param = av_shift(params);
1054 XPUSHs(sv_2mortal(param));
1055 }
1056
1057 /* Free return value array */
1058 SvREFCNT_dec(params);
1059
1060
1061void
1062ithread_yield(...)
1063 CODE:
1064 YIELD;
1065
1066
1067void
1068ithread_detach(...)
1069 PREINIT:
1070 ithread *thread;
fcea4b7c 1071 int detach_err;
1072 int cleanup;
f4cc38af 1073 CODE:
1074 thread = SV_to_ithread(aTHX_ ST(0));
fcea4b7c 1075 MUTEX_LOCK(&thread->mutex);
1076
1077 /* Check if the thread is detachable */
1078 if ((detach_err = (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))) {
1079 MUTEX_UNLOCK(&thread->mutex);
1080 if (detach_err & PERL_ITHR_DETACHED) {
1081 Perl_croak(aTHX_ "Thread already detached");
1082 } else {
1083 Perl_croak(aTHX_ "Cannot detach a joined thread");
1084 }
1085 }
1086
1087 /* Detach the thread */
1088 thread->state |= PERL_ITHR_DETACHED;
1089#ifdef WIN32
1090 /* Windows has no 'detach thread' function */
1091#else
1092 PERL_THREAD_DETACH(thread->thr);
1093#endif
1094 /* Cleanup if finished */
1095 cleanup = (thread->state & PERL_ITHR_FINISHED);
1096 MUTEX_UNLOCK(&thread->mutex);
1097
4dcb9e53 1098 MUTEX_LOCK(&create_destruct_mutex);
1099 if (cleanup) {
1100 joinable_threads--;
1101 } else {
1102 running_threads--;
1103 detached_threads++;
1104 }
1105 MUTEX_UNLOCK(&create_destruct_mutex);
1106
1107 if (cleanup) {
fcea4b7c 1108 S_ithread_destruct(aTHX_ thread);
4dcb9e53 1109 }
f4cc38af 1110
47ba8780 1111
1112void
c0003851 1113ithread_kill(...)
1114 PREINIT:
1115 ithread *thread;
1116 char *sig_name;
1117 IV signal;
1118 CODE:
1119 /* Must have safe signals */
1120 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4dcb9e53 1121 Perl_croak(aTHX_ "Cannot signal threads without safe signals");
c0003851 1122
1123 /* Object method only */
1124 if (! sv_isobject(ST(0)))
1125 Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')");
1126
c0003851 1127 /* Get signal */
1128 sig_name = SvPV_nolen(ST(1));
1129 if (isALPHA(*sig_name)) {
1130 if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G')
1131 sig_name += 3;
404aaa48 1132 if ((signal = whichsig(sig_name)) < 0)
c0003851 1133 Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name);
1134 } else
1135 signal = SvIV(ST(1));
1136
1137 /* Set the signal for the thread */
4dcb9e53 1138 thread = SV_to_ithread(aTHX_ ST(0));
1139 MUTEX_LOCK(&thread->mutex);
3ceb02cd 1140 if (thread->interp) {
c0003851 1141 dTHXa(thread->interp);
1142 PL_psig_pend[signal]++;
1143 PL_sig_pending = 1;
1144 }
4dcb9e53 1145 MUTEX_UNLOCK(&thread->mutex);
c0003851 1146
1147 /* Return the thread to allow for method chaining */
1148 ST(0) = ST(0);
1149 /* XSRETURN(1); - implied */
1150
1151
1152void
f4cc38af 1153ithread_DESTROY(...)
1154 CODE:
fcea4b7c 1155 sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar);
f4cc38af 1156
1157
1158void
1159ithread_equal(...)
fc04eb16 1160 PREINIT:
1161 int are_equal = 0;
f4cc38af 1162 CODE:
fc04eb16 1163 /* Compares TIDs to determine thread equality */
f4cc38af 1164 if (sv_isobject(ST(0)) && sv_isobject(ST(1))) {
1165 ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1166 ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
fc04eb16 1167 are_equal = (thr1->tid == thr2->tid);
1168 }
1169 if (are_equal) {
1170 XST_mYES(0);
f4cc38af 1171 } else {
fc04eb16 1172 /* Return 0 on false for backward compatibility */
f4cc38af 1173 XST_mIV(0, 0);
1174 }
1175 /* XSRETURN(1); - implied */
1176
47ba8780 1177
47ba8780 1178void
f4cc38af 1179ithread_object(...)
1180 PREINIT:
1181 char *classname;
1182 UV tid;
fc04eb16 1183 ithread *thread;
f4cc38af 1184 int found = 0;
1185 CODE:
1186 /* Class method only */
1187 if (SvROK(ST(0)))
1188 Perl_croak(aTHX_ "Usage: threads->object($tid)");
1189 classname = (char *)SvPV_nolen(ST(0));
1190
1191 if ((items < 2) || ! SvOK(ST(1))) {
1192 XSRETURN_UNDEF;
1193 }
1194
fc04eb16 1195 /* threads->object($tid) */
f4cc38af 1196 tid = SvUV(ST(1));
1197
1198 /* Walk through threads list */
1199 MUTEX_LOCK(&create_destruct_mutex);
fc04eb16 1200 for (thread = threads->next;
1201 thread != threads;
1202 thread = thread->next)
f4cc38af 1203 {
1204 /* Look for TID, but ignore detached or joined threads */
fc04eb16 1205 if ((thread->tid != tid) ||
1206 (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))
f4cc38af 1207 {
1208 continue;
1209 }
1210 /* Put object on stack */
fc04eb16 1211 ST(0) = sv_2mortal(ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
f4cc38af 1212 found = 1;
1213 break;
1214 }
1215 MUTEX_UNLOCK(&create_destruct_mutex);
1216 if (! found) {
1217 XSRETURN_UNDEF;
1218 }
1219 /* XSRETURN(1); - implied */
1220
1221
1222void
1223ithread__handle(...);
1224 PREINIT:
1225 ithread *thread;
1226 CODE:
1227 thread = SV_to_ithread(aTHX_ ST(0));
1228#ifdef WIN32
fcea4b7c 1229 XST_mUV(0, PTR2UV(&thread->handle));
f4cc38af 1230#else
75ba4ae2 1231 XST_mUV(0, PTR2UV(&thread->thr));
f4cc38af 1232#endif
1233 /* XSRETURN(1); - implied */
68795e93 1234
514612b7 1235
1236void
1237ithread_get_stack_size(...)
1238 PREINIT:
1239 IV stack_size;
1240 CODE:
1241 if (sv_isobject(ST(0))) {
1242 /* $thr->get_stack_size() */
1243 ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1244 stack_size = thread->stack_size;
1245 } else {
1246 /* threads->get_stack_size() */
1247 stack_size = default_stack_size;
1248 }
1249 XST_mIV(0, stack_size);
1250 /* XSRETURN(1); - implied */
1251
1252
1253void
1254ithread_set_stack_size(...)
1255 PREINIT:
1256 IV old_size;
1257 CODE:
1258 if (items != 2)
1259 Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
1260 if (sv_isobject(ST(0)))
1261 Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
1262
1263 old_size = default_stack_size;
1264 default_stack_size = good_stack_size(aTHX_ SvIV(ST(1)));
1265 XST_mIV(0, old_size);
1266 /* XSRETURN(1); - implied */
1267
ead32952 1268
1269void
1270ithread_is_running(...)
1271 PREINIT:
1272 ithread *thread;
1273 CODE:
1274 /* Object method only */
1275 if (! sv_isobject(ST(0)))
1276 Perl_croak(aTHX_ "Usage: $thr->is_running()");
1277
1278 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1279 MUTEX_LOCK(&thread->mutex);
1280 ST(0) = (thread->state & PERL_ITHR_FINISHED) ? &PL_sv_no : &PL_sv_yes;
1281 MUTEX_UNLOCK(&thread->mutex);
1282 /* XSRETURN(1); - implied */
1283
1284
1285void
1286ithread_is_detached(...)
1287 PREINIT:
1288 ithread *thread;
1289 CODE:
1290 thread = SV_to_ithread(aTHX_ ST(0));
1291 MUTEX_LOCK(&thread->mutex);
1292 ST(0) = (thread->state & PERL_ITHR_DETACHED) ? &PL_sv_yes : &PL_sv_no;
1293 MUTEX_UNLOCK(&thread->mutex);
1294 /* XSRETURN(1); - implied */
1295
1296
1297void
1298ithread_is_joinable(...)
1299 PREINIT:
1300 ithread *thread;
1301 CODE:
1302 /* Object method only */
1303 if (! sv_isobject(ST(0)))
1304 Perl_croak(aTHX_ "Usage: $thr->is_joinable()");
1305
1306 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1307 MUTEX_LOCK(&thread->mutex);
1308 ST(0) = ((thread->state & PERL_ITHR_FINISHED) &&
1309 ! (thread->state & (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)))
1310 ? &PL_sv_yes : &PL_sv_no;
1311 MUTEX_UNLOCK(&thread->mutex);
1312 /* XSRETURN(1); - implied */
1313
1314
1315void
1316ithread_wantarray(...)
1317 PREINIT:
1318 ithread *thread;
1319 CODE:
1320 thread = SV_to_ithread(aTHX_ ST(0));
1321 MUTEX_LOCK(&thread->mutex);
1322 ST(0) = (thread->gimme & G_ARRAY) ? &PL_sv_yes :
1323 (thread->gimme & G_VOID) ? &PL_sv_undef
1324 /* G_SCALAR */ : &PL_sv_no;
1325 MUTEX_UNLOCK(&thread->mutex);
1326 /* XSRETURN(1); - implied */
1327
69a9b4b8 1328
1329void
1330ithread_set_thread_exit_only(...)
1331 PREINIT:
1332 ithread *thread;
1333 CODE:
1334 if (items != 2)
1335 Perl_croak(aTHX_ "Usage: ->set_thread_exit_only(boolean)");
1336 thread = SV_to_ithread(aTHX_ ST(0));
1337 MUTEX_LOCK(&thread->mutex);
1338 if (SvTRUE(ST(1))) {
1339 thread->state |= PERL_ITHR_THREAD_EXIT_ONLY;
1340 } else {
1341 thread->state &= ~PERL_ITHR_THREAD_EXIT_ONLY;
1342 }
1343 MUTEX_UNLOCK(&thread->mutex);
1344
73e09c8f 1345#endif /* USE_ITHREADS */
1346
fc04eb16 1347
68795e93 1348BOOT:
1349{
73e09c8f 1350#ifdef USE_ITHREADS
fc04eb16 1351 /* The 'main' thread is thread 0.
1352 * It is detached (unjoinable) and immortal.
1353 */
62375a60 1354
fc04eb16 1355 ithread *thread;
1356 MY_CXT_INIT;
1357
1358 PL_perl_destruct_level = 2;
1359 MUTEX_INIT(&create_destruct_mutex);
1360 MUTEX_LOCK(&create_destruct_mutex);
1361
1362 PL_threadhook = &Perl_ithread_hook;
1363
1364 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
1365 if (! thread) {
1366 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
1367 my_exit(1);
1368 }
1369 Zero(thread, 1, ithread);
1370
fc04eb16 1371 MUTEX_INIT(&thread->mutex);
1372
1373 thread->tid = tid_counter++; /* Thread 0 */
1374
1375 /* Head of the threads list */
1376 threads = thread;
1377 thread->next = thread;
1378 thread->prev = thread;
1379
1380 thread->count = 1; /* Immortal */
1381
1382 thread->interp = aTHX;
1383 thread->state = PERL_ITHR_DETACHED; /* Detached */
514612b7 1384 thread->stack_size = default_stack_size;
fc04eb16 1385# ifdef WIN32
1386 thread->thr = GetCurrentThreadId();
1387# else
1388 thread->thr = pthread_self();
1389# endif
1390
fc04eb16 1391 S_ithread_set(aTHX_ thread);
1392 MUTEX_UNLOCK(&create_destruct_mutex);
73e09c8f 1393#endif /* USE_ITHREADS */
68795e93 1394}