annotate win32-mingw/include/kj/threadlocal.h @ 150:0a1a4a299a5d

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