summaryrefslogtreecommitdiff
path: root/platform/default
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <thiago@mapbox.com>2016-08-03 19:24:04 +0300
committerThiago Marcos P. Santos <tmpsantos@gmail.com>2017-07-07 18:10:06 +0300
commit7c117281bc8d53d9d6a1e3a9ff9760a5a5b44cf8 (patch)
treeb94bade3a59aceaba9fb5117411ce850006de123 /platform/default
parent6b5f8fd6ef845fbf834968d94ec26cf26f645838 (diff)
downloadqtlocation-mapboxgl-7c117281bc8d53d9d6a1e3a9ff9760a5a5b44cf8.tar.gz
[core] Isolate pthread-based tls implementation
Diffstat (limited to 'platform/default')
-rw-r--r--platform/default/thread_local.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/platform/default/thread_local.cpp b/platform/default/thread_local.cpp
new file mode 100644
index 0000000000..b754a04b7d
--- /dev/null
+++ b/platform/default/thread_local.cpp
@@ -0,0 +1,62 @@
+#include <mbgl/util/thread_local.hpp>
+
+#include <mbgl/map/backend_scope.hpp>
+#include <mbgl/util/logging.hpp>
+#include <mbgl/util/run_loop.hpp>
+
+#include <stdexcept>
+#include <cassert>
+
+#include <pthread.h>
+
+namespace mbgl {
+namespace util {
+
+template <class T>
+class ThreadLocal<T>::Impl {
+public:
+ pthread_key_t key;
+};
+
+template <class T>
+ThreadLocal<T>::ThreadLocal() : impl(std::make_unique<Impl>()) {
+ int ret = pthread_key_create(&impl->key, [](void *) {});
+
+ if (ret) {
+ throw std::runtime_error("Failed to init local storage key.");
+ }
+}
+
+template <class T>
+ThreadLocal<T>::~ThreadLocal() {
+ delete get();
+
+ if (pthread_key_delete(impl->key)) {
+ Log::Error(Event::General, "Failed to delete local storage key.");
+ assert(false);
+ }
+}
+
+template <class T>
+T* ThreadLocal<T>::get() {
+ auto* ret = reinterpret_cast<T*>(pthread_getspecific(impl->key));
+ if (!ret) {
+ return nullptr;
+ }
+
+ return ret;
+}
+
+template <class T>
+void ThreadLocal<T>::set(T* ptr) {
+ if (pthread_setspecific(impl->key, ptr)) {
+ throw std::runtime_error("Failed to set local storage.");
+ }
+}
+
+template class ThreadLocal<RunLoop>;
+template class ThreadLocal<BackendScope>;
+template class ThreadLocal<int>; // For unit tests
+
+} // namespace util
+} // namespace mbgl