[core] Add hooks for setting experimental thread priorities for mbgl threads
This commit is contained in:
@@ -3,10 +3,10 @@
|
||||
#include <mbgl/actor/actor.hpp>
|
||||
#include <mbgl/actor/mailbox.hpp>
|
||||
#include <mbgl/actor/scheduler.hpp>
|
||||
#include <mbgl/platform/thread.hpp>
|
||||
#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>
|
||||
@@ -37,25 +37,20 @@ namespace util {
|
||||
// - A `RunLoop` is created for the `Object` thread.
|
||||
// - `Object` can use `Timer` and do asynchronous I/O, like wait for sockets events.
|
||||
//
|
||||
template<class Object>
|
||||
template <typename Object>
|
||||
class Thread {
|
||||
public:
|
||||
template <class... Args>
|
||||
Thread(const std::string& name, Args&&... args) {
|
||||
|
||||
template <typename TupleArgs>
|
||||
Thread(std::function<void()> prioritySetter_, const std::string& name, TupleArgs&& args) {
|
||||
std::promise<void> running_;
|
||||
running = running_.get_future();
|
||||
|
||||
auto capturedArgs = std::make_tuple(std::forward<Args>(args)...);
|
||||
|
||||
thread = std::thread([
|
||||
this,
|
||||
name,
|
||||
capturedArgs = std::move(capturedArgs),
|
||||
runningPromise = std::move(running_)
|
||||
] () mutable {
|
||||
thread = std::thread([this,
|
||||
name,
|
||||
capturedArgs = std::forward<TupleArgs>(args),
|
||||
runningPromise = std::move(running_),
|
||||
prioritySetter = std::move(prioritySetter_)]() mutable {
|
||||
platform::setCurrentThreadName(name);
|
||||
platform::makeThreadLowPriority();
|
||||
if (prioritySetter) prioritySetter();
|
||||
platform::attachThread();
|
||||
|
||||
// narrowing the scope to release the Object before we detach the thread
|
||||
@@ -77,6 +72,14 @@ public:
|
||||
});
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
Thread(const std::string& name, Args&&... args)
|
||||
: Thread([] { platform::makeThreadLowPriority(); }, name, std::make_tuple(std::forward<Args>(args)...)) {}
|
||||
|
||||
template <typename... Args>
|
||||
Thread(std::function<void()> prioritySetter, const std::string& name, Args&&... args)
|
||||
: Thread(std::move(prioritySetter), name, std::make_tuple(std::forward<Args>(args)...)) {}
|
||||
|
||||
~Thread() {
|
||||
if (paused) {
|
||||
resume();
|
||||
@@ -158,5 +161,10 @@ private:
|
||||
util::RunLoop* loop = nullptr;
|
||||
};
|
||||
|
||||
// Returns function, that once invoked, will set a thread priority for
|
||||
// a thread `threadType` based on a setting provided by corresponding
|
||||
// Settings' platform::EXPERIMENTAL_THREAD_PRIORITY_* value.
|
||||
std::function<void()> makeThreadPrioritySetter(std::string threadType);
|
||||
|
||||
} // namespace util
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -780,6 +780,7 @@ add_library(
|
||||
${MBGL_ROOT}/src/mbgl/util/stopwatch.cpp
|
||||
${MBGL_ROOT}/src/mbgl/util/stopwatch.hpp
|
||||
${MBGL_ROOT}/src/mbgl/util/string.cpp
|
||||
${MBGL_ROOT}/src/mbgl/util/thread.cpp
|
||||
${MBGL_ROOT}/src/mbgl/util/thread_local.hpp
|
||||
${MBGL_ROOT}/src/mbgl/util/thread_pool.cpp
|
||||
${MBGL_ROOT}/src/mbgl/util/thread_pool.hpp
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "asset_manager_file_source.hpp"
|
||||
|
||||
#include <mbgl/platform/settings.hpp>
|
||||
#include <mbgl/storage/file_source_request.hpp>
|
||||
#include <mbgl/storage/resource.hpp>
|
||||
#include <mbgl/storage/response.hpp>
|
||||
@@ -39,11 +40,13 @@ private:
|
||||
AAssetManager* assetManager;
|
||||
};
|
||||
|
||||
AssetManagerFileSource::AssetManagerFileSource(jni::JNIEnv& env, const jni::Object<android::AssetManager>& assetManager_)
|
||||
AssetManagerFileSource::AssetManagerFileSource(jni::JNIEnv& env,
|
||||
const jni::Object<android::AssetManager>& assetManager_)
|
||||
: assetManager(jni::NewGlobal(env, assetManager_)),
|
||||
impl(std::make_unique<util::Thread<Impl>>("AssetManagerFileSource",
|
||||
AAssetManager_fromJava(&env, jni::Unwrap(assetManager.get())))) {
|
||||
}
|
||||
impl(std::make_unique<util::Thread<Impl>>(
|
||||
util::makeThreadPrioritySetter(platform::EXPERIMENTAL_THREAD_PRIORITY_FILE),
|
||||
"AssetManagerFileSource",
|
||||
AAssetManager_fromJava(&env, jni::Unwrap(assetManager.get())))) {}
|
||||
|
||||
AssetManagerFileSource::~AssetManagerFileSource() = default;
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <mbgl/platform/settings.hpp>
|
||||
#include <mbgl/storage/asset_file_source.hpp>
|
||||
#include <mbgl/storage/file_source_request.hpp>
|
||||
#include <mbgl/storage/local_file_request.hpp>
|
||||
@@ -42,8 +43,8 @@ private:
|
||||
};
|
||||
|
||||
AssetFileSource::AssetFileSource(const std::string& root)
|
||||
: impl(std::make_unique<util::Thread<Impl>>("AssetFileSource", root)) {
|
||||
}
|
||||
: impl(std::make_unique<util::Thread<Impl>>(
|
||||
util::makeThreadPrioritySetter(platform::EXPERIMENTAL_THREAD_PRIORITY_FILE), "AssetFileSource", root)) {}
|
||||
|
||||
AssetFileSource::~AssetFileSource() = default;
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <mbgl/platform/settings.hpp>
|
||||
#include <mbgl/storage/database_file_source.hpp>
|
||||
#include <mbgl/storage/file_source_manager.hpp>
|
||||
#include <mbgl/storage/file_source_request.hpp>
|
||||
@@ -7,6 +8,7 @@
|
||||
#include <mbgl/storage/response.hpp>
|
||||
#include <mbgl/util/constants.hpp>
|
||||
#include <mbgl/util/logging.hpp>
|
||||
#include <mbgl/util/platform.hpp>
|
||||
#include <mbgl/util/thread.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
@@ -149,7 +151,10 @@ class DatabaseFileSource::Impl {
|
||||
public:
|
||||
Impl(std::shared_ptr<FileSource> onlineFileSource, const std::string& cachePath)
|
||||
: thread(std::make_unique<util::Thread<DatabaseFileSourceThread>>(
|
||||
"DatabaseFileSource", std::move(onlineFileSource), cachePath)) {}
|
||||
util::makeThreadPrioritySetter(platform::EXPERIMENTAL_THREAD_PRIORITY_DATABASE),
|
||||
"DatabaseFileSource",
|
||||
std::move(onlineFileSource),
|
||||
cachePath)) {}
|
||||
|
||||
ActorRef<DatabaseFileSourceThread> actor() const { return thread->actor(); }
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <mbgl/platform/settings.hpp>
|
||||
#include <mbgl/storage/file_source_request.hpp>
|
||||
#include <mbgl/storage/local_file_request.hpp>
|
||||
#include <mbgl/storage/local_file_source.hpp>
|
||||
@@ -36,8 +37,8 @@ public:
|
||||
};
|
||||
|
||||
LocalFileSource::LocalFileSource()
|
||||
: impl(std::make_unique<util::Thread<Impl>>("LocalFileSource")) {
|
||||
}
|
||||
: impl(std::make_unique<util::Thread<Impl>>(
|
||||
util::makeThreadPrioritySetter(platform::EXPERIMENTAL_THREAD_PRIORITY_FILE), "LocalFileSource")) {}
|
||||
|
||||
LocalFileSource::~LocalFileSource() = default;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <mbgl/actor/actor.hpp>
|
||||
#include <mbgl/actor/scheduler.hpp>
|
||||
#include <mbgl/platform/settings.hpp>
|
||||
#include <mbgl/storage/file_source_manager.hpp>
|
||||
#include <mbgl/storage/file_source_request.hpp>
|
||||
#include <mbgl/storage/main_resource_loader.hpp>
|
||||
@@ -131,7 +132,12 @@ public:
|
||||
onlineFileSource(std::move(onlineFileSource_)),
|
||||
supportsCacheOnlyRequests_(bool(databaseFileSource)),
|
||||
thread(std::make_unique<util::Thread<MainResourceLoaderThread>>(
|
||||
"ResourceLoaderThread", assetFileSource, databaseFileSource, localFileSource, onlineFileSource)) {}
|
||||
util::makeThreadPrioritySetter(platform::EXPERIMENTAL_THREAD_PRIORITY_WORKER),
|
||||
"ResourceLoaderThread",
|
||||
assetFileSource,
|
||||
databaseFileSource,
|
||||
localFileSource,
|
||||
onlineFileSource)) {}
|
||||
|
||||
std::unique_ptr<AsyncRequest> request(const Resource& resource, Callback callback) {
|
||||
auto req = std::make_unique<FileSourceRequest>(std::move(callback));
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include <mbgl/storage/online_file_source.hpp>
|
||||
#include <mbgl/platform/settings.hpp>
|
||||
#include <mbgl/storage/file_source_request.hpp>
|
||||
#include <mbgl/storage/http_file_source.hpp>
|
||||
#include <mbgl/storage/network_status.hpp>
|
||||
|
||||
#include <mbgl/storage/file_source_request.hpp>
|
||||
#include <mbgl/storage/online_file_source.hpp>
|
||||
#include <mbgl/storage/resource_transform.hpp>
|
||||
#include <mbgl/storage/response.hpp>
|
||||
#include <mbgl/util/logging.hpp>
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <mbgl/util/http_timeout.hpp>
|
||||
#include <mbgl/util/mapbox.hpp>
|
||||
#include <mbgl/util/noncopyable.hpp>
|
||||
#include <mbgl/util/platform.hpp>
|
||||
#include <mbgl/util/run_loop.hpp>
|
||||
#include <mbgl/util/thread.hpp>
|
||||
#include <mbgl/util/timer.hpp>
|
||||
@@ -293,7 +294,9 @@ private:
|
||||
|
||||
class OnlineFileSource::Impl {
|
||||
public:
|
||||
Impl() : thread(std::make_unique<util::Thread<OnlineFileSourceThread>>("OnlineFileSource")) {}
|
||||
Impl()
|
||||
: thread(std::make_unique<util::Thread<OnlineFileSourceThread>>(
|
||||
util::makeThreadPrioritySetter(platform::EXPERIMENTAL_THREAD_PRIORITY_NETWORK), "OnlineFileSource")) {}
|
||||
|
||||
std::unique_ptr<AsyncRequest> request(Callback callback, Resource res) {
|
||||
auto req = std::make_unique<FileSourceRequest>(std::move(callback));
|
||||
|
||||
@@ -62,6 +62,7 @@
|
||||
"src/mbgl/map/transform_state.cpp",
|
||||
"src/mbgl/math/log2.cpp",
|
||||
"src/mbgl/platform/gl_functions.cpp",
|
||||
"src/mbgl/platform/settings.cpp",
|
||||
"src/mbgl/programs/background_program.cpp",
|
||||
"src/mbgl/programs/circle_program.cpp",
|
||||
"src/mbgl/programs/clipping_mask_program.cpp",
|
||||
@@ -316,6 +317,7 @@
|
||||
"src/mbgl/util/rapidjson.cpp",
|
||||
"src/mbgl/util/stopwatch.cpp",
|
||||
"src/mbgl/util/string.cpp",
|
||||
"src/mbgl/util/thread.cpp",
|
||||
"src/mbgl/util/thread_pool.cpp",
|
||||
"src/mbgl/util/tile_cover.cpp",
|
||||
"src/mbgl/util/tile_cover_impl.cpp",
|
||||
@@ -369,6 +371,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/settings.hpp": "include/mbgl/platform/settings.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",
|
||||
|
||||
19
src/mbgl/util/thread.cpp
Normal file
19
src/mbgl/util/thread.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <functional>
|
||||
#include <mbgl/platform/settings.hpp>
|
||||
#include <mbgl/util/platform.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace util {
|
||||
std::function<void()> makeThreadPrioritySetter(std::string threadType_) {
|
||||
return [threadType = std::move(threadType_)] {
|
||||
auto& settings = platform::Settings::getInstance();
|
||||
auto value = settings.get(threadType);
|
||||
if (auto* priority = value.getDouble()) {
|
||||
platform::setCurrentThreadPriority(*priority);
|
||||
} else {
|
||||
platform::makeThreadLowPriority();
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace util
|
||||
} // namespace mbgl
|
||||
@@ -1,8 +1,9 @@
|
||||
#include <mbgl/util/thread_pool.hpp>
|
||||
|
||||
#include <mbgl/platform/settings.hpp>
|
||||
#include <mbgl/platform/thread.hpp>
|
||||
#include <mbgl/util/platform.hpp>
|
||||
#include <mbgl/util/string.hpp>
|
||||
#include <mbgl/platform/thread.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
@@ -17,7 +18,13 @@ void ThreadedSchedulerBase::terminate() {
|
||||
}
|
||||
|
||||
std::thread ThreadedSchedulerBase::makeSchedulerThread(size_t index) {
|
||||
return std::thread([this, index]() {
|
||||
return std::thread([this, index] {
|
||||
auto& settings = platform::Settings::getInstance();
|
||||
auto value = settings.get(platform::EXPERIMENTAL_THREAD_PRIORITY_WORKER);
|
||||
if (auto* priority = value.getDouble()) {
|
||||
platform::setCurrentThreadPriority(*priority);
|
||||
}
|
||||
|
||||
platform::setCurrentThreadName(std::string{"Worker "} + util::toString(index + 1));
|
||||
platform::attachThread();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user