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 17, 2019
1 parent cd5f87e commit 5d833b1
Show file tree
Hide file tree
Showing 6 changed files with 70 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
18 changes: 18 additions & 0 deletions platform/android/src/android_thread_pool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "android_thread_pool.hpp"
#include <mbgl/util/platform.hpp>

namespace mbgl {
namespace android {

void AndroidThreadLifecycle::onThreadCreated() const {
attach_jni_thread(theJVM, &env, platform::getCurrentThreadName());
}

void AndroidThreadLifecycle::onThreadDestroyed() const {
detach_jni_thread(theJVM, &env, true);
}

AndroidThreadPool::AndroidThreadPool(size_t count) : mbgl::ThreadPool(count, std::make_unique<AndroidThreadLifecycle>()) {}

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

namespace mbgl {
namespace android {

class AndroidThreadLifecycle : public mbgl::ThreadLifecycle {
public:
void onThreadCreated() const override;
void onThreadDestroyed() const override;

private:
mutable JNIEnv *env = nullptr;
};

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
9 changes: 8 additions & 1 deletion platform/default/include/mbgl/util/default_thread_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@

namespace mbgl {

struct ThreadLifecycle {
virtual void onThreadCreated() const {};
virtual void onThreadDestroyed() const {};
virtual ~ThreadLifecycle() = default;
};

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

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

private:
std::unique_ptr<ThreadLifecycle> lifecycle;
std::vector<std::thread> threads;
std::queue<std::weak_ptr<Mailbox>> queue;
std::mutex mutex;
Expand Down
4 changes: 3 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,11 +5,12 @@

namespace mbgl {

ThreadPool::ThreadPool(std::size_t count) {
ThreadPool::ThreadPool(std::size_t count, std::unique_ptr<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));
lifecycle->onThreadCreated();

while (true) {
std::unique_lock<std::mutex> lock(mutex);
Expand All @@ -19,6 +20,7 @@ ThreadPool::ThreadPool(std::size_t count) {
});

if (terminate) {
lifecycle->onThreadDestroyed();
return;
}

Expand Down

0 comments on commit 5d833b1

Please sign in to comment.