Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] Android specific thread pool that keeps threads attached to…
Browse files Browse the repository at this point in the history
… JVM
  • Loading branch information
LukasPaczos committed Apr 30, 2019
1 parent d22b88e commit 3938e16
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 3 deletions.
4 changes: 3 additions & 1 deletion platform/android/core-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@
"platform/android/src/thread.cpp",
"platform/android/src/timer.cpp",
"platform/android/src/unaccent.cpp",
"platform/android/src/android_thread_pool.cpp",
"platform/android/src/shared_thread_pool.cpp",
"platform/default/src/mbgl/gl/headless_backend.cpp",
"platform/default/src/mbgl/gl/headless_frontend.cpp",
"platform/default/src/mbgl/map/map_snapshotter.cpp",
"platform/default/src/mbgl/text/bidi.cpp",
"platform/default/src/mbgl/util/default_thread_pool.cpp",
"platform/default/src/mbgl/util/png_writer.cpp",
"platform/default/src/mbgl/util/shared_thread_pool.cpp",
"platform/default/src/mbgl/util/thread_local.cpp",
"platform/default/src/mbgl/util/utf.cpp",
"platform/linux/src/headless_backend_egl.cpp"
Expand Down Expand Up @@ -150,6 +151,7 @@
"map_renderer_runnable.hpp": "platform/android/src/map_renderer_runnable.hpp",
"native_map_view.hpp": "platform/android/src/native_map_view.hpp",
"run_loop_impl.hpp": "platform/android/src/run_loop_impl.hpp",
"android_thread_pool.hpp": "platform/android/src/android_thread_pool.hpp",
"snapshotter/map_snapshot.hpp": "platform/android/src/snapshotter/map_snapshot.hpp",
"snapshotter/map_snapshotter.hpp": "platform/android/src/snapshotter/map_snapshotter.hpp",
"style/android_conversion.hpp": "platform/android/src/style/android_conversion.hpp",
Expand Down
23 changes: 23 additions & 0 deletions platform/android/src/android_thread_pool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "android_thread_pool.hpp"
#include <mbgl/util/platform.hpp>
#include <cassert>

namespace mbgl {
namespace android {

AndroidThreadPool::AndroidThreadPool(size_t count)
: mbgl::ThreadPool(count, {
[] {
JNIEnv* env = nullptr;
attach_jni_thread(theJVM, &env, platform::getCurrentThreadName());
return env;
},
[](ThreadLifecycle::ThreadData threadData_) {
assert(threadData_);
auto env = static_cast<JNIEnv*>(threadData_);
detach_jni_thread(theJVM, &env, true);
}
}) {}

} // namespace android
} // namespace mbgl
15 changes: 15 additions & 0 deletions platform/android/src/android_thread_pool.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include <mbgl/util/default_thread_pool.hpp>
#include <map>
#include "jni.hpp"

namespace mbgl {
namespace android {

class AndroidThreadPool : public mbgl::ThreadPool {
public:
explicit AndroidThreadPool(size_t count);
};

} // namespace android
} // namespace mbgl
15 changes: 15 additions & 0 deletions platform/android/src/shared_thread_pool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <mbgl/util/shared_thread_pool.hpp>
#include "android_thread_pool.hpp"

namespace mbgl {

std::shared_ptr<ThreadPool> sharedThreadPool() {
static std::weak_ptr<ThreadPool> weak;
auto pool = weak.lock();
if (!pool) {
weak = pool = std::make_shared<android::AndroidThreadPool>(4);
}
return pool;
}

} // namespace mbgl
25 changes: 24 additions & 1 deletion platform/default/include/mbgl/util/default_thread_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,40 @@
#include <mutex>
#include <queue>
#include <thread>
#include <functional>

namespace mbgl {

struct ThreadLifecycle {
using ThreadData = void*;
ThreadLifecycle() = default;
ThreadLifecycle(std::function<ThreadData()> onThreadCreated_,
std::function<void(ThreadData)> onThreadDestroyed_) :
onThreadCreatedFn(onThreadCreated_),
onThreadDestroyedFn(onThreadDestroyed_) {}

ThreadData onThreadCreated() const {
return onThreadCreatedFn();
}

void onThreadDestroyed(ThreadData threadData_) const {
onThreadDestroyedFn(threadData_);
}

private:
std::function<ThreadData()> onThreadCreatedFn = [] { return nullptr; };
std::function<void(ThreadData)> onThreadDestroyedFn = [](ThreadData) {};
};

class ThreadPool : public Scheduler {
public:
ThreadPool(std::size_t count);
explicit ThreadPool(std::size_t count, ThreadLifecycle _lifecycle = ThreadLifecycle());
~ThreadPool() override;

void schedule(std::weak_ptr<Mailbox>) override;

private:
ThreadLifecycle lifecycle;
std::vector<std::thread> threads;
std::queue<std::weak_ptr<Mailbox>> queue;
std::mutex mutex;
Expand Down
5 changes: 4 additions & 1 deletion platform/default/src/mbgl/util/default_thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

namespace mbgl {

ThreadPool::ThreadPool(std::size_t count) {
ThreadPool::ThreadPool(std::size_t count, ThreadLifecycle _lifecycle) : lifecycle(std::move(_lifecycle)) {
threads.reserve(count);
for (std::size_t i = 0; i < count; ++i) {
threads.emplace_back([this, i]() {
platform::setCurrentThreadName(std::string{ "Worker " } + util::toString(i + 1));

ThreadLifecycle::ThreadData threadData = lifecycle.onThreadCreated();

while (true) {
std::unique_lock<std::mutex> lock(mutex);

Expand All @@ -19,6 +21,7 @@ ThreadPool::ThreadPool(std::size_t count) {
});

if (terminate) {
lifecycle.onThreadDestroyed(threadData);
return;
}

Expand Down

0 comments on commit 3938e16

Please sign in to comment.