4#include "Stroika/Foundation/StroikaPreComp.h"
6#if qStroika_Foundation_Common_Platform_Windows
8#elif qStroika_Foundation_Common_Platform_Linux
13#include "Stroika/Foundation/Execution/Exceptions.h"
14#include "Stroika/Foundation/Execution/FeatureNotSupportedException.h"
29 [[noreturn]]
void ThrowCPUAffinityUnsupported_ ()
39 constexpr unsigned int kMaxRepresentableCore_ =
40#if qStroika_Foundation_Common_Platform_Windows
41 static_cast<unsigned int> (
sizeof (DWORD_PTR) * 8)
42#elif qStroika_Foundation_Common_Platform_Linux
43 static_cast<unsigned int> (CPU_SETSIZE)
51#if qStroika_Foundation_Common_Platform_Windows
57 constexpr unsigned int kMaxCoreInAMask_ =
static_cast<unsigned int> (
sizeof (DWORD_PTR) * 8);
62 for (
unsigned int c : cores) {
63 Require (c < kMaxCoreInAMask_);
64 mask |= (
static_cast<DWORD_PTR
> (1) << c);
71 for (
unsigned int c = 0; c < kMaxCoreInAMask_; ++c) {
72 if (mask & (
static_cast<DWORD_PTR
> (1) << c)) {
78#elif qStroika_Foundation_Common_Platform_Linux
83 for (
unsigned int c : cores) {
84 Require (c < CPU_SETSIZE);
92 for (
unsigned int c = 0; c < CPU_SETSIZE; ++c) {
93 if (CPU_ISSET (c, &cpuSet)) {
107optional<LogicalCPUCoreSet> Execution::GetCPUAffinity ()
109#if qStroika_Foundation_Common_Platform_Windows
110 DWORD_PTR processMask{};
111 DWORD_PTR systemMask{};
112 if (::GetProcessAffinityMask (::GetCurrentProcess (), &processMask, &systemMask) == 0) {
115 return mkSet_ (processMask);
116#elif qStroika_Foundation_Common_Platform_Linux
121 if (::sched_getaffinity (0,
sizeof (cpuSet), &cpuSet) != 0) {
124 return mkSet_ (cpuSet);
137 Require (not cores.empty ());
149 if (
kCPUAffinitySupported and not cores.All ([] (
unsigned int c) { return c < kMaxRepresentableCore_; })) {
152#if qStroika_Foundation_Common_Platform_Windows
153 if (::SetProcessAffinityMask (::GetCurrentProcess (), mkMask_ (cores)) == 0) {
156#elif qStroika_Foundation_Common_Platform_Linux
157 cpu_set_t cpuSet = mkMask_ (cores);
163 ThrowCPUAffinityUnsupported_ ();
172optional<unsigned int> Execution::PinToOneLogicalCPUCoreQuietly () noexcept
182 if (not allowed or allowed->empty ()) {
185 unsigned int pinTo = *allowed->Min ();
Set<T> is a container of T, where once an item is added, additionally adds () do nothing.
nonvirtual void Add(ArgByValueType< value_type > item)
optional< LogicalCPUCoreSet > GetCPUAffinity()
Which logical CPU cores this process may currently run on; nullopt if not knowable.
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
constexpr bool kCPUAffinitySupported
Can CPU affinity be controlled on this platform at all?
bool SetCPUAffinityQuietly(const LogicalCPUCoreSet &cores) noexcept
Like SetCPUAffinity (), but returns false rather than throwing.
INT_TYPE ThrowPOSIXErrNoIfNegative(INT_TYPE returnCode)