diff options
author | Thiago Marcos P. Santos <thiago@mapbox.com> | 2016-08-03 19:24:04 +0300 |
---|---|---|
committer | Thiago Marcos P. Santos <tmpsantos@gmail.com> | 2017-07-07 18:10:06 +0300 |
commit | 7c117281bc8d53d9d6a1e3a9ff9760a5a5b44cf8 (patch) | |
tree | b94bade3a59aceaba9fb5117411ce850006de123 /src/mbgl | |
parent | 6b5f8fd6ef845fbf834968d94ec26cf26f645838 (diff) | |
download | qtlocation-mapboxgl-7c117281bc8d53d9d6a1e3a9ff9760a5a5b44cf8.tar.gz |
[core] Isolate pthread-based tls implementation
Diffstat (limited to 'src/mbgl')
-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 |