util: add util_get_current_cpu using sched_getcpu and Windows equivalent

Acked-by: Jose Fonseca <jfonseca@vmware.com>
Acked-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7054>
diff --git a/src/mesa/state_tracker/st_draw.c b/src/mesa/state_tracker/st_draw.c
index cbc8b13..0e22d0c 100644
--- a/src/mesa/state_tracker/st_draw.c
+++ b/src/mesa/state_tracker/st_draw.c
@@ -68,13 +68,6 @@
 #include "draw/draw_context.h"
 #include "cso_cache/cso_context.h"
 
-#if defined(PIPE_OS_LINUX) && !defined(ANDROID)
-#include <sched.h>
-#define HAVE_SCHED_GETCPU 1
-#else
-#define sched_getcpu() 0
-#define HAVE_SCHED_GETCPU 0
-#endif
 
 /**
  * Set the restart index.
@@ -136,8 +129,7 @@
    /* Pin threads regularly to the same Zen CCX that the main thread is
     * running on. The main thread can move between CCXs.
     */
-   if (unlikely(HAVE_SCHED_GETCPU && /* Linux */
-                /* AMD Zen */
+   if (unlikely(/* AMD Zen */
                 util_cpu_caps.nr_cpus != util_cpu_caps.cores_per_L3 &&
                 /* no glthread */
                 ctx->CurrentClientDispatch != ctx->MarshalExec &&
@@ -145,7 +137,7 @@
                 pipe->set_context_param &&
                 /* do it occasionally */
                 ++st->pin_thread_counter % 512 == 0)) {
-      int cpu = sched_getcpu();
+      int cpu = util_get_current_cpu();
       if (cpu >= 0) {
          unsigned L3_cache = cpu / util_cpu_caps.cores_per_L3;
 
diff --git a/src/util/u_thread.h b/src/util/u_thread.h
index 3f47335..93d8b0f 100644
--- a/src/util/u_thread.h
+++ b/src/util/u_thread.h
@@ -46,6 +46,12 @@
 #include <OS.h>
 #endif
 
+#if DETECT_OS_LINUX && !defined(ANDROID)
+#include <sched.h>
+#elif defined(_WIN32) && !defined(__CYGWIN__) && _WIN32_WINNT >= 0x0600
+#include <windows.h>
+#endif
+
 #ifdef __FreeBSD__
 /* pthread_np.h -> sys/param.h -> machine/param.h
  * - defines ALIGN which clashes with our ALIGN
@@ -57,6 +63,20 @@
 /* For util_set_thread_affinity to size the mask. */
 #define UTIL_MAX_CPUS               1024  /* this should be enough */
 
+static inline int
+util_get_current_cpu(void)
+{
+#if DETECT_OS_LINUX && !defined(ANDROID)
+   return sched_getcpu();
+
+#elif defined(_WIN32) && !defined(__CYGWIN__) && _WIN32_WINNT >= 0x0600
+   return GetCurrentProcessorNumber();
+
+#else
+   return -1;
+#endif
+}
+
 static inline thrd_t u_thread_create(int (*routine)(void *), void *param)
 {
    thrd_t thread;