diff --git a/src/common/threading.cpp b/src/common/threading.cpp index ed221f2ed..278a56441 100644 --- a/src/common/threading.cpp +++ b/src/common/threading.cpp @@ -40,6 +40,8 @@ #include #include #include +#include +#include #else #include #endif @@ -602,6 +604,26 @@ u64 Threading::GetThreadTicksPerSecond() #endif } +u32 Threading::GetProcessorCount() +{ +#if defined(_WIN32) + const DWORD count = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); + return count > 0 ? static_cast(count) : 1; +#elif defined(__linux__) + const long count = sysconf(_SC_NPROCESSORS_ONLN); + return count > 0 ? static_cast(count) : 1; +#elif defined(__APPLE__) + int count = 0; + size_t size = sizeof(count); + if (sysctlbyname("hw.logicalcpu", &count, &size, nullptr, 0) == 0 && count > 0) + return static_cast(count); + else + return 1; +#else +#error Unsupported platform +#endif +} + void Threading::SetNameOfCurrentThread(const char* name) { // This feature needs Windows headers and MSVC's SEH support: diff --git a/src/common/threading.h b/src/common/threading.h index 49a436467..b6f71ca81 100644 --- a/src/common/threading.h +++ b/src/common/threading.h @@ -17,6 +17,7 @@ namespace Threading { extern u64 GetThreadCpuTime(); extern u64 GetThreadTicksPerSecond(); +extern u32 GetProcessorCount(); /// Set the name of the current thread extern void SetNameOfCurrentThread(const char* name);