This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix threads build on Win32
[perl5.git] / ext / threads / threads.xs
CommitLineData
68795e93
NIS
1#define PERL_NO_GET_CONTEXT
2#include "EXTERN.h"
3#include "perl.h"
4#include "XSUB.h"
4dcb9e53
JH
5/* Workaround for XSUB.h bug under WIN32 */
6#ifdef WIN32
7# undef setjmp
c608f8c0
JH
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
dcefd27c 15# define NEED_sv_2pv_flags
0f1612a7
JH
16# include "ppport.h"
17# include "threads.h"
18#endif
68795e93 19
73e09c8f
JH
20#ifdef USE_ITHREADS
21
68795e93 22#ifdef WIN32
fc04eb16 23# include <windows.h>
514612b7
JH
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
JH
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
JH
43#if !defined(HAS_GETPAGESIZE) && defined(I_SYS_PARAM)
44# include <sys/param.h>
45#endif
68795e93 46
62375a60 47/* Values for 'state' member */
6ebc233e
RGS
48#define PERL_ITHR_DETACHED 1 /* Thread has been detached */
49#define PERL_ITHR_JOINED 2 /* Thread has been joined */
50#define PERL_ITHR_FINISHED 4 /* Thread has finished execution */
6158f8b3 51#define PERL_ITHR_THREAD_EXIT_ONLY 8 /* exit() only exits current thread */
6ebc233e
RGS
52#define PERL_ITHR_NONVIABLE 16 /* Thread creation failed */
53#define PERL_ITHR_DIED 32 /* Thread finished by dying */
6158f8b3
DM
54
55#define PERL_ITHR_UNCALLABLE (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)
56
fc04eb16
JH
57
58typedef struct _ithread {
59 struct _ithread *next; /* Next thread in the list */
60 struct _ithread *prev; /* Prev thread in the list */
61 PerlInterpreter *interp; /* The threads interpreter */
62 UV tid; /* Threads module's thread id */
63 perl_mutex mutex; /* Mutex for updating things in this struct */
6ebc233e 64 int count; /* Reference count. See S_ithread_create. */
fc04eb16
JH
65 int state; /* Detached, joined, finished, etc. */
66 int gimme; /* Context of create */
67 SV *init_function; /* Code to run */
68 SV *params; /* Args to pass function */
68795e93 69#ifdef WIN32
fc04eb16
JH
70 DWORD thr; /* OS's idea if thread id */
71 HANDLE handle; /* OS's waitable handle */
68795e93 72#else
fc04eb16 73 pthread_t thr; /* OS's handle for the thread */
68795e93 74#endif
514612b7 75 IV stack_size;
955c272e
JH
76 SV *err; /* Error from abnormally terminated thread */
77 char *err_class; /* Error object's classname if applicable */
8264cf32 78#ifndef WIN32
b762d866 79 sigset_t initial_sigmask; /* Thread wakes up with signals blocked */
8264cf32 80#endif
68795e93
NIS
81} ithread;
82
fc04eb16 83
5c6ff896 84#define MY_CXT_KEY "threads::_cxt" XS_VERSION
628ab322
DM
85
86typedef struct {
861d5cbe
JH
87 /* Used by Perl interpreter for thread context switching */
88 ithread *context;
628ab322
DM
89} my_cxt_t;
90
91START_MY_CXT
92
68795e93 93
5c6ff896 94#define MY_POOL_KEY "threads::_pool" XS_VERSION
68795e93 95
5c6ff896
JH
96typedef struct {
97 /* Structure for 'main' thread
98 * Also forms the 'base' for the doubly-linked list of threads */
99 ithread main_thread;
100
101 /* Protects the creation and destruction of threads*/
102 perl_mutex create_destruct_mutex;
103
104 UV tid_counter;
105 IV joinable_threads;
106 IV running_threads;
107 IV detached_threads;
e9a908c9 108 IV total_threads;
5c6ff896
JH
109 IV default_stack_size;
110 IV page_size;
111} my_pool_t;
112
113#define dMY_POOL \
114 SV *my_pool_sv = *hv_fetch(PL_modglobal, MY_POOL_KEY, \
115 sizeof(MY_POOL_KEY)-1, TRUE); \
116 my_pool_t *my_poolp = INT2PTR(my_pool_t*, SvUV(my_pool_sv))
117
118#define MY_POOL (*my_poolp)
c05ae023 119
8264cf32 120#ifndef WIN32
b762d866
JW
121/* Block most signals for calling thread, setting the old signal mask to
122 * oldmask, if it is not NULL */
123STATIC int
124S_block_most_signals(sigset_t *oldmask)
125{
126 sigset_t newmask;
127
128 sigfillset(&newmask);
129 /* Don't block certain "important" signals (stolen from mg.c) */
130#ifdef SIGILL
131 sigdelset(&newmask, SIGILL);
132#endif
133#ifdef SIGBUS
134 sigdelset(&newmask, SIGBUS);
135#endif
136#ifdef SIGSEGV
137 sigdelset(&newmask, SIGSEGV);
138#endif
139
8264cf32 140#if defined(VMS)
d2aa556d
CB
141 /* no per-thread blocking available */
142 return sigprocmask(SIG_BLOCK, &newmask, oldmask);
b762d866
JW
143#else
144 return pthread_sigmask(SIG_BLOCK, &newmask, oldmask);
bcbea5d2 145#endif /* VMS */
b762d866
JW
146}
147
148/* Set the signal mask for this thread to newmask */
149STATIC int
150S_set_sigmask(sigset_t *newmask)
151{
8264cf32 152#if defined(VMS)
d2aa556d 153 return sigprocmask(SIG_SETMASK, newmask, NULL);
b762d866
JW
154#else
155 return pthread_sigmask(SIG_SETMASK, newmask, NULL);
bcbea5d2 156#endif /* VMS */
b762d866 157}
bcbea5d2 158#endif /* WIN32 */
c05ae023 159
fc04eb16 160/* Used by Perl interpreter for thread context switching */
861d5cbe 161STATIC void
fc04eb16 162S_ithread_set(pTHX_ ithread *thread)
c05ae023 163{
628ab322 164 dMY_CXT;
861d5cbe 165 MY_CXT.context = thread;
c05ae023
AB
166}
167
861d5cbe 168STATIC ithread *
fc04eb16
JH
169S_ithread_get(pTHX)
170{
628ab322 171 dMY_CXT;
861d5cbe 172 return (MY_CXT.context);
c05ae023
AB
173}
174
175
fc04eb16
JH
176/* Free any data (such as the Perl interpreter) attached to an ithread
177 * structure. This is a bit like undef on SVs, where the SV isn't freed,
6ebc233e
RGS
178 * but the PVX is. Must be called with thread->mutex already locked. Also,
179 * must be called with MY_POOL.create_destruct_mutex unlocked as destruction
180 * of the interpreter can lead to recursive destruction calls that could
181 * lead to a deadlock on that mutex.
2e676467 182 */
861d5cbe 183STATIC void
fc04eb16 184S_ithread_clear(pTHX_ ithread *thread)
2e676467
DM
185{
186 PerlInterpreter *interp;
bcbea5d2 187#ifndef WIN32
b762d866 188 sigset_t origmask;
bcbea5d2 189#endif
fc04eb16 190
adc09a0e 191 assert(((thread->state & PERL_ITHR_FINISHED) &&
8718f9a1 192 (thread->state & PERL_ITHR_UNCALLABLE))
adc09a0e
JH
193 ||
194 (thread->state & PERL_ITHR_NONVIABLE));
2e676467 195
8264cf32 196#ifndef WIN32
b762d866
JW
197 /* We temporarily set the interpreter context to the interpreter being
198 * destroyed. It's in no condition to handle signals while it's being
199 * taken apart.
200 */
201 S_block_most_signals(&origmask);
8264cf32 202#endif
b762d866 203
2e676467
DM
204 interp = thread->interp;
205 if (interp) {
fc04eb16
JH
206 dTHXa(interp);
207
208 PERL_SET_CONTEXT(interp);
209 S_ithread_set(aTHX_ thread);
f2cba68d 210
fc04eb16
JH
211 SvREFCNT_dec(thread->params);
212 thread->params = Nullsv;
2e676467 213
955c272e
JH
214 if (thread->err) {
215 SvREFCNT_dec(thread->err);
216 thread->err = Nullsv;
217 }
218
fc04eb16 219 perl_destruct(interp);
9ca4d7fd 220 perl_free(interp);
fc04eb16 221 thread->interp = NULL;
2e676467 222 }
fc04eb16 223
2e676467 224 PERL_SET_CONTEXT(aTHX);
8264cf32 225#ifndef WIN32
b762d866 226 S_set_sigmask(&origmask);
8264cf32 227#endif
2e676467
DM
228}
229
68795e93 230
6158f8b3
DM
231/* Decrement the refcount of an ithread, and if it reaches zero, free it.
232 * Must be called with the mutex held.
6ebc233e
RGS
233 * On return, mutex is released (or destroyed).
234 */
861d5cbe 235STATIC void
6158f8b3 236S_ithread_free(pTHX_ ithread *thread)
68795e93 237{
385d56e4 238#ifdef WIN32
fc04eb16 239 HANDLE handle;
385d56e4 240#endif
adc09a0e
JH
241 dMY_POOL;
242
6158f8b3
DM
243 if (! (thread->state & PERL_ITHR_NONVIABLE)) {
244 assert(thread->count > 0);
245 if (--thread->count > 0) {
246 MUTEX_UNLOCK(&thread->mutex);
247 return;
248 }
6ebc233e
RGS
249 assert((thread->state & PERL_ITHR_FINISHED) &&
250 (thread->state & PERL_ITHR_UNCALLABLE));
fc04eb16 251 }
adc09a0e 252 MUTEX_UNLOCK(&thread->mutex);
9feacc09 253
fc04eb16
JH
254 /* Main thread (0) is immortal and should never get here */
255 assert(thread->tid != 0);
256
257 /* Remove from circular list of threads */
5c6ff896 258 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
adc09a0e
JH
259 assert(thread->prev && thread->next);
260 thread->next->prev = thread->prev;
261 thread->prev->next = thread->next;
fc04eb16
JH
262 thread->next = NULL;
263 thread->prev = NULL;
5c6ff896 264 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
c2f2a82b 265
fc04eb16 266 /* Thread is now disowned */
9ca4d7fd 267 MUTEX_LOCK(&thread->mutex);
fc04eb16 268 S_ithread_clear(aTHX_ thread);
385d56e4
JH
269
270#ifdef WIN32
fc04eb16
JH
271 handle = thread->handle;
272 thread->handle = NULL;
385d56e4 273#endif
fc04eb16
JH
274 MUTEX_UNLOCK(&thread->mutex);
275 MUTEX_DESTROY(&thread->mutex);
385d56e4 276
c7667023 277#ifdef WIN32
fea7688c 278 if (handle) {
fc04eb16 279 CloseHandle(handle);
fea7688c 280 }
c7667023 281#endif
385d56e4 282
fc04eb16 283 PerlMemShared_free(thread);
ae3fba3d
DM
284
285 /* total_threads >= 1 is used to veto cleanup by the main thread,
286 * should it happen to exit while other threads still exist.
6ebc233e
RGS
287 * Decrement this as the very last thing in the thread's existence.
288 * Otherwise, MY_POOL and global state such as PL_op_mutex may get
289 * freed while we're still using it.
ae3fba3d
DM
290 */
291 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
292 MY_POOL.total_threads--;
293 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
68795e93
NIS
294}
295
fc04eb16 296
6158f8b3
DM
297static void
298S_ithread_count_inc(pTHX_ ithread *thread)
299{
300 MUTEX_LOCK(&thread->mutex);
301 thread->count++;
302 MUTEX_UNLOCK(&thread->mutex);
303}
304
305
69a9b4b8 306/* Warn if exiting with any unjoined threads */
861d5cbe 307STATIC int
69a9b4b8 308S_exit_warning(pTHX)
62375a60 309{
e9a908c9 310 int veto_cleanup, warn;
adc09a0e 311 dMY_POOL;
69a9b4b8 312
5c6ff896 313 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
e9a908c9
DM
314 veto_cleanup = (MY_POOL.total_threads > 0);
315 warn = (MY_POOL.running_threads || MY_POOL.joinable_threads);
5c6ff896 316 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
60bd5ef6 317
e9a908c9 318 if (warn) {
fc04eb16 319 if (ckWARN_d(WARN_THREADS)) {
4dcb9e53
JH
320 Perl_warn(aTHX_ "Perl exited with active threads:\n\t%"
321 IVdf " running and unjoined\n\t%"
322 IVdf " finished and unjoined\n\t%"
323 IVdf " running and detached\n",
5c6ff896
JH
324 MY_POOL.running_threads,
325 MY_POOL.joinable_threads,
326 MY_POOL.detached_threads);
fc04eb16 327 }
62375a60 328 }
69a9b4b8 329
fc04eb16 330 return (veto_cleanup);
62375a60
NIS
331}
332
ae3fba3d 333
6ebc233e
RGS
334/* Called from perl_destruct() in each thread. If it's the main thread,
335 * stop it from freeing everything if there are other threads still running.
336 */
69a9b4b8
RGS
337int
338Perl_ithread_hook(pTHX)
339{
5c6ff896 340 dMY_POOL;
b5c80a23 341 return ((aTHX == MY_POOL.main_thread.interp) ? S_exit_warning(aTHX) : 0);
69a9b4b8
RGS
342}
343
68795e93
NIS
344
345/* MAGIC (in mg.h sense) hooks */
346
347int
348ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
349{
fc04eb16 350 ithread *thread = (ithread *)mg->mg_ptr;
45977657 351 SvIV_set(sv, PTR2IV(thread));
68795e93 352 SvIOK_on(sv);
fc04eb16 353 return (0);
68795e93
NIS
354}
355
356int
357ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
358{
f2cba68d 359 ithread *thread = (ithread *)mg->mg_ptr;
68795e93 360 MUTEX_LOCK(&thread->mutex);
6ebc233e 361 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
fc04eb16 362 return (0);
68795e93
NIS
363}
364
365int
366ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
367{
6158f8b3 368 S_ithread_count_inc(aTHX_ (ithread *)mg->mg_ptr);
fc04eb16 369 return (0);
68795e93
NIS
370}
371
372MGVTBL ithread_vtbl = {
fc04eb16
JH
373 ithread_mg_get, /* get */
374 0, /* set */
375 0, /* len */
376 0, /* clear */
377 ithread_mg_free, /* free */
378 0, /* copy */
379 ithread_mg_dup /* dup */
68795e93
NIS
380};
381
47ba8780 382
514612b7 383/* Provided default, minimum and rational stack sizes */
861d5cbe
JH
384STATIC IV
385S_good_stack_size(pTHX_ IV stack_size)
514612b7 386{
5c6ff896
JH
387 dMY_POOL;
388
514612b7 389 /* Use default stack size if no stack size specified */
fea7688c 390 if (! stack_size) {
5c6ff896 391 return (MY_POOL.default_stack_size);
fea7688c 392 }
514612b7
JH
393
394#ifdef PTHREAD_STACK_MIN
395 /* Can't use less than minimum */
396 if (stack_size < PTHREAD_STACK_MIN) {
4dcb9e53 397 if (ckWARN(WARN_THREADS)) {
514612b7
JH
398 Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
399 }
400 return (PTHREAD_STACK_MIN);
401 }
402#endif
403
404 /* Round up to page size boundary */
5c6ff896 405 if (MY_POOL.page_size <= 0) {
d305c2c9 406#if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
514612b7 407 SETERRNO(0, SS_NORMAL);
d305c2c9 408# ifdef _SC_PAGESIZE
5c6ff896 409 MY_POOL.page_size = sysconf(_SC_PAGESIZE);
d305c2c9 410# else
5c6ff896 411 MY_POOL.page_size = sysconf(_SC_MMAP_PAGE_SIZE);
d305c2c9 412# endif
5c6ff896 413 if ((long)MY_POOL.page_size < 0) {
514612b7 414 if (errno) {
8583b257 415 SV * const error = get_sv("@", 0);
514612b7
JH
416 (void)SvUPGRADE(error, SVt_PV);
417 Perl_croak(aTHX_ "PANIC: sysconf: %s", SvPV_nolen(error));
418 } else {
419 Perl_croak(aTHX_ "PANIC: sysconf: pagesize unknown");
420 }
421 }
d305c2c9
JH
422#else
423# ifdef HAS_GETPAGESIZE
5c6ff896 424 MY_POOL.page_size = getpagesize();
514612b7 425# else
d305c2c9 426# if defined(I_SYS_PARAM) && defined(PAGESIZE)
5c6ff896 427 MY_POOL.page_size = PAGESIZE;
d305c2c9 428# else
5c6ff896 429 MY_POOL.page_size = 8192; /* A conservative default */
d305c2c9 430# endif
514612b7 431# endif
5c6ff896
JH
432 if (MY_POOL.page_size <= 0) {
433 Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)MY_POOL.page_size);
fea7688c 434 }
514612b7
JH
435#endif
436 }
5c6ff896 437 stack_size = ((stack_size + (MY_POOL.page_size - 1)) / MY_POOL.page_size) * MY_POOL.page_size;
514612b7
JH
438
439 return (stack_size);
440}
441
442
fc04eb16
JH
443/* Starts executing the thread.
444 * Passed as the C level function to run in the new thread.
b1edfb69 445 */
47ba8780 446#ifdef WIN32
861d5cbe 447STATIC THREAD_RET_TYPE
fc04eb16 448S_ithread_run(LPVOID arg)
47ba8780 449#else
861d5cbe 450STATIC void *
fc04eb16 451S_ithread_run(void * arg)
47ba8780 452#endif
fc04eb16
JH
453{
454 ithread *thread = (ithread *)arg;
69a9b4b8
RGS
455 int jmp_rc = 0;
456 I32 oldscope;
955c272e 457 int exit_app = 0; /* Thread terminated using 'exit' */
69a9b4b8 458 int exit_code = 0;
955c272e 459 int died = 0; /* Thread terminated abnormally */
f2cba68d 460
69a9b4b8
RGS
461 dJMPENV;
462
fc04eb16 463 dTHXa(thread->interp);
47ba8780 464
5c6ff896
JH
465 dMY_POOL;
466
9ca4d7fd 467 /* Blocked until ->create() call finishes */
fc04eb16 468 MUTEX_LOCK(&thread->mutex);
fc04eb16 469 MUTEX_UNLOCK(&thread->mutex);
9ca4d7fd
JH
470
471 PERL_SET_CONTEXT(thread->interp);
472 S_ithread_set(aTHX_ thread);
47ba8780 473
8264cf32 474#ifndef WIN32
b762d866
JW
475 /* Thread starts with most signals blocked - restore the signal mask from
476 * the ithread struct.
477 */
478 S_set_sigmask(&thread->initial_sigmask);
8264cf32 479#endif
b762d866 480
fc04eb16 481 PL_perl_destruct_level = 2;
f2cba68d 482
fc04eb16
JH
483 {
484 AV *params = (AV *)SvRV(thread->params);
485 int len = (int)av_len(params)+1;
486 int ii;
487
488 dSP;
489 ENTER;
490 SAVETMPS;
491
492 /* Put args on the stack */
493 PUSHMARK(SP);
494 for (ii=0; ii < len; ii++) {
495 XPUSHs(av_shift(params));
496 }
497 PUTBACK;
498
4dcb9e53
JH
499 oldscope = PL_scopestack_ix;
500 JMPENV_PUSH(jmp_rc);
501 if (jmp_rc == 0) {
502 /* Run the specified function */
503 len = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
504 } else if (jmp_rc == 2) {
69a9b4b8
RGS
505 /* Thread exited */
506 exit_app = 1;
507 exit_code = STATUS_CURRENT;
4dcb9e53
JH
508 while (PL_scopestack_ix > oldscope) {
509 LEAVE;
510 }
511 }
512 JMPENV_POP;
fc04eb16 513
8264cf32 514#ifndef WIN32
b762d866
JW
515 /* The interpreter is finished, so this thread can stop receiving
516 * signals. This way, our signal handler doesn't get called in the
517 * middle of our parent thread calling perl_destruct()...
518 */
519 S_block_most_signals(NULL);
8264cf32 520#endif
b762d866 521
fc04eb16
JH
522 /* Remove args from stack and put back in params array */
523 SPAGAIN;
524 for (ii=len-1; ii >= 0; ii--) {
525 SV *sv = POPs;
7df0357e 526 if (jmp_rc == 0 && (thread->gimme & G_WANT) != G_VOID) {
4dcb9e53
JH
527 av_store(params, ii, SvREFCNT_inc(sv));
528 }
fc04eb16
JH
529 }
530
4dcb9e53
JH
531 FREETMPS;
532 LEAVE;
533
955c272e
JH
534 /* Check for abnormal termination */
535 if (SvTRUE(ERRSV)) {
536 died = PERL_ITHR_DIED;
537 thread->err = newSVsv(ERRSV);
538 /* If ERRSV is an object, remember the classname and then
539 * rebless into 'main' so it will survive 'cloning'
540 */
541 if (sv_isobject(thread->err)) {
542 thread->err_class = HvNAME(SvSTASH(SvRV(thread->err)));
543 sv_bless(thread->err, gv_stashpv("main", 0));
544 }
545
546 if (ckWARN_d(WARN_THREADS)) {
547 oldscope = PL_scopestack_ix;
548 JMPENV_PUSH(jmp_rc);
549 if (jmp_rc == 0) {
550 /* Warn that thread died */
551 Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV);
552 } else if (jmp_rc == 2) {
553 /* Warn handler exited */
554 exit_app = 1;
555 exit_code = STATUS_CURRENT;
556 while (PL_scopestack_ix > oldscope) {
557 LEAVE;
558 }
4dcb9e53 559 }
955c272e 560 JMPENV_POP;
4dcb9e53 561 }
fc04eb16
JH
562 }
563
fc04eb16
JH
564 /* Release function ref */
565 SvREFCNT_dec(thread->init_function);
566 thread->init_function = Nullsv;
567 }
62375a60 568
fc04eb16
JH
569 PerlIO_flush((PerlIO *)NULL);
570
5c6ff896 571 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
fc04eb16
JH
572 MUTEX_LOCK(&thread->mutex);
573 /* Mark as finished */
955c272e 574 thread->state |= (PERL_ITHR_FINISHED | died);
69a9b4b8 575 /* Clear exit flag if required */
fea7688c 576 if (thread->state & PERL_ITHR_THREAD_EXIT_ONLY) {
69a9b4b8 577 exit_app = 0;
fea7688c 578 }
fc04eb16 579
69a9b4b8 580 /* Adjust thread status counts */
adc09a0e 581 if (thread->state & PERL_ITHR_DETACHED) {
5c6ff896 582 MY_POOL.detached_threads--;
4dcb9e53 583 } else {
5c6ff896
JH
584 MY_POOL.running_threads--;
585 MY_POOL.joinable_threads++;
5168baf3 586 }
adc09a0e 587 MUTEX_UNLOCK(&thread->mutex);
5c6ff896 588 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
69a9b4b8
RGS
589
590 /* Exit application if required */
591 if (exit_app) {
592 oldscope = PL_scopestack_ix;
593 JMPENV_PUSH(jmp_rc);
594 if (jmp_rc == 0) {
595 /* Warn if there are unjoined threads */
596 S_exit_warning(aTHX);
597 } else if (jmp_rc == 2) {
598 /* Warn handler exited */
599 exit_code = STATUS_CURRENT;
600 while (PL_scopestack_ix > oldscope) {
601 LEAVE;
602 }
603 }
604 JMPENV_POP;
605
606 my_exit(exit_code);
607 }
608
6ebc233e
RGS
609 /* At this point, the interpreter may have been freed, so call
610 * free in the the context of of the 'main' interpreter which
611 * can't have been freed due to the veto_cleanup mechanism.
612 */
46c5d8f1
DM
613 aTHX = MY_POOL.main_thread.interp;
614
6158f8b3 615 MUTEX_LOCK(&thread->mutex);
6ebc233e 616 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
91604d21 617
47ba8780 618#ifdef WIN32
fc04eb16 619 return ((DWORD)0);
e8f2bb9a 620#else
fc04eb16 621 return (0);
47ba8780 622#endif
68795e93
NIS
623}
624
fc04eb16
JH
625
626/* Type conversion helper functions */
fea7688c 627
861d5cbe
JH
628STATIC SV *
629S_ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
68795e93
NIS
630{
631 SV *sv;
632 MAGIC *mg;
fc04eb16 633
6158f8b3
DM
634 if (inc)
635 S_ithread_count_inc(aTHX_ thread);
fc04eb16
JH
636
637 if (! obj) {
638 obj = newSV(0);
68795e93 639 }
fc04eb16
JH
640
641 sv = newSVrv(obj, classname);
642 sv_setiv(sv, PTR2IV(thread));
643 mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0);
68795e93
NIS
644 mg->mg_flags |= MGf_DUP;
645 SvREADONLY_on(sv);
fc04eb16
JH
646
647 return (obj);
68795e93 648}
47ba8780 649
861d5cbe
JH
650STATIC ithread *
651S_SV_to_ithread(pTHX_ SV *sv)
68795e93 652{
fc04eb16
JH
653 /* Argument is a thread */
654 if (SvROK(sv)) {
655 return (INT2PTR(ithread *, SvIV(SvRV(sv))));
656 }
657 /* Argument is classname, therefore return current thread */
658 return (S_ithread_get(aTHX));
47ba8780
AB
659}
660
47ba8780 661
fc04eb16
JH
662/* threads->create()
663 * Called in context of parent thread.
5c6ff896 664 * Called with MY_POOL.create_destruct_mutex locked. (Unlocked on error.)
fc04eb16 665 */
861d5cbe 666STATIC ithread *
fc04eb16 667S_ithread_create(
9ca4d7fd 668 pTHX_ SV *init_function,
514612b7 669 IV stack_size,
9d9ff5b1 670 int gimme,
69a9b4b8 671 int exit_opt,
fc04eb16 672 SV *params)
68795e93 673{
fc04eb16 674 ithread *thread;
fc04eb16 675 ithread *current_thread = S_ithread_get(aTHX);
3b1c3273 676
fc04eb16
JH
677 SV **tmps_tmp = PL_tmps_stack;
678 IV tmps_ix = PL_tmps_ix;
d94006e8 679#ifndef WIN32
fc04eb16
JH
680 int rc_stack_size = 0;
681 int rc_thread_create = 0;
d94006e8 682#endif
adc09a0e 683 dMY_POOL;
3b1c3273 684
adc09a0e 685 /* Allocate thread structure in context of the main thread's interpreter */
5c6ff896
JH
686 {
687 PERL_SET_CONTEXT(MY_POOL.main_thread.interp);
688 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
689 }
690 PERL_SET_CONTEXT(aTHX);
fc04eb16 691 if (!thread) {
5c6ff896 692 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
fc04eb16
JH
693 PerlLIO_write(PerlIO_fileno(Perl_error_log), PL_no_mem, strlen(PL_no_mem));
694 my_exit(1);
695 }
696 Zero(thread, 1, ithread);
697
698 /* Add to threads list */
5c6ff896
JH
699 thread->next = &MY_POOL.main_thread;
700 thread->prev = MY_POOL.main_thread.prev;
701 MY_POOL.main_thread.prev = thread;
fc04eb16 702 thread->prev->next = thread;
e9a908c9 703 MY_POOL.total_threads++;
c05ae023 704
6ebc233e 705 /* 1 ref to be held by the local var 'thread' in S_ithread_run().
6158f8b3 706 * 1 ref to be held by the threads object that we assume we will
6ebc233e
RGS
707 * be embedded in upon our return.
708 * 1 ref to be the responsibility of join/detach, so we don't get
709 * freed until join/detach, even if no thread objects remain.
710 * This allows the following to work:
878090d5 711 * { threads->create(sub{...}); } threads->object(1)->join;
fc04eb16 712 */
6158f8b3 713 thread->count = 3;
fc04eb16 714
9ca4d7fd 715 /* Block new thread until ->create() call finishes */
fc04eb16 716 MUTEX_INIT(&thread->mutex);
9ca4d7fd
JH
717 MUTEX_LOCK(&thread->mutex);
718
5c6ff896 719 thread->tid = MY_POOL.tid_counter++;
861d5cbe 720 thread->stack_size = S_good_stack_size(aTHX_ stack_size);
9d9ff5b1 721 thread->gimme = gimme;
69a9b4b8 722 thread->state = exit_opt;
fc04eb16
JH
723
724 /* "Clone" our interpreter into the thread's interpreter.
725 * This gives thread access to "static data" and code.
726 */
727 PerlIO_flush((PerlIO *)NULL);
728 S_ithread_set(aTHX_ thread);
729
730 SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct value */
731 PL_srand_called = FALSE; /* Set it to false so we can detect if it gets
732 set during the clone */
3b1c3273 733
8264cf32 734#ifndef WIN32
b762d866
JW
735 /* perl_clone() will leave us the new interpreter's context. This poses
736 * two problems for our signal handler. First, it sets the new context
737 * before the new interpreter struct is fully initialized, so our signal
738 * handler might find bogus data in the interpreter struct it gets.
739 * Second, even if the interpreter is initialized before a signal comes in,
740 * we would like to avoid that interpreter receiving notifications for
741 * signals (especially when they ought to be for the one running in this
742 * thread), until it is running in its own thread. Another problem is that
743 * the new thread will not have set the context until some time after it
744 * has started, so it won't be safe for our signal handler to run until
745 * that time.
746 *
747 * So we block most signals here, so the new thread will inherit the signal
748 * mask, and unblock them right after the thread creation. The original
749 * mask is saved in the thread struct so that the new thread can restore
750 * the original mask.
751 */
752 S_block_most_signals(&thread->initial_sigmask);
8264cf32 753#endif
b762d866 754
47ba8780 755#ifdef WIN32
fc04eb16 756 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
47ba8780 757#else
fc04eb16 758 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
47ba8780 759#endif
47ba8780 760
fc04eb16
JH
761 /* perl_clone() leaves us in new interpreter's context. As it is tricky
762 * to spot an implicit aTHX, create a new scope with aTHX matching the
763 * context for the duration of our work for new interpreter.
764 */
765 {
894eec8b
JH
766 CLONE_PARAMS clone_param;
767
fc04eb16
JH
768 dTHXa(thread->interp);
769
770 MY_CXT_CLONE;
771
772 /* Here we remove END blocks since they should only run in the thread
773 * they are created
774 */
775 SvREFCNT_dec(PL_endav);
776 PL_endav = newAV();
404aaa48 777
894eec8b 778 clone_param.flags = 0;
f2e0bb91
JH
779 if (SvPOK(init_function)) {
780 thread->init_function = newSV(0);
781 sv_copypv(thread->init_function, init_function);
782 } else {
dd5ef8e0
DM
783 thread->init_function =
784 SvREFCNT_inc(sv_dup(init_function, &clone_param));
fc04eb16
JH
785 }
786
787 thread->params = sv_dup(params, &clone_param);
d4315dd6 788 SvREFCNT_inc_void(thread->params);
fc04eb16
JH
789
790 /* The code below checks that anything living on the tmps stack and
791 * has been cloned (so it lives in the ptr_table) has a refcount
792 * higher than 0.
793 *
794 * If the refcount is 0 it means that a something on the stack/context
795 * was holding a reference to it and since we init_stacks() in
796 * perl_clone that won't get cleaned and we will get a leaked scalar.
797 * The reason it was cloned was that it lived on the @_ stack.
798 *
799 * Example of this can be found in bugreport 15837 where calls in the
800 * parameter list end up as a temp.
801 *
802 * One could argue that this fix should be in perl_clone.
803 */
804 while (tmps_ix > 0) {
805 SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
806 tmps_ix--;
807 if (sv && SvREFCNT(sv) == 0) {
d4315dd6 808 SvREFCNT_inc_void(sv);
fc04eb16
JH
809 SvREFCNT_dec(sv);
810 }
811 }
812
813 SvTEMP_off(thread->init_function);
814 ptr_table_free(PL_ptr_table);
815 PL_ptr_table = NULL;
816 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
817 }
818 S_ithread_set(aTHX_ current_thread);
819 PERL_SET_CONTEXT(aTHX);
820
821 /* Create/start the thread */
47ba8780 822#ifdef WIN32
fc04eb16 823 thread->handle = CreateThread(NULL,
514612b7 824 (DWORD)thread->stack_size,
fc04eb16
JH
825 S_ithread_run,
826 (LPVOID)thread,
514612b7 827 STACK_SIZE_PARAM_IS_A_RESERVATION,
fc04eb16 828 &thread->thr);
82c40bf6 829#else
fc04eb16 830 {
861d5cbe
JH
831 STATIC pthread_attr_t attr;
832 STATIC int attr_inited = 0;
833 STATIC int attr_joinable = PTHREAD_CREATE_JOINABLE;
fc04eb16
JH
834 if (! attr_inited) {
835 pthread_attr_init(&attr);
836 attr_inited = 1;
837 }
838
fa26028c 839# ifdef PTHREAD_ATTR_SETDETACHSTATE
fc04eb16
JH
840 /* Threads start out joinable */
841 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
fa26028c 842# endif
fc04eb16 843
514612b7 844# ifdef _POSIX_THREAD_ATTR_STACKSIZE
fc04eb16 845 /* Set thread's stack size */
514612b7
JH
846 if (thread->stack_size > 0) {
847 rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
848 }
3eb37d38
AB
849# endif
850
fc04eb16
JH
851 /* Create the thread */
852 if (! rc_stack_size) {
853# ifdef OLD_PTHREADS_API
854 rc_thread_create = pthread_create(&thread->thr,
855 attr,
856 S_ithread_run,
857 (void *)thread);
858# else
859# if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
860 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
861# endif
862 rc_thread_create = pthread_create(&thread->thr,
863 &attr,
864 S_ithread_run,
865 (void *)thread);
19a077f6 866# endif
fc04eb16 867 }
514612b7 868
8264cf32 869#ifndef WIN32
b762d866
JW
870 /* Now it's safe to accept signals, since we're in our own interpreter's
871 * context and we have created the thread.
872 */
873 S_set_sigmask(&thread->initial_sigmask);
8264cf32 874#endif
b762d866 875
514612b7
JH
876# ifdef _POSIX_THREAD_ATTR_STACKSIZE
877 /* Try to get thread's actual stack size */
878 {
879 size_t stacksize;
58a3a76c
JH
880#ifdef HPUX1020
881 stacksize = pthread_attr_getstacksize(attr);
882#else
883 if (! pthread_attr_getstacksize(&attr, &stacksize))
884#endif
885 if (stacksize > 0) {
514612b7
JH
886 thread->stack_size = (IV)stacksize;
887 }
514612b7
JH
888 }
889# endif
fc04eb16 890 }
82c40bf6 891#endif
bcd9ca9b 892
fc04eb16 893 /* Check for errors */
d94006e8 894#ifdef WIN32
fc04eb16 895 if (thread->handle == NULL) {
d94006e8 896#else
fc04eb16 897 if (rc_stack_size || rc_thread_create) {
d94006e8 898#endif
9ca4d7fd 899 /* Must unlock mutex for destruct call */
5c6ff896 900 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
fc04eb16 901 sv_2mortal(params);
adc09a0e 902 thread->state |= PERL_ITHR_NONVIABLE;
6ebc233e 903 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
d94006e8 904#ifndef WIN32
514612b7 905 if (ckWARN_d(WARN_THREADS)) {
fea7688c 906 if (rc_stack_size) {
514612b7 907 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
fea7688c 908 } else {
514612b7 909 Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
fea7688c 910 }
514612b7 911 }
d94006e8 912#endif
9ca4d7fd 913 return (NULL);
fc04eb16
JH
914 }
915
5c6ff896 916 MY_POOL.running_threads++;
fc04eb16 917 sv_2mortal(params);
9ca4d7fd 918 return (thread);
68795e93 919}
47ba8780 920
73e09c8f 921#endif /* USE_ITHREADS */
e1c44605 922
fcea4b7c 923
fc04eb16 924MODULE = threads PACKAGE = threads PREFIX = ithread_
68795e93 925PROTOTYPES: DISABLE
8222d950 926
73e09c8f
JH
927#ifdef USE_ITHREADS
928
68795e93 929void
f4cc38af
JH
930ithread_create(...)
931 PREINIT:
932 char *classname;
514612b7 933 ithread *thread;
f4cc38af
JH
934 SV *function_to_call;
935 AV *params;
514612b7
JH
936 HV *specs;
937 IV stack_size;
9d9ff5b1 938 int context;
69a9b4b8
RGS
939 int exit_opt;
940 SV *thread_exit_only;
9d9ff5b1 941 char *str;
514612b7 942 int idx;
f4cc38af 943 int ii;
5c6ff896 944 dMY_POOL;
f4cc38af 945 CODE:
514612b7 946 if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
fea7688c 947 if (--items < 2) {
18b9e6f5 948 Perl_croak(aTHX_ "Usage: threads->create(\\%%specs, function, ...)");
fea7688c 949 }
514612b7
JH
950 specs = (HV*)SvRV(ST(1));
951 idx = 1;
952 } else {
fea7688c 953 if (items < 2) {
514612b7 954 Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
fea7688c 955 }
514612b7
JH
956 specs = NULL;
957 idx = 0;
958 }
f4cc38af 959
514612b7
JH
960 if (sv_isobject(ST(0))) {
961 /* $thr->create() */
962 classname = HvNAME(SvSTASH(SvRV(ST(0))));
963 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
8718f9a1 964 MUTEX_LOCK(&thread->mutex);
514612b7 965 stack_size = thread->stack_size;
69a9b4b8 966 exit_opt = thread->state & PERL_ITHR_THREAD_EXIT_ONLY;
8718f9a1 967 MUTEX_UNLOCK(&thread->mutex);
514612b7
JH
968 } else {
969 /* threads->create() */
970 classname = (char *)SvPV_nolen(ST(0));
5c6ff896 971 stack_size = MY_POOL.default_stack_size;
8583b257 972 thread_exit_only = get_sv("threads::thread_exit_only", GV_ADD);
69a9b4b8
RGS
973 exit_opt = (SvTRUE(thread_exit_only))
974 ? PERL_ITHR_THREAD_EXIT_ONLY : 0;
514612b7
JH
975 }
976
977 function_to_call = ST(idx+1);
978
9d9ff5b1 979 context = -1;
514612b7
JH
980 if (specs) {
981 /* stack_size */
982 if (hv_exists(specs, "stack", 5)) {
983 stack_size = SvIV(*hv_fetch(specs, "stack", 5, 0));
984 } else if (hv_exists(specs, "stacksize", 9)) {
985 stack_size = SvIV(*hv_fetch(specs, "stacksize", 9, 0));
986 } else if (hv_exists(specs, "stack_size", 10)) {
987 stack_size = SvIV(*hv_fetch(specs, "stack_size", 10, 0));
988 }
9d9ff5b1
JH
989
990 /* context */
991 if (hv_exists(specs, "context", 7)) {
992 str = (char *)SvPV_nolen(*hv_fetch(specs, "context", 7, 0));
993 switch (*str) {
994 case 'a':
995 case 'A':
da140a40
JH
996 case 'l':
997 case 'L':
9d9ff5b1
JH
998 context = G_ARRAY;
999 break;
1000 case 's':
1001 case 'S':
1002 context = G_SCALAR;
1003 break;
1004 case 'v':
1005 case 'V':
1006 context = G_VOID;
1007 break;
1008 default:
1009 Perl_croak(aTHX_ "Invalid context: %s", str);
1010 }
1011 } else if (hv_exists(specs, "array", 5)) {
1012 if (SvTRUE(*hv_fetch(specs, "array", 5, 0))) {
1013 context = G_ARRAY;
1014 }
da140a40
JH
1015 } else if (hv_exists(specs, "list", 4)) {
1016 if (SvTRUE(*hv_fetch(specs, "list", 4, 0))) {
1017 context = G_ARRAY;
1018 }
9d9ff5b1
JH
1019 } else if (hv_exists(specs, "scalar", 6)) {
1020 if (SvTRUE(*hv_fetch(specs, "scalar", 6, 0))) {
1021 context = G_SCALAR;
1022 }
1023 } else if (hv_exists(specs, "void", 4)) {
1024 if (SvTRUE(*hv_fetch(specs, "void", 4, 0))) {
1025 context = G_VOID;
1026 }
1027 }
69a9b4b8
RGS
1028
1029 /* exit => thread_only */
1030 if (hv_exists(specs, "exit", 4)) {
1031 str = (char *)SvPV_nolen(*hv_fetch(specs, "exit", 4, 0));
1032 exit_opt = (*str == 't' || *str == 'T')
1033 ? PERL_ITHR_THREAD_EXIT_ONLY : 0;
1034 }
9d9ff5b1
JH
1035 }
1036 if (context == -1) {
1037 context = GIMME_V; /* Implicit context */
1038 } else {
1039 context |= (GIMME_V & (~(G_ARRAY|G_SCALAR|G_VOID)));
514612b7 1040 }
f4cc38af
JH
1041
1042 /* Function args */
1043 params = newAV();
1044 if (items > 2) {
514612b7
JH
1045 for (ii=2; ii < items ; ii++) {
1046 av_push(params, SvREFCNT_inc(ST(idx+ii)));
f4cc38af
JH
1047 }
1048 }
1049
1050 /* Create thread */
5c6ff896 1051 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
9ca4d7fd
JH
1052 thread = S_ithread_create(aTHX_ function_to_call,
1053 stack_size,
1054 context,
1055 exit_opt,
1056 newRV_noinc((SV*)params));
1057 if (! thread) {
1058 XSRETURN_UNDEF; /* Mutex already unlocked */
1059 }
861d5cbe 1060 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, FALSE));
adc09a0e 1061 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
9ca4d7fd
JH
1062
1063 /* Let thread run */
1064 MUTEX_UNLOCK(&thread->mutex);
9ca4d7fd 1065
f4cc38af
JH
1066 /* XSRETURN(1); - implied */
1067
8222d950 1068
68795e93 1069void
f4cc38af
JH
1070ithread_list(...)
1071 PREINIT:
1072 char *classname;
fc04eb16 1073 ithread *thread;
f4cc38af
JH
1074 int list_context;
1075 IV count = 0;
11db694d 1076 int want_running = 0;
8718f9a1 1077 int state;
5c6ff896 1078 dMY_POOL;
f4cc38af
JH
1079 PPCODE:
1080 /* Class method only */
fea7688c 1081 if (SvROK(ST(0))) {
ead32952 1082 Perl_croak(aTHX_ "Usage: threads->list(...)");
fea7688c 1083 }
f4cc38af
JH
1084 classname = (char *)SvPV_nolen(ST(0));
1085
1086 /* Calling context */
1087 list_context = (GIMME_V == G_ARRAY);
1088
ead32952
JH
1089 /* Running or joinable parameter */
1090 if (items > 1) {
1091 want_running = SvTRUE(ST(1));
1092 }
1093
f4cc38af 1094 /* Walk through threads list */
5c6ff896
JH
1095 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1096 for (thread = MY_POOL.main_thread.next;
1097 thread != &MY_POOL.main_thread;
fc04eb16 1098 thread = thread->next)
f4cc38af 1099 {
8718f9a1
JH
1100 MUTEX_LOCK(&thread->mutex);
1101 state = thread->state;
1102 MUTEX_UNLOCK(&thread->mutex);
1103
f4cc38af 1104 /* Ignore detached or joined threads */
8718f9a1 1105 if (state & PERL_ITHR_UNCALLABLE) {
f4cc38af
JH
1106 continue;
1107 }
ead32952
JH
1108
1109 /* Filter per parameter */
1110 if (items > 1) {
1111 if (want_running) {
8718f9a1 1112 if (state & PERL_ITHR_FINISHED) {
ead32952
JH
1113 continue; /* Not running */
1114 }
1115 } else {
8718f9a1 1116 if (! (state & PERL_ITHR_FINISHED)) {
ead32952
JH
1117 continue; /* Still running - not joinable yet */
1118 }
1119 }
1120 }
1121
f4cc38af
JH
1122 /* Push object on stack if list context */
1123 if (list_context) {
861d5cbe 1124 XPUSHs(sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
f4cc38af
JH
1125 }
1126 count++;
1127 }
5c6ff896 1128 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
f4cc38af
JH
1129 /* If scalar context, send back count */
1130 if (! list_context) {
1131 XSRETURN_IV(count);
1132 }
678a9b6c
AB
1133
1134
1135void
f4cc38af
JH
1136ithread_self(...)
1137 PREINIT:
1138 char *classname;
fcea4b7c 1139 ithread *thread;
f4cc38af
JH
1140 CODE:
1141 /* Class method only */
11db694d 1142 if ((items != 1) || SvROK(ST(0))) {
f4cc38af 1143 Perl_croak(aTHX_ "Usage: threads->self()");
fea7688c 1144 }
f4cc38af
JH
1145 classname = (char *)SvPV_nolen(ST(0));
1146
fcea4b7c
JH
1147 thread = S_ithread_get(aTHX);
1148
861d5cbe 1149 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
f4cc38af 1150 /* XSRETURN(1); - implied */
47ba8780 1151
47ba8780
AB
1152
1153void
f4cc38af
JH
1154ithread_tid(...)
1155 PREINIT:
1156 ithread *thread;
1157 CODE:
11db694d 1158 PERL_UNUSED_VAR(items);
861d5cbe 1159 thread = S_SV_to_ithread(aTHX_ ST(0));
f4cc38af
JH
1160 XST_mUV(0, thread->tid);
1161 /* XSRETURN(1); - implied */
1162
e1c44605 1163
f9dff5f5 1164void
f4cc38af
JH
1165ithread_join(...)
1166 PREINIT:
fcea4b7c 1167 ithread *thread;
8718f9a1 1168 ithread *current_thread;
fcea4b7c 1169 int join_err;
6ebc233e 1170 AV *params = NULL;
f4cc38af
JH
1171 int len;
1172 int ii;
12701bb8 1173#ifndef WIN32
8718f9a1 1174 int rc_join;
fcea4b7c
JH
1175 void *retval;
1176#endif
5c6ff896 1177 dMY_POOL;
f4cc38af
JH
1178 PPCODE:
1179 /* Object method only */
11db694d 1180 if ((items != 1) || ! sv_isobject(ST(0))) {
f4cc38af 1181 Perl_croak(aTHX_ "Usage: $thr->join()");
fea7688c 1182 }
f4cc38af 1183
8718f9a1 1184 /* Check if the thread is joinable and not ourselves */
861d5cbe 1185 thread = S_SV_to_ithread(aTHX_ ST(0));
8718f9a1
JH
1186 current_thread = S_ithread_get(aTHX);
1187
1188 MUTEX_LOCK(&thread->mutex);
1189 if ((join_err = (thread->state & PERL_ITHR_UNCALLABLE))) {
1190 MUTEX_UNLOCK(&thread->mutex);
1191 Perl_croak(aTHX_ (join_err & PERL_ITHR_DETACHED)
1192 ? "Cannot join a detached thread"
1193 : "Thread already joined");
1194 } else if (thread->tid == current_thread->tid) {
1195 MUTEX_UNLOCK(&thread->mutex);
1196 Perl_croak(aTHX_ "Cannot join self");
fcea4b7c
JH
1197 }
1198
8718f9a1
JH
1199 /* Mark as joined */
1200 thread->state |= PERL_ITHR_JOINED;
1201 MUTEX_UNLOCK(&thread->mutex);
1202
1203 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1204 MY_POOL.joinable_threads--;
1205 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1206
fcea4b7c
JH
1207 /* Join the thread */
1208#ifdef WIN32
8718f9a1
JH
1209 if (WaitForSingleObject(thread->handle, INFINITE) != WAIT_OBJECT_0) {
1210 /* Timeout/abandonment unexpected here; check $^E */
1211 Perl_croak(aTHX_ "PANIC: underlying join failed");
1212 };
fcea4b7c 1213#else
8718f9a1
JH
1214 if ((rc_join = pthread_join(thread->thr, &retval)) != 0) {
1215 /* In progress/deadlock/unknown unexpected here; check $! */
1216 errno = rc_join;
1217 Perl_croak(aTHX_ "PANIC: underlying join failed");
1218 };
fcea4b7c
JH
1219#endif
1220
1221 MUTEX_LOCK(&thread->mutex);
fcea4b7c 1222 /* Get the return value from the call_sv */
955c272e 1223 /* Objects do not survive this process - FIXME */
7df0357e 1224 if ((thread->gimme & G_WANT) != G_VOID) {
fcea4b7c
JH
1225 AV *params_copy;
1226 PerlInterpreter *other_perl;
1227 CLONE_PARAMS clone_params;
fcea4b7c
JH
1228
1229 params_copy = (AV *)SvRV(thread->params);
1230 other_perl = thread->interp;
1231 clone_params.stashes = newAV();
1232 clone_params.flags = CLONEf_JOIN_IN;
1233 PL_ptr_table = ptr_table_new();
fcea4b7c
JH
1234 S_ithread_set(aTHX_ thread);
1235 /* Ensure 'meaningful' addresses retain their meaning */
1236 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1237 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1238 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1239 params = (AV *)sv_dup((SV*)params_copy, &clone_params);
1240 S_ithread_set(aTHX_ current_thread);
1241 SvREFCNT_dec(clone_params.stashes);
d4315dd6 1242 SvREFCNT_inc_void(params);
fcea4b7c
JH
1243 ptr_table_free(PL_ptr_table);
1244 PL_ptr_table = NULL;
1245 }
1246
955c272e
JH
1247 /* If thread didn't die, then we can free its interpreter */
1248 if (! (thread->state & PERL_ITHR_DIED)) {
1249 S_ithread_clear(aTHX_ thread);
1250 }
6ebc233e 1251 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
955c272e 1252
fcea4b7c 1253 /* If no return values, then just return */
f4cc38af
JH
1254 if (! params) {
1255 XSRETURN_UNDEF;
1256 }
1257
1258 /* Put return values on stack */
1259 len = (int)AvFILL(params);
1260 for (ii=0; ii <= len; ii++) {
1261 SV* param = av_shift(params);
1262 XPUSHs(sv_2mortal(param));
1263 }
1264
1265 /* Free return value array */
1266 SvREFCNT_dec(params);
1267
1268
1269void
1270ithread_yield(...)
1271 CODE:
11db694d 1272 PERL_UNUSED_VAR(items);
f4cc38af
JH
1273 YIELD;
1274
1275
1276void
1277ithread_detach(...)
1278 PREINIT:
1279 ithread *thread;
fcea4b7c 1280 int detach_err;
5c6ff896 1281 dMY_POOL;
f4cc38af 1282 CODE:
11db694d
JH
1283 PERL_UNUSED_VAR(items);
1284
fcea4b7c 1285 /* Detach the thread */
8718f9a1 1286 thread = S_SV_to_ithread(aTHX_ ST(0));
5c6ff896 1287 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
9ca4d7fd 1288 MUTEX_LOCK(&thread->mutex);
8718f9a1
JH
1289 if (! (detach_err = (thread->state & PERL_ITHR_UNCALLABLE))) {
1290 /* Thread is detachable */
1291 thread->state |= PERL_ITHR_DETACHED;
fcea4b7c 1292#ifdef WIN32
8718f9a1 1293 /* Windows has no 'detach thread' function */
fcea4b7c 1294#else
8718f9a1 1295 PERL_THREAD_DETACH(thread->thr);
fcea4b7c 1296#endif
8718f9a1
JH
1297 if (thread->state & PERL_ITHR_FINISHED) {
1298 MY_POOL.joinable_threads--;
1299 } else {
1300 MY_POOL.running_threads--;
1301 MY_POOL.detached_threads++;
1302 }
4dcb9e53 1303 }
adc09a0e 1304 MUTEX_UNLOCK(&thread->mutex);
5c6ff896 1305 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
4dcb9e53 1306
8718f9a1
JH
1307 if (detach_err) {
1308 Perl_croak(aTHX_ (detach_err & PERL_ITHR_DETACHED)
1309 ? "Thread already detached"
1310 : "Cannot detach a joined thread");
1311 }
1312
955c272e
JH
1313 /* If thread is finished and didn't die,
1314 * then we can free its interpreter */
1315 MUTEX_LOCK(&thread->mutex);
1316 if ((thread->state & PERL_ITHR_FINISHED) &&
1317 ! (thread->state & PERL_ITHR_DIED))
1318 {
1319 S_ithread_clear(aTHX_ thread);
1320 }
6ebc233e 1321 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
f4cc38af 1322
47ba8780
AB
1323
1324void
c0003851
JH
1325ithread_kill(...)
1326 PREINIT:
1327 ithread *thread;
1328 char *sig_name;
1329 IV signal;
1330 CODE:
1331 /* Must have safe signals */
fea7688c 1332 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) {
4dcb9e53 1333 Perl_croak(aTHX_ "Cannot signal threads without safe signals");
fea7688c 1334 }
c0003851
JH
1335
1336 /* Object method only */
11db694d 1337 if ((items != 2) || ! sv_isobject(ST(0))) {
c0003851 1338 Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')");
fea7688c 1339 }
c0003851 1340
c0003851
JH
1341 /* Get signal */
1342 sig_name = SvPV_nolen(ST(1));
1343 if (isALPHA(*sig_name)) {
fea7688c 1344 if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G') {
c0003851 1345 sig_name += 3;
fea7688c
JH
1346 }
1347 if ((signal = whichsig(sig_name)) < 0) {
c0003851 1348 Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name);
fea7688c
JH
1349 }
1350 } else {
c0003851 1351 signal = SvIV(ST(1));
fea7688c 1352 }
c0003851
JH
1353
1354 /* Set the signal for the thread */
861d5cbe 1355 thread = S_SV_to_ithread(aTHX_ ST(0));
4dcb9e53 1356 MUTEX_LOCK(&thread->mutex);
3ceb02cd 1357 if (thread->interp) {
c0003851
JH
1358 dTHXa(thread->interp);
1359 PL_psig_pend[signal]++;
1360 PL_sig_pending = 1;
1361 }
4dcb9e53 1362 MUTEX_UNLOCK(&thread->mutex);
c0003851
JH
1363
1364 /* Return the thread to allow for method chaining */
1365 ST(0) = ST(0);
1366 /* XSRETURN(1); - implied */
1367
1368
1369void
f4cc38af
JH
1370ithread_DESTROY(...)
1371 CODE:
11db694d 1372 PERL_UNUSED_VAR(items);
fcea4b7c 1373 sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar);
f4cc38af
JH
1374
1375
1376void
1377ithread_equal(...)
fc04eb16
JH
1378 PREINIT:
1379 int are_equal = 0;
f4cc38af 1380 CODE:
11db694d
JH
1381 PERL_UNUSED_VAR(items);
1382
fc04eb16 1383 /* Compares TIDs to determine thread equality */
f4cc38af
JH
1384 if (sv_isobject(ST(0)) && sv_isobject(ST(1))) {
1385 ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1386 ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
fc04eb16
JH
1387 are_equal = (thr1->tid == thr2->tid);
1388 }
1389 if (are_equal) {
1390 XST_mYES(0);
f4cc38af 1391 } else {
fc04eb16 1392 /* Return 0 on false for backward compatibility */
f4cc38af
JH
1393 XST_mIV(0, 0);
1394 }
1395 /* XSRETURN(1); - implied */
1396
47ba8780 1397
47ba8780 1398void
f4cc38af
JH
1399ithread_object(...)
1400 PREINIT:
1401 char *classname;
1402 UV tid;
fc04eb16 1403 ithread *thread;
8718f9a1 1404 int state;
9ca4d7fd 1405 int have_obj = 0;
5c6ff896 1406 dMY_POOL;
f4cc38af
JH
1407 CODE:
1408 /* Class method only */
fea7688c 1409 if (SvROK(ST(0))) {
f4cc38af 1410 Perl_croak(aTHX_ "Usage: threads->object($tid)");
fea7688c 1411 }
f4cc38af
JH
1412 classname = (char *)SvPV_nolen(ST(0));
1413
1414 if ((items < 2) || ! SvOK(ST(1))) {
1415 XSRETURN_UNDEF;
1416 }
1417
fc04eb16 1418 /* threads->object($tid) */
f4cc38af
JH
1419 tid = SvUV(ST(1));
1420
1421 /* Walk through threads list */
5c6ff896
JH
1422 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1423 for (thread = MY_POOL.main_thread.next;
1424 thread != &MY_POOL.main_thread;
fc04eb16 1425 thread = thread->next)
f4cc38af 1426 {
9ca4d7fd
JH
1427 /* Look for TID */
1428 if (thread->tid == tid) {
1429 /* Ignore if detached or joined */
8718f9a1
JH
1430 MUTEX_LOCK(&thread->mutex);
1431 state = thread->state;
1432 MUTEX_UNLOCK(&thread->mutex);
1433 if (! (state & PERL_ITHR_UNCALLABLE)) {
9ca4d7fd 1434 /* Put object on stack */
861d5cbe 1435 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
9ca4d7fd
JH
1436 have_obj = 1;
1437 }
1438 break;
f4cc38af 1439 }
f4cc38af 1440 }
5c6ff896 1441 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
9ca4d7fd
JH
1442
1443 if (! have_obj) {
f4cc38af
JH
1444 XSRETURN_UNDEF;
1445 }
1446 /* XSRETURN(1); - implied */
1447
1448
1449void
1450ithread__handle(...);
1451 PREINIT:
1452 ithread *thread;
1453 CODE:
11db694d 1454 PERL_UNUSED_VAR(items);
861d5cbe 1455 thread = S_SV_to_ithread(aTHX_ ST(0));
f4cc38af 1456#ifdef WIN32
fcea4b7c 1457 XST_mUV(0, PTR2UV(&thread->handle));
f4cc38af 1458#else
75ba4ae2 1459 XST_mUV(0, PTR2UV(&thread->thr));
f4cc38af
JH
1460#endif
1461 /* XSRETURN(1); - implied */
68795e93 1462
514612b7
JH
1463
1464void
1465ithread_get_stack_size(...)
1466 PREINIT:
1467 IV stack_size;
5c6ff896 1468 dMY_POOL;
514612b7 1469 CODE:
11db694d 1470 PERL_UNUSED_VAR(items);
514612b7
JH
1471 if (sv_isobject(ST(0))) {
1472 /* $thr->get_stack_size() */
1473 ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1474 stack_size = thread->stack_size;
1475 } else {
1476 /* threads->get_stack_size() */
5c6ff896 1477 stack_size = MY_POOL.default_stack_size;
514612b7
JH
1478 }
1479 XST_mIV(0, stack_size);
1480 /* XSRETURN(1); - implied */
1481
1482
1483void
1484ithread_set_stack_size(...)
1485 PREINIT:
1486 IV old_size;
5c6ff896 1487 dMY_POOL;
514612b7 1488 CODE:
fea7688c 1489 if (items != 2) {
514612b7 1490 Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
fea7688c
JH
1491 }
1492 if (sv_isobject(ST(0))) {
514612b7 1493 Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
fea7688c 1494 }
6ebc233e
RGS
1495 if (! looks_like_number(ST(1))) {
1496 Perl_croak(aTHX_ "Stack size must be numeric");
1497 }
514612b7 1498
5c6ff896
JH
1499 old_size = MY_POOL.default_stack_size;
1500 MY_POOL.default_stack_size = S_good_stack_size(aTHX_ SvIV(ST(1)));
514612b7
JH
1501 XST_mIV(0, old_size);
1502 /* XSRETURN(1); - implied */
1503
ead32952
JH
1504
1505void
1506ithread_is_running(...)
1507 PREINIT:
1508 ithread *thread;
1509 CODE:
1510 /* Object method only */
11db694d 1511 if ((items != 1) || ! sv_isobject(ST(0))) {
ead32952 1512 Perl_croak(aTHX_ "Usage: $thr->is_running()");
fea7688c 1513 }
ead32952
JH
1514
1515 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
8718f9a1 1516 MUTEX_LOCK(&thread->mutex);
ead32952 1517 ST(0) = (thread->state & PERL_ITHR_FINISHED) ? &PL_sv_no : &PL_sv_yes;
8718f9a1 1518 MUTEX_UNLOCK(&thread->mutex);
ead32952
JH
1519 /* XSRETURN(1); - implied */
1520
1521
1522void
1523ithread_is_detached(...)
1524 PREINIT:
1525 ithread *thread;
1526 CODE:
11db694d 1527 PERL_UNUSED_VAR(items);
861d5cbe 1528 thread = S_SV_to_ithread(aTHX_ ST(0));
8718f9a1 1529 MUTEX_LOCK(&thread->mutex);
ead32952 1530 ST(0) = (thread->state & PERL_ITHR_DETACHED) ? &PL_sv_yes : &PL_sv_no;
8718f9a1 1531 MUTEX_UNLOCK(&thread->mutex);
ead32952
JH
1532 /* XSRETURN(1); - implied */
1533
1534
1535void
1536ithread_is_joinable(...)
1537 PREINIT:
1538 ithread *thread;
1539 CODE:
1540 /* Object method only */
11db694d 1541 if ((items != 1) || ! sv_isobject(ST(0))) {
ead32952 1542 Perl_croak(aTHX_ "Usage: $thr->is_joinable()");
fea7688c 1543 }
ead32952
JH
1544
1545 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1546 MUTEX_LOCK(&thread->mutex);
1547 ST(0) = ((thread->state & PERL_ITHR_FINISHED) &&
8718f9a1 1548 ! (thread->state & PERL_ITHR_UNCALLABLE))
ead32952
JH
1549 ? &PL_sv_yes : &PL_sv_no;
1550 MUTEX_UNLOCK(&thread->mutex);
1551 /* XSRETURN(1); - implied */
1552
1553
1554void
1555ithread_wantarray(...)
1556 PREINIT:
1557 ithread *thread;
1558 CODE:
11db694d 1559 PERL_UNUSED_VAR(items);
861d5cbe 1560 thread = S_SV_to_ithread(aTHX_ ST(0));
7df0357e
NC
1561 ST(0) = ((thread->gimme & G_WANT) == G_ARRAY) ? &PL_sv_yes :
1562 ((thread->gimme & G_WANT) == G_VOID) ? &PL_sv_undef
7ef93cb2 1563 /* G_SCALAR */ : &PL_sv_no;
ead32952
JH
1564 /* XSRETURN(1); - implied */
1565
69a9b4b8
RGS
1566
1567void
1568ithread_set_thread_exit_only(...)
1569 PREINIT:
1570 ithread *thread;
1571 CODE:
fea7688c 1572 if (items != 2) {
69a9b4b8 1573 Perl_croak(aTHX_ "Usage: ->set_thread_exit_only(boolean)");
fea7688c 1574 }
861d5cbe 1575 thread = S_SV_to_ithread(aTHX_ ST(0));
69a9b4b8
RGS
1576 MUTEX_LOCK(&thread->mutex);
1577 if (SvTRUE(ST(1))) {
1578 thread->state |= PERL_ITHR_THREAD_EXIT_ONLY;
1579 } else {
1580 thread->state &= ~PERL_ITHR_THREAD_EXIT_ONLY;
1581 }
1582 MUTEX_UNLOCK(&thread->mutex);
1583
955c272e
JH
1584
1585void
1586ithread_error(...)
1587 PREINIT:
1588 ithread *thread;
1589 SV *err = NULL;
1590 CODE:
1591 /* Object method only */
1592 if ((items != 1) || ! sv_isobject(ST(0))) {
1593 Perl_croak(aTHX_ "Usage: $thr->err()");
1594 }
1595
1596 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1597 MUTEX_LOCK(&thread->mutex);
1598
1599 /* If thread died, then clone the error into the calling thread */
1600 if (thread->state & PERL_ITHR_DIED) {
1601 PerlInterpreter *other_perl;
1602 CLONE_PARAMS clone_params;
1603 ithread *current_thread;
1604
1605 other_perl = thread->interp;
1606 clone_params.stashes = newAV();
1607 clone_params.flags = CLONEf_JOIN_IN;
1608 PL_ptr_table = ptr_table_new();
1609 current_thread = S_ithread_get(aTHX);
1610 S_ithread_set(aTHX_ thread);
1611 /* Ensure 'meaningful' addresses retain their meaning */
1612 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1613 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1614 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1615 err = sv_dup(thread->err, &clone_params);
1616 S_ithread_set(aTHX_ current_thread);
1617 SvREFCNT_dec(clone_params.stashes);
1618 SvREFCNT_inc_void(err);
1619 /* If error was an object, bless it into the correct class */
1620 if (thread->err_class) {
1621 sv_bless(err, gv_stashpv(thread->err_class, 1));
1622 }
1623 ptr_table_free(PL_ptr_table);
1624 PL_ptr_table = NULL;
1625 }
1626
1627 MUTEX_UNLOCK(&thread->mutex);
1628
1629 if (! err) {
1630 XSRETURN_UNDEF;
1631 }
1632
1633 ST(0) = sv_2mortal(err);
1634 /* XSRETURN(1); - implied */
1635
1636
73e09c8f
JH
1637#endif /* USE_ITHREADS */
1638
fc04eb16 1639
68795e93
NIS
1640BOOT:
1641{
73e09c8f 1642#ifdef USE_ITHREADS
5c6ff896
JH
1643 SV *my_pool_sv = *hv_fetch(PL_modglobal, MY_POOL_KEY,
1644 sizeof(MY_POOL_KEY)-1, TRUE);
1645 my_pool_t *my_poolp = (my_pool_t*)SvPVX(newSV(sizeof(my_pool_t)-1));
1646
fc04eb16
JH
1647 MY_CXT_INIT;
1648
5c6ff896
JH
1649 Zero(my_poolp, 1, my_pool_t);
1650 sv_setuv(my_pool_sv, PTR2UV(my_poolp));
1651
fc04eb16 1652 PL_perl_destruct_level = 2;
5c6ff896
JH
1653 MUTEX_INIT(&MY_POOL.create_destruct_mutex);
1654 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
fc04eb16
JH
1655
1656 PL_threadhook = &Perl_ithread_hook;
1657
5c6ff896
JH
1658 MY_POOL.tid_counter = 1;
1659# ifdef THREAD_CREATE_NEEDS_STACK
1660 MY_POOL.default_stack_size = THREAD_CREATE_NEEDS_STACK;
1661# endif
1662
c372d929
JH
1663 /* The 'main' thread is thread 0.
1664 * It is detached (unjoinable) and immortal.
1665 */
fc04eb16 1666
5c6ff896 1667 MUTEX_INIT(&MY_POOL.main_thread.mutex);
fc04eb16
JH
1668
1669 /* Head of the threads list */
5c6ff896
JH
1670 MY_POOL.main_thread.next = &MY_POOL.main_thread;
1671 MY_POOL.main_thread.prev = &MY_POOL.main_thread;
fc04eb16 1672
5c6ff896 1673 MY_POOL.main_thread.count = 1; /* Immortal */
fc04eb16 1674
5c6ff896
JH
1675 MY_POOL.main_thread.interp = aTHX;
1676 MY_POOL.main_thread.state = PERL_ITHR_DETACHED; /* Detached */
1677 MY_POOL.main_thread.stack_size = MY_POOL.default_stack_size;
fc04eb16 1678# ifdef WIN32
5c6ff896 1679 MY_POOL.main_thread.thr = GetCurrentThreadId();
fc04eb16 1680# else
5c6ff896 1681 MY_POOL.main_thread.thr = pthread_self();
fc04eb16
JH
1682# endif
1683
5c6ff896
JH
1684 S_ithread_set(aTHX_ &MY_POOL.main_thread);
1685 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
73e09c8f 1686#endif /* USE_ITHREADS */
68795e93 1687}