Add an ID to Environment

Environment will now have an incremental unique ID to that will
help distinguish between threads scoped to different Environments
when logging.
This commit is contained in:
Thiago Marcos P. Santos
2015-03-15 01:55:11 +02:00
committed by Konstantin Käfer
parent aa9adbb8f8
commit 66841bf7eb
2 changed files with 14 additions and 1 deletions

View File

@@ -3,6 +3,7 @@
#include <uv.h>
#include <atomic>
#include <cassert>
#include <mutex>
#include <unordered_map>
@@ -66,6 +67,11 @@ private:
mutable std::mutex mtx;
};
unsigned makeEnvironmentID() {
static std::atomic<unsigned> id(0);
return id++;
}
ThreadInfoStore threadInfoStore;
} // namespace
@@ -80,7 +86,8 @@ Environment::Scope::~Scope() {
threadInfoStore.unregisterThread();
}
Environment::Environment(FileSource& fs) : fileSource(fs), loop(uv_loop_new()) {
Environment::Environment(FileSource& fs)
: id(makeEnvironmentID()), fileSource(fs), loop(uv_loop_new()) {
}
Environment& Environment::Get() {
@@ -102,6 +109,10 @@ std::string Environment::threadName() {
return threadInfoStore.getThreadInfo().name;
}
unsigned Environment::getID() const {
return id;
}
void Environment::requestAsync(const Resource& resource,
std::function<void(const Response&)> callback) {
fileSource.request(resource, *this, std::move(callback));

View File

@@ -41,6 +41,7 @@ public:
static bool currentlyOn(ThreadType);
static std::string threadName();
unsigned getID() const;
void requestAsync(const Resource&, std::function<void(const Response&)>);
Request* request(const Resource&, std::function<void(const Response&)>);
void cancelRequest(Request*);
@@ -49,6 +50,7 @@ public:
void terminate();
private:
unsigned id;
FileSource& fileSource;
public: