From c65aa4e4889939c1afa82001db349cac237a13f8 Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Sun, 23 Apr 2023 16:09:41 +0800 Subject: [PATCH] os-inl.h: fix for missing pthread_threadid_np (#2715) --- include/spdlog/details/os-inl.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index b7817184..e1080d05 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -62,6 +62,10 @@ #endif // unix +#if defined __APPLE__ +# include +#endif + #ifndef __has_feature // Clang - feature checking macros. # define __has_feature(x) 0 // Compatibility with non-clang compilers. #endif @@ -355,7 +359,19 @@ SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT return static_cast(::thr_self()); #elif __APPLE__ uint64_t tid; - pthread_threadid_np(nullptr, &tid); + // There is no pthread_threadid_np prior to 10.6, and it is not supported on any PPC, + // including 10.6.8 Rosetta. __POWERPC__ is Apple-specific define encompassing ppc and ppc64. +# 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) { + pthread_threadid_np(nullptr, &tid); + } else { + tid = pthread_mach_thread_np(pthread_self()); + } +# else + pthread_threadid_np(nullptr, &tid); +# endif return static_cast(tid); #else // Default to standard C++11 (other Unix) return static_cast(std::hash()(std::this_thread::get_id()));