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

Commit

Permalink
[core] Add hooks for setting experimental thread priorities for mbgl …
Browse files Browse the repository at this point in the history
…threads
  • Loading branch information
alexshalamov committed Feb 5, 2020
1 parent 755c9f1 commit 98bfdb1
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 31 deletions.
36 changes: 21 additions & 15 deletions include/mbgl/util/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
#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/thread_priority_setter.hpp>
#include <mbgl/util/util.hpp>
#include <mbgl/platform/thread.hpp>

#include <cassert>
#include <future>
Expand Down Expand Up @@ -37,25 +38,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 PrioritySetter, typename TupleArgs>
Thread(PrioritySetter&& ps, 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::move(args),
runningPromise = std::move(running_),
prioritySetter = std::move(ps)]() mutable {
platform::setCurrentThreadName(name);
platform::makeThreadLowPriority();
prioritySetter.setThreadPriority();
platform::attachThread();

// narrowing the scope to release the Object before we detach the thread
Expand All @@ -77,6 +73,16 @@ class Thread {
});
}

template <typename... Args>
Thread(const std::string& name, Args&&... args)
: Thread(ThreadPrioritySetterLow{}, name, std::make_tuple(std::forward<Args>(args)...)) {}

template <typename PrioritySetter,
typename = typename std::enable_if<std::is_base_of<ThreadPrioritySetter, PrioritySetter>::value>::type,
typename... Args>
Thread(PrioritySetter&& ps, const std::string& name, Args&&... args)
: Thread(std::forward<PrioritySetter>(ps), name, std::make_tuple(std::forward<Args>(args)...)) {}

