[core] refactor ThreadLocal backend

This commit is contained in:
Konstantin Käfer
2019-04-07 13:55:47 +02:00
parent 9c8ee53cd8
commit 8f331f5ae3
3 changed files with 63 additions and 81 deletions

View File

@@ -1,66 +1,46 @@
#include <mbgl/util/thread_local.hpp>
#include <mbgl/gfx/backend_scope.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/util/run_loop.hpp>
#include <stdexcept>
#include <cassert>
#include <cstdlib>
#include <pthread.h>
namespace mbgl {
namespace util {
namespace impl {
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.");
ThreadLocalBase::ThreadLocalBase() {
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();
}
}
template <class T>
ThreadLocal<T>::~ThreadLocal() {
// ThreadLocal will not take ownership
// of the pointer it is managing. The pointer
// needs to be explicitly cleared before we
// destroy this object.
ThreadLocalBase::~ThreadLocalBase() {
// 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());
if (pthread_key_delete(impl->key)) {
Log::Error(Event::General, "Failed to delete local storage key.");
assert(false);
if (pthread_key_delete(reinterpret_cast<pthread_key_t&>(storage)) != 0) {
Log::Error(Event::General, "Failed to delete thread-specific storage key");
abort();
}
}
template <class T>
T* ThreadLocal<T>::get() {
auto* ret = reinterpret_cast<T*>(pthread_getspecific(impl->key));
if (!ret) {
return nullptr;
}
return ret;
void* ThreadLocalBase::get() {
return pthread_getspecific(reinterpret_cast<pthread_key_t&>(storage));
}
template <class T>
void ThreadLocal<T>::set(T* ptr) {
if (pthread_setspecific(impl->key, ptr)) {
throw std::runtime_error("Failed to set local storage.");
void ThreadLocalBase::set(void* ptr) {
if (pthread_setspecific(reinterpret_cast<pthread_key_t&>(storage), ptr) != 0) {
Log::Error(Event::General, "Failed to set thread-specific storage");
abort();
}
}
template class ThreadLocal<gfx::BackendScope>;
template class ThreadLocal<Scheduler>;
template class ThreadLocal<int>; // For unit tests
} // namespace impl
} // namespace util
} // namespace mbgl