Lines Matching defs:thread

58 #  include <thread.h>
74 #define PERL_ITHR_THREAD_EXIT_ONLY 8 /* exit() only exits current thread */
82 struct _ithread *next; /* Next thread in the list */
83 struct _ithread *prev; /* Prev thread in the list */
84 PerlInterpreter *interp; /* The thread's interpreter */
85 UV tid; /* Thread's module's thread id */
93 DWORD thr; /* OS's idea if thread id */
96 pthread_t thr; /* OS's handle for the thread */
99 SV *err; /* Error from abnormally terminated thread */
110 /* Used by Perl interpreter for thread context switching */
120 /* Structure for 'main' thread
151 /* Block most signals for calling thread, setting the old signal mask to
171 /* no per-thread blocking available */
178 /* Set the signal mask for this thread to newmask */
190 /* Used by Perl interpreter for thread context switching */
192 S_ithread_set(pTHX_ ithread *thread)
195 MY_CXT.context = thread;
208 * but the PVX is. Must be called with thread->mutex already locked. Also,
214 S_ithread_clear(pTHX_ ithread *thread)
221 assert(((thread->state & PERL_ITHR_FINISHED) &&
222 (thread->state & PERL_ITHR_UNCALLABLE))
224 (thread->state & PERL_ITHR_NONVIABLE));
238 interp = thread->interp;
242 /* We will pretend to be a thread that we are not by switching tTHX,
252 S_ithread_set(aTHX_ thread);
254 SvREFCNT_dec(thread->params);
255 thread->params = NULL;
257 if (thread->err) {
258 SvREFCNT_dec_NN(thread->err);
259 thread->err = Nullsv;
264 thread->interp = NULL;
283 S_ithread_free(pTHX_ ithread *thread)
284 PERL_TSA_RELEASE(thread->mutex)
291 if (! (thread->state & PERL_ITHR_NONVIABLE)) {
292 assert(thread->count > 0);
293 if (--thread->count > 0) {
294 MUTEX_UNLOCK(&thread->mutex);
297 assert((thread->state & PERL_ITHR_FINISHED) &&
298 (thread->state & PERL_ITHR_UNCALLABLE));
300 MUTEX_UNLOCK(&thread->mutex);
302 /* Main thread (0) is immortal and should never get here */
303 assert(thread->tid != 0);
307 assert(thread->prev && thread->next);
308 thread->next->prev = thread->prev;
309 thread->prev->next = thread->next;
310 thread->next = NULL;
311 thread->prev = NULL;
315 MUTEX_LOCK(&thread->mutex);
316 S_ithread_clear(aTHX_ thread);
319 handle = thread->handle;
320 thread->handle = NULL;
322 MUTEX_UNLOCK(&thread->mutex);
323 MUTEX_DESTROY(&thread->mutex);
331 PerlMemShared_free(thread);
333 /* total_threads >= 1 is used to veto cleanup by the main thread,
335 * Decrement this as the very last thing in the thread's existence.
346 S_ithread_count_inc(pTHX_ ithread *thread)
347 PERL_TSA_EXCLUDES(thread->mutex)
349 MUTEX_LOCK(&thread->mutex);
350 thread->count++;
351 MUTEX_UNLOCK(&thread->mutex);
383 /* Called from perl_destruct() in each thread. If it's the main thread,
399 ithread *thread = (ithread *)mg->mg_ptr;
400 SvIV_set(sv, PTR2IV(thread));
408 ithread *thread = (ithread *)mg->mg_ptr;
410 MUTEX_LOCK(&thread->mutex);
411 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
452 Perl_warn(aTHX_ "Using minimum thread stack size of %" IVdf, (IV)PTHREAD_STACK_MIN);
504 S_jmpenv_run(pTHX_ int action, ithread *thread,
515 *len_p = (int)call_sv(thread->init_function, thread->gimme|G_EVAL);
517 /* Warn that thread died */
518 Perl_warn(aTHX_ "Thread %" UVuf " terminated abnormally: %" SVf, thread->tid, ERRSV);
535 /* Starts executing the thread.
536 * Passed as the C level function to run in the new thread.
547 ithread *thread = (ithread *)arg;
553 dTHXa(thread->interp);
561 * - creates the new thread
562 * - does MUTEX_LOCK(&thread->mutex)
566 * - tries to MUTEX_LOCK(&thread->mutex)
570 * - does MUTEX_UNLOCK(&thread->mutex)
573 * - finishes MUTEX_LOCK(&thread->mutex)
574 * - does MUTEX_UNLOCK(&thread->mutex)
577 MUTEX_LOCK(&thread->mutex);
578 MUTEX_UNLOCK(&thread->mutex);
580 PERL_SET_CONTEXT(thread->interp);
581 S_ithread_set(aTHX_ thread);
587 S_set_sigmask(&thread->initial_sigmask);
595 AV *params = thread->params;
621 jmp_rc = S_jmpenv_run(aTHX_ 0, thread, &len, &exit_app, &exit_code);
624 /* The interpreter is finished, so this thread can stop receiving
626 * middle of our parent thread calling perl_destruct()...
634 if (jmp_rc == 0 && (thread->gimme & G_WANT) != G_VOID) {
651 thread->err = newSVsv(ERRSV);
655 if (sv_isobject(thread->err)) {
656 thread->err_class = HvNAME(SvSTASH(SvRV(thread->err)));
657 sv_bless(thread->err, gv_stashpv("main", 0));
661 (void)S_jmpenv_run(aTHX_ 1, thread, NULL,
667 SvREFCNT_dec(thread->init_function);
668 thread->init_function = Nullsv;
674 MUTEX_LOCK(&thread->mutex);
676 thread->state |= (PERL_ITHR_FINISHED | died);
678 if (thread->state & PERL_ITHR_THREAD_EXIT_ONLY) {
682 /* Adjust thread status counts */
683 if (thread->state & PERL_ITHR_DETACHED) {
689 MUTEX_UNLOCK(&thread->mutex);
696 (void)S_jmpenv_run(aTHX_ 2, thread, NULL, &exit_app, &exit_code);
706 MUTEX_LOCK(&thread->mutex);
707 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
720 S_ithread_to_SV(pTHX_ SV *obj, ithread *thread, char *classname, bool inc)
726 S_ithread_count_inc(aTHX_ thread);
733 sv_setiv(sv, PTR2IV(thread));
734 mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar, &ithread_vtbl, (char *)thread, 0);
744 /* Argument is a thread */
748 /* Argument is classname, therefore return current thread */
754 * Called in context of parent thread.
771 ithread *thread;
785 /* Allocate thread structure in context of the main thread's interpreter */
788 thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
791 if (!thread) {
805 Zero(thread, 1, ithread);
808 thread->next = &my_pool->main_thread;
809 thread->prev = my_pool->main_thread.prev;
810 my_pool->main_thread.prev = thread;
811 thread->prev->next = thread;
814 /* 1 ref to be held by the local var 'thread' in S_ithread_run().
818 * freed until join/detach, even if no thread objects remain.
822 thread->count = 3;
824 /* Block new thread until ->create() call finishes */
825 MUTEX_INIT(&thread->mutex);
826 MUTEX_LOCK(&thread->mutex); /* See S_ithread_run() for more detail. */
828 thread->tid = my_pool->tid_counter++;
829 thread->stack_size = S_good_stack_size(aTHX_ stack_size);
830 thread->gimme = gimme;
831 thread->state = exit_opt;
834 /* "Clone" our interpreter into the thread's interpreter.
835 * This gives thread access to "static data" and code.
838 S_ithread_set(aTHX_ thread);
852 * thread), until it is running in its own thread. Another problem is that
853 * the new thread will not have set the context until some time after it
857 * So we block most signals here, so the new thread will inherit the signal
858 * mask, and unblock them right after the thread creation. The original
859 * mask is saved in the thread struct so that the new thread can restore
862 S_block_most_signals(&thread->initial_sigmask);
866 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE | CLONEf_CLONE_HOST);
868 thread->interp = perl_clone(aTHX, CLONEf_KEEP_PTR_TABLE);
877 CLONE_PARAMS *clone_param = Perl_clone_params_new(aTHX, thread->interp);
882 dTHXa(thread->interp);
890 /* Here we remove END blocks since they should only run in the thread
897 thread->init_function = newSV(0);
898 sv_copypv(thread->init_function, init_function);
900 thread->init_function = sv_dup_inc(init_function, clone_param);
903 thread->params = params = newAV();
953 SvTEMP_off(thread->init_function);
961 /* Create/start the thread */
963 thread->handle = CreateThread(NULL,
964 (DWORD)thread->stack_size,
966 (LPVOID)thread,
968 &thread->thr);
985 /* Set thread's stack size */
986 if (thread->stack_size > 0) {
987 rc_stack_size = pthread_attr_setstacksize(&attr, (size_t)thread->stack_size);
991 /* Create the thread */
994 rc_thread_create = pthread_create(&thread->thr,
997 (void *)thread);
1002 rc_thread_create = pthread_create(&thread->thr,
1005 (void *)thread);
1011 * context and we have created the thread.
1013 S_set_sigmask(&thread->initial_sigmask);
1017 /* Try to get thread's actual stack size */
1026 thread->stack_size = (IV)stacksize;
1035 if (thread->handle == NULL) {
1043 thread->state |= PERL_ITHR_NONVIABLE;
1044 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
1048 Perl_warn(aTHX_ "Thread creation failed: pthread_attr_setstacksize(%" IVdf ") returned %d", thread->stack_size, rc_stack_size);
1059 return (thread);
1062 /* warning: mutex 'thread->mutex' is not held on every path through here [-Wthread-safety-analysis] */
1078 ithread *thread;
1106 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1107 MUTEX_LOCK(&thread->mutex);
1108 stack_size = thread->stack_size;
1109 exit_opt = thread->state & PERL_ITHR_THREAD_EXIT_ONLY;
1110 MUTEX_UNLOCK(&thread->mutex);
1186 /* Create thread */
1188 thread = S_ithread_create(aTHX_ &MY_POOL,
1195 if (! thread) {
1199 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, FALSE));
1201 /* Let thread run. */
1204 /* warning: releasing mutex 'thread->mutex' that was not held [-Wthread-safety-analysis] */
1205 MUTEX_UNLOCK(&thread->mutex);
1214 ithread *thread;
1237 for (thread = MY_POOL.main_thread.next;
1238 thread != &MY_POOL.main_thread;
1239 thread = thread->next)
1241 MUTEX_LOCK(&thread->mutex);
1242 state = thread->state;
1243 MUTEX_UNLOCK(&thread->mutex);
1265 XPUSHs(sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE)));
1280 ithread *thread;
1288 thread = S_ithread_get(aTHX);
1290 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1297 ithread *thread;
1300 thread = S_SV_to_ithread(aTHX_ ST(0));
1301 XST_mUV(0, thread->tid);
1308 ithread *thread;
1325 /* Check if the thread is joinable and not ourselves */
1326 thread = S_SV_to_ithread(aTHX_ ST(0));
1329 MUTEX_LOCK(&thread->mutex);
1330 if ((join_err = (thread->state & PERL_ITHR_UNCALLABLE))) {
1331 MUTEX_UNLOCK(&thread->mutex);
1333 ? "Cannot join a detached thread"
1335 } else if (thread->tid == current_thread->tid) {
1336 MUTEX_UNLOCK(&thread->mutex);
1341 thread->state |= PERL_ITHR_JOINED;
1342 MUTEX_UNLOCK(&thread->mutex);
1348 /* Join the thread */
1350 if (WaitForSingleObject(thread->handle, INFINITE) != WAIT_OBJECT_0) {
1355 if ((rc_join = pthread_join(thread->thr, &retval)) != 0) {
1362 MUTEX_LOCK(&thread->mutex);
1365 if ((thread->gimme & G_WANT) != G_VOID) {
1371 params_copy = thread->params;
1372 other_perl = thread->interp;
1376 S_ithread_set(aTHX_ thread);
1389 PerlInterpreter *other_perl = thread->interp;
1392 params_copy = thread->params;
1395 S_ithread_set(aTHX_ thread);
1412 /* If thread didn't die, then we can free its interpreter */
1413 if (! (thread->state & PERL_ITHR_DIED)) {
1414 S_ithread_clear(aTHX_ thread);
1416 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
1444 ithread *thread;
1450 /* Detach the thread */
1451 thread = S_SV_to_ithread(aTHX_ ST(0));
1453 MUTEX_LOCK(&thread->mutex);
1454 if (! (detach_err = (thread->state & PERL_ITHR_UNCALLABLE))) {
1456 thread->state |= PERL_ITHR_DETACHED;
1458 /* Windows has no 'detach thread' function */
1460 PERL_THREAD_DETACH(thread->thr);
1462 if (thread->state & PERL_ITHR_FINISHED) {
1469 MUTEX_UNLOCK(&thread->mutex);
1475 : "Cannot detach a joined thread");
1478 /* If thread is finished and didn't die,
1480 MUTEX_LOCK(&thread->mutex);
1481 if ((thread->state & PERL_ITHR_FINISHED) &&
1482 ! (thread->state & PERL_ITHR_DIED))
1484 S_ithread_clear(aTHX_ thread);
1486 S_ithread_free(aTHX_ thread); /* Releases MUTEX */
1492 ithread *thread;
1520 /* Set the signal for the thread */
1521 thread = S_SV_to_ithread(aTHX_ ST(0));
1522 MUTEX_LOCK(&thread->mutex);
1523 if (thread->interp && ! (thread->state & PERL_ITHR_FINISHED)) {
1524 dTHXa(thread->interp);
1531 /* Ignore signal to terminated/finished thread */
1534 MUTEX_UNLOCK(&thread->mutex);
1537 Perl_croak(aTHX_ "Signal %s received in thread %" UVuf
1539 sig_name, thread->tid);
1542 /* Return the thread to allow for method chaining */
1561 /* Compares TIDs to determine thread equality */
1582 ithread *thread;
1608 /* If current thread wants its own object, then behave the same as
1610 thread = S_ithread_get(aTHX);
1611 if (thread->tid == tid) {
1612 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1618 for (thread = MY_POOL.main_thread.next;
1619 thread != &MY_POOL.main_thread;
1620 thread = thread->next)
1623 if (thread->tid == tid) {
1625 MUTEX_LOCK(&thread->mutex);
1626 state = thread->state;
1627 MUTEX_UNLOCK(&thread->mutex);
1630 ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, TRUE));
1648 ithread *thread;
1651 thread = S_SV_to_ithread(aTHX_ ST(0));
1653 XST_mUV(0, PTR2UV(&thread->handle));
1655 XST_mUV(0, PTR2UV(&thread->thr));
1669 ithread *thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1670 stack_size = thread->stack_size;
1689 Perl_croak(aTHX_ "Cannot change stack size of an existing thread");
1704 ithread *thread;
1711 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1712 MUTEX_LOCK(&thread->mutex);
1713 ST(0) = (thread->state & PERL_ITHR_FINISHED) ? &PL_sv_no : &PL_sv_yes;
1714 MUTEX_UNLOCK(&thread->mutex);
1721 ithread *thread;
1724 thread = S_SV_to_ithread(aTHX_ ST(0));
1725 MUTEX_LOCK(&thread->mutex);
1726 ST(0) = (thread->state & PERL_ITHR_DETACHED) ? &PL_sv_yes : &PL_sv_no;
1727 MUTEX_UNLOCK(&thread->mutex);
1734 ithread *thread;
1741 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1742 MUTEX_LOCK(&thread->mutex);
1743 ST(0) = ((thread->state & PERL_ITHR_FINISHED) &&
1744 ! (thread->state & PERL_ITHR_UNCALLABLE))
1746 MUTEX_UNLOCK(&thread->mutex);
1753 ithread *thread;
1756 thread = S_SV_to_ithread(aTHX_ ST(0));
1757 ST(0) = ((thread->gimme & G_WANT) == G_LIST) ? &PL_sv_yes :
1758 ((thread->gimme & G_WANT) == G_VOID) ? &PL_sv_undef
1766 ithread *thread;
1771 thread = S_SV_to_ithread(aTHX_ ST(0));
1772 MUTEX_LOCK(&thread->mutex);
1774 thread->state |= PERL_ITHR_THREAD_EXIT_ONLY;
1776 thread->state &= ~PERL_ITHR_THREAD_EXIT_ONLY;
1778 MUTEX_UNLOCK(&thread->mutex);
1784 ithread *thread;
1792 thread = INT2PTR(ithread *, SvIV(SvRV(ST(0))));
1793 MUTEX_LOCK(&thread->mutex);
1795 /* If thread died, then clone the error into the calling thread */
1796 if (thread->state & PERL_ITHR_DIED) {
1802 other_perl = thread->interp;
1807 S_ithread_set(aTHX_ thread);
1812 err = sv_dup(thread->err, &clone_params);
1817 if (thread->err_class) {
1818 sv_bless(err, gv_stashpv(thread->err_class, 1));
1823 PerlInterpreter *other_perl = thread->interp;
1830 S_ithread_set(aTHX_ thread);
1838 err = sv_dup(thread->err, clone_params);
1843 if (thread->err_class) {
1844 sv_bless(err, gv_stashpv(thread->err_class, 1));
1851 MUTEX_UNLOCK(&thread->mutex);
1887 /* The 'main' thread is thread 0.