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;