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.
This commit is contained in:
34
src/mbgl/util/thread_context.cpp
Normal file
34
src/mbgl/util/thread_context.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <mbgl/util/thread_context.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace util {
|
||||
|
||||
class MainThreadContextRegistrar {
|
||||
public:
|
||||
MainThreadContextRegistrar() : context("Main", ThreadType::Main, ThreadPriority::Regular) {
|
||||
ThreadContext::current.set(&context);
|
||||
}
|
||||
|
||||
~MainThreadContextRegistrar() {
|
||||
ThreadContext::current.set(nullptr);
|
||||
}
|
||||
|
||||
private:
|
||||
ThreadContext context;
|
||||
};
|
||||
|
||||
ThreadContext::ThreadContext(const std::string& name_, ThreadType type_, ThreadPriority priority_)
|
||||
: name(name_),
|
||||
type(type_),
|
||||
priority(priority_) {
|
||||
}
|
||||
|
||||
uv::tls<ThreadContext> ThreadContext::current;
|
||||
|
||||
// Will auto register the main thread context
|
||||
// at startup. Must be instantiated after the
|
||||
// ThreadContext::current object.
|
||||
MainThreadContextRegistrar registrar;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user