From 27d8e82e1ce7c6210a33f100cfb4f157ea9583d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 28 Apr 2015 18:23:32 +0200 Subject: lower thread priority of worker threads --- gyp/platform-android.gypi | 1 + gyp/platform-ios.gypi | 1 + gyp/platform-linux.gypi | 1 + gyp/platform-osx.gypi | 1 + include/mbgl/platform/platform.hpp | 3 +++ platform/darwin/nsthread.mm | 13 +++++++++++++ platform/default/sqlite_cache.cpp | 2 +- platform/default/thread.cpp | 11 +++++++++++ src/mbgl/map/map.cpp | 2 +- src/mbgl/storage/default_file_source.cpp | 2 +- src/mbgl/util/thread.hpp | 14 ++++++++++++-- src/mbgl/util/worker.cpp | 3 ++- 12 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 platform/darwin/nsthread.mm create mode 100644 platform/default/thread.cpp diff --git a/gyp/platform-android.gypi b/gyp/platform-android.gypi index bfaac7d4f6..7701393bb9 100644 --- a/gyp/platform-android.gypi +++ b/gyp/platform-android.gypi @@ -12,6 +12,7 @@ 'sources': [ '../platform/android/log_android.cpp', '../platform/android/asset_root.cpp', + '../platform/default/thread.cpp', '../platform/default/string_stdlib.cpp', '../platform/default/image.cpp', '../platform/default/image_reader.cpp', diff --git a/gyp/platform-ios.gypi b/gyp/platform-ios.gypi index f1f95d908e..d23192c8c6 100644 --- a/gyp/platform-ios.gypi +++ b/gyp/platform-ios.gypi @@ -15,6 +15,7 @@ '../platform/darwin/application_root.mm', '../platform/darwin/asset_root.mm', '../platform/darwin/image.mm', + '../platform/darwin/nsthread.mm', '../platform/darwin/reachability.m', '../include/mbgl/ios/MapboxGL.h', '../include/mbgl/ios/MGLMapboxEvents.h', diff --git a/gyp/platform-linux.gypi b/gyp/platform-linux.gypi index 30715f8289..394433870d 100644 --- a/gyp/platform-linux.gypi +++ b/gyp/platform-linux.gypi @@ -14,6 +14,7 @@ '../platform/default/string_stdlib.cpp', '../platform/default/application_root.cpp', '../platform/default/asset_root.cpp', + '../platform/default/thread.cpp', '../platform/default/image.cpp', '../platform/default/image_reader.cpp', '../platform/default/png_reader.cpp', diff --git a/gyp/platform-osx.gypi b/gyp/platform-osx.gypi index 6d6c35b294..ae5194761c 100644 --- a/gyp/platform-osx.gypi +++ b/gyp/platform-osx.gypi @@ -15,6 +15,7 @@ '../platform/darwin/application_root.mm', '../platform/darwin/asset_root.mm', '../platform/darwin/image.mm', + '../platform/darwin/nsthread.mm', ], 'variables': { diff --git a/include/mbgl/platform/platform.hpp b/include/mbgl/platform/platform.hpp index cd87e2256d..f828af37f4 100644 --- a/include/mbgl/platform/platform.hpp +++ b/include/mbgl/platform/platform.hpp @@ -21,6 +21,9 @@ const std::string &applicationRoot(); // Returns the path to the asset location. const std::string &assetRoot(); +// Makes the current thread low priority. +void makeThreadLowPriority(); + // Shows an alpha image with the specified dimensions in a named window. void showDebugImage(std::string name, const char *data, size_t width, size_t height); diff --git a/platform/darwin/nsthread.mm b/platform/darwin/nsthread.mm new file mode 100644 index 0000000000..9ac1d2caa0 --- /dev/null +++ b/platform/darwin/nsthread.mm @@ -0,0 +1,13 @@ +#import + +#include + +namespace mbgl { +namespace platform { + +void makeThreadLowPriority() { + [[NSThread currentThread] setThreadPriority:0.0]; +} + +} +} diff --git a/platform/default/sqlite_cache.cpp b/platform/default/sqlite_cache.cpp index 83d42d5a5c..fb4cdf74e7 100644 --- a/platform/default/sqlite_cache.cpp +++ b/platform/default/sqlite_cache.cpp @@ -62,7 +62,7 @@ std::string unifyMapboxURLs(const std::string &url) { using namespace mapbox::sqlite; SQLiteCache::SQLiteCache(const std::string& path_) - : thread(util::make_unique>("SQLite Cache", path_)) { + : thread(util::make_unique>("SQLite Cache", util::ThreadPriority::Low, path_)) { } SQLiteCache::~SQLiteCache() = default; diff --git a/platform/default/thread.cpp b/platform/default/thread.cpp new file mode 100644 index 0000000000..c0a1069b9c --- /dev/null +++ b/platform/default/thread.cpp @@ -0,0 +1,11 @@ +#include + +namespace mbgl { +namespace platform { + +void makeThreadLowPriority() { + // no-op +} + +} +} diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index be2355206c..0464ed3f1e 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -11,7 +11,7 @@ namespace mbgl { Map::Map(View& view, FileSource& fileSource, MapMode mode, bool startPaused) : data(util::make_unique(view, mode)), - context(util::make_unique>("Map", view, fileSource, *data, startPaused)) + context(util::make_unique>("Map", util::ThreadPriority::Regular, view, fileSource, *data, startPaused)) { view.initialize(this); } diff --git a/src/mbgl/storage/default_file_source.cpp b/src/mbgl/storage/default_file_source.cpp index 1063b22ecc..ddfcc89845 100644 --- a/src/mbgl/storage/default_file_source.cpp +++ b/src/mbgl/storage/default_file_source.cpp @@ -27,7 +27,7 @@ namespace algo = boost::algorithm; namespace mbgl { DefaultFileSource::DefaultFileSource(FileCache* cache, const std::string& root) - : thread(util::make_unique>("FileSource", cache, root)) { + : thread(util::make_unique>("FileSource", util::ThreadPriority::Low, cache, root)) { } DefaultFileSource::~DefaultFileSource() { diff --git a/src/mbgl/util/thread.hpp b/src/mbgl/util/thread.hpp index 55c3d4d281..e97872a502 100644 --- a/src/mbgl/util/thread.hpp +++ b/src/mbgl/util/thread.hpp @@ -7,6 +7,7 @@ #include #include +#include namespace { @@ -34,11 +35,16 @@ namespace util { // Thread<> constructor blocks until the thread and the Object are fully created, so after the // object creation, it's safe to obtain the Object stored in this thread. +enum class ThreadPriority : bool { + Regular, + Low, +}; + template class Thread { public: template - Thread(const std::string& name, Args&&... args); + Thread(const std::string& name, ThreadPriority priority, Args&&... args); ~Thread(); // Invoke object->fn(args...) in the runloop thread. @@ -97,7 +103,7 @@ private: template template -Thread::Thread(const std::string& name, Args&&... args) { +Thread::Thread(const std::string& name, ThreadPriority priority, Args&&... args) { // Note: We're using std::tuple<> to store the arguments because GCC 4.9 has a bug // when expanding parameters packs captured in lambdas. std::tuple params = std::forward_as_tuple(::std::forward(args)...); @@ -109,6 +115,10 @@ Thread::Thread(const std::string& name, Args&&... args) { (void(name)); #endif + if (priority == ThreadPriority::Low) { + platform::makeThreadLowPriority(); + } + constexpr auto seq = typename integer_sequence::type(); run(std::move(params), seq); }); diff --git a/src/mbgl/util/worker.cpp b/src/mbgl/util/worker.cpp index 1a6eaf4d96..9792f1a099 100644 --- a/src/mbgl/util/worker.cpp +++ b/src/mbgl/util/worker.cpp @@ -1,4 +1,5 @@ #include +#include #include @@ -15,7 +16,7 @@ public: Worker::Worker(std::size_t count) { for (std::size_t i = 0; i < count; i++) { - threads.emplace_back(util::make_unique>("Worker")); + threads.emplace_back(util::make_unique>("Worker", util::ThreadPriority::Low)); } } -- cgit v1.2.1