Threading: Add GetProcessorCount()

pull/3797/merge
Stenzek 1 week ago
parent 563c355131
commit efecb78dbd
No known key found for this signature in database

@ -40,6 +40,8 @@
#include <mach/mach_time.h>
#include <mach/semaphore.h>
#include <mach/task.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#else
#include <pthread_np.h>
#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<uint32_t>(count) : 1;
#elif defined(__linux__)
const long count = sysconf(_SC_NPROCESSORS_ONLN);
return count > 0 ? static_cast<uint32_t>(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<uint32_t>(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:

@ -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);

Loading…
Cancel
Save