From 74ad75d7abdf1b55a6bf2c735b3596bd523a665e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Fri, 12 Apr 2019 13:15:06 +0200 Subject: jni thread re-attaching --- platform/android/src/attach_env.cpp | 4 ++++ platform/android/src/style/sources/geojson_source.cpp | 15 +++++++++------ platform/android/src/style/sources/geojson_source.hpp | 4 ++-- platform/default/src/mbgl/util/default_thread_pool.cpp | 3 +++ 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/platform/android/src/attach_env.cpp b/platform/android/src/attach_env.cpp index 6da075ee3e..0dc8069284 100644 --- a/platform/android/src/attach_env.cpp +++ b/platform/android/src/attach_env.cpp @@ -1,3 +1,5 @@ +#include +#include #include "attach_env.hpp" #include "jni.hpp" @@ -10,8 +12,10 @@ UniqueEnv AttachEnv() { switch (err) { case JNI_OK: + __android_log_write(ANDROID_LOG_ERROR, "JNI_THREAD", ("is attached: " + mbgl::platform::getCurrentThreadName()).c_str()); return UniqueEnv(env, JNIEnvDeleter(*theJVM, false)); case JNI_EDETACHED: + __android_log_write(ANDROID_LOG_ERROR, "JNI_THREAD", ("is detached: " + mbgl::platform::getCurrentThreadName()).c_str()); return UniqueEnv(jni::AttachCurrentThread(*theJVM).release(), JNIEnvDeleter(*theJVM, true)); default: throw std::system_error(err, jni::ErrorCategory()); diff --git a/platform/android/src/style/sources/geojson_source.cpp b/platform/android/src/style/sources/geojson_source.cpp index bf4f95e4c4..7fccc36a9f 100644 --- a/platform/android/src/style/sources/geojson_source.cpp +++ b/platform/android/src/style/sources/geojson_source.cpp @@ -17,6 +17,7 @@ #include "../conversion/url_or_tileset.hpp" #include +#include // GeoJSONSource uses a "coalescing" model for high frequency asynchronous data update calls, // which in practice means, that any update that started processing is going to finish @@ -47,14 +48,16 @@ namespace android { : Source(env, std::make_unique( jni::Make(env, sourceId), convertGeoJSONOptions(env, options))) - , converter(std::make_unique>("GeoJSONSource")) { + , threadPool(sharedThreadPool()) + , converter(std::make_unique>(*threadPool)) { } GeoJSONSource::GeoJSONSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend& frontend) : Source(env, coreSource, createJavaPeer(env), frontend) - , converter(std::make_unique>("GeoJSONSource")) { + , threadPool(sharedThreadPool()) + , converter(std::make_unique>(*threadPool)) { } GeoJSONSource::~GeoJSONSource() = default; @@ -64,7 +67,7 @@ namespace android { std::shared_ptr json = std::make_shared(jni::Make(env, jString)); Update::Converter converterFn = [this, json](ActorRef _callback) { - converter->actor().invoke(&FeatureConverter::convertJson, json, _callback); + converter->self().invoke(&FeatureConverter::convertJson, json, _callback); }; setAsync(converterFn); @@ -167,7 +170,7 @@ namespace android { auto object = std::make_shared(std::move(global)); Update::Converter converterFn = [this, object](ActorRef _callback) { - converter->actor().invoke(&FeatureConverter::convertObject, object, _callback); + converter->self().invoke(&FeatureConverter::convertObject, object, _callback); }; setAsync(converterFn); @@ -178,12 +181,12 @@ namespace android { std::move(converterFn), std::make_unique>( *Scheduler::GetCurrent(), - [this](GeoJSON geoJSON) { + [this](GeoJSON /*geoJSON*/) { // conversion from Java features to core ones finished android::UniqueEnv _env = android::AttachEnv(); // Update the core source - source.as()->GeoJSONSource::setGeoJSON(geoJSON); +// source.as()->GeoJSONSource::setGeoJSON(geoJSON); // if there is an awaiting update, execute it, otherwise, release resources if (awaitingUpdate) { diff --git a/platform/android/src/style/sources/geojson_source.hpp b/platform/android/src/style/sources/geojson_source.hpp index fcfbf06f38..e737e41924 100644 --- a/platform/android/src/style/sources/geojson_source.hpp +++ b/platform/android/src/style/sources/geojson_source.hpp @@ -7,7 +7,6 @@ #include "../../geojson/feature_collection.hpp" #include "../../android_renderer_frontend.hpp" #include -#include namespace mbgl { namespace android { @@ -60,7 +59,8 @@ private: jni::Local> createJavaPeer(jni::JNIEnv&); std::unique_ptr awaitingUpdate; std::unique_ptr update; - std::unique_ptr> converter; + std::shared_ptr threadPool; + std::unique_ptr> converter; template void setCollectionAsync(jni::JNIEnv&, const jni::Object&); diff --git a/platform/default/src/mbgl/util/default_thread_pool.cpp b/platform/default/src/mbgl/util/default_thread_pool.cpp index d3950bb8aa..75f1e053ae 100644 --- a/platform/default/src/mbgl/util/default_thread_pool.cpp +++ b/platform/default/src/mbgl/util/default_thread_pool.cpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace mbgl { @@ -10,11 +11,13 @@ ThreadPool::ThreadPool(std::size_t count) { for (std::size_t i = 0; i < count; ++i) { threads.emplace_back([this, i]() { platform::setCurrentThreadName(std::string{ "Worker " } + util::toString(i + 1)); + __android_log_write(ANDROID_LOG_ERROR, "JNI_THREAD", ("created new: " + mbgl::platform::getCurrentThreadName()).c_str()); while (true) { std::unique_lock lock(mutex); cv.wait(lock, [this] { + __android_log_write(ANDROID_LOG_ERROR, "JNI_THREAD", ("aweaken: " + mbgl::platform::getCurrentThreadName()).c_str()); return !queue.empty() || terminate; }); -- cgit v1.2.1