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

Android specific thread pool that keeps threads attached to JVM #14450

Merged
merged 2 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions include/mbgl/platform/thread.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

namespace mbgl {
namespace platform {

// Called when a thread is created
void attachThread();

// Called when a thread is destroyed
void detachThread();

} // namespace platform
} // namespace mbgl
3 changes: 3 additions & 0 deletions include/mbgl/util/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <mbgl/util/platform.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/util.hpp>
#include <mbgl/platform/thread.hpp>

#include <cassert>
#include <future>
Expand Down Expand Up @@ -55,6 +56,7 @@ class Thread {
] () mutable {
platform::setCurrentThreadName(name);
platform::makeThreadLowPriority();
platform::attachThread();

util::RunLoop loop_(util::RunLoop::Type::New);
loop = &loop_;
Expand All @@ -67,6 +69,7 @@ class Thread {
(void) establishedActor;

loop = nullptr;
platform::detachThread();
});
}

Expand Down
6 changes: 0 additions & 6 deletions platform/android/src/run_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ class Alarm {
};

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);

Expand Down Expand Up @@ -129,9 +126,6 @@ RunLoop::Impl::~Impl() {
}

ALooper_release(loop);

using namespace mbgl::android;
detach_jni_thread(theJVM, &env, detach);
}

void RunLoop::Impl::wake() {
Expand Down
3 changes: 0 additions & 3 deletions platform/android/src/run_loop_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ class RunLoop::Impl {

int fds[2];

JNIEnv *env = nullptr;
bool detach = false;

std::unique_ptr<Thread<Alarm>> alarm;

std::mutex mutex;
Expand Down
19 changes: 19 additions & 0 deletions platform/android/src/thread.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
#include <mbgl/util/logging.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/platform/thread.hpp>

#include <sys/prctl.h>
#include <sys/resource.h>

#include <cassert>
#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;
tmpsantos marked this conversation as resolved.
Show resolved Hide resolved

std::string getCurrentThreadName() {
char name[32] = "unknown";

Expand All @@ -33,5 +40,17 @@ void makeThreadLowPriority() {
setpriority(PRIO_PROCESS, 0, 19);
}

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
} // namespace mbgl
7 changes: 7 additions & 0 deletions platform/darwin/src/nsthread.mm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import <Foundation/Foundation.h>

#include <mbgl/util/platform.hpp>
#include <mbgl/platform/thread.hpp>

#include <pthread.h>

Expand All @@ -23,5 +24,11 @@ void makeThreadLowPriority() {
[[NSThread currentThread] setThreadPriority:0.0];
}

void attachThread() {
}

void detachThread() {
}

}
}
7 changes: 7 additions & 0 deletions platform/default/src/mbgl/util/thread.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <mbgl/util/platform.hpp>
#include <mbgl/platform/thread.hpp>
#include <mbgl/util/logging.hpp>

#include <string>
Expand Down Expand Up @@ -33,5 +34,11 @@ void makeThreadLowPriority() {
}
}

void attachThread() {
}

void detachThread() {
}

} // namespace platform
} // namespace mbgl
7 changes: 7 additions & 0 deletions platform/qt/src/thread.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <mbgl/util/platform.hpp>
#include <mbgl/platform/thread.hpp>

#include <string>

Expand All @@ -15,5 +16,11 @@ void setCurrentThreadName(const std::string&) {
void makeThreadLowPriority() {
}

void attachThread() {
}

void detachThread() {
}

} // namespace platform
} // namespace mbgl
1 change: 1 addition & 0 deletions src/core-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@
"mbgl/math/minmax.hpp": "include/mbgl/math/minmax.hpp",
"mbgl/math/wrap.hpp": "include/mbgl/math/wrap.hpp",
"mbgl/platform/gl_functions.hpp": "include/mbgl/platform/gl_functions.hpp",
"mbgl/platform/thread.hpp": "include/mbgl/platform/thread.hpp",
"mbgl/renderer/query.hpp": "include/mbgl/renderer/query.hpp",
"mbgl/renderer/renderer.hpp": "include/mbgl/renderer/renderer.hpp",
"mbgl/renderer/renderer_frontend.hpp": "include/mbgl/renderer/renderer_frontend.hpp",
Expand Down
3 changes: 3 additions & 0 deletions src/mbgl/util/thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <mbgl/util/platform.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/platform/thread.hpp>

namespace mbgl {

Expand All @@ -11,6 +12,7 @@ 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));
platform::attachThread();

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

if (terminate) {
platform::detachThread();
return;
}

Expand Down