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