summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAlexander Shalamov <alexander.shalamov@mapbox.com>2020-01-31 17:06:37 +0200
committerAlexander Shalamov <alexander.shalamov@mapbox.com>2020-02-06 23:54:19 +0200
commitbb85ec442dd7b5b0966d73bc6b868d33bb03e5e9 (patch)
treee0fef3271ab6e4f21b05f4f00dd6aa4386efd499 /include
parent4be2fa4f9af29cfad869412d2d794964554d4eba (diff)
downloadqtlocation-mapboxgl-bb85ec442dd7b5b0966d73bc6b868d33bb03e5e9.tar.gz
[core] Add hooks for setting experimental thread priorities for mbgl threads
Diffstat (limited to 'include')
-rw-r--r--include/mbgl/util/thread.hpp38
1 files changed, 23 insertions, 15 deletions
diff --git a/include/mbgl/util/thread.hpp b/include/mbgl/util/thread.hpp
index ab0403e44e..4bc948fdbd 100644
--- a/include/mbgl/util/thread.hpp
+++ b/include/mbgl/util/thread.hpp
@@ -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