From d52faadf73d8b66e39b1d5fde5d20d3cdce12d8c Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Fri, 15 Jul 2016 12:18:08 +0300 Subject: [PATCH] [android] Set thread name properly --- platform/android/platform.gyp | 2 +- platform/android/src/run_loop.cpp | 3 ++- platform/android/src/thread.cpp | 37 +++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 platform/android/src/thread.cpp diff --git a/platform/android/platform.gyp b/platform/android/platform.gyp index c5b81c974..171fc0b51 100644 --- a/platform/android/platform.gyp +++ b/platform/android/platform.gyp @@ -31,7 +31,7 @@ 'src/log_android.cpp', 'src/http_file_source.cpp', 'src/asset_file_source.cpp', - '../default/thread.cpp', + 'src/thread.cpp', '../default/string_stdlib.cpp', '../default/image.cpp', '../default/png_reader.cpp', diff --git a/platform/android/src/run_loop.cpp b/platform/android/src/run_loop.cpp index 007ea2919..1e5fc9b4b 100644 --- a/platform/android/src/run_loop.cpp +++ b/platform/android/src/run_loop.cpp @@ -1,5 +1,6 @@ #include "run_loop_impl.hpp" +#include #include #include #include @@ -75,7 +76,7 @@ private: RunLoop::Impl::Impl(RunLoop* runLoop_, RunLoop::Type type) : runLoop(runLoop_) { using namespace mbgl::android; - detach = attach_jni_thread(theJVM, &env, ""); + detach = attach_jni_thread(theJVM, &env, platform::getCurrentThreadName()); loop = ALooper_prepare(0); assert(loop); diff --git a/platform/android/src/thread.cpp b/platform/android/src/thread.cpp new file mode 100644 index 000000000..77f981586 --- /dev/null +++ b/platform/android/src/thread.cpp @@ -0,0 +1,37 @@ +#include +#include + +#include +#include + +// Implementation based on Chromium's platform_thread_android.cc. + +namespace mbgl { +namespace platform { + +std::string getCurrentThreadName() { + char name[32] = "unknown"; + + if (prctl(PR_GET_NAME, name) == -1) { + Log::Warning(Event::General, "Couldn't get thread name"); + } + + return name; +} + +void setCurrentThreadName(const std::string& name) { + if (prctl(PR_SET_NAME, name.c_str()) == -1) { + Log::Warning(Event::General, "Couldn't set thread name"); + } +} + +void makeThreadLowPriority() { + // ANDROID_PRIORITY_LOWEST = 19 + // + // Supposedly would set the priority for the whole process, but + // on Linux/Android it only sets for the current thread. + setpriority(PRIO_PROCESS, 0, 19); +} + +} // namespace platform +} // namespace mbgl