diff options
Diffstat (limited to 'src/mbgl/util/thread_local.hpp')
-rw-r--r-- | src/mbgl/util/thread_local.hpp | 44 |
1 files changed, 10 insertions, 34 deletions
diff --git a/src/mbgl/util/thread_local.hpp b/src/mbgl/util/thread_local.hpp index 15d4d56524..b0e26356b4 100644 --- a/src/mbgl/util/thread_local.hpp +++ b/src/mbgl/util/thread_local.hpp @@ -1,12 +1,8 @@ #pragma once -#include <mbgl/util/logging.hpp> #include <mbgl/util/noncopyable.hpp> -#include <cassert> -#include <stdexcept> - -#include <pthread.h> +#include <memory> namespace mbgl { namespace util { @@ -14,40 +10,20 @@ namespace util { template <class T> class ThreadLocal : public noncopyable { public: - ThreadLocal() { - int ret = pthread_key_create(&key, [](void *ptr) { - delete reinterpret_cast<T *>(ptr); - }); - - if (ret) { - throw std::runtime_error("Failed to init local storage key."); - } - } - - ~ThreadLocal() { - if (pthread_key_delete(key)) { - Log::Error(Event::General, "Failed to delete local storage key."); - assert(false); - } + ThreadLocal(T* val) { + ThreadLocal(); + set(val); } - T* get() { - auto* ret = reinterpret_cast<T*>(pthread_getspecific(key)); - if (!ret) { - return nullptr; - } + ThreadLocal(); + ~ThreadLocal(); - return ret; - } - - void set(T* ptr) { - if (pthread_setspecific(key, ptr)) { - throw std::runtime_error("Failed to set local storage."); - } - } + T* get(); + void set(T* ptr); private: - pthread_key_t key; + class Impl; + std::unique_ptr<Impl> impl; }; } // namespace util |