cannam@147
|
1 // Copyright (c) 2014, Jason Choy <jjwchoy@gmail.com>
|
cannam@147
|
2 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
|
cannam@147
|
3 // Licensed under the MIT License:
|
cannam@147
|
4 //
|
cannam@147
|
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
cannam@147
|
6 // of this software and associated documentation files (the "Software"), to deal
|
cannam@147
|
7 // in the Software without restriction, including without limitation the rights
|
cannam@147
|
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
cannam@147
|
9 // copies of the Software, and to permit persons to whom the Software is
|
cannam@147
|
10 // furnished to do so, subject to the following conditions:
|
cannam@147
|
11 //
|
cannam@147
|
12 // The above copyright notice and this permission notice shall be included in
|
cannam@147
|
13 // all copies or substantial portions of the Software.
|
cannam@147
|
14 //
|
cannam@147
|
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
cannam@147
|
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
cannam@147
|
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
cannam@147
|
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
cannam@147
|
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
cannam@147
|
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
cannam@147
|
21 // THE SOFTWARE.
|
cannam@147
|
22
|
cannam@147
|
23 #ifndef KJ_THREADLOCAL_H_
|
cannam@147
|
24 #define KJ_THREADLOCAL_H_
|
cannam@147
|
25
|
cannam@147
|
26 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
|
cannam@147
|
27 #pragma GCC system_header
|
cannam@147
|
28 #endif
|
cannam@147
|
29 // This file declares a macro `KJ_THREADLOCAL_PTR` for declaring thread-local pointer-typed
|
cannam@147
|
30 // variables. Use like:
|
cannam@147
|
31 // KJ_THREADLOCAL_PTR(MyType) foo = nullptr;
|
cannam@147
|
32 // This is equivalent to:
|
cannam@147
|
33 // thread_local MyType* foo = nullptr;
|
cannam@147
|
34 // This can only be used at the global scope.
|
cannam@147
|
35 //
|
cannam@147
|
36 // AVOID USING THIS. Use of thread-locals is discouraged because they often have many of the same
|
cannam@147
|
37 // properties as singletons: http://www.object-oriented-security.org/lets-argue/singletons
|
cannam@147
|
38 //
|
cannam@147
|
39 // Also, thread-locals tend to be hostile to event-driven code, which can be particularly
|
cannam@147
|
40 // surprising when using fibers (all fibers in the same thread will share the same threadlocals,
|
cannam@147
|
41 // even though they do not share a stack).
|
cannam@147
|
42 //
|
cannam@147
|
43 // That said, thread-locals are sometimes needed for runtime logistics in the KJ framework. For
|
cannam@147
|
44 // example, the current exception callback and current EventLoop are stored as thread-local
|
cannam@147
|
45 // pointers. Since KJ only ever needs to store pointers, not values, we avoid the question of
|
cannam@147
|
46 // whether these values' destructors need to be run, and we avoid the need for heap allocation.
|
cannam@147
|
47
|
cannam@147
|
48 #include "common.h"
|
cannam@147
|
49
|
cannam@147
|
50 #if !defined(KJ_USE_PTHREAD_THREADLOCAL) && defined(__APPLE__)
|
cannam@147
|
51 #include "TargetConditionals.h"
|
cannam@147
|
52 #if TARGET_OS_IPHONE
|
cannam@147
|
53 // iOS apparently does not support __thread (nor C++11 thread_local).
|
cannam@147
|
54 #define KJ_USE_PTHREAD_TLS 1
|
cannam@147
|
55 #endif
|
cannam@147
|
56 #endif
|
cannam@147
|
57
|
cannam@147
|
58 #if KJ_USE_PTHREAD_TLS
|
cannam@147
|
59 #include <pthread.h>
|
cannam@147
|
60 #endif
|
cannam@147
|
61
|
cannam@147
|
62 namespace kj {
|
cannam@147
|
63
|
cannam@147
|
64 #if KJ_USE_PTHREAD_TLS
|
cannam@147
|
65 // If __thread is unavailable, we'll fall back to pthreads.
|
cannam@147
|
66
|
cannam@147
|
67 #define KJ_THREADLOCAL_PTR(type) \
|
cannam@147
|
68 namespace { struct KJ_UNIQUE_NAME(_kj_TlpTag); } \
|
cannam@147
|
69 static ::kj::_::ThreadLocalPtr< type, KJ_UNIQUE_NAME(_kj_TlpTag)>
|
cannam@147
|
70 // Hack: In order to ensure each thread-local results in a unique template instance, we declare
|
cannam@147
|
71 // a one-off dummy type to use as the second type parameter.
|
cannam@147
|
72
|
cannam@147
|
73 namespace _ { // private
|
cannam@147
|
74
|
cannam@147
|
75 template <typename T, typename>
|
cannam@147
|
76 class ThreadLocalPtr {
|
cannam@147
|
77 // Hacky type to emulate __thread T*. We need a separate instance of the ThreadLocalPtr template
|
cannam@147
|
78 // for every thread-local variable, because we don't want to require a global constructor, and in
|
cannam@147
|
79 // order to initialize the TLS on first use we need to use a local static variable (in getKey()).
|
cannam@147
|
80 // Each template instance will get a separate such local static variable, fulfilling our need.
|
cannam@147
|
81
|
cannam@147
|
82 public:
|
cannam@147
|
83 ThreadLocalPtr() = default;
|
cannam@147
|
84 constexpr ThreadLocalPtr(decltype(nullptr)) {}
|
cannam@147
|
85 // Allow initialization to nullptr without a global constructor.
|
cannam@147
|
86
|
cannam@147
|
87 inline ThreadLocalPtr& operator=(T* val) {
|
cannam@147
|
88 pthread_setspecific(getKey(), val);
|
cannam@147
|
89 return *this;
|
cannam@147
|
90 }
|
cannam@147
|
91
|
cannam@147
|
92 inline operator T*() const {
|
cannam@147
|
93 return get();
|
cannam@147
|
94 }
|
cannam@147
|
95
|
cannam@147
|
96 inline T& operator*() const {
|
cannam@147
|
97 return *get();
|
cannam@147
|
98 }
|
cannam@147
|
99
|
cannam@147
|
100 inline T* operator->() const {
|
cannam@147
|
101 return get();
|
cannam@147
|
102 }
|
cannam@147
|
103
|
cannam@147
|
104 private:
|
cannam@147
|
105 inline T* get() const {
|
cannam@147
|
106 return reinterpret_cast<T*>(pthread_getspecific(getKey()));
|
cannam@147
|
107 }
|
cannam@147
|
108
|
cannam@147
|
109 inline static pthread_key_t getKey() {
|
cannam@147
|
110 static pthread_key_t key = createKey();
|
cannam@147
|
111 return key;
|
cannam@147
|
112 }
|
cannam@147
|
113
|
cannam@147
|
114 static pthread_key_t createKey() {
|
cannam@147
|
115 pthread_key_t key;
|
cannam@147
|
116 pthread_key_create(&key, 0);
|
cannam@147
|
117 return key;
|
cannam@147
|
118 }
|
cannam@147
|
119 };
|
cannam@147
|
120
|
cannam@147
|
121 } // namespace _ (private)
|
cannam@147
|
122
|
cannam@147
|
123 #elif __GNUC__
|
cannam@147
|
124
|
cannam@147
|
125 #define KJ_THREADLOCAL_PTR(type) static __thread type*
|
cannam@147
|
126 // GCC's __thread is lighter-weight than thread_local and is good enough for our purposes.
|
cannam@147
|
127
|
cannam@147
|
128 #else
|
cannam@147
|
129
|
cannam@147
|
130 #define KJ_THREADLOCAL_PTR(type) static thread_local type*
|
cannam@147
|
131
|
cannam@147
|
132 #endif // KJ_USE_PTHREAD_TLS
|
cannam@147
|
133
|
cannam@147
|
134 } // namespace kj
|
cannam@147
|
135
|
cannam@147
|
136 #endif // KJ_THREADLOCAL_H_
|