Acme-Parataxis
view release on metacpan or search on metacpan
lib/Acme/Parataxis.c view on Meta::CPAN
PARA_ASM_TYPE(PARA_ASM_SWITCH)
PARA_ASM_SWITCH ":\n"
" sub sp, sp, #176\n"
" stp x19, x20, [sp, #0]\n"
" stp x21, x22, [sp, #16]\n"
" stp x23, x24, [sp, #32]\n"
" stp x25, x26, [sp, #48]\n"
" stp x27, x28, [sp, #64]\n"
" stp x29, x30, [sp, #80]\n"
" stp d8, d9, [sp, #96]\n"
" stp d10, d11, [sp, #112]\n"
" stp d12, d13, [sp, #128]\n"
" stp d14, d15, [sp, #144]\n"
" mrs x16, fpcr\n"
" str x16, [sp, #160]\n"
" mrs x16, fpsr\n"
" str x16, [sp, #168]\n"
" mov x16, sp\n"
" str x16, [x0]\n" /* *from = saved frame */
" ldr x16, [x1]\n" /* x16 = *to */
" mov sp, x16\n"
" ldr x16, [sp, #160]\n"
" msr fpcr, x16\n"
" ldr x16, [sp, #168]\n"
" msr fpsr, x16\n"
" ldp d14, d15, [sp, #144]\n"
" ldp d12, d13, [sp, #128]\n"
" ldp d10, d11, [sp, #112]\n"
" ldp d8, d9, [sp, #96]\n"
" ldp x27, x28, [sp, #64]\n"
" ldp x25, x26, [sp, #48]\n"
" ldp x23, x24, [sp, #32]\n"
" ldp x21, x22, [sp, #16]\n"
" ldp x19, x20, [sp, #0]\n"
" ldp x29, x30, [sp, #80]\n"
" add sp, sp, #176\n"
" ret\n" /* x30 was restored from the target frame */
PARA_ASM_SIZE(PARA_ASM_SWITCH)
".p2align 4\n"
PARA_ASM_VIS(PARA_ASM_TRAMP)
PARA_ASM_TYPE(PARA_ASM_TRAMP)
PARA_ASM_TRAMP ":\n"
" mov x0, x19\n" /* arm_fiber_context puts the fiber pointer in saved x19 */
" bl " PARA_ASM_ENTRY "\n"
" udf #0\n" /* para_entry_point must never return */
PARA_ASM_SIZE(PARA_ASM_TRAMP));
#undef PARA_ASM_SWITCH
#undef PARA_ASM_TRAMP
#undef PARA_ASM_ENTRY
#undef PARA_ASM_VIS
#undef PARA_ASM_TYPE
#undef PARA_ASM_SIZE
#endif /* USE_AARCH64_ASM_CORO */
#endif /* USE_ASM_CORO */
/**
* @brief Get the Operating System's unique Thread ID.
*
* Useful for debugging to prove that background tasks are running on different OS threads than the main Perl
* interpreter.
*
* @return int The TID (Windows) or LWP ID (Linux/BSD/macOS).
*/
int get_os_thread_id() {
#ifdef _WIN32
return (int)GetCurrentThreadId();
#elif defined(__APPLE__)
uint64_t tid;
pthread_threadid_np(NULL, &tid);
return (int)tid;
#elif defined(SYS_gettid)
return (int)syscall(SYS_gettid);
#else
return (int)(intptr_t)pthread_self();
#endif
}
/**
* @brief Pin the current thread to a specific CPU core.
*
* Used by the Thread Pool to ensure worker threads are distributed across available hardware cores
* for maximum parallelism.
*
* @param core_id The zero-based index of the CPU core.
*/
void pin_to_core(int core_id) {
#ifdef _WIN32
DWORD_PTR mask = (1ULL << core_id);
SetThreadAffinityMask(GetCurrentThread(), mask);
#elif defined(__linux__) && !defined(__ANDROID__)
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
#if defined(__ANDROID__)
/* Bionic has no pthread_setaffinity_np(); sched_setaffinity() takes a kernel TID, not a pthread_t. */
(void)sched_setaffinity(syscall(SYS_gettid), sizeof(cpu_set_t), &cpuset);
#else
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
#endif
#else
(void)core_id; /* Not supported on Android, macOS, or standard BSD APIs */
#endif
}
/**
* @brief Get the index of the CPU core currently executing this thread.
*
* @return int Core ID (0..N) or -1 if unsupported.
*/
int get_current_cpu() {
#ifdef _WIN32
return GetCurrentProcessorNumber();
#elif defined(__linux__)
return sched_getcpu();
#else
return -1;
#endif
}
lib/Acme/Parataxis.c view on Meta::CPAN
* cache can claim a depth that nothing occupies. Callers that need to know whether a CV is *really*
* active must ask the stacks, not the cache. */
static int para_si_cx_has_frame(PERL_SI * si, CV * cv);
static int para_si_has_frame(PERL_SI * si, CV * cv);
static int para_cv_frame_live(pTHX_ CV * cv) {
PERL_SI * si = PL_curstackinfo;
if (para_si_has_frame(si, cv))
return 1;
/* The main context (id -1) is not in fibers[], but it is parked with a live frame whenever a
* fiber runs, so it has to be asked directly. */
if (main_context.si && main_context.si != si && para_si_has_frame(main_context.si, cv))
return 1;
for (int i = 0; i < fiber_capacity; i++) {
PERL_SI * fs = fibers[i] ? fibers[i]->si : NULL;
if (!fs || fs == si)
continue;
if (para_si_has_frame(fs, cv))
return 1;
}
return 0;
}
/** @brief TRUE if a live CXt_SUB/CXt_FORMAT frame for cv sits in this single si's own context stack. */
static int para_si_cx_has_frame(PERL_SI * si, CV * cv) {
if (!si || !si->si_cxstack)
return 0;
for (I32 i = 0; i <= si->si_cxix; i++) {
PERL_CONTEXT * cx = &(si->si_cxstack[i]);
if ((CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) && cx->blk_sub.cv == cv)
return 1;
}
return 0;
}
/** @brief TRUE if a live CXt_SUB/CXt_FORMAT frame for cv sits anywhere in this si's runlevel chain.
*
* A runlevel chain is not one context stack. push_scope()/new_stackinfo_flags() hands each nested
* runlevel its own si with a fresh cxstack whose si_cxix starts at -1, and links it into the current
* chain with si_prev/si_next. Frames created inside the nested runlevel therefore live in *its*
* cxstack and are absent from the outer one, so a scan of si->si_cxstack alone reports "not live"
* for a CV whose frame is live two runlevels down. Test2's subtest buffer is exactly such a runlevel:
* subtest_buffered()/run_subtest() frames sit in a nested si of the main context, which is why the
* settle pass used to zero CvDEPTH for a CV that had a live frame. */
static int para_si_has_frame(PERL_SI * si, CV * cv) {
for (PERL_SI * s = si; s; s = s->si_prev)
if (para_si_cx_has_frame(s, cv))
return 1;
for (PERL_SI * s = si ? si->si_next : NULL; s; s = s->si_next)
if (para_si_cx_has_frame(s, cv))
return 1;
return 0;
}
/** @brief TRUE if a live frame for cv sits on the depositing (from) or resuming (to) stack.
*
* Pass 1c runs after PL_curstackinfo already points at the resuming stack, so the depositing stack is
* only reachable through the fiber struct. Both are asked directly, over their whole runlevel chain,
* because a pinned CV can still own a frame that no claim in the register accounts for. Settling such
* a CV to CvDEPTH 0 zeroes the depth its live frame needs, and the frame's own pop then trips core's
* cx_popsub_args assert (Padlist[CvDEPTH] == PL_curpad) or, in a non-debug build, reads @_ out of
* the wrong pad. */
static int para_cv_live_on_swap(pTHX_ para_fiber_t * from, para_fiber_t * to, CV * cv) {
if (from && from->si && para_si_has_frame(from->si, cv))
return 1;
if (to && to != from && to->si && para_si_has_frame(to->si, cv))
return 1;
return 0;
}
/** @brief Drops every claim the registry holds for a CV, leaving the (dead) key in place.
*
* Withdrawing a claim is not optional once the depth it justifies is being cleared: Pass 1b pins
* CvDEPTH back to max_depth for every still-active entry, so a stale claim would resurrect the very
* value we just released on the next swap. */
static void para_cvreg_purge(CV * cv) {
para_cvreg_entry_t * e = para_cvreg_lookup(cv, 0);
if (!e)
return;
para_cvreg_unpin(e);
para_cvreg_deactivate(e);
if (e->counts)
Safefree(e->counts);
e->counts = NULL;
e->arr_len = 0;
e->max_depth = 0;
}
/* Reset slot 0 of the pad a call would land on: slot 0 of a fresh perl pad is a REIFY-only,
* empty AV (pad_push), so a call re-entering a shared subroutine must find its @_ slot in
* exactly that state for pp_entersub's assert(!AvREAL(av)) / AvFILLp(av) == -1 invariants.
* AvREIFY_only -- not bare AvREAL_off -- restores the full canonical state perl expects (else
* the slot is neither-REAL-nor-REIFY and a later av_store re-turns it AvREAL). */
static void _clean_landing_pad(pTHX_ CV * cv, I32 depth) {
PADLIST * pl = CvPADLIST(cv);
if (!pl || depth <= 0 || depth > PadlistMAX(pl))
return;
/* A depth the registry still counts as occupied belongs to a fiber parked *inside* this CV, which will read
* its @_ again on resume. Resetting it destroys the victim's live state two ways: the handle read back out
* of $_[0] comes back undef, and since $_[1] is an alias for the caller's buffer, the caller's buffer is
* emptied silently, after the read itself already succeeded. Only a genuinely unoccupied depth may be reset.
* See the depth invariant above for why an occupied landing depth exists in the first place. */
para_cvreg_entry_t * e = para_cvreg_lookup(cv, 0);
if (e && e->counts && depth < e->arr_len && e->counts[depth] > 0)
return;
AV * pad = (AV *)PadlistARRAY(pl)[depth];
if (!pad || SvTYPE((SV *)pad) != SVt_PVAV)
return;
SV ** array = AvARRAY(pad);
if (!array || AvMAX(pad) < 0)
return;
SV * args = array[0];
if (args && SvTYPE(args) == SVt_PVAV) {
if (AvREAL(args))
para_report_avreal_pad(aTHX_ cv, depth, "pass2-cleaning");
AvFILLp((AV *)args) = -1;
AvREIFY_only((AV *)args);
}
}
/* diagnostic: swap-time pad dump */
( run in 1.424 second using v1.01-cache-2.11-cpan-c32d3c3b8f1 )