summaryrefslogtreecommitdiff
path: root/platform/qt/src/thread_local.cpp
blob: e48a9d6e740695effbb014d0558f68fd360577f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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