From 62990f0a21eab309f03db0d92119a9a299ae475d Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Fri, 20 Nov 2015 14:30:05 +0200 Subject: [PATCH] [core] Port uv::tls to pthreads --- platform/default/run_loop.cpp | 10 +++- src/mbgl/util/thread.hpp | 4 +- src/mbgl/util/thread_context.cpp | 95 +++++++++++++++++++++++++++----- src/mbgl/util/thread_context.hpp | 65 +++------------------- src/mbgl/util/thread_local.hpp | 59 ++++++++++++++++++++ 5 files changed, 157 insertions(+), 76 deletions(-) create mode 100644 src/mbgl/util/thread_local.hpp diff --git a/platform/default/run_loop.cpp b/platform/default/run_loop.cpp index a0262364c..845ee1b2a 100644 --- a/platform/default/run_loop.cpp +++ b/platform/default/run_loop.cpp @@ -1,12 +1,18 @@ #include #include #include +#include + +namespace { + +using namespace mbgl::util; +static ThreadLocal& current = *new ThreadLocal; + +} namespace mbgl { namespace util { -static uv::tls current; - RunLoop* RunLoop::Get() { return current.get(); } diff --git a/src/mbgl/util/thread.hpp b/src/mbgl/util/thread.hpp index 7924d3d4c..e1ea89c39 100644 --- a/src/mbgl/util/thread.hpp +++ b/src/mbgl/util/thread.hpp @@ -112,7 +112,7 @@ Thread::Thread(const ThreadContext& context, Args&&... args) { template template void Thread::run(ThreadContext context, P&& params, std::index_sequence) { - ThreadContext::current.set(&context); + ThreadContext::Set(&context); RunLoop loop_(RunLoop::Type::New); loop = &loop_; @@ -126,7 +126,7 @@ void Thread::run(ThreadContext context, P&& params, std::index_sequence< loop = nullptr; object = nullptr; - ThreadContext::current.set(nullptr); + ThreadContext::Set(nullptr); joinable.get_future().get(); } diff --git a/src/mbgl/util/thread_context.cpp b/src/mbgl/util/thread_context.cpp index 6742d5e75..79c75528c 100644 --- a/src/mbgl/util/thread_context.cpp +++ b/src/mbgl/util/thread_context.cpp @@ -1,29 +1,94 @@ #include +#include + +#include + +namespace { + +using namespace mbgl::util; +static ThreadLocal& current = *new ThreadLocal; + +} + 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::current; +void ThreadContext::Set(ThreadContext* context) { + current.set(context); +} + +bool ThreadContext::currentlyOn(ThreadType type) { + return current.get()->type == type; +} + +std::string ThreadContext::getName() { + if (current.get() != nullptr) { + return current.get()->name; + } else { + return "Unknown"; + } +} + +ThreadPriority ThreadContext::getPriority() { + if (current.get() != nullptr) { + return current.get()->priority; + } else { + return ThreadPriority::Regular; + } +} + +FileSource* ThreadContext::getFileSource() { + if (current.get() != nullptr) { + return current.get()->fileSource; + } else { + return nullptr; + } +} + +void ThreadContext::setFileSource(FileSource* fileSource) { + if (current.get() != nullptr) { + current.get()->fileSource = fileSource; + } else { + throw new std::runtime_error("Current thread has no current ThreadContext."); + } +} + +GLObjectStore* ThreadContext::getGLObjectStore() { + if (current.get() != nullptr) { + return current.get()->glObjectStore; + } else { + return nullptr; + } +} + +void ThreadContext::setGLObjectStore(GLObjectStore* glObjectStore) { + if (current.get() != nullptr) { + current.get()->glObjectStore = glObjectStore; + } else { + throw new std::runtime_error("Current thread has no current ThreadContext."); + } +} + +class MainThreadContextRegistrar { +public: + MainThreadContextRegistrar() : context("Main", ThreadType::Main, ThreadPriority::Regular) { + ThreadContext::Set(&context); + } + + ~MainThreadContextRegistrar() { + ThreadContext::Set(nullptr); + } + +private: + ThreadContext context; +}; // Will auto register the main thread context // at startup. Must be instantiated after the diff --git a/src/mbgl/util/thread_context.hpp b/src/mbgl/util/thread_context.hpp index 5a91affda..0c97611eb 100644 --- a/src/mbgl/util/thread_context.hpp +++ b/src/mbgl/util/thread_context.hpp @@ -1,8 +1,6 @@ #ifndef MBGL_UTIL_THREAD_CONTEXT #define MBGL_UTIL_THREAD_CONTEXT -#include - #include #include #include @@ -31,70 +29,23 @@ struct ThreadContext { public: ThreadContext(const std::string& name, ThreadType type, ThreadPriority priority); - static bool currentlyOn(ThreadType type) { - return current.get()->type == type; - } + static void Set(ThreadContext* context); - static std::string getName() { - if (current.get() != nullptr) { - return current.get()->name; - } else { - return "Unknown"; - } - } + static bool currentlyOn(ThreadType type); + static std::string getName(); + static ThreadPriority getPriority(); - static ThreadPriority getPriority() { - if (current.get() != nullptr) { - return current.get()->priority; - } else { - return ThreadPriority::Regular; - } - } + static FileSource* getFileSource(); + static void setFileSource(FileSource* fileSource); + static GLObjectStore* getGLObjectStore(); + static void setGLObjectStore(GLObjectStore* glObjectStore); - static FileSource* getFileSource() { - if (current.get() != nullptr) { - return current.get()->fileSource; - } else { - return nullptr; - } - } - - static void setFileSource(FileSource* fileSource) { - if (current.get() != nullptr) { - current.get()->fileSource = fileSource; - } else { - throw new std::runtime_error("Current thread has no current ThreadContext."); - } - } - - static GLObjectStore* getGLObjectStore() { - if (current.get() != nullptr) { - return current.get()->glObjectStore; - } else { - return nullptr; - } - } - - static void setGLObjectStore(GLObjectStore* glObjectStore) { - if (current.get() != nullptr) { - current.get()->glObjectStore = glObjectStore; - } else { - throw new std::runtime_error("Current thread has no current ThreadContext."); - } - } - -private: std::string name; ThreadType type; ThreadPriority priority; FileSource* fileSource = nullptr; GLObjectStore* glObjectStore = nullptr; - - static uv::tls current; - - friend class MainThreadContextRegistrar; - template friend class Thread; }; } diff --git a/src/mbgl/util/thread_local.hpp b/src/mbgl/util/thread_local.hpp new file mode 100644 index 000000000..1d580dc23 --- /dev/null +++ b/src/mbgl/util/thread_local.hpp @@ -0,0 +1,59 @@ +#ifndef MBGL_UTIL_THREAD_LOCAL +#define MBGL_UTIL_THREAD_LOCAL + +#include + +#include + +#include + +namespace mbgl { +namespace util { + +template +class ThreadLocal : public noncopyable { +public: + inline ThreadLocal(T* val) { + ThreadLocal(); + set(val); + } + + inline ThreadLocal() { + int ret = pthread_key_create(&key, [](void *ptr) { + delete reinterpret_cast(ptr); + }); + + if (ret) { + throw new std::runtime_error("Failed to init local storage key."); + } + } + + inline ~ThreadLocal() { + if (pthread_key_delete(key)) { + throw new std::runtime_error("Failed to delete local storage key."); + } + } + + inline T* get() { + T* ret = reinterpret_cast(pthread_getspecific(key)); + if (!ret) { + return nullptr; + } + + return ret; + } + + inline void set(T* ptr) { + if (pthread_setspecific(key, ptr)) { + throw new std::runtime_error("Failed to set local storage."); + } + } + +private: + pthread_key_t key; +}; + +} +} + +#endif