comparison win64-msvc/include/kj/threadlocal.h @ 47:d93140aac40b

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