annotate win32-mingw/include/kj/threadlocal.h @ 52:44a948c37b77

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