summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <thiago@mapbox.com>2016-08-03 19:56:10 +0300
committerThiago Marcos P. Santos <tmpsantos@gmail.com>2017-07-07 18:10:06 +0300
commita24d89d9f8499123a03376e58da0c4d6ebe43261 (patch)
tree2d87a8465eba34fd79c6bab5ab885503749f39ca /platform
parent7c117281bc8d53d9d6a1e3a9ff9760a5a5b44cf8 (diff)
downloadqtlocation-mapboxgl-a24d89d9f8499123a03376e58da0c4d6ebe43261.tar.gz
[Qt] Use QThreadStorage for thread local
Portable.
Diffstat (limited to 'platform')
-rw-r--r--platform/qt/qt.cmake2
-rw-r--r--platform/qt/src/thread_local.cpp44
2 files changed, 45 insertions, 1 deletions
diff --git a/platform/qt/qt.cmake b/platform/qt/qt.cmake
index 9febee88b5..5b09dbf9ab 100644
--- a/platform/qt/qt.cmake
+++ b/platform/qt/qt.cmake
@@ -37,7 +37,7 @@ set(MBGL_QT_FILES
PRIVATE platform/default/mbgl/util/default_thread_pool.hpp
# Thread
- PRIVATE platform/default/thread_local.cpp
+ PRIVATE platform/qt/src/thread_local.cpp
# Platform integration
PRIVATE platform/qt/src/async_task.cpp
diff --git a/platform/qt/src/thread_local.cpp b/platform/qt/src/thread_local.cpp
new file mode 100644
index 0000000000..e48a9d6e74
--- /dev/null
+++ b/platform/qt/src/thread_local.cpp
@@ -0,0 +1,44 @@
+#include <mbgl/util/thread_local.hpp>
+
+#include <mbgl/util/run_loop.hpp>
+#include <mbgl/map/backend_scope.hpp>
+
+#include <array>
+
+#include <QThreadStorage>
+
+namespace mbgl {
+namespace util {
+
+template <class T>
+class ThreadLocal<T>::Impl {
+public:
+ QThreadStorage<std::array<T*, 1>> local;
+};
+
+template <class T>
+ThreadLocal<T>::ThreadLocal() : impl(std::make_unique<Impl>()) {
+ set(nullptr);
+}
+
+template <class T>
+ThreadLocal<T>::~ThreadLocal() {
+ delete get();
+}
+
+template <class T>
+T* ThreadLocal<T>::get() {
+ return impl->local.localData()[0];
+}
+
+template <class T>
+void ThreadLocal<T>::set(T* ptr) {
+ impl->local.localData()[0] = ptr;
+}
+
+template class ThreadLocal<RunLoop>;
+template class ThreadLocal<BackendScope>;
+template class ThreadLocal<int>; // For unit tests
+
+} // namespace util
+} // namespace mbgl