diff options
author | Thiago Marcos P. Santos <thiago@mapbox.com> | 2015-06-23 20:08:30 +0300 |
---|---|---|
committer | Thiago Marcos P. Santos <thiago@mapbox.com> | 2015-06-25 16:26:21 +0300 |
commit | a8b6b67dfeb79a2e7904a2aec6354161c4eb1b16 (patch) | |
tree | ffe6ebd296b440287979f8df74b9f59da0ee998d /src/mbgl/util/thread.hpp | |
parent | 7bb86368d4730c889f9b1f7007a7bada580db8ea (diff) | |
download | qtlocation-mapboxgl-a8b6b67dfeb79a2e7904a2aec6354161c4eb1b16.tar.gz |
Introduce the ThreadContext
mbgl::Thread will keep a ThreadContext for each running instance
in a thread_local so we don't need to lookup a man in the Environment
every time we need some info about the current thread.
This patch is moving the ::currentlyOn check used on Debug build from
the Environment class to the ThreadContext.
Diffstat (limited to 'src/mbgl/util/thread.hpp')
-rw-r--r-- | src/mbgl/util/thread.hpp | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/src/mbgl/util/thread.hpp b/src/mbgl/util/thread.hpp index f3a9baa6f3..5bd856cbbc 100644 --- a/src/mbgl/util/thread.hpp +++ b/src/mbgl/util/thread.hpp @@ -8,6 +8,7 @@ #include <functional> #include <mbgl/util/run_loop.hpp> +#include <mbgl/util/thread_context.hpp> #include <mbgl/platform/platform.hpp> namespace mbgl { @@ -21,16 +22,11 @@ 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 Object> class Thread { public: template <class... Args> - Thread(const std::string& name, ThreadPriority priority, Args&&... args); + Thread(const ThreadContext&, Args&&... args); ~Thread(); // Invoke object->fn(args...) in the runloop thread. @@ -81,7 +77,7 @@ private: } template <typename P, std::size_t... I> - void run(P&& params, std::index_sequence<I...>); + void run(ThreadContext, P&& params, std::index_sequence<I...>); std::promise<void> running; std::promise<void> joinable; @@ -94,23 +90,21 @@ private: template <class Object> template <class... Args> -Thread<Object>::Thread(const std::string& name, ThreadPriority priority, Args&&... args) { +Thread<Object>::Thread(const ThreadContext& context, 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<Args...> params = std::forward_as_tuple(::std::forward<Args>(args)...); thread = std::thread([&] { #ifdef __APPLE__ - pthread_setname_np(name.c_str()); - #else - (void(name)); + pthread_setname_np(context.name.c_str()); #endif - if (priority == ThreadPriority::Low) { + if (context.priority == ThreadPriority::Low) { platform::makeThreadLowPriority(); } - run(std::move(params), std::index_sequence_for<Args...>{}); + run(context, std::move(params), std::index_sequence_for<Args...>{}); }); running.get_future().get(); @@ -118,10 +112,12 @@ Thread<Object>::Thread(const std::string& name, ThreadPriority priority, Args&&. template <class Object> template <typename P, std::size_t... I> -void Thread<Object>::run(P&& params, std::index_sequence<I...>) { +void Thread<Object>::run(ThreadContext context, P&& params, std::index_sequence<I...>) { uv::loop l; { + ThreadContext::current.set(&context); + RunLoop loop_(l.get()); loop = &loop_; @@ -133,6 +129,8 @@ void Thread<Object>::run(P&& params, std::index_sequence<I...>) { loop = nullptr; object = nullptr; + + ThreadContext::current.set(nullptr); } // Run the loop again to ensure that async close callbacks have been called. |