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.
This commit is contained in:
Thiago Marcos P. Santos
2015-03-13 14:22:09 +02:00
committed by Konstantin Käfer
parent 7b996012a5
commit b52aab5f7c
2 changed files with 113 additions and 2 deletions

View File

@@ -4,12 +4,104 @@
#include <uv.h>
#include <cassert>
#include <mutex>
#include <unordered_map>
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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::thread::id, ThreadInfo> 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<uint8_t>(threadInfoStore.getThreadInfo().type) & static_cast<uint8_t>(type);
}
std::string Environment::threadName() {
return threadInfoStore.getThreadInfo().name;
}
void Environment::setup() {
mapThread = std::this_thread::get_id();
}

View File

@@ -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;