From b52aab5f7c6625b219a68cc7a956cf895d158139 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Fri, 13 Mar 2015 14:22:09 +0200 Subject: [PATCH] Introduce the ThreadInfoStore and Environment::Scope The Environment will now keep track of every thread that is running within a Environment::Scope. Once the Scope object is created, it will automatically register the thread to the ThreadInfoStore with some additional information and it will automatically deregister as the stack frame rewinds (which can be useful for Threads that are temporally in use by an Environment like Workers). The ThreadInfoStore could be using the thread-local storage, but I think that making the Environment aware of what threads are associated to it can be useful for writing a mechanism for posting messages between threads. Withing a Scope, the Environment associated to the thread can be queried at any time which will save us from juggling a reference to the Environemnt around. Finally, the type of thread (Main, Map, etc) can also be queried at any moment to make sure a piece of code is running on the desired thread. --- src/mbgl/map/environment.cpp | 92 ++++++++++++++++++++++++++++++++++++ src/mbgl/map/environment.hpp | 23 ++++++++- 2 files changed, 113 insertions(+), 2 deletions(-) diff --git a/src/mbgl/map/environment.cpp b/src/mbgl/map/environment.cpp index ee13d33ea..251954327 100644 --- a/src/mbgl/map/environment.cpp +++ b/src/mbgl/map/environment.cpp @@ -4,12 +4,104 @@ #include #include +#include +#include namespace mbgl { +namespace { + +class ThreadInfoStore { +private: + struct ThreadInfo { + Environment* env; + ThreadType type; + std::string name; + }; + +public: + ThreadInfoStore() { + registerThread(nullptr, ThreadType::Main, "Main"); + } + + ~ThreadInfoStore() { + unregisterThread(); + assert(threadSet.size() == 0); + } + + void registerThread(Environment* env, ThreadType type, const std::string& name) { + std::lock_guard lock(mtx); + + // FIXME: We should never need to overwrite a thread here and we only allow + // this today because on the Static mode, the Map thread and the Main thread + // are same. Replace this with emplace() when this gets fixed. + threadSet[std::this_thread::get_id()] = ThreadInfo{env, type, name}; + } + + void unregisterThread() { + std::lock_guard lock(mtx); + + ThreadSet::iterator it = threadSet.find(std::this_thread::get_id()); + if (it != threadSet.end()) { + threadSet.erase(it); + } + } + + const ThreadInfo& getThreadInfo() const { + static ThreadInfo emptyInfo; + std::lock_guard lock(mtx); + + ThreadSet::const_iterator it = threadSet.find(std::this_thread::get_id()); + if (it != threadSet.end()) { + return it->second; + } else { + return emptyInfo; + } + } + +private: + typedef std::unordered_map ThreadSet; + ThreadSet threadSet; + + mutable std::mutex mtx; +}; + +ThreadInfoStore threadInfoStore; + +} // namespace + +Environment::Scope::Scope(Environment& env, ThreadType type, const std::string& name) + : id(std::this_thread::get_id()) { + threadInfoStore.registerThread(&env, type, name); +} + +Environment::Scope::~Scope() { + assert(id == std::this_thread::get_id()); + threadInfoStore.unregisterThread(); +} + Environment::Environment(FileSource &fs) : fileSource(fs), loop(uv_loop_new()) { } +Environment& Environment::Get() { + Environment* env = threadInfoStore.getThreadInfo().env; + assert(env); + + return *env; +} + +bool Environment::inScope() { + return threadInfoStore.getThreadInfo().env; +} + +bool Environment::currentlyOn(ThreadType type) { + return static_cast(threadInfoStore.getThreadInfo().type) & static_cast(type); +} + +std::string Environment::threadName() { + return threadInfoStore.getThreadInfo().name; +} + void Environment::setup() { mapThread = std::this_thread::get_id(); } diff --git a/src/mbgl/map/environment.hpp b/src/mbgl/map/environment.hpp index b68a7b9e2..86d40cc38 100644 --- a/src/mbgl/map/environment.hpp +++ b/src/mbgl/map/environment.hpp @@ -16,10 +16,30 @@ class Request; class Response; struct Resource; -class Environment : private util::noncopyable { +enum class ThreadType : uint8_t { + Unknown = 0, + Main = 1 << 0, + Map = 1 << 1, +}; + +class Environment final : private util::noncopyable { public: + class Scope final { + public: + Scope(Environment&, ThreadType, const std::string& name); + ~Scope(); + + private: + std::thread::id id; + }; + Environment(FileSource &); + static Environment& Get(); + static bool inScope(); + static bool currentlyOn(ThreadType); + static std::string threadName(); + void setup(); bool inMapThread() const; @@ -33,7 +53,6 @@ public: private: FileSource &fileSource; - std::thread::id mapThread; public: uv_loop_t *const loop;