From 04088e6cb1b1614ddfab29b60aebff316e009bb4 Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <barracuda@macos-powerpc.org>
Date: Wed, 14 Aug 2024 19:00:28 +0800
Subject: [PATCH] mk_scheduler: fix for missing pthread_threadid_np

---
 lib/monkey/mk_server/mk_scheduler.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git lib/monkey/mk_server/mk_scheduler.c lib/monkey/mk_server/mk_scheduler.c
index a680d3cdf..2ccec3bed 100644
--- lib/monkey/mk_server/mk_scheduler.c
+++ lib/monkey/mk_server/mk_scheduler.c
@@ -37,6 +37,10 @@
 
 #include <signal.h>
 
+#ifdef __APPLE__
+#include <AvailabilityMacros.h>
+#endif
+
 #ifndef _WIN32
 #include <sys/syscall.h>
 #endif
@@ -288,7 +292,17 @@ static int mk_sched_register_thread(struct mk_server *server)
     worker->pid = syscall(__NR_gettid);
 #elif defined(__APPLE__)
     uint64_t tid;
+#if MAC_OS_X_VERSION_MAX_ALLOWED < 1060 || defined(__POWERPC__)
+    tid = pthread_mach_thread_np(pthread_self());
+#elif MAC_OS_X_VERSION_MIN_REQUIRED < 1060
+    if (&pthread_threadid_np != NULL) {
+        pthread_threadid_np(NULL, &tid);
+    } else {
+        tid = pthread_mach_thread_np(pthread_self());
+    }
+#else
     pthread_threadid_np(NULL, &tid);
+#endif
     worker->pid = tid;
 #else
     worker->pid = 0xdeadbeef;
-- 
2.46.0

