annotate win64-msvc/include/kj/threadlocal.h @ 166:cbd6d7e562c7

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