threads.c

filter: Statementstmt 0.0% 0 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 5 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/threads.pm ↑ Summary
1 /*
2 * This file was generated automatically by ExtUtils::ParseXS version 3.63 from the
3 * contents of threads.xs. Do not edit this file, edit threads.xs instead.
4 *
5 * ANY CHANGES MADE HERE WILL BE LOST!
6 *
7 */
8  
9 #line 1 "threads.xs"
10 #define PERL_NO_GET_CONTEXT
11 /* Workaround for mingw 32-bit compiler by mingw-w64.sf.net - has to come before any #include.
12 * It also defines USE_NO_MINGW_SETJMP_TWO_ARGS for the mingw.org 32-bit compilers ... but
13 * that's ok as that compiler makes no use of that symbol anyway */
14 #if defined(WIN32) && defined(__MINGW32__) && !defined(__MINGW64__)
15 # define USE_NO_MINGW_SETJMP_TWO_ARGS 1
16 #endif
17 #include "EXTERN.h"
18 #include "perl.h"
19 #include "XSUB.h"
20 /* Workaround for XSUB.h bug under WIN32 */
21 #ifdef WIN32
22 # undef setjmp
23 # if defined(USE_NO_MINGW_SETJMP_TWO_ARGS) || (!defined(__BORLANDC__) && !defined(__MINGW64__))
24 # define setjmp(x) _setjmp(x)
25 # endif
26 # if defined(__MINGW64__)
27 # define setjmp(x) _setjmpex((x), mingw_getsp())
28 # endif
29 #endif
30 #ifdef HAS_PPPORT_H
31 # define NEED_PL_signals
32 # define NEED_sv_2pv_flags
33 # include "ppport.h"
34 # include "threads.h"
35 #endif
36 #ifndef sv_dup_inc
37 # define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t))
38 #endif
39 #ifndef PERL_UNUSED_RESULT
40 # if defined(__GNUC__) && defined(HASATTRIBUTE_WARN_UNUSED_RESULT)
41 # define PERL_UNUSED_RESULT(v) STMT_START { __typeof__(v) z = (v); (void)sizeof(z); } STMT_END
42 # else
43 # define PERL_UNUSED_RESULT(v) ((void)(v))
44 # endif
45 #endif
46  
47 #ifndef CLANG_DIAG_IGNORE
48 # define CLANG_DIAG_IGNORE(x)
49 # define CLANG_DIAG_RESTORE
50 #endif
51 #ifndef CLANG_DIAG_IGNORE_STMT
52 # define CLANG_DIAG_IGNORE_STMT(x) CLANG_DIAG_IGNORE(x) NOOP
53 # define CLANG_DIAG_RESTORE_STMT CLANG_DIAG_RESTORE NOOP
54 # define CLANG_DIAG_IGNORE_DECL(x) CLANG_DIAG_IGNORE(x) dNOOP
55 # define CLANG_DIAG_RESTORE_DECL CLANG_DIAG_RESTORE dNOOP
56 #endif
57  
58 #ifdef USE_ITHREADS
59  
60 #ifdef __amigaos4__
61 # undef YIELD
62 # define YIELD sleep(0)
63 #endif
64 #ifdef WIN32
65 # include <windows.h>
66 /* Supposed to be in Winbase.h */
67 # ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
68 # define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
69 # endif
70 # include <win32thread.h>
71 #else
72 # ifdef OS2
73 typedef perl_os_thread pthread_t;
74 # else
75 # include <pthread.h>
76 # endif
77 # include <thread.h>
78 # define PERL_THREAD_SETSPECIFIC(k,v) pthread_setspecific(k,v)
79 # ifdef OLD_PTHREADS_API
80 # define PERL_THREAD_DETACH(t) pthread_detach(&(t))
81 # else
82 # define PERL_THREAD_DETACH(t) pthread_detach((t))
83 # endif
84 #endif
85 #if !defined(HAS_GETPAGESIZE) && defined(I_SYS_PARAM)
86 # include <sys/param.h>
87 #endif
88  
89 /* Values for 'state' member */
90 #define PERL_ITHR_DETACHED 1 /* Thread has been detached */
91 #define PERL_ITHR_JOINED 2 /* Thread is being / has been joined */
92 #define PERL_ITHR_FINISHED 4 /* Thread has finished execution */
93 #define PERL_ITHR_THREAD_EXIT_ONLY 8 /* exit() only exits current thread */
94 #define PERL_ITHR_NONVIABLE 16 /* Thread creation failed */
95 #define PERL_ITHR_DIED 32 /* Thread finished by dying */
96  
97 #define PERL_ITHR_UNCALLABLE (PERL_ITHR_DETACHED|PERL_ITHR_JOINED)
98  
99  
100 typedef struct _ithread {
101 struct _ithread *next; /* Next thread in the list */
102 struct _ithread *prev; /* Prev thread in the list */
103 PerlInterpreter *interp; /* The threads interpreter */
104 UV tid; /* Threads module's thread id */
105 perl_mutex mutex; /* Mutex for updating things in this struct */
106 int count; /* Reference count. See S_ithread_create. */
107 int state; /* Detached, joined, finished, etc. */
108 int gimme; /* Context of create */
109 SV *init_function; /* Code to run */
110 AV *params; /* Args to pass function */
111 #ifdef WIN32
112 DWORD thr; /* OS's idea if thread id */
113 HANDLE handle; /* OS's waitable handle */
114 #else
115 pthread_t thr; /* OS's handle for the thread */
116 #endif
117 IV stack_size;
118 SV *err; /* Error from abnormally terminated thread */
119 char *err_class; /* Error object's classname if applicable */
120 #ifndef WIN32
121 sigset_t initial_sigmask; /* Thread wakes up with signals blocked */
122 #endif
123 } ithread;
124  
125  
126 #define MY_CXT_KEY "threads::_cxt" XS_VERSION
127  
128 typedef struct {
129 /* Used by Perl interpreter for thread context switching */
130 ithread *context;
131 } my_cxt_t;
132  
133 START_MY_CXT
134  
135  
136 #define MY_POOL_KEY "threads::_pool" XS_VERSION
137  
138 typedef struct {
139 /* Structure for 'main' thread
140 * Also forms the 'base' for the doubly-linked list of threads */
141 ithread main_thread;
142  
143 /* Protects the creation and destruction of threads*/
144 perl_mutex create_destruct_mutex;
145  
146 UV tid_counter;
147 IV joinable_threads;
148 IV running_threads;
149 IV detached_threads;
150 IV total_threads;
151 IV default_stack_size;
152 IV page_size;
153 } my_pool_t;
154  
155 #define dMY_POOL \
156 SV *my_pool_sv = *hv_fetch(PL_modglobal, MY_POOL_KEY, \
157 sizeof(MY_POOL_KEY)-1, TRUE); \
158 my_pool_t *my_poolp = INT2PTR(my_pool_t*, SvUV(my_pool_sv))
159  
160 #define MY_POOL (*my_poolp)
161  
162 #if defined(WIN32) || (defined(__amigaos4__) && defined(__NEWLIB__))
163 # undef THREAD_SIGNAL_BLOCKING
164 #else
165 # define THREAD_SIGNAL_BLOCKING
166 #endif
167  
168 #ifdef THREAD_SIGNAL_BLOCKING
169  
170 /* Block most signals for calling thread, setting the old signal mask to
171 * oldmask, if it is not NULL */
172 STATIC int
173 S_block_most_signals(sigset_t *oldmask)
174 {
175 sigset_t newmask;
176  
177 sigfillset(&newmask);
178 /* Don't block certain "important" signals (stolen from mg.c) */
179 #ifdef SIGILL
180 sigdelset(&newmask, SIGILL);
181 #endif
182 #ifdef SIGBUS
183 sigdelset(&newmask, SIGBUS);
184 #endif
185 #ifdef SIGSEGV
186 sigdelset(&newmask, SIGSEGV);
187 #endif
188  
189 #if defined(VMS)
190 /* no per-thread blocking available */
191 return sigprocmask(SIG_BLOCK, &newmask, oldmask);
192 #else
193 return pthread_sigmask(SIG_BLOCK, &newmask, oldmask);
194 #endif /* VMS */
195 }
196  
197 /* Set the signal mask for this thread to newmask */
198 STATIC int
199 S_set_sigmask(sigset_t *newmask)
200 {
201 #if defined(VMS)
202 return sigprocmask(SIG_SETMASK, newmask, NULL);
203 #else
204 return pthread_sigmask(SIG_SETMASK, newmask, NULL);
205 #endif /* VMS */
206 }
207 #endif /* WIN32 */
208  
209 /* Used by Perl interpreter for thread context switching */
210 STATIC void
211 S_ithread_set(pTHX_ ithread *thread)
212 {
213 dMY_CXT;
214 MY_CXT.context = thread;
215 }
216  
217 STATIC ithread *
218 S_ithread_get(pTHX)
219 {
220 dMY_CXT;
221 return (MY_CXT.context);
222 }
223  
224  
225 /* Free any data (such as the Perl interpreter) attached to an ithread
226 * structure. This is a bit like undef on SVs, where the SV isn't freed,
227 * but the PVX is. Must be called with thread->mutex already locked. Also,
228 * must be called with MY_POOL.create_destruct_mutex unlocked as destruction
229 * of the interpreter can lead to recursive destruction calls that could
230 * lead to a deadlock on that mutex.
231 */
232 STATIC void
233 S_ithread_clear(pTHX_ ithread *thread)
234 {
235 PerlInterpreter *interp;
236 #ifndef WIN32
237 sigset_t origmask;
238 #endif
239  
240 assert(((thread->state & PERL_ITHR_FINISHED) &&
241 (thread->state & PERL_ITHR_UNCALLABLE))
242 ||
243 (thread->state & PERL_ITHR_NONVIABLE));
244  
245 #ifdef THREAD_SIGNAL_BLOCKING
246 /* We temporarily set the interpreter context to the interpreter being
247 * destroyed. It's in no condition to handle signals while it's being
248 * taken apart.
249 */
250 S_block_most_signals(&origmask);
251 #endif
252  
253 interp = thread->interp;
254 if (interp) {
255 dTHXa(interp);
256  
257 PERL_SET_CONTEXT(interp);
258 S_ithread_set(aTHX_ thread);
259  
260 SvREFCNT_dec(thread->params);
261 thread->params = NULL;
262  
263 if (thread->err) {
264 SvREFCNT_dec(thread->err);
265 thread->err = Nullsv;
266 }
267  
268 perl_destruct(interp);
269 perl_free(interp);
270 thread->interp = NULL;
271 }
272  
273 PERL_SET_CONTEXT(aTHX);
274 #ifdef THREAD_SIGNAL_BLOCKING
275 S_set_sigmask(&origmask);
276 #endif
277 }
278  
279  
280 /* Decrement the refcount of an ithread, and if it reaches zero, free it.
281 * Must be called with the mutex held.
282 * On return, mutex is released (or destroyed).
283 */
284 STATIC void
285 S_ithread_free(pTHX_ ithread *thread)
286 PERL_TSA_RELEASE(thread->mutex)
287 {
288 #ifdef WIN32
289 HANDLE handle;
290 #endif
291 dMY_POOL;
292  
293 if (! (thread->state & PERL_ITHR_NONVIABLE)) {
294 assert(thread->count > 0);
295 if (--thread->count > 0) {
296 MUTEX_UNLOCK(&thread->mutex);
297 return;
298 }
299 assert((thread->state & PERL_ITHR_FINISHED) &&
300 (thread->state & PERL_ITHR_UNCALLABLE));
301 }
302 MUTEX_UNLOCK(&thread->mutex);
303  
304 /* Main thread (0) is immortal and should never get here */
305 assert(thread->tid != 0);
306  
307 /* Remove from circular list of threads */
308 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
309 assert(thread->prev && thread->next);
310 thread->next->prev = thread->prev;
311 thread->prev->next = thread->next;
312 thread->next = NULL;
313 thread->prev = NULL;
314 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
315  
316 /* Thread is now disowned */
317 MUTEX_LOCK(&thread->mutex);
318 S_ithread_clear(aTHX_ thread);
319  
320 #ifdef WIN32
321 handle = thread->handle;
322 thread->handle = NULL;
323 #endif
324 MUTEX_UNLOCK(&thread->mutex);
325 MUTEX_DESTROY(&thread->mutex);
326  
327 #ifdef WIN32
328 if (handle) {
329 CloseHandle(handle);
330 }
331 #endif
332  
333 PerlMemShared_free(thread);
334  
335 /* total_threads >= 1 is used to veto cleanup by the main thread,
336 * should it happen to exit while other threads still exist.
337 * Decrement this as the very last thing in the thread's existence.
338 * Otherwise, MY_POOL and global state such as PL_op_mutex may get
339 * freed while we're still using it.
340 */
341 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
342 MY_POOL.total_threads--;
343 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
344 }
345  
346  
347 static void
348 S_ithread_count_inc(pTHX_ ithread *thread)
349 PERL_TSA_EXCLUDES(thread->mutex)
350 {
351 MUTEX_LOCK(&thread->mutex);
352 thread->count++;
353 MUTEX_UNLOCK(&thread->mutex);
354 }
355  
356  
357 /* Warn if exiting with any unjoined threads */
358 STATIC int
359 S_exit_warning(pTHX)
360 {
361 int veto_cleanup, warn;
362 dMY_POOL;
363  
364 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
365 veto_cleanup = (MY_POOL.total_threads > 0);
366 warn = (MY_POOL.running_threads || MY_POOL.joinable_threads);
367 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
368  
369 if (warn) {
370 if (ckWARN_d(WARN_THREADS)) {
371 Perl_warn(aTHX_ "Perl exited with active threads:\n\t%"
372 IVdf " running and unjoined\n\t%"
373 IVdf " finished and unjoined\n\t%"
374 IVdf " running and detached\n",
375 MY_POOL.running_threads,
376 MY_POOL.joinable_threads,
377 MY_POOL.detached_threads);
378 }
379 }
380  
381 return (veto_cleanup);
382 }
383  
384  
385 /* Called from perl_destruct() in each thread. If it's the main thread,
386 * stop it from freeing everything if there are other threads still running.
387 */
388 STATIC int
389 Perl_ithread_hook(pTHX)
390 {
391 dMY_POOL;
392 return ((aTHX == MY_POOL.main_thread.interp) ? S_exit_warning(aTHX) : 0);
393 }
394  
395  
396 /* MAGIC (in mg.h sense) hooks */
397  
398 STATIC int
399 ithread_mg_get(pTHX_ SV *sv, MAGIC *mg)
400 {
401 ithread *thread = (ithread *)mg->mg_ptr;
402 SvIV_set(sv, PTR2IV(thread));
403 SvIOK_on(sv);
404 return (0);
405 }
406  
407 STATIC int
408 ithread_mg_free(pTHX_ SV *sv, MAGIC *mg)
409 {
410 ithread *thread = (ithread *)mg->mg_ptr;
411 PERL_UNUSED_ARG(sv);
412 MUTEX_LOCK(&thread->mutex);
413 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
414 return (0);
415 }
416  
417 STATIC int
418 ithread_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
419 {
420 PERL_UNUSED_ARG(param);
421 S_ithread_count_inc(aTHX_ (ithread *)mg->mg_ptr);
422 return (0);
423 }
424  
425 STATIC const MGVTBL ithread_vtbl = {
426 ithread_mg_get, /* get */
427 0, /* set */
428 0, /* len */
429 0, /* clear */
430 ithread_mg_free, /* free */
431 0, /* copy */
432 ithread_mg_dup, /* dup */
433 #if (PERL_VERSION > 8) || (PERL_VERSION == 8 && PERL_SUBVERSION > 8)
434 0 /* local */
435 #endif
436 };
437  
438  
439 /* Provided default, minimum and rational stack sizes */
440 STATIC IV
441 S_good_stack_size(pTHX_ IV stack_size)
442 {
443 dMY_POOL;
444  
445 /* Use default stack size if no stack size specified */
446 if (! stack_size) {
447 return (MY_POOL.default_stack_size);
448 }
449  
450 #ifdef PTHREAD_STACK_MIN
451 /* Can't use less than minimum */
452 if (stack_size < PTHREAD_STACK_MIN) {
453 if (ckWARN(WARN_THREADS)) {
454 Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
455 }
456 return (PTHREAD_STACK_MIN);
457 }
458 #endif
459  
460 /* Round up to page size boundary */
461 if (MY_POOL.page_size <= 0) {
462 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_MMAP_PAGE_SIZE))
463 SETERRNO(0, SS_NORMAL);
464 # ifdef _SC_PAGESIZE
465 MY_POOL.page_size = sysconf(_SC_PAGESIZE);
466 # else
467 MY_POOL.page_size = sysconf(_SC_MMAP_PAGE_SIZE);
468 # endif
469 if ((long)MY_POOL.page_size < 0) {
470 if (errno) {
471 SV * const error = get_sv("@", 0);
472 (void)SvUPGRADE(error, SVt_PV);
473 Perl_croak(aTHX_ "PANIC: sysconf: %s", SvPV_nolen(error));
474 } else {
475 Perl_croak(aTHX_ "PANIC: sysconf: pagesize unknown");
476 }
477 }
478 #else
479 # ifdef HAS_GETPAGESIZE
480 MY_POOL.page_size = getpagesize();
481 # else
482 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
483 MY_POOL.page_size = PAGESIZE;
484 # else
485 MY_POOL.page_size = 8192; /* A conservative default */
486 # endif
487 # endif
488 if (MY_POOL.page_size <= 0) {
489 Perl_croak(aTHX_ "PANIC: bad pagesize %" IVdf, (IV)MY_POOL.page_size);
490 }
491 #endif
492 }
493 stack_size = ((stack_size + (MY_POOL.page_size - 1)) / MY_POOL.page_size) * MY_POOL.page_size;
494  
495 return (stack_size);
496 }
497  
498  
499 /* Run code within a JMPENV environment.
500 * Using a separate function avoids
501 * "variable 'foo' might be clobbered by 'longjmp'"
502 * warnings.
503 * The three _p vars return values to the caller
504 */
505 static int
506 S_jmpenv_run(pTHX_ int action, ithread *thread,
507 int *len_p, int *exit_app_p, int *exit_code_p)
508 {
509 dJMPENV;
510 volatile I32 oldscope = PL_scopestack_ix;
511 int jmp_rc = 0;
512  
513 JMPENV_PUSH(jmp_rc);
514 if (jmp_rc == 0) {
515 if (action == 0) {
516 /* Run the specified function */
517 *len_p = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
518 } else if (action == 1) {
519 /* Warn that thread died */
520 Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV);
521 } else {
522 /* Warn if there are unjoined threads */
523 S_exit_warning(aTHX);
524 }
525 } else if (jmp_rc == 2) {
526 /* Thread exited */
527 *exit_app_p = 1;
528 *exit_code_p = STATUS_CURRENT;
529 while (PL_scopestack_ix > oldscope) {
530 LEAVE;
531 }
532 }
533 JMPENV_POP;
534 return jmp_rc;
535 }
536  
537  
538 /* Starts executing the thread.
539 * Passed as the C level function to run in the new thread.
540 */
541 #ifdef WIN32
542 STATIC THREAD_RET_TYPE
543 S_ithread_run(LPVOID arg)
544 #else
545 STATIC void *
546 S_ithread_run(void * arg)
547 #endif
548 {
549 ithread *thread = (ithread *)arg;
550 int exit_app = 0; /* Thread terminated using 'exit' */
551 int exit_code = 0;
552 int died = 0; /* Thread terminated abnormally */
553  
554  
555 dTHXa(thread->interp);
556  
557 dMY_POOL;
558  
559 /* The following mutex lock + mutex unlock pair explained.
560 *
561 * parent:
562 * - calls ithread_create (and S_ithread_create), which:
563 * - creates the new thread
564 * - does MUTEX_LOCK(&thread->mutex)
565 * - calls pthread_create(..., S_ithread_run,...)
566 * child:
567 * - starts the S_ithread_run (where we are now), which:
568 * - tries to MUTEX_LOCK(&thread->mutex)
569 * - blocks
570 * parent:
571 * - continues doing more createy stuff
572 * - does MUTEX_UNLOCK(&thread->mutex)
573 * - continues
574 * child:
575 * - finishes MUTEX_LOCK(&thread->mutex)
576 * - does MUTEX_UNLOCK(&thread->mutex)
577 * - continues
578 */
579 MUTEX_LOCK(&thread->mutex);
580 MUTEX_UNLOCK(&thread->mutex);
581  
582 PERL_SET_CONTEXT(thread->interp);
583 S_ithread_set(aTHX_ thread);
584  
585 #ifdef THREAD_SIGNAL_BLOCKING
586 /* Thread starts with most signals blocked - restore the signal mask from
587 * the ithread struct.
588 */
589 S_set_sigmask(&thread->initial_sigmask);
590 #endif
591  
592 PL_perl_destruct_level = 2;
593  
594 {
595 AV *params = thread->params;
596 int len = (int)av_len(params)+1;
597 int ii;
598 int jmp_rc;
599  
600 dSP;
601 ENTER;
602 SAVETMPS;
603  
604 /* Put args on the stack */
605 PUSHMARK(SP);
606 for (ii=0; ii < len; ii++) {
607 XPUSHs(av_shift(params));
608 }
609 PUTBACK;
610  
611 jmp_rc = S_jmpenv_run(aTHX_ 0, thread, &len, &exit_app, &exit_code);
612  
613 #ifdef THREAD_SIGNAL_BLOCKING
614 /* The interpreter is finished, so this thread can stop receiving
615 * signals. This way, our signal handler doesn't get called in the
616 * middle of our parent thread calling perl_destruct()...
617 */
618 S_block_most_signals(NULL);
619 #endif
620  
621 /* Remove args from stack and put back in params array */
622 SPAGAIN;
623 for (ii=len-1; ii >= 0; ii--) {
624 SV *sv = POPs;
625 if (jmp_rc == 0 && (thread->gimme & G_WANT) != G_VOID) {
626 av_store(params, ii, SvREFCNT_inc(sv));
627 }
628 }
629  
630 FREETMPS;
631 LEAVE;
632  
633 /* Check for abnormal termination */
634 if (SvTRUE(ERRSV)) {
635 died = PERL_ITHR_DIED;
636 thread->err = newSVsv(ERRSV);
637 /* If ERRSV is an object, remember the classname and then
638 * rebless into 'main' so it will survive 'cloning'
639 */
640 if (sv_isobject(thread->err)) {
641 thread->err_class = HvNAME(SvSTASH(SvRV(thread->err)));
642 sv_bless(thread->err, gv_stashpv("main", 0));
643 }
644  
645 if (ckWARN_d(WARN_THREADS)) {
646 (void)S_jmpenv_run(aTHX_ 1, thread, NULL,
647 &exit_app, &exit_code);
648 }
649 }
650  
651 /* Release function ref */
652 SvREFCNT_dec(thread->init_function);
653 thread->init_function = Nullsv;
654 }
655  
656 PerlIO_flush((PerlIO *)NULL);
657  
658 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
659 MUTEX_LOCK(&thread->mutex);
660 /* Mark as finished */
661 thread->state |= (PERL_ITHR_FINISHED | died);
662 /* Clear exit flag if required */
663 if (thread->state & PERL_ITHR_THREAD_EXIT_ONLY) {
664 exit_app = 0;
665 }
666  
667 /* Adjust thread status counts */
668 if (thread->state & PERL_ITHR_DETACHED) {
669 MY_POOL.detached_threads--;
670 } else {
671 MY_POOL.running_threads--;
672 MY_POOL.joinable_threads++;
673 }
674 MUTEX_UNLOCK(&thread->mutex);
675 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
676  
677 /* Exit application if required */
678 if (exit_app) {
679 (void)S_jmpenv_run(aTHX_ 2, thread, NULL, &exit_app, &exit_code);
680 my_exit(exit_code);
681 }
682  
683 /* At this point, the interpreter may have been freed, so call
684 * free in the the context of of the 'main' interpreter which
685 * can't have been freed due to the veto_cleanup mechanism.
686 */
687 aTHX = MY_POOL.main_thread.interp;
688  
689 MUTEX_LOCK(&thread->mutex);
690 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
691  
692 #ifdef WIN32
693 return ((DWORD)0);
694 #else
695 return (0);
696 #endif
697 }
698  
699  
700 /* Type conversion helper functions */
701  
702 STATIC SV *
703 S_ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
704 {
705 SV *sv;
706 MAGIC *mg;
707  
708 if (inc)
709 S_ithread_count_inc(aTHX_ thread);
710  
711 if (! obj) {
712 obj = newSV(0);
713 }
714  
715 sv = newSVrv(obj, classname);
716 sv_setiv(sv, PTR2IV(thread));
717 mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0);
718 mg->mg_flags |= MGf_DUP;
719 SvREADONLY_on(sv);
720  
721 return (obj);
722 }
723  
724 STATIC ithread *
725 S_SV_to_ithread(pTHX_ SV *sv)
726 {
727 /* Argument is a thread */
728 if (SvROK(sv)) {
729 return (INT2PTR(ithread *, SvIV(SvRV(sv))));
730 }
731 /* Argument is classname, therefore return current thread */
732 return (S_ithread_get(aTHX));
733 }
734  
735  
736 /* threads->create()
737 * Called in context of parent thread.
738 * Called with my_pool->create_destruct_mutex locked.
739 * (Unlocked both on error and on success.)
740 */
741 STATIC ithread *
742 S_ithread_create(
743 PerlInterpreter *parent_perl,
744 my_pool_t *my_pool,
745 SV *init_function,
746 IV stack_size,
747 int gimme,
748 int exit_opt,
749 int params_start,
750 int num_params)
751 PERL_TSA_RELEASE(my_pool->create_destruct_mutex)
752 {
753 dTHXa(parent_perl);
754 ithread *thread;
755 ithread *current_thread = S_ithread_get(aTHX);
756 AV *params;
757 SV **array;
758  
759 #if PERL_VERSION <= 8 && PERL_SUBVERSION <= 7
760 SV **tmps_tmp = PL_tmps_stack;
761 IV tmps_ix = PL_tmps_ix;
762 #endif
763 #ifndef WIN32
764 int rc_stack_size = 0;
765 int rc_thread_create = 0;
766 #endif
767  
768 /* Allocate thread structure in context of the main thread's interpreter */
769 {
770 PERL_SET_CONTEXT(my_pool->main_thread.interp);
771 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
772 }
773 PERL_SET_CONTEXT(aTHX);
774 if (!thread) {
775 /* This lock was acquired in ithread_create()
776 * prior to calling S_ithread_create(). */
777 MUTEX_UNLOCK(&my_pool->create_destruct_mutex);
778 {
779 int fd = PerlIO_fileno(Perl_error_log);
780 if (fd >= 0) {
781 /* If there's no error_log, we cannot scream about it missing. */
782 PERL_UNUSED_RESULT(PerlLIO_write(fd, PL_no_mem, strlen(PL_no_mem)));
783 }
784 }
785 my_exit(1);
786 }
787 Zero(thread, 1, ithread);
788  
789 /* Add to threads list */
790 thread->next = &my_pool->main_thread;
791 thread->prev = my_pool->main_thread.prev;
792 my_pool->main_thread.prev = thread;
793 thread->prev->next = thread;
794 my_pool->total_threads++;
795  
796 /* 1 ref to be held by the local var 'thread' in S_ithread_run().
797 * 1 ref to be held by the threads object that we assume we will
798 * be embedded in upon our return.
799 * 1 ref to be the responsibility of join/detach, so we don't get
800 * freed until join/detach, even if no thread objects remain.
801 * This allows the following to work:
802 * { threads->create(sub{...}); } threads->object(1)->join;
803 */
804 thread->count = 3;
805  
806 /* Block new thread until ->create() call finishes */
807 MUTEX_INIT(&thread->mutex);
808 MUTEX_LOCK(&thread->mutex); /* See S_ithread_run() for more detail. */
809  
810 thread->tid = my_pool->tid_counter++;
811 thread->stack_size = S_good_stack_size(aTHX_ stack_size);
812 thread->gimme = gimme;
813 thread->state = exit_opt;
814  
815 /* "Clone" our interpreter into the thread's interpreter.
816 * This gives thread access to "static data" and code.
817 */
818 PerlIO_flush((PerlIO *)NULL);
819 S_ithread_set(aTHX_ thread);
820  
821 SAVEBOOL(PL_srand_called); /* Save this so it becomes the correct value */
822 PL_srand_called = FALSE; /* Set it to false so we can detect if it gets
823 set during the clone */
824  
825 #ifdef THREAD_SIGNAL_BLOCKING
826 /* perl_clone() will leave us the new interpreter's context. This poses
827 * two problems for our signal handler. First, it sets the new context
828 * before the new interpreter struct is fully initialized, so our signal
829 * handler might find bogus data in the interpreter struct it gets.
830 * Second, even if the interpreter is initialized before a signal comes in,
831 * we would like to avoid that interpreter receiving notifications for
832 * signals (especially when they ought to be for the one running in this
833 * thread), until it is running in its own thread. Another problem is that
834 * the new thread will not have set the context until some time after it
835 * has started, so it won't be safe for our signal handler to run until
836 * that time.
837 *
838 * So we block most signals here, so the new thread will inherit the signal
839 * mask, and unblock them right after the thread creation. The original
840 * mask is saved in the thread struct so that the new thread can restore
841 * the original mask.
842 */
843 S_block_most_signals(&thread->initial_sigmask);
844 #endif
845  
846 #ifdef WIN32
847 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
848 #else
849 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
850 #endif
851  
852 /* perl_clone() leaves us in new interpreter's context. As it is tricky
853 * to spot an implicit aTHX, create a new scope with aTHX matching the
854 * context for the duration of our work for new interpreter.
855 */
856 {
857 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION > 1)
858 CLONE_PARAMS *clone_param = Perl_clone_params_new(aTHX, thread->interp);
859 #else
860 CLONE_PARAMS clone_param_s;
861 CLONE_PARAMS *clone_param = &clone_param_s;
862 #endif
863 dTHXa(thread->interp);
864  
865 MY_CXT_CLONE;
866  
867 #if (PERL_VERSION < 13) || (PERL_VERSION == 13 && PERL_SUBVERSION <= 1)
868 clone_param->flags = 0;
869 #endif
870  
871 /* Here we remove END blocks since they should only run in the thread
872 * they are created
873 */
874 SvREFCNT_dec(PL_endav);
875 PL_endav = NULL;
876  
877 if (SvPOK(init_function)) {
878 thread->init_function = newSV(0);
879 sv_copypv(thread->init_function, init_function);
880 } else {
881 thread->init_function = sv_dup_inc(init_function, clone_param);
882 }
883  
884 thread->params = params = newAV();
885 av_extend(params, num_params - 1);
886 AvFILLp(params) = num_params - 1;
887 array = AvARRAY(params);
888  
889 /* params_start is an offset onto the Perl stack. This can be
890 reallocated (and hence move) as a side effect of calls to
891 perl_clone() and sv_dup_inc(). Hence copy the parameters
892 somewhere under our control first, before duplicating. */
893 if (num_params) {
894 #if (PERL_VERSION > 8)
895 Copy(parent_perl->Istack_base + params_start, array, num_params, SV *);
896 #else
897 Copy(parent_perl->Tstack_base + params_start, array, num_params, SV *);
898 #endif
899 while (num_params--) {
900 *array = sv_dup_inc(*array, clone_param);
901 ++array;
902 }
903 }
904  
905 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION > 1)
906 Perl_clone_params_del(clone_param);
907 #endif
908  
909 #if PERL_VERSION <= 8 && PERL_SUBVERSION <= 7
910 /* The code below checks that anything living on the tmps stack and
911 * has been cloned (so it lives in the ptr_table) has a refcount
912 * higher than 0.
913 *
914 * If the refcount is 0 it means that a something on the stack/context
915 * was holding a reference to it and since we init_stacks() in
916 * perl_clone that won't get cleaned and we will get a leaked scalar.
917 * The reason it was cloned was that it lived on the @_ stack.
918 *
919 * Example of this can be found in bugreport 15837 where calls in the
920 * parameter list end up as a temp.
921 *
922 * As of 5.8.8 this is done in perl_clone.
923 */
924 while (tmps_ix > 0) {
925 SV* sv = (SV*)ptr_table_fetch(PL_ptr_table, tmps_tmp[tmps_ix]);
926 tmps_ix--;
927 if (sv && SvREFCNT(sv) == 0) {
928 SvREFCNT_inc_void(sv);
929 SvREFCNT_dec(sv);
930 }
931 }
932 #endif
933  
934 SvTEMP_off(thread->init_function);
935 ptr_table_free(PL_ptr_table);
936 PL_ptr_table = NULL;
937 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
938 }
939 S_ithread_set(aTHX_ current_thread);
940 PERL_SET_CONTEXT(aTHX);
941  
942 /* Create/start the thread */
943 #ifdef WIN32
944 thread->handle = CreateThread(NULL,
945 (DWORD)thread->stack_size,
946 S_ithread_run,
947 (LPVOID)thread,
948 STACK_SIZE_PARAM_IS_A_RESERVATION,
949 &thread->thr);
950 #else
951 {
952 STATIC pthread_attr_t attr;
953 STATIC int attr_inited = 0;
954 STATIC int attr_joinable = PTHREAD_CREATE_JOINABLE;
955 if (! attr_inited) {
956 pthread_attr_init(&attr);
957 attr_inited = 1;
958 }
959  
960 # ifdef PTHREAD_ATTR_SETDETACHSTATE
961 /* Threads start out joinable */
962 PTHREAD_ATTR_SETDETACHSTATE(&attr, attr_joinable);
963 # endif
964  
965 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
966 /* Set thread's stack size */
967 if (thread->stack_size > 0) {
968 rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
969 }
970 # endif
971  
972 /* Create the thread */
973 if (! rc_stack_size) {
974 # ifdef OLD_PTHREADS_API
975 rc_thread_create = pthread_create(&thread->thr,
976 attr,
977 S_ithread_run,
978 (void *)thread);
979 # else
980 # if defined(HAS_PTHREAD_ATTR_SETSCOPE) && defined(PTHREAD_SCOPE_SYSTEM)
981 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
982 # endif
983 rc_thread_create = pthread_create(&thread->thr,
984 &attr,
985 S_ithread_run,
986 (void *)thread);
987 # endif
988 }
989  
990 #ifdef THREAD_SIGNAL_BLOCKING
991 /* Now it's safe to accept signals, since we're in our own interpreter's
992 * context and we have created the thread.
993 */
994 S_set_sigmask(&thread->initial_sigmask);
995 #endif
996  
997 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
998 /* Try to get thread's actual stack size */
999 {
1000 size_t stacksize;
1001 #ifdef HPUX1020
1002 stacksize = pthread_attr_getstacksize(attr);
1003 #else
1004 if (! pthread_attr_getstacksize(&attr, &stacksize))
1005 #endif
1006 if (stacksize > 0) {
1007 thread->stack_size = (IV)stacksize;
1008 }
1009 }
1010 # endif
1011 }
1012 #endif
1013  
1014 /* Check for errors */
1015 #ifdef WIN32
1016 if (thread->handle == NULL) {
1017 #else
1018 if (rc_stack_size || rc_thread_create) {
1019 #endif
1020 /* Must unlock mutex for destruct call */
1021 /* This lock was acquired in ithread_create()
1022 * prior to calling S_ithread_create(). */
1023 MUTEX_UNLOCK(&my_pool->create_destruct_mutex);
1024 thread->state |= PERL_ITHR_NONVIABLE;
1025 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
1026 #ifndef WIN32
1027 if (ckWARN_d(WARN_THREADS)) {
1028 if (rc_stack_size) {
1029 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
1030 } else {
1031 Perl_warn(aTHX_ "Thread creation failed: pthread_create returned %d", rc_thread_create);
1032 }
1033 }
1034 #endif
1035 return (NULL);
1036 }
1037  
1038 my_pool->running_threads++;
1039 MUTEX_UNLOCK(&my_pool->create_destruct_mutex);
1040 return (thread);
1041  
1042 CLANG_DIAG_IGNORE_STMT(-Wthread-safety);
1043 /* warning: mutex 'thread->mutex' is not held on every path through here [-Wthread-safety-analysis] */
1044 }
1045 CLANG_DIAG_RESTORE_DECL;
1046  
1047 #endif /* USE_ITHREADS */
1048  
1049  
1050 #line 1051 "threads.c"
1051 #ifndef PERL_UNUSED_VAR
1052 # define PERL_UNUSED_VAR(var) if (0) var = var
1053 #endif
1054  
1055 #ifndef dVAR
1056 # define dVAR dNOOP
1057 #endif
1058  
1059  
1060 /* This stuff is not part of the API! You have been warned. */
1061 #ifndef PERL_VERSION_DECIMAL
1062 # define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
1063 #endif
1064 #ifndef PERL_DECIMAL_VERSION
1065 # define PERL_DECIMAL_VERSION \
1066 PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
1067 #endif
1068 #ifndef PERL_VERSION_GE
1069 # define PERL_VERSION_GE(r,v,s) \
1070 (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
1071 #endif
1072 #ifndef PERL_VERSION_LE
1073 # define PERL_VERSION_LE(r,v,s) \
1074 (PERL_DECIMAL_VERSION <= PERL_VERSION_DECIMAL(r,v,s))
1075 #endif
1076  
1077 /* XS_INTERNAL is the explicit static-linkage variant of the default
1078 * XS macro.
1079 *
1080 * XS_EXTERNAL is the same as XS_INTERNAL except it does not include
1081 * "static", ie. it exports XSUB symbols. You probably don't want that
1082 * for anything but the BOOT XSUB.
1083 *
1084 * See XSUB.h in core!
1085 */
1086  
1087  
1088 /* TODO: This might be compatible further back than 5.10.0. */
1089 #if PERL_VERSION_GE(5, 10, 0) && PERL_VERSION_LE(5, 15, 1)
1090 # undef XS_EXTERNAL
1091 # undef XS_INTERNAL
1092 # if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
1093 # define XS_EXTERNAL(name) __declspec(dllexport) XSPROTO(name)
1094 # define XS_INTERNAL(name) static XSPROTO(name)
1095 # endif
1096 # if defined(__SYMBIAN32__)
1097 # define XS_EXTERNAL(name) EXPORT_C XSPROTO(name)
1098 # define XS_INTERNAL(name) EXPORT_C static XSPROTO(name)
1099 # endif
1100 # ifndef XS_EXTERNAL
1101 # if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
1102 # define XS_EXTERNAL(name) void name(pTHX_ CV* cv __attribute__unused__)
1103 # define XS_INTERNAL(name) static void name(pTHX_ CV* cv __attribute__unused__)
1104 # else
1105 # ifdef __cplusplus
1106 # define XS_EXTERNAL(name) extern "C" XSPROTO(name)
1107 # define XS_INTERNAL(name) static XSPROTO(name)
1108 # else
1109 # define XS_EXTERNAL(name) XSPROTO(name)
1110 # define XS_INTERNAL(name) static XSPROTO(name)
1111 # endif
1112 # endif
1113 # endif
1114 #endif
1115  
1116 /* perl >= 5.10.0 && perl <= 5.15.1 */
1117  
1118  
1119 /* The XS_EXTERNAL macro is used for functions that must not be static
1120 * like the boot XSUB of a module. If perl didn't have an XS_EXTERNAL
1121 * macro defined, the best we can do is assume XS is the same.
1122 * Dito for XS_INTERNAL.
1123 */
1124 #ifndef XS_EXTERNAL
1125 # define XS_EXTERNAL(name) XS(name)
1126 #endif
1127 #ifndef XS_INTERNAL
1128 # define XS_INTERNAL(name) XS(name)
1129 #endif
1130  
1131 /* Now, finally, after all this mess, we want an ExtUtils::ParseXS
1132 * internal macro that we're free to redefine for varying linkage due
1133 * to the EXPORT_XSUB_SYMBOLS XS keyword. This is internal, use
1134 * XS_EXTERNAL(name) or XS_INTERNAL(name) in your code if you need to!
1135 */
1136  
1137 #undef XS_EUPXS
1138 #if defined(PERL_EUPXS_ALWAYS_EXPORT)
1139 # define XS_EUPXS(name) XS_EXTERNAL(name)
1140 #else
1141 /* default to internal */
1142 # define XS_EUPXS(name) XS_INTERNAL(name)
1143 #endif
1144  
1145 #ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
1146 #define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)
1147  
1148 /* prototype to pass -Wmissing-prototypes */
1149 static void
1150 S_croak_xs_usage(const CV *const cv, const char *const params);
1151  
1152 static void
1153 S_croak_xs_usage(const CV *const cv, const char *const params)
1154 {
1155 const GV *const gv = CvGV(cv);
1156  
1157 PERL_ARGS_ASSERT_CROAK_XS_USAGE;
1158  
1159 if (gv) {
1160 const char *const gvname = GvNAME(gv);
1161 const HV *const stash = GvSTASH(gv);
1162 const char *const hvname = stash ? HvNAME(stash) : NULL;
1163  
1164 if (hvname)
1165 Perl_croak_nocontext("Usage: %s::%s(%s)", hvname, gvname, params);
1166 else
1167 Perl_croak_nocontext("Usage: %s(%s)", gvname, params);
1168 } else {
1169 /* Pants. I don't think that it should be possible to get here. */
1170 Perl_croak_nocontext("Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
1171 }
1172 }
1173 #undef PERL_ARGS_ASSERT_CROAK_XS_USAGE
1174  
1175 #define croak_xs_usage S_croak_xs_usage
1176  
1177 #endif
1178  
1179 /* NOTE: the prototype of newXSproto() is different in versions of perls,
1180 * so we define a portable version of newXSproto()
1181 */
1182 #ifdef newXS_flags
1183 #define newXSproto_portable(name, c_impl, file, proto) newXS_flags(name, c_impl, file, proto, 0)
1184 #else
1185 #define newXSproto_portable(name, c_impl, file, proto) (PL_Sv=(SV*)newXS(name, c_impl, file), sv_setpv(PL_Sv, proto), (CV*)PL_Sv)
1186 #endif /* !defined(newXS_flags) */
1187  
1188 #if PERL_VERSION_LE(5, 21, 5)
1189 # define newXS_deffile(a,b) Perl_newXS(aTHX_ a,b,file)
1190 #else
1191 # define newXS_deffile(a,b) Perl_newXS_deffile(aTHX_ a,b)
1192 #endif
1193  
1194 /* simple backcompat versions of the TARGx() macros with no optimisation */
1195 #ifndef TARGi
1196 # define TARGi(iv, do_taint) sv_setiv_mg(TARG, iv)
1197 # define TARGu(uv, do_taint) sv_setuv_mg(TARG, uv)
1198 # define TARGn(nv, do_taint) sv_setnv_mg(TARG, nv)
1199 #endif
1200  
1201 #line 1202 "threads.c"
1202 #ifdef USE_ITHREADS
1203 #define XSubPPtmpAAAA 1
1204  
1205  
1206 XS_EUPXS(XS_threads_create); /* prototype to pass -Wmissing-prototypes */
1207 XS_EUPXS(XS_threads_create)
1208 {
1209 dVAR; dXSARGS;
1210 PERL_UNUSED_VAR(cv); /* -W */
1211 PERL_UNUSED_VAR(items); /* -W */
1212 {
1213 #line 1049 "threads.xs"
1214 char *classname;
1215 ithread *thread;
1216 SV *function_to_call;
1217 HV *specs;
1218 IV stack_size;
1219 int context;
1220 int exit_opt;
1221 SV *thread_exit_only;
1222 char *str;
1223 int idx;
1224 dMY_POOL;
1225 #line 1226 "threads.c"
1226 #line 1061 "threads.xs"
1227 if ((items >= 2) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1)))==SVt_PVHV) {
1228 if (--items < 2) {
1229 Perl_croak(aTHX_ "Usage: threads->create(\\%%specs, function, ...)");
1230 }
1231 specs = (HV*)SvRV(ST(1));
1232 idx = 1;
1233 } else {
1234 if (items < 2) {
1235 Perl_croak(aTHX_ "Usage: threads->create(function, ...)");
1236 }
1237 specs = NULL;
1238 idx = 0;
1239 }
1240  
1241 if (sv_isobject(ST(0))) {
1242 /* $thr->create() */
1243 classname = HvNAME(SvSTASH(SvRV(ST(0))));
1244 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1245 MUTEX_LOCK(&thread->mutex);
1246 stack_size = thread->stack_size;
1247 exit_opt = thread->state & PERL_ITHR_THREAD_EXIT_ONLY;
1248 MUTEX_UNLOCK(&thread->mutex);
1249 } else {
1250 /* threads->create() */
1251 classname = (char *)SvPV_nolen(ST(0));
1252 stack_size = MY_POOL.default_stack_size;
1253 thread_exit_only = get_sv("threads::thread_exit_only", GV_ADD);
1254 exit_opt = (SvTRUE(thread_exit_only))
1255 ? PERL_ITHR_THREAD_EXIT_ONLY : 0;
1256 }
1257  
1258 function_to_call = ST(idx+1);
1259  
1260 context = -1;
1261 if (specs) {
1262 SV **svp;
1263 /* stack_size */
1264 if ((svp = hv_fetchs(specs, "stack", 0))) {
1265 stack_size = SvIV(*svp);
1266 } else if ((svp = hv_fetchs(specs, "stacksize", 0))) {
1267 stack_size = SvIV(*svp);
1268 } else if ((svp = hv_fetchs(specs, "stack_size", 0))) {
1269 stack_size = SvIV(*svp);
1270 }
1271  
1272 /* context */
1273 if ((svp = hv_fetchs(specs, "context", 0))) {
1274 str = (char *)SvPV_nolen(*svp);
1275 switch (*str) {
1276 case 'a':
1277 case 'A':
1278 case 'l':
1279 case 'L':
1280 context = G_ARRAY;
1281 break;
1282 case 's':
1283 case 'S':
1284 context = G_SCALAR;
1285 break;
1286 case 'v':
1287 case 'V':
1288 context = G_VOID;
1289 break;
1290 default:
1291 Perl_croak(aTHX_ "Invalid context: %s", str);
1292 }
1293 } else if ((svp = hv_fetchs(specs, "array", 0))) {
1294 if (SvTRUE(*svp)) {
1295 context = G_ARRAY;
1296 }
1297 } else if ((svp = hv_fetchs(specs, "list", 0))) {
1298 if (SvTRUE(*svp)) {
1299 context = G_ARRAY;
1300 }
1301 } else if ((svp = hv_fetchs(specs, "scalar", 0))) {
1302 if (SvTRUE(*svp)) {
1303 context = G_SCALAR;
1304 }
1305 } else if ((svp = hv_fetchs(specs, "void", 0))) {
1306 if (SvTRUE(*svp)) {
1307 context = G_VOID;
1308 }
1309 }
1310  
1311 /* exit => thread_only */
1312 if ((svp = hv_fetchs(specs, "exit", 0))) {
1313 str = (char *)SvPV_nolen(*svp);
1314 exit_opt = (*str == 't' || *str == 'T')
1315 ? PERL_ITHR_THREAD_EXIT_ONLY : 0;
1316 }
1317 }
1318 if (context == -1) {
1319 context = GIMME_V; /* Implicit context */
1320 } else {
1321 context |= (GIMME_V & (~(G_ARRAY|G_SCALAR|G_VOID)));
1322 }
1323  
1324 /* Create thread */
1325 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1326 thread = S_ithread_create(aTHX_ &MY_POOL,
1327 function_to_call,
1328 stack_size,
1329 context,
1330 exit_opt,
1331 ax + idx + 2,
1332 items > 2 ? items - 2 : 0);
1333 if (! thread) {
1334 XSRETURN_UNDEF; /* Mutex already unlocked */
1335 }
1336 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, FALSE));
1337  
1338 /* Let thread run. */
1339 /* See S_ithread_run() for more detail. */
1340 CLANG_DIAG_IGNORE_STMT(-Wthread-safety);
1341 /* warning: releasing mutex 'thread->mutex' that was not held [-Wthread-safety-analysis] */
1342 MUTEX_UNLOCK(&thread->mutex);
1343 CLANG_DIAG_RESTORE_STMT;
1344  
1345 /* XSRETURN(1); - implied */
1346 #line 1347 "threads.c"
1347 }
1348 XSRETURN(1);
1349 }
1350  
1351  
1352 XS_EUPXS(XS_threads_list); /* prototype to pass -Wmissing-prototypes */
1353 XS_EUPXS(XS_threads_list)
1354 {
1355 dVAR; dXSARGS;
1356 PERL_UNUSED_VAR(cv); /* -W */
1357 PERL_UNUSED_VAR(items); /* -W */
1358 PERL_UNUSED_VAR(ax); /* -Wall */
1359 SP -= items;
1360 {
1361 #line 1185 "threads.xs"
1362 char *classname;
1363 ithread *thread;
1364 int list_context;
1365 IV count = 0;
1366 int want_running = 0;
1367 int state;
1368 dMY_POOL;
1369 #line 1370 "threads.c"
1370 #line 1193 "threads.xs"
1371 /* Class method only */
1372 if (SvROK(ST(0))) {
1373 Perl_croak(aTHX_ "Usage: threads->list(...)");
1374 }
1375 classname = (char *)SvPV_nolen(ST(0));
1376  
1377 /* Calling context */
1378 list_context = (GIMME_V == G_ARRAY);
1379  
1380 /* Running or joinable parameter */
1381 if (items > 1) {
1382 want_running = SvTRUE(ST(1));
1383 }
1384  
1385 /* Walk through threads list */
1386 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1387 for (thread = MY_POOL.main_thread.next;
1388 thread != &MY_POOL.main_thread;
1389 thread = thread->next)
1390 {
1391 MUTEX_LOCK(&thread->mutex);
1392 state = thread->state;
1393 MUTEX_UNLOCK(&thread->mutex);
1394  
1395 /* Ignore detached or joined threads */
1396 if (state & PERL_ITHR_UNCALLABLE) {
1397 continue;
1398 }
1399  
1400 /* Filter per parameter */
1401 if (items > 1) {
1402 if (want_running) {
1403 if (state & PERL_ITHR_FINISHED) {
1404 continue; /* Not running */
1405 }
1406 } else {
1407 if (! (state & PERL_ITHR_FINISHED)) {
1408 continue; /* Still running - not joinable yet */
1409 }
1410 }
1411 }
1412  
1413 /* Push object on stack if list context */
1414 if (list_context) {
1415 XPUSHs(sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
1416 }
1417 count++;
1418 }
1419 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1420 /* If scalar context, send back count */
1421 if (! list_context) {
1422 XSRETURN_IV(count);
1423 }
1424 #line 1425 "threads.c"
1425 PUTBACK;
1426 return;
1427 }
1428 }
1429  
1430  
1431 XS_EUPXS(XS_threads_self); /* prototype to pass -Wmissing-prototypes */
1432 XS_EUPXS(XS_threads_self)
1433 {
1434 dVAR; dXSARGS;
1435 PERL_UNUSED_VAR(cv); /* -W */
1436 PERL_UNUSED_VAR(items); /* -W */
1437 {
1438 #line 1251 "threads.xs"
1439 char *classname;
1440 ithread *thread;
1441 #line 1442 "threads.c"
1442 #line 1254 "threads.xs"
1443 /* Class method only */
1444 if ((items != 1) || SvROK(ST(0))) {
1445 Perl_croak(aTHX_ "Usage: threads->self()");
1446 }
1447 classname = (char *)SvPV_nolen(ST(0));
1448  
1449 thread = S_ithread_get(aTHX);
1450  
1451 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1452 /* XSRETURN(1); - implied */
1453 #line 1454 "threads.c"
1454 }
1455 XSRETURN(1);
1456 }
1457  
1458  
1459 XS_EUPXS(XS_threads_tid); /* prototype to pass -Wmissing-prototypes */
1460 XS_EUPXS(XS_threads_tid)
1461 {
1462 dVAR; dXSARGS;
1463 PERL_UNUSED_VAR(cv); /* -W */
1464 PERL_UNUSED_VAR(items); /* -W */
1465 {
1466 #line 1269 "threads.xs"
1467 ithread *thread;
1468 #line 1469 "threads.c"
1469 #line 1271 "threads.xs"
1470 PERL_UNUSED_VAR(items);
1471 thread = S_SV_to_ithread(aTHX_ ST(0));
1472 XST_mUV(0, thread->tid);
1473 /* XSRETURN(1); - implied */
1474 #line 1475 "threads.c"
1475 }
1476 XSRETURN(1);
1477 }
1478  
1479  
1480 XS_EUPXS(XS_threads_join); /* prototype to pass -Wmissing-prototypes */
1481 XS_EUPXS(XS_threads_join)
1482 {
1483 dVAR; dXSARGS;
1484 PERL_UNUSED_VAR(cv); /* -W */
1485 PERL_UNUSED_VAR(items); /* -W */
1486 PERL_UNUSED_VAR(ax); /* -Wall */
1487 SP -= items;
1488 {
1489 #line 1280 "threads.xs"
1490 ithread *thread;
1491 ithread *current_thread;
1492 int join_err;
1493 AV *params = NULL;
1494 int len;
1495 int ii;
1496 #ifndef WIN32
1497 int rc_join;
1498 void *retval;
1499 #endif
1500 dMY_POOL;
1501 #line 1502 "threads.c"
1502 #line 1292 "threads.xs"
1503 /* Object method only */
1504 if ((items != 1) || ! sv_isobject(ST(0))) {
1505 Perl_croak(aTHX_ "Usage: $thr->join()");
1506 }
1507  
1508 /* Check if the thread is joinable and not ourselves */
1509 thread = S_SV_to_ithread(aTHX_ ST(0));
1510 current_thread = S_ithread_get(aTHX);
1511  
1512 MUTEX_LOCK(&thread->mutex);
1513 if ((join_err = (thread->state & PERL_ITHR_UNCALLABLE))) {
1514 MUTEX_UNLOCK(&thread->mutex);
1515 Perl_croak(aTHX_ (join_err & PERL_ITHR_DETACHED)
1516 ? "Cannot join a detached thread"
1517 : "Thread already joined");
1518 } else if (thread->tid == current_thread->tid) {
1519 MUTEX_UNLOCK(&thread->mutex);
1520 Perl_croak(aTHX_ "Cannot join self");
1521 }
1522  
1523 /* Mark as joined */
1524 thread->state |= PERL_ITHR_JOINED;
1525 MUTEX_UNLOCK(&thread->mutex);
1526  
1527 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1528 MY_POOL.joinable_threads--;
1529 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1530  
1531 /* Join the thread */
1532 #ifdef WIN32
1533 if (WaitForSingleObject(thread->handle, INFINITE) != WAIT_OBJECT_0) {
1534 /* Timeout/abandonment unexpected here; check $^E */
1535 Perl_croak(aTHX_ "PANIC: underlying join failed");
1536 };
1537 #else
1538 if ((rc_join = pthread_join(thread->thr, &retval)) != 0) {
1539 /* In progress/deadlock/unknown unexpected here; check $! */
1540 errno = rc_join;
1541 Perl_croak(aTHX_ "PANIC: underlying join failed");
1542 };
1543 #endif
1544  
1545 MUTEX_LOCK(&thread->mutex);
1546 /* Get the return value from the call_sv */
1547 /* Objects do not survive this process - FIXME */
1548 if ((thread->gimme & G_WANT) != G_VOID) {
1549 #if (PERL_VERSION < 13) || (PERL_VERSION == 13 && PERL_SUBVERSION <= 1)
1550 AV *params_copy;
1551 PerlInterpreter *other_perl;
1552 CLONE_PARAMS clone_params;
1553  
1554 params_copy = thread->params;
1555 other_perl = thread->interp;
1556 clone_params.stashes = newAV();
1557 clone_params.flags = CLONEf_JOIN_IN;
1558 PL_ptr_table = ptr_table_new();
1559 S_ithread_set(aTHX_ thread);
1560 /* Ensure 'meaningful' addresses retain their meaning */
1561 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1562 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1563 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1564 params = (AV *)sv_dup((SV*)params_copy, &clone_params);
1565 S_ithread_set(aTHX_ current_thread);
1566 SvREFCNT_dec(clone_params.stashes);
1567 SvREFCNT_inc_void(params);
1568 ptr_table_free(PL_ptr_table);
1569 PL_ptr_table = NULL;
1570 #else
1571 AV *params_copy;
1572 PerlInterpreter *other_perl = thread->interp;
1573 CLONE_PARAMS *clone_params = Perl_clone_params_new(other_perl, aTHX);
1574  
1575 params_copy = thread->params;
1576 clone_params->flags |= CLONEf_JOIN_IN;
1577 PL_ptr_table = ptr_table_new();
1578 S_ithread_set(aTHX_ thread);
1579 /* Ensure 'meaningful' addresses retain their meaning */
1580 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
1581 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
1582 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
1583 # ifdef PL_sv_zero
1584 ptr_table_store(PL_ptr_table, &other_perl->Isv_zero, &PL_sv_zero);
1585 # endif
1586 params = (AV *)sv_dup((SV*)params_copy, clone_params);
1587 S_ithread_set(aTHX_ current_thread);
1588 Perl_clone_params_del(clone_params);
1589 SvREFCNT_inc_void(params);
1590 ptr_table_free(PL_ptr_table);
1591 PL_ptr_table = NULL;
1592 #endif
1593 }
1594  
1595 /* If thread didn't die, then we can free its interpreter */
1596 if (! (thread->state & PERL_ITHR_DIED)) {
1597 S_ithread_clear(aTHX_ thread);
1598 }
1599 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
1600  
1601 /* If no return values, then just return */
1602 if (! params) {
1603 XSRETURN_UNDEF;
1604 }
1605  
1606 /* Put return values on stack */
1607 len = (int)AvFILL(params);
1608 for (ii=0; ii <= len; ii++) {
1609 SV* param = av_shift(params);
1610 XPUSHs(sv_2mortal(param));
1611 }
1612  
1613 /* Free return value array */
1614 SvREFCNT_dec(params);
1615 #line 1616 "threads.c"
1616 PUTBACK;
1617 return;
1618 }
1619 }
1620  
1621  
1622 XS_EUPXS(XS_threads_yield); /* prototype to pass -Wmissing-prototypes */
1623 XS_EUPXS(XS_threads_yield)
1624 {
1625 dVAR; dXSARGS;
1626 PERL_UNUSED_VAR(cv); /* -W */
1627 PERL_UNUSED_VAR(items); /* -W */
1628 {
1629 #line 1409 "threads.xs"
1630 PERL_UNUSED_VAR(items);
1631 YIELD;
1632 #line 1633 "threads.c"
1633 }
1634 XSRETURN_EMPTY;
1635 }
1636  
1637  
1638 XS_EUPXS(XS_threads_detach); /* prototype to pass -Wmissing-prototypes */
1639 XS_EUPXS(XS_threads_detach)
1640 {
1641 dVAR; dXSARGS;
1642 PERL_UNUSED_VAR(cv); /* -W */
1643 PERL_UNUSED_VAR(items); /* -W */
1644 {
1645 #line 1416 "threads.xs"
1646 ithread *thread;
1647 int detach_err;
1648 dMY_POOL;
1649 #line 1650 "threads.c"
1650 #line 1420 "threads.xs"
1651 PERL_UNUSED_VAR(items);
1652  
1653 /* Detach the thread */
1654 thread = S_SV_to_ithread(aTHX_ ST(0));
1655 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1656 MUTEX_LOCK(&thread->mutex);
1657 if (! (detach_err = (thread->state & PERL_ITHR_UNCALLABLE))) {
1658 /* Thread is detachable */
1659 thread->state |= PERL_ITHR_DETACHED;
1660 #ifdef WIN32
1661 /* Windows has no 'detach thread' function */
1662 #else
1663 PERL_THREAD_DETACH(thread->thr);
1664 #endif
1665 if (thread->state & PERL_ITHR_FINISHED) {
1666 MY_POOL.joinable_threads--;
1667 } else {
1668 MY_POOL.running_threads--;
1669 MY_POOL.detached_threads++;
1670 }
1671 }
1672 MUTEX_UNLOCK(&thread->mutex);
1673 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1674  
1675 if (detach_err) {
1676 Perl_croak(aTHX_ (detach_err & PERL_ITHR_DETACHED)
1677 ? "Thread already detached"
1678 : "Cannot detach a joined thread");
1679 }
1680  
1681 /* If thread is finished and didn't die,
1682 * then we can free its interpreter */
1683 MUTEX_LOCK(&thread->mutex);
1684 if ((thread->state & PERL_ITHR_FINISHED) &&
1685 ! (thread->state & PERL_ITHR_DIED))
1686 {
1687 S_ithread_clear(aTHX_ thread);
1688 }
1689 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
1690 #line 1691 "threads.c"
1691 }
1692 XSRETURN_EMPTY;
1693 }
1694  
1695  
1696 XS_EUPXS(XS_threads_kill); /* prototype to pass -Wmissing-prototypes */
1697 XS_EUPXS(XS_threads_kill)
1698 {
1699 dVAR; dXSARGS;
1700 PERL_UNUSED_VAR(cv); /* -W */
1701 PERL_UNUSED_VAR(items); /* -W */
1702 {
1703 #line 1464 "threads.xs"
1704 ithread *thread;
1705 char *sig_name;
1706 IV signal;
1707 int no_handler = 1;
1708 #line 1709 "threads.c"
1709 #line 1469 "threads.xs"
1710 /* Must have safe signals */
1711 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) {
1712 Perl_croak(aTHX_ "Cannot signal threads without safe signals");
1713 }
1714  
1715 /* Object method only */
1716 if ((items != 2) || ! sv_isobject(ST(0))) {
1717 Perl_croak(aTHX_ "Usage: $thr->kill('SIG...')");
1718 }
1719  
1720 /* Get signal */
1721 sig_name = SvPV_nolen(ST(1));
1722 if (isALPHA(*sig_name)) {
1723 if (*sig_name == 'S' && sig_name[1] == 'I' && sig_name[2] == 'G') {
1724 sig_name += 3;
1725 }
1726 if ((signal = whichsig(sig_name)) < 0) {
1727 Perl_croak(aTHX_ "Unrecognized signal name: %s", sig_name);
1728 }
1729 } else {
1730 signal = SvIV(ST(1));
1731 }
1732  
1733 /* Set the signal for the thread */
1734 thread = S_SV_to_ithread(aTHX_ ST(0));
1735 MUTEX_LOCK(&thread->mutex);
1736 if (thread->interp && ! (thread->state & PERL_ITHR_FINISHED)) {
1737 dTHXa(thread->interp);
1738 if (PL_psig_pend && PL_psig_ptr[signal]) {
1739 PL_psig_pend[signal]++;
1740 PL_sig_pending = 1;
1741 no_handler = 0;
1742 }
1743 } else {
1744 /* Ignore signal to terminated/finished thread */
1745 no_handler = 0;
1746 }
1747 MUTEX_UNLOCK(&thread->mutex);
1748  
1749 if (no_handler) {
1750 Perl_croak(aTHX_ "Signal %s received in thread %" UVuf
1751 ", but no signal handler set.",
1752 sig_name, thread->tid);
1753 }
1754  
1755 /* Return the thread to allow for method chaining */
1756 ST(0) = ST(0);
1757 /* XSRETURN(1); - implied */
1758 #line 1759 "threads.c"
1759 }
1760 XSRETURN(1);
1761 }
1762  
1763  
1764 XS_EUPXS(XS_threads_DESTROY); /* prototype to pass -Wmissing-prototypes */
1765 XS_EUPXS(XS_threads_DESTROY)
1766 {
1767 dVAR; dXSARGS;
1768 PERL_UNUSED_VAR(cv); /* -W */
1769 PERL_UNUSED_VAR(items); /* -W */
1770 {
1771 #line 1522 "threads.xs"
1772 PERL_UNUSED_VAR(items);
1773 sv_unmagic(SvRV(ST(0)), PERL_MAGIC_shared_scalar);
1774 #line 1775 "threads.c"
1775 }
1776 XSRETURN_EMPTY;
1777 }
1778  
1779  
1780 XS_EUPXS(XS_threads_equal); /* prototype to pass -Wmissing-prototypes */
1781 XS_EUPXS(XS_threads_equal)
1782 {
1783 dVAR; dXSARGS;
1784 PERL_UNUSED_VAR(cv); /* -W */
1785 PERL_UNUSED_VAR(items); /* -W */
1786 {
1787 #line 1529 "threads.xs"
1788 int are_equal = 0;
1789 #line 1790 "threads.c"
1790 #line 1531 "threads.xs"
1791 PERL_UNUSED_VAR(items);
1792  
1793 /* Compares TIDs to determine thread equality */
1794 if (sv_isobject(ST(0)) && sv_isobject(ST(1))) {
1795 ithread *thr1 = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1796 ithread *thr2 = INT2PTR(ithread *, SvIV(SvRV(ST(1))));
1797 are_equal = (thr1->tid == thr2->tid);
1798 }
1799 if (are_equal) {
1800 XST_mYES(0);
1801 } else {
1802 /* Return 0 on false for backward compatibility */
1803 XST_mIV(0, 0);
1804 }
1805 /* XSRETURN(1); - implied */
1806 #line 1807 "threads.c"
1807 }
1808 XSRETURN(1);
1809 }
1810  
1811  
1812 XS_EUPXS(XS_threads_object); /* prototype to pass -Wmissing-prototypes */
1813 XS_EUPXS(XS_threads_object)
1814 {
1815 dVAR; dXSARGS;
1816 PERL_UNUSED_VAR(cv); /* -W */
1817 PERL_UNUSED_VAR(items); /* -W */
1818 {
1819 #line 1551 "threads.xs"
1820 char *classname;
1821 SV *arg;
1822 UV tid;
1823 ithread *thread;
1824 int state;
1825 int have_obj = 0;
1826 dMY_POOL;
1827 #line 1828 "threads.c"
1828 #line 1559 "threads.xs"
1829 /* Class method only */
1830 if (SvROK(ST(0))) {
1831 Perl_croak(aTHX_ "Usage: threads->object($tid)");
1832 }
1833 classname = (char *)SvPV_nolen(ST(0));
1834  
1835 /* Turn $tid from PVLV to SV if needed (bug #73330) */
1836 arg = ST(1);
1837 SvGETMAGIC(arg);
1838  
1839 if ((items < 2) || ! SvOK(arg)) {
1840 XSRETURN_UNDEF;
1841 }
1842  
1843 /* threads->object($tid) */
1844 tid = SvUV(arg);
1845  
1846 /* If current thread wants its own object, then behave the same as
1847 ->self() */
1848 thread = S_ithread_get(aTHX);
1849 if (thread->tid == tid) {
1850 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1851 have_obj = 1;
1852  
1853 } else {
1854 /* Walk through threads list */
1855 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
1856 for (thread = MY_POOL.main_thread.next;
1857 thread != &MY_POOL.main_thread;
1858 thread = thread->next)
1859 {
1860 /* Look for TID */
1861 if (thread->tid == tid) {
1862 /* Ignore if detached or joined */
1863 MUTEX_LOCK(&thread->mutex);
1864 state = thread->state;
1865 MUTEX_UNLOCK(&thread->mutex);
1866 if (! (state & PERL_ITHR_UNCALLABLE)) {
1867 /* Put object on stack */
1868 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1869 have_obj = 1;
1870 }
1871 break;
1872 }
1873 }
1874 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
1875 }
1876  
1877 if (! have_obj) {
1878 XSRETURN_UNDEF;
1879 }
1880 /* XSRETURN(1); - implied */
1881 #line 1882 "threads.c"
1882 }
1883 XSRETURN(1);
1884 }
1885  
1886  
1887 XS_EUPXS(XS_threads__handle); /* prototype to pass -Wmissing-prototypes */
1888 XS_EUPXS(XS_threads__handle)
1889 {
1890 dVAR; dXSARGS;
1891 PERL_UNUSED_VAR(cv); /* -W */
1892 PERL_UNUSED_VAR(items); /* -W */
1893 {
1894 #line 1616 "threads.xs"
1895 ithread *thread;
1896 #line 1897 "threads.c"
1897 #line 1618 "threads.xs"
1898 PERL_UNUSED_VAR(items);
1899 thread = S_SV_to_ithread(aTHX_ ST(0));
1900 #ifdef WIN32
1901 XST_mUV(0, PTR2UV(&thread->handle));
1902 #else
1903 XST_mUV(0, PTR2UV(&thread->thr));
1904 #endif
1905 /* XSRETURN(1); - implied */
1906 #line 1907 "threads.c"
1907 }
1908 XSRETURN(1);
1909 }
1910  
1911  
1912 XS_EUPXS(XS_threads_get_stack_size); /* prototype to pass -Wmissing-prototypes */
1913 XS_EUPXS(XS_threads_get_stack_size)
1914 {
1915 dVAR; dXSARGS;
1916 PERL_UNUSED_VAR(cv); /* -W */
1917 PERL_UNUSED_VAR(items); /* -W */
1918 {
1919 #line 1631 "threads.xs"
1920 IV stack_size;
1921 dMY_POOL;
1922 #line 1923 "threads.c"
1923 #line 1634 "threads.xs"
1924 PERL_UNUSED_VAR(items);
1925 if (sv_isobject(ST(0))) {
1926 /* $thr->get_stack_size() */
1927 ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1928 stack_size = thread->stack_size;
1929 } else {
1930 /* threads->get_stack_size() */
1931 stack_size = MY_POOL.default_stack_size;
1932 }
1933 XST_mIV(0, stack_size);
1934 /* XSRETURN(1); - implied */
1935 #line 1936 "threads.c"
1936 }
1937 XSRETURN(1);
1938 }
1939  
1940  
1941 XS_EUPXS(XS_threads_set_stack_size); /* prototype to pass -Wmissing-prototypes */
1942 XS_EUPXS(XS_threads_set_stack_size)
1943 {
1944 dVAR; dXSARGS;
1945 PERL_UNUSED_VAR(cv); /* -W */
1946 PERL_UNUSED_VAR(items); /* -W */
1947 {
1948 #line 1650 "threads.xs"
1949 IV old_size;
1950 dMY_POOL;
1951 #line 1952 "threads.c"
1952 #line 1653 "threads.xs"
1953 if (items != 2) {
1954 Perl_croak(aTHX_ "Usage: threads->set_stack_size($size)");
1955 }
1956 if (sv_isobject(ST(0))) {
1957 Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
1958 }
1959 if (! looks_like_number(ST(1))) {
1960 Perl_croak(aTHX_ "Stack size must be numeric");
1961 }
1962  
1963 old_size = MY_POOL.default_stack_size;
1964 MY_POOL.default_stack_size = S_good_stack_size(aTHX_ SvIV(ST(1)));
1965 XST_mIV(0, old_size);
1966 /* XSRETURN(1); - implied */
1967 #line 1968 "threads.c"
1968 }
1969 XSRETURN(1);
1970 }
1971  
1972  
1973 XS_EUPXS(XS_threads_is_running); /* prototype to pass -Wmissing-prototypes */
1974 XS_EUPXS(XS_threads_is_running)
1975 {
1976 dVAR; dXSARGS;
1977 PERL_UNUSED_VAR(cv); /* -W */
1978 PERL_UNUSED_VAR(items); /* -W */
1979 {
1980 #line 1672 "threads.xs"
1981 ithread *thread;
1982 #line 1983 "threads.c"
1983 #line 1674 "threads.xs"
1984 /* Object method only */
1985 if ((items != 1) || ! sv_isobject(ST(0))) {
1986 Perl_croak(aTHX_ "Usage: $thr->is_running()");
1987 }
1988  
1989 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1990 MUTEX_LOCK(&thread->mutex);
1991 ST(0) = (thread->state & PERL_ITHR_FINISHED) ? &PL_sv_no : &PL_sv_yes;
1992 MUTEX_UNLOCK(&thread->mutex);
1993 /* XSRETURN(1); - implied */
1994 #line 1995 "threads.c"
1995 }
1996 XSRETURN(1);
1997 }
1998  
1999  
2000 XS_EUPXS(XS_threads_is_detached); /* prototype to pass -Wmissing-prototypes */
2001 XS_EUPXS(XS_threads_is_detached)
2002 {
2003 dVAR; dXSARGS;
2004 PERL_UNUSED_VAR(cv); /* -W */
2005 PERL_UNUSED_VAR(items); /* -W */
2006 {
2007 #line 1689 "threads.xs"
2008 ithread *thread;
2009 #line 2010 "threads.c"
2010 #line 1691 "threads.xs"
2011 PERL_UNUSED_VAR(items);
2012 thread = S_SV_to_ithread(aTHX_ ST(0));
2013 MUTEX_LOCK(&thread->mutex);
2014 ST(0) = (thread->state & PERL_ITHR_DETACHED) ? &PL_sv_yes : &PL_sv_no;
2015 MUTEX_UNLOCK(&thread->mutex);
2016 /* XSRETURN(1); - implied */
2017 #line 2018 "threads.c"
2018 }
2019 XSRETURN(1);
2020 }
2021  
2022  
2023 XS_EUPXS(XS_threads_is_joinable); /* prototype to pass -Wmissing-prototypes */
2024 XS_EUPXS(XS_threads_is_joinable)
2025 {
2026 dVAR; dXSARGS;
2027 PERL_UNUSED_VAR(cv); /* -W */
2028 PERL_UNUSED_VAR(items); /* -W */
2029 {
2030 #line 1702 "threads.xs"
2031 ithread *thread;
2032 #line 2033 "threads.c"
2033 #line 1704 "threads.xs"
2034 /* Object method only */
2035 if ((items != 1) || ! sv_isobject(ST(0))) {
2036 Perl_croak(aTHX_ "Usage: $thr->is_joinable()");
2037 }
2038  
2039 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
2040 MUTEX_LOCK(&thread->mutex);
2041 ST(0) = ((thread->state & PERL_ITHR_FINISHED) &&
2042 ! (thread->state & PERL_ITHR_UNCALLABLE))
2043 ? &PL_sv_yes : &PL_sv_no;
2044 MUTEX_UNLOCK(&thread->mutex);
2045 /* XSRETURN(1); - implied */
2046 #line 2047 "threads.c"
2047 }
2048 XSRETURN(1);
2049 }
2050  
2051  
2052 XS_EUPXS(XS_threads_wantarray); /* prototype to pass -Wmissing-prototypes */
2053 XS_EUPXS(XS_threads_wantarray)
2054 {
2055 dVAR; dXSARGS;
2056 PERL_UNUSED_VAR(cv); /* -W */
2057 PERL_UNUSED_VAR(items); /* -W */
2058 {
2059 #line 1721 "threads.xs"
2060 ithread *thread;
2061 #line 2062 "threads.c"
2062 #line 1723 "threads.xs"
2063 PERL_UNUSED_VAR(items);
2064 thread = S_SV_to_ithread(aTHX_ ST(0));
2065 ST(0) = ((thread->gimme & G_WANT) == G_ARRAY) ? &PL_sv_yes :
2066 ((thread->gimme & G_WANT) == G_VOID) ? &PL_sv_undef
2067 /* G_SCALAR */ : &PL_sv_no;
2068 /* XSRETURN(1); - implied */
2069 #line 2070 "threads.c"
2070 }
2071 XSRETURN(1);
2072 }
2073  
2074  
2075 XS_EUPXS(XS_threads_set_thread_exit_only); /* prototype to pass -Wmissing-prototypes */
2076 XS_EUPXS(XS_threads_set_thread_exit_only)
2077 {
2078 dVAR; dXSARGS;
2079 PERL_UNUSED_VAR(cv); /* -W */
2080 PERL_UNUSED_VAR(items); /* -W */
2081 {
2082 #line 1734 "threads.xs"
2083 ithread *thread;
2084 #line 2085 "threads.c"
2085 #line 1736 "threads.xs"
2086 if (items != 2) {
2087 Perl_croak(aTHX_ "Usage: ->set_thread_exit_only(boolean)");
2088 }
2089 thread = S_SV_to_ithread(aTHX_ ST(0));
2090 MUTEX_LOCK(&thread->mutex);
2091 if (SvTRUE(ST(1))) {
2092 thread->state |= PERL_ITHR_THREAD_EXIT_ONLY;
2093 } else {
2094 thread->state &= ~PERL_ITHR_THREAD_EXIT_ONLY;
2095 }
2096 MUTEX_UNLOCK(&thread->mutex);
2097 #line 2098 "threads.c"
2098 }
2099 XSRETURN(1);
2100 }
2101  
2102  
2103 XS_EUPXS(XS_threads_error); /* prototype to pass -Wmissing-prototypes */
2104 XS_EUPXS(XS_threads_error)
2105 {
2106 dVAR; dXSARGS;
2107 PERL_UNUSED_VAR(cv); /* -W */
2108 PERL_UNUSED_VAR(items); /* -W */
2109 {
2110 #line 1752 "threads.xs"
2111 ithread *thread;
2112 SV *err = NULL;
2113 #line 2114 "threads.c"
2114 #line 1755 "threads.xs"
2115 /* Object method only */
2116 if ((items != 1) || ! sv_isobject(ST(0))) {
2117 Perl_croak(aTHX_ "Usage: $thr->err()");
2118 }
2119  
2120 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
2121 MUTEX_LOCK(&thread->mutex);
2122  
2123 /* If thread died, then clone the error into the calling thread */
2124 if (thread->state & PERL_ITHR_DIED) {
2125 #if (PERL_VERSION < 13) || (PERL_VERSION == 13 && PERL_SUBVERSION <= 1)
2126 PerlInterpreter *other_perl;
2127 CLONE_PARAMS clone_params;
2128 ithread *current_thread;
2129  
2130 other_perl = thread->interp;
2131 clone_params.stashes = newAV();
2132 clone_params.flags = CLONEf_JOIN_IN;
2133 PL_ptr_table = ptr_table_new();
2134 current_thread = S_ithread_get(aTHX);
2135 S_ithread_set(aTHX_ thread);
2136 /* Ensure 'meaningful' addresses retain their meaning */
2137 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
2138 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
2139 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
2140 err = sv_dup(thread->err, &clone_params);
2141 S_ithread_set(aTHX_ current_thread);
2142 SvREFCNT_dec(clone_params.stashes);
2143 SvREFCNT_inc_void(err);
2144 /* If error was an object, bless it into the correct class */
2145 if (thread->err_class) {
2146 sv_bless(err, gv_stashpv(thread->err_class, 1));
2147 }
2148 ptr_table_free(PL_ptr_table);
2149 PL_ptr_table = NULL;
2150 #else
2151 PerlInterpreter *other_perl = thread->interp;
2152 CLONE_PARAMS *clone_params = Perl_clone_params_new(other_perl, aTHX);
2153 ithread *current_thread;
2154  
2155 clone_params->flags |= CLONEf_JOIN_IN;
2156 PL_ptr_table = ptr_table_new();
2157 current_thread = S_ithread_get(aTHX);
2158 S_ithread_set(aTHX_ thread);
2159 /* Ensure 'meaningful' addresses retain their meaning */
2160 ptr_table_store(PL_ptr_table, &other_perl->Isv_undef, &PL_sv_undef);
2161 ptr_table_store(PL_ptr_table, &other_perl->Isv_no, &PL_sv_no);
2162 ptr_table_store(PL_ptr_table, &other_perl->Isv_yes, &PL_sv_yes);
2163 # ifdef PL_sv_zero
2164 ptr_table_store(PL_ptr_table, &other_perl->Isv_zero, &PL_sv_zero);
2165 # endif
2166 err = sv_dup(thread->err, clone_params);
2167 S_ithread_set(aTHX_ current_thread);
2168 Perl_clone_params_del(clone_params);
2169 SvREFCNT_inc_void(err);
2170 /* If error was an object, bless it into the correct class */
2171 if (thread->err_class) {
2172 sv_bless(err, gv_stashpv(thread->err_class, 1));
2173 }
2174 ptr_table_free(PL_ptr_table);
2175 PL_ptr_table = NULL;
2176 #endif
2177 }
2178  
2179 MUTEX_UNLOCK(&thread->mutex);
2180  
2181 if (! err) {
2182 XSRETURN_UNDEF;
2183 }
2184  
2185 ST(0) = sv_2mortal(err);
2186 /* XSRETURN(1); - implied */
2187 #line 2188 "threads.c"
2188 }
2189 XSRETURN(1);
2190 }
2191  
2192 #endif /* USE_ITHREADS */
2193 #ifdef __cplusplus
2194 extern "C" {
2195 #endif
2196 XS_EXTERNAL(boot_threads); /* prototype to pass -Wmissing-prototypes */
2197 0 XS_EXTERNAL(boot_threads)
2198 {
2199 #if PERL_VERSION_LE(5, 21, 5)
2200 dVAR; dXSARGS;
2201 #else
2202 0 dVAR; dXSBOOTARGSXSAPIVERCHK;
2203 #endif
2204 #if PERL_VERSION_LE(5, 8, 999) /* PERL_VERSION_LT is 5.33+ */
2205 char* file = __FILE__;
2206 #else
2207 0 const char* file = __FILE__;
2208 #endif
2209  
2210 PERL_UNUSED_VAR(file);
2211  
2212 PERL_UNUSED_VAR(cv); /* -W */
2213 PERL_UNUSED_VAR(items); /* -W */
2214 #if PERL_VERSION_LE(5, 21, 5)
2215 XS_VERSION_BOOTCHECK;
2216 # ifdef XS_APIVERSION_BOOTCHECK
2217 XS_APIVERSION_BOOTCHECK;
2218 # endif
2219 #endif
2220  
2221 #if XSubPPtmpAAAA
2222 newXS_deffile("threads::create", XS_threads_create);
2223 newXS_deffile("threads::list", XS_threads_list);
2224 newXS_deffile("threads::self", XS_threads_self);
2225 newXS_deffile("threads::tid", XS_threads_tid);
2226 newXS_deffile("threads::join", XS_threads_join);
2227 newXS_deffile("threads::yield", XS_threads_yield);
2228 newXS_deffile("threads::detach", XS_threads_detach);
2229 newXS_deffile("threads::kill", XS_threads_kill);
2230 newXS_deffile("threads::DESTROY", XS_threads_DESTROY);
2231 newXS_deffile("threads::equal", XS_threads_equal);
2232 newXS_deffile("threads::object", XS_threads_object);
2233 newXS_deffile("threads::_handle", XS_threads__handle);
2234 newXS_deffile("threads::get_stack_size", XS_threads_get_stack_size);
2235 newXS_deffile("threads::set_stack_size", XS_threads_set_stack_size);
2236 newXS_deffile("threads::is_running", XS_threads_is_running);
2237 newXS_deffile("threads::is_detached", XS_threads_is_detached);
2238 newXS_deffile("threads::is_joinable", XS_threads_is_joinable);
2239 newXS_deffile("threads::wantarray", XS_threads_wantarray);
2240 newXS_deffile("threads::set_thread_exit_only", XS_threads_set_thread_exit_only);
2241 newXS_deffile("threads::error", XS_threads_error);
2242 #endif
2243  
2244 /* Initialisation Section */
2245  
2246 #if XSubPPtmpAAAA
2247 #endif
2248 #line 1833 "threads.xs"
2249 {
2250 #ifdef USE_ITHREADS
2251 SV *my_pool_sv = *hv_fetch(PL_modglobal, MY_POOL_KEY,
2252 sizeof(MY_POOL_KEY)-1, TRUE);
2253 my_pool_t *my_poolp = (my_pool_t*)SvPVX(newSV(sizeof(my_pool_t)-1));
2254  
2255 MY_CXT_INIT;
2256  
2257 Zero(my_poolp, 1, my_pool_t);
2258 sv_setuv(my_pool_sv, PTR2UV(my_poolp));
2259  
2260 PL_perl_destruct_level = 2;
2261 MUTEX_INIT(&MY_POOL.create_destruct_mutex);
2262 MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
2263  
2264 PL_threadhook = &Perl_ithread_hook;
2265  
2266 MY_POOL.tid_counter = 1;
2267 # ifdef THREAD_CREATE_NEEDS_STACK
2268 MY_POOL.default_stack_size = THREAD_CREATE_NEEDS_STACK;
2269 # endif
2270  
2271 /* The 'main' thread is thread 0.
2272 * It is detached (unjoinable) and immortal.
2273 */
2274  
2275 MUTEX_INIT(&MY_POOL.main_thread.mutex);
2276  
2277 /* Head of the threads list */
2278 MY_POOL.main_thread.next = &MY_POOL.main_thread;
2279 MY_POOL.main_thread.prev = &MY_POOL.main_thread;
2280  
2281 MY_POOL.main_thread.count = 1; /* Immortal */
2282  
2283 MY_POOL.main_thread.interp = aTHX;
2284 MY_POOL.main_thread.state = PERL_ITHR_DETACHED; /* Detached */
2285 MY_POOL.main_thread.stack_size = MY_POOL.default_stack_size;
2286 # ifdef WIN32
2287 MY_POOL.main_thread.thr = GetCurrentThreadId();
2288 # else
2289 MY_POOL.main_thread.thr = pthread_self();
2290 # endif
2291  
2292 S_ithread_set(aTHX_ &MY_POOL.main_thread);
2293 MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
2294 #endif /* USE_ITHREADS */
2295 }
2296  
2297 #line 2298 "threads.c"
2298  
2299 /* End of Initialisation Section */
2300  
2301 #if PERL_VERSION_LE(5, 21, 5)
2302 # if PERL_VERSION_GE(5, 9, 0)
2303 if (PL_unitcheckav)
2304 call_list(PL_scopestack_ix, PL_unitcheckav);
2305 # endif
2306 XSRETURN_YES;
2307 #else
2308 0 Perl_xs_boot_epilog(aTHX_ ax);
2309 #endif
2310 0 }
2311  
2312 #ifdef __cplusplus
2313 }
2314 #endif
« blib/lib/threads.pm ↑ Summary