annotate win64-msvc/include/kj/threadlocal.h @ 64:eccd51b72864

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