From 08ae4179317024ea943cdba7eb4650d2ad0d2429 Mon Sep 17 00:00:00 2001 From: Rokas Kupstys Date: Tue, 23 May 2017 19:49:21 +0300 Subject: [PATCH] Few more MacOS fixes. Thread id changed to size_t, required for MacOS because older versions do not have integral thread ids and we must use a pointer returned by pthread_self()/ --- easy_profiler_core/easy_socket.cpp | 2 +- easy_profiler_core/include/easy/profiler.h | 2 +- easy_profiler_core/profile_manager.h | 20 ++++++++++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/easy_profiler_core/easy_socket.cpp b/easy_profiler_core/easy_socket.cpp index c937ade..dc7a4c8 100644 --- a/easy_profiler_core/easy_socket.cpp +++ b/easy_profiler_core/easy_socket.cpp @@ -204,7 +204,7 @@ int EasySocket::send(const void *buf, size_t nbyte) { if(!checkSocket(m_replySocket)) return -1; int res = 0; -#ifdef _WIN32 +#if defined(_WIN32) || defined(__APPLE__) res = ::send(m_replySocket, (const char*)buf, (int)nbyte, 0); #else res = ::send(m_replySocket,buf,nbyte,MSG_NOSIGNAL); diff --git a/easy_profiler_core/include/easy/profiler.h b/easy_profiler_core/include/easy/profiler.h index 617871d..c0bdc20 100644 --- a/easy_profiler_core/include/easy/profiler.h +++ b/easy_profiler_core/include/easy/profiler.h @@ -445,7 +445,7 @@ namespace profiler { const uint16_t DEFAULT_PORT = EASY_DEFAULT_PORT; typedef uint64_t timestamp_t; - typedef uint32_t thread_id_t; + typedef size_t thread_id_t; typedef uint32_t block_id_t; enum BlockType : uint8_t diff --git a/easy_profiler_core/profile_manager.h b/easy_profiler_core/profile_manager.h index ac12680..214e207 100644 --- a/easy_profiler_core/profile_manager.h +++ b/easy_profiler_core/profile_manager.h @@ -61,6 +61,9 @@ The Apache License, Version 2.0 (the "License"); #ifdef _WIN32 #include +#elif defined(__APPLE__) +#include +#include #else #include #include @@ -73,13 +76,22 @@ The Apache License, Version 2.0 (the "License"); #undef max #endif -inline uint32_t getCurrentThreadId() +inline profiler::thread_id_t getCurrentThreadId() { #ifdef _WIN32 - return (uint32_t)::GetCurrentThreadId(); + return (profiler::thread_id_t)::GetCurrentThreadId(); +#elif defined(__APPLE__) +# if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_6) || \ + (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0) + EASY_THREAD_LOCAL static uint64_t _id = 0; + if (!_id) + pthread_threadid_np(NULL, &_id); + return (profiler::thread_id_t)_id; +# else + return (profiler::thread_id_t)pthread_self(); +# endif #else - EASY_THREAD_LOCAL static const pid_t x = syscall(__NR_gettid); - EASY_THREAD_LOCAL static const uint32_t _id = (uint32_t)x;//std::hash()(std::this_thread::get_id()); + EASY_THREAD_LOCAL static const profiler::thread_id_t _id = (profiler::thread_id_t)syscall(__NR_gettid); return _id; #endif }