Sched_getcpu () equivalent for OS X?

In OS X, is there a way to find out which processor a thread is running on? Equivalent function for Linux - sched_getcpu

+4
source share
1 answer

The GetCurrentProcessorNumber example shows code that implements this function using the CPUID instruction. I tried this myself and can confirm that it works on Mac OS X.

Here is my version that I used on Mac OS X

#include <cpuid.h>

#define CPUID(INFO, LEAF, SUBLEAF) __cpuid_count(LEAF, SUBLEAF, INFO[0], INFO[1], INFO[2], INFO[3])

#define GETCPU(CPU) {                              \
        uint32_t CPUInfo[4];                           \
        CPUID(CPUInfo, 1, 0);                          \
        /* CPUInfo[1] is EBX, bits 24-31 are APIC ID */ \
        if ( (CPUInfo[3] & (1 << 9)) == 0) {           \
          CPU = -1;  /* no APIC on chip */             \
        }                                              \
        else {                                         \
          CPU = (unsigned)CPUInfo[1] >> 24;                    \
        }                                              \
        if (CPU < 0) CPU = 0;                          \
      }
+3
source

Source: https://habr.com/ru/post/1616121/


All Articles