#include "thread_session.h" #include using Workload = void (*)(void *context); using AcceptScheduling = bool (*)(const single_core::SessionReport &) noexcept; struct Measurement { single_core::SessionReport scheduling; single_core::SessionReport schedulingAtEntry; single_core::SessionReport schedulingAfterWork; bool schedulingAccepted = false; bool workloadCompleted = false; bool workloadThrew = false; bool cleanupPassed = false; }; // Call and join this from a native background runner, never an ArkUI thread. // The caller owns the actual workload, its timer, output checks and cancellation. Measurement ExecuteOnComputeThread(int cpu, Workload workload, void *context, AcceptScheduling accept) { Measurement result{}; if (workload == nullptr || accept == nullptr) return result; std::thread compute([&] { single_core::Options options{}; options.useQos = true; options.requestRealtime = false; single_core::ThreadSession session(cpu, options); result.schedulingAtEntry = session.Report(); result.schedulingAccepted = accept(result.schedulingAtEntry); if (result.schedulingAccepted) { try { workload(context); // Put the real timer and compute loop here. result.workloadCompleted = true; } catch (...) { result.workloadThrew = true; } } (void)session.ObserveNow(); result.schedulingAfterWork = session.Report(); // Stop and join any workload-owned observers before this boundary. result.cleanupPassed = session.Finish(); result.scheduling = session.Report(); }); compute.join(); return result; } // One explicit policy for a strict boundary check. A SOFT production policy // must supply its own target-group/attribute criteria and runtime observation. bool RequireExactAtBoundary(const single_core::SessionReport &report) noexcept { return report.snapshotComplete && report.validTarget && !report.wrongThread && report.exactAtReadback; }