summaryrefslogtreecommitdiff
path: root/src/mbgl/util/thread_local.hpp
blob: 84417e631cae3daa08a0afea83550c21f9439da9 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once

#include <type_traits>

#ifdef DETECT_THREAD_LOCAL
#include "thread_local_compiler_detection.h"
#endif

#ifdef __APPLE__
# include <TargetConditionals.h>
# include <Availability.h>
# if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0
#   undef MB_COMPILER_CXX_THREAD_LOCAL
# endif
# if defined(__i386__) && defined(TARGET_OS_SIMULATOR)
#   undef MB_COMPILER_CXX_THREAD_LOCAL
# endif
#endif

namespace mbgl {
namespace util {
namespace impl {

class ThreadLocalBase {
protected:
    ThreadLocalBase();
    ~ThreadLocalBase();

    void* get();
    void set(void*);

private:
#ifndef MB_COMPILER_CXX_THREAD_LOCAL
    std::aligned_storage_t<sizeof(void*), alignof(void*)> storage;
#endif
};

} // namespace impl

template <class T>
class ThreadLocal : public impl::ThreadLocalBase {
public:
    ThreadLocal() = default;

    ThreadLocal(T* val) {
        set(val);
    }

    T* get() {
        return reinterpret_cast<T*>(impl::ThreadLocalBase::get());
    }

    void set(T* ptr) {
        impl::ThreadLocalBase::set(ptr);
    }
};

} // namespace util
} // namespace mbgl