summaryrefslogtreecommitdiff
path: root/platform/default/src/mbgl/util/thread_local.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'platform/default/src/mbgl/util/thread_local.cpp')
-rw-r--r--platform/default/src/mbgl/util/thread_local.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/platform/default/src/mbgl/util/thread_local.cpp b/platform/default/src/mbgl/util/thread_local.cpp
index 5fb66c3440..159a8e8e17 100644
--- a/platform/default/src/mbgl/util/thread_local.cpp
+++ b/platform/default/src/mbgl/util/thread_local.cpp
@@ -6,20 +6,33 @@
#include <pthread.h>
+#ifdef MB_COMPILER_CXX_THREAD_LOCAL
+#include <unordered_map>
+#endif
+
namespace mbgl {
namespace util {
namespace impl {
+#ifdef MB_COMPILER_CXX_THREAD_LOCAL
+namespace {
+thread_local std::unordered_map<void*, void*> mapOfPointers;
+}
+#endif
+
ThreadLocalBase::ThreadLocalBase() {
+#ifndef MB_COMPILER_CXX_THREAD_LOCAL
static_assert(sizeof(storage) >= sizeof(pthread_key_t), "storage is too small");
static_assert(alignof(decltype(storage)) % alignof(pthread_key_t) == 0, "storage is incorrectly aligned");
if (pthread_key_create(&reinterpret_cast<pthread_key_t&>(storage), nullptr) != 0) {
Log::Error(Event::General, "Failed to initialize thread-specific storage key");
abort();
}
+#endif
}
ThreadLocalBase::~ThreadLocalBase() {
+#ifndef MB_COMPILER_CXX_THREAD_LOCAL
// ThreadLocal will not take ownership of the pointer it is managing. The pointer
// needs to be explicitly cleared before we destroy this object.
assert(!get());
@@ -28,17 +41,26 @@ ThreadLocalBase::~ThreadLocalBase() {
Log::Error(Event::General, "Failed to delete thread-specific storage key");
abort();
}
+#endif
}
void* ThreadLocalBase::get() {
+#ifdef MB_COMPILER_CXX_THREAD_LOCAL
+ return mapOfPointers[this];
+#else
return pthread_getspecific(reinterpret_cast<pthread_key_t&>(storage));
+#endif
}
void ThreadLocalBase::set(void* ptr) {
+#ifdef MB_COMPILER_CXX_THREAD_LOCAL
+ mapOfPointers[this] = ptr;
+#else
if (pthread_setspecific(reinterpret_cast<pthread_key_t&>(storage), ptr) != 0) {
Log::Error(Event::General, "Failed to set thread-specific storage");
abort();
}
+#endif
}
} // namespace impl