[core] Move set/get thread names to platform::

Android needs its own implementation.
This commit is contained in:
Thiago Marcos P. Santos
2016-07-15 15:01:16 +03:00
parent 5c037f4745
commit 4a1a7937ae
4 changed files with 33 additions and 29 deletions

View File

@@ -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();

View File

@@ -1,16 +1,37 @@
#include <mbgl/platform/platform.hpp>
#include <mbgl/platform/log.hpp>
#include <string>
#include <pthread.h>
#ifdef __linux__
#include <linux/sched.h>
#endif
#include <sched.h>
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;

View File

@@ -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<Event>::toString(event) << "]";
if (code >= 0) {

View File

@@ -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 <class Object>
template <class... Args>
Thread<Object>::Thread(const ThreadContext& context, Args&&... args) {
@@ -117,7 +94,7 @@ Thread<Object>::Thread(const ThreadContext& context, Args&&... args) {
std::tuple<Args...> params = std::forward_as_tuple(::std::forward<Args>(args)...);
thread = std::thread([&] {
setCurrentThreadName(context.name);
platform::setCurrentThreadName(context.name);
if (context.priority == ThreadPriority::Low) {
platform::makeThreadLowPriority();