From 470b99e7957b44fb929fe95d0a8b58c41583e7f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Tue, 21 May 2019 12:45:58 +0200 Subject: [android] attach/detach core thread pool's threads to JVM --- platform/android/src/run_loop.cpp | 6 ------ platform/android/src/run_loop_impl.hpp | 3 --- platform/android/src/thread.cpp | 12 ++++++++++++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/platform/android/src/run_loop.cpp b/platform/android/src/run_loop.cpp index fc45539e5e..2966ecdfb0 100644 --- a/platform/android/src/run_loop.cpp +++ b/platform/android/src/run_loop.cpp @@ -81,9 +81,6 @@ private: }; RunLoop::Impl::Impl(RunLoop* runLoop_, RunLoop::Type type) : runLoop(runLoop_) { - using namespace mbgl::android; - detach = attach_jni_thread(theJVM, &env, platform::getCurrentThreadName()); - loop = ALooper_prepare(0); assert(loop); @@ -129,9 +126,6 @@ RunLoop::Impl::~Impl() { } ALooper_release(loop); - - using namespace mbgl::android; - detach_jni_thread(theJVM, &env, detach); } void RunLoop::Impl::wake() { diff --git a/platform/android/src/run_loop_impl.hpp b/platform/android/src/run_loop_impl.hpp index 0a9ea156fd..a76d636188 100644 --- a/platform/android/src/run_loop_impl.hpp +++ b/platform/android/src/run_loop_impl.hpp @@ -47,9 +47,6 @@ private: int fds[2]; - JNIEnv *env = nullptr; - bool detach = false; - std::unique_ptr> alarm; std::mutex mutex; diff --git a/platform/android/src/thread.cpp b/platform/android/src/thread.cpp index 721c63a531..cd0a72306e 100644 --- a/platform/android/src/thread.cpp +++ b/platform/android/src/thread.cpp @@ -5,11 +5,17 @@ #include #include +#include +#include "jni.hpp" + // Implementation based on Chromium's platform_thread_android.cc. namespace mbgl { namespace platform { +thread_local static JNIEnv* env; +thread_local static bool detach; + std::string getCurrentThreadName() { char name[32] = "unknown"; @@ -35,9 +41,15 @@ void makeThreadLowPriority() { } void attachThread() { + using namespace android; + assert(env == nullptr); + detach = attach_jni_thread(theJVM, &env, platform::getCurrentThreadName()); } void detachThread() { + using namespace android; + assert(env); + detach_jni_thread(theJVM, &env, detach); } } // namespace platform -- cgit v1.2.1