~Thread() {
if (paused) {
resume();
Expand Down
38 changes: 38 additions & 0 deletions include/mbgl/util/thread_priority_setter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

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

namespace mbgl {
namespace util {

// Thread priority strategies
//
// ThreadPrioritySetterLow: Sets thread priority to low. Platform must set
// correct thread priority via implementing platform::makeThreadLowPriority()
//
// ThreadPrioritySetterCustom: Sets thread priority for a thread type based on
// a setting platform::EXPERIMENTAL_THREAD_PRIORITY_* for a provided thread type.

struct ThreadPrioritySetter {};

struct ThreadPrioritySetterLow final : public ThreadPrioritySetter {
void setThreadPriority() { platform::makeThreadLowPriority(); }
};

struct ThreadPrioritySetterCustom final : public ThreadPrioritySetter {
ThreadPrioritySetterCustom(std::string threadType_) : threadType(std::move(threadType_)) {}
void setThreadPriority() {
auto& settings = platform::Settings::getInstance();
auto value = settings.get(threadType);
if (auto* priority = value.getDouble()) {
platform::setCurrentThreadPriority(*priority);
} else {
platform::makeThreadLowPriority();
}
}
std::string threadType;
};

} // namespace util
} // namespace mbgl
1 change: 1 addition & 0 deletions next/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ add_library(
${MBGL_ROOT}/include/mbgl/util/size.hpp
${MBGL_ROOT}/include/mbgl/util/string.hpp
${MBGL_ROOT}/include/mbgl/util/thread.hpp
${MBGL_ROOT}/include/mbgl/util/thread_priority_setter.hpp
${MBGL_ROOT}/include/mbgl/util/tileset.hpp
${MBGL_ROOT}/include/mbgl/util/timer.hpp
${MBGL_ROOT}/include/mbgl/util/traits.hpp
Expand Down
10 changes: 6 additions & 4 deletions platform/android/src/asset_manager_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ class AssetManagerFileSource::Impl {
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::ThreadPrioritySetterCustom(platform::EXPERIMENTAL_THREAD_PRIORITY_FILE),
"AssetManagerFileSource",
AAssetManager_fromJava(&env, jni::Unwrap(assetManager.get())))) {}

AssetManagerFileSource::~AssetManagerFileSource() = default;

Expand Down
4 changes: 2 additions & 2 deletions platform/default/src/mbgl/storage/asset_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class AssetFileSource::Impl {
};

AssetFileSource::AssetFileSource(const std::string& root)
: impl(std::make_unique<util::Thread<Impl>>("AssetFileSource", root)) {
}
: impl(std::make_unique<util::Thread<Impl>>(
util::ThreadPrioritySetterCustom(platform::EXPERIMENTAL_THREAD_PRIORITY_FILE), "AssetFileSource", root)) {}

AssetFileSource::~AssetFileSource() = default;

Expand Down
6 changes: 5 additions & 1 deletion platform/default/src/mbgl/storage/database_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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 {
Expand Down Expand Up @@ -149,7 +150,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::ThreadPrioritySetterCustom(platform::EXPERIMENTAL_THREAD_PRIORITY_DATABASE),
"DatabaseFileSource",
std::move(onlineFileSource),
cachePath)) {}

ActorRef<DatabaseFileSourceThread> actor() const { return thread->actor(); }

Expand Down
4 changes: 2 additions & 2 deletions platform/default/src/mbgl/storage/local_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class LocalFileSource::Impl {
};

LocalFileSource::LocalFileSource()
: impl(std::make_unique<util::Thread<Impl>>("LocalFileSource")) {
}
: impl(std::make_unique<util::Thread<Impl>>(
util::ThreadPrioritySetterCustom(platform::EXPERIMENTAL_THREAD_PRIORITY_FILE), "LocalFileSource")) {}

LocalFileSource::~LocalFileSource() = default;

Expand Down
7 changes: 6 additions & 1 deletion platform/default/src/mbgl/storage/main_resource_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ class MainResourceLoader::Impl {
onlineFileSource(std::move(onlineFileSource_)),
supportsCacheOnlyRequests_(bool(databaseFileSource)),
thread(std::make_unique<util::Thread<MainResourceLoaderThread>>(
"ResourceLoaderThread", assetFileSource, databaseFileSource, localFileSource, onlineFileSource)) {}
util::ThreadPrioritySetterCustom(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));
Expand Down
10 changes: 6 additions & 4 deletions platform/default/src/mbgl/storage/online_file_source.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <mbgl/storage/online_file_source.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>
Expand All @@ -15,6 +14,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>
Expand Down Expand Up @@ -293,7 +293,9 @@ class OnlineFileSourceThread {

class OnlineFileSource::Impl {
public:
Impl() : thread(std::make_unique<util::Thread<OnlineFileSourceThread>>("OnlineFileSource")) {}
Impl()
: thread(std::make_unique<util::Thread<OnlineFileSourceThread>>(
util::ThreadPrioritySetterCustom(platform::EXPERIMENTAL_THREAD_PRIORITY_NETWORK), "OnlineFileSource")) {}

std::unique_ptr<AsyncRequest> request(Callback callback, Resource res) {
auto req = std::make_unique<FileSourceRequest>(std::move(callback));
Expand Down
3 changes: 3 additions & 0 deletions src/core-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -369,6 +370,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",
Expand Down Expand Up @@ -505,6 +507,7 @@
"mbgl/util/size.hpp": "include/mbgl/util/size.hpp",
"mbgl/util/string.hpp": "include/mbgl/util/string.hpp",
"mbgl/util/thread.hpp": "include/mbgl/util/thread.hpp",
"mbgl/util/thread_priority_setter.hpp": "include/mbgl/util/thread_priority_setter.hpp",
"mbgl/util/tileset.hpp": "include/mbgl/util/tileset.hpp",
"mbgl/util/timer.hpp": "include/mbgl/util/timer.hpp",
"mbgl/util/traits.hpp": "include/mbgl/util/traits.hpp",
Expand Down
11 changes: 9 additions & 2 deletions src/mbgl/util/thread_pool.cpp
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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();

Expand Down

0 comments on commit 98bfdb1

Please sign in to comment.