summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBruno de Oliveira Abinader <bruno@mapbox.com>2016-06-18 17:08:26 +0300
committerBruno de Oliveira Abinader <bruno@mapbox.com>2016-06-20 13:42:30 +0300
commit77f309e7a07e248b4e9460e3e38b6b76f95f0dc9 (patch)
treef08e921397754f7c68e4c382fcfec81b5e5e19cd /src
parent6c9e00be3ae6c864f9fc1aa910ad752a3704a08c (diff)
downloadqtlocation-mapboxgl-77f309e7a07e248b4e9460e3e38b6b76f95f0dc9.tar.gz
[core] Factor out thread name setter/getter
Diffstat (limited to 'src')
-rw-r--r--src/mbgl/platform/log.cpp14
-rw-r--r--src/mbgl/util/thread.hpp31
2 files changed, 26 insertions, 19 deletions
diff --git a/src/mbgl/platform/log.cpp b/src/mbgl/platform/log.cpp
index 648c3bd18c..0f334ae3e7 100644
--- a/src/mbgl/platform/log.cpp
+++ b/src/mbgl/platform/log.cpp
@@ -1,5 +1,6 @@
#include <mbgl/platform/log.hpp>
#include <mbgl/util/enum.hpp>
+#include <mbgl/util/thread.hpp>
#include <cstdio>
#include <cstdarg>
@@ -49,18 +50,7 @@ void Log::record(EventSeverity severity, Event event, int64_t code, const std::s
std::stringstream logStream;
-#if defined(__APPLE__)
- char name[32];
- pthread_getname_np(pthread_self(), name, sizeof(name));
- logStream << "{" << name << "}";
-#elif defined(__GLIBC__) && defined(__GLIBC_PREREQ)
-#if __GLIBC_PREREQ(2, 12)
- char name[32];
- pthread_getname_np(pthread_self(), name, sizeof(name));
- logStream << "{" << name << "}";
-#endif
-#endif
-
+ logStream << "{" << util::getCurrentThreadName() << "}";
logStream << "[" << Enum<Event>::toString(event) << "]";
if (code >= 0) {
diff --git a/src/mbgl/util/thread.hpp b/src/mbgl/util/thread.hpp
index c5f9d7338a..0de897d726 100644
--- a/src/mbgl/util/thread.hpp
+++ b/src/mbgl/util/thread.hpp
@@ -87,6 +87,29 @@ 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 <class Object>
template <class... Args>
Thread<Object>::Thread(const ThreadContext& context, Args&&... args) {
@@ -95,13 +118,7 @@ Thread<Object>::Thread(const ThreadContext& context, Args&&... args) {
std::tuple<Args...> params = std::forward_as_tuple(::std::forward<Args>(args)...);
thread = std::thread([&] {
-#if defined(__APPLE__)
- pthread_setname_np(context.name.c_str());
-#elif defined(__GLIBC__) && defined(__GLIBC_PREREQ)
-#if __GLIBC_PREREQ(2, 12)
- pthread_setname_np(pthread_self(), context.name.c_str());
-#endif
-#endif
+ setCurrentThreadName(context.name);
if (context.priority == ThreadPriority::Low) {
platform::makeThreadLowPriority();