From 4a1a7937ae795b46c885fd3edf871fe8cbb2468e Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Fri, 15 Jul 2016 15:01:16 +0300 Subject: [PATCH] [core] Move set/get thread names to platform:: Android needs its own implementation. --- include/mbgl/platform/platform.hpp | 6 ++++++ platform/default/thread.cpp | 29 +++++++++++++++++++++++++---- src/mbgl/platform/log.cpp | 2 +- src/mbgl/util/thread.hpp | 25 +------------------------ 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/include/mbgl/platform/platform.hpp b/include/mbgl/platform/platform.hpp index 59ba7f97f..cc8327c47 100644 --- a/include/mbgl/platform/platform.hpp +++ b/include/mbgl/platform/platform.hpp @@ -14,6 +14,12 @@ std::string uppercase(const std::string &string); // Lowercase a string, potentially using platform-specific routines. std::string lowercase(const std::string &string); +// Gets the name of the current thread. +std::string getCurrentThreadName(); + +// Set the name of the current thread, truncated at 15. +void setCurrentThreadName(const std::string& name); + // Makes the current thread low priority. void makeThreadLowPriority(); diff --git a/platform/default/thread.cpp b/platform/default/thread.cpp index 3ef325792..d398bc926 100644 --- a/platform/default/thread.cpp +++ b/platform/default/thread.cpp @@ -1,16 +1,37 @@ #include - #include +#include + #include -#ifdef __linux__ -#include -#endif #include namespace mbgl { namespace platform { +std::string getCurrentThreadName() { + char name[32] = "unknown"; +#if defined(__APPLE__) + pthread_getname_np(pthread_self(), name, sizeof(name)); +#elif defined(__GLIBC__) && defined(__GLIBC_PREREQ) +#if __GLIBC_PREREQ(2, 12) + pthread_getname_np(pthread_self(), name, sizeof(name)); +#endif +#endif + return name; +} + +void setCurrentThreadName(const std::string& name) { +#if defined(__APPLE__) + pthread_setname_np(name.c_str()); +#elif defined(__GLIBC__) && defined(__GLIBC_PREREQ) +#if __GLIBC_PREREQ(2, 12) + pthread_setname_np(pthread_self(), name.c_str()); +#endif +#endif + (void)name; +} + void makeThreadLowPriority() { #ifdef SCHED_IDLE struct sched_param param; diff --git a/src/mbgl/platform/log.cpp b/src/mbgl/platform/log.cpp index 0f334ae3e..b140485dd 100644 --- a/src/mbgl/platform/log.cpp +++ b/src/mbgl/platform/log.cpp @@ -50,7 +50,7 @@ void Log::record(EventSeverity severity, Event event, int64_t code, const std::s std::stringstream logStream; - logStream << "{" << util::getCurrentThreadName() << "}"; + logStream << "{" << platform::getCurrentThreadName() << "}"; logStream << "[" << Enum::toString(event) << "]"; if (code >= 0) { diff --git a/src/mbgl/util/thread.hpp b/src/mbgl/util/thread.hpp index 643d272b2..cd90e0804 100644 --- a/src/mbgl/util/thread.hpp +++ b/src/mbgl/util/thread.hpp @@ -86,29 +86,6 @@ private: RunLoop* loop = nullptr; }; -inline std::string getCurrentThreadName() { - char name[32] = "unknown"; -#if defined(__APPLE__) - pthread_getname_np(pthread_self(), name, sizeof(name)); -#elif defined(__GLIBC__) && defined(__GLIBC_PREREQ) -#if __GLIBC_PREREQ(2, 12) - pthread_getname_np(pthread_self(), name, sizeof(name)); -#endif -#endif - return name; -} - -inline void setCurrentThreadName(const std::string& name) { -#if defined(__APPLE__) - pthread_setname_np(name.c_str()); -#elif defined(__GLIBC__) && defined(__GLIBC_PREREQ) -#if __GLIBC_PREREQ(2, 12) - pthread_setname_np(pthread_self(), name.c_str()); -#endif -#endif - (void)name; -} - template template Thread::Thread(const ThreadContext& context, Args&&... args) { @@ -117,7 +94,7 @@ Thread::Thread(const ThreadContext& context, Args&&... args) { std::tuple params = std::forward_as_tuple(::std::forward(args)...); thread = std::thread([&] { - setCurrentThreadName(context.name); + platform::setCurrentThreadName(context.name); if (context.priority == ThreadPriority::Low) { platform::makeThreadLowPriority();