cannam@62: // Copyright (c) 2014, Jason Choy cannam@62: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@62: // Licensed under the MIT License: cannam@62: // cannam@62: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@62: // of this software and associated documentation files (the "Software"), to deal cannam@62: // in the Software without restriction, including without limitation the rights cannam@62: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@62: // copies of the Software, and to permit persons to whom the Software is cannam@62: // furnished to do so, subject to the following conditions: cannam@62: // cannam@62: // The above copyright notice and this permission notice shall be included in cannam@62: // all copies or substantial portions of the Software. cannam@62: // cannam@62: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@62: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@62: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@62: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@62: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@62: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@62: // THE SOFTWARE. cannam@62: cannam@62: #ifndef KJ_THREADLOCAL_H_ cannam@62: #define KJ_THREADLOCAL_H_ cannam@62: cannam@62: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS cannam@62: #pragma GCC system_header cannam@62: #endif cannam@62: // This file declares a macro `KJ_THREADLOCAL_PTR` for declaring thread-local pointer-typed cannam@62: // variables. Use like: cannam@62: // KJ_THREADLOCAL_PTR(MyType) foo = nullptr; cannam@62: // This is equivalent to: cannam@62: // thread_local MyType* foo = nullptr; cannam@62: // This can only be used at the global scope. cannam@62: // cannam@62: // AVOID USING THIS. Use of thread-locals is discouraged because they often have many of the same cannam@62: // properties as singletons: http://www.object-oriented-security.org/lets-argue/singletons cannam@62: // cannam@62: // Also, thread-locals tend to be hostile to event-driven code, which can be particularly cannam@62: // surprising when using fibers (all fibers in the same thread will share the same threadlocals, cannam@62: // even though they do not share a stack). cannam@62: // cannam@62: // That said, thread-locals are sometimes needed for runtime logistics in the KJ framework. For cannam@62: // example, the current exception callback and current EventLoop are stored as thread-local cannam@62: // pointers. Since KJ only ever needs to store pointers, not values, we avoid the question of cannam@62: // whether these values' destructors need to be run, and we avoid the need for heap allocation. cannam@62: cannam@62: #include "common.h" cannam@62: cannam@62: #if !defined(KJ_USE_PTHREAD_THREADLOCAL) && defined(__APPLE__) cannam@62: #include "TargetConditionals.h" cannam@62: #if TARGET_OS_IPHONE cannam@62: // iOS apparently does not support __thread (nor C++11 thread_local). cannam@62: #define KJ_USE_PTHREAD_TLS 1 cannam@62: #endif cannam@62: #endif cannam@62: cannam@62: #if KJ_USE_PTHREAD_TLS cannam@62: #include cannam@62: #endif cannam@62: cannam@62: namespace kj { cannam@62: cannam@62: #if KJ_USE_PTHREAD_TLS cannam@62: // If __thread is unavailable, we'll fall back to pthreads. cannam@62: cannam@62: #define KJ_THREADLOCAL_PTR(type) \ cannam@62: namespace { struct KJ_UNIQUE_NAME(_kj_TlpTag); } \ cannam@62: static ::kj::_::ThreadLocalPtr< type, KJ_UNIQUE_NAME(_kj_TlpTag)> cannam@62: // Hack: In order to ensure each thread-local results in a unique template instance, we declare cannam@62: // a one-off dummy type to use as the second type parameter. cannam@62: cannam@62: namespace _ { // private cannam@62: cannam@62: template cannam@62: class ThreadLocalPtr { cannam@62: // Hacky type to emulate __thread T*. We need a separate instance of the ThreadLocalPtr template cannam@62: // for every thread-local variable, because we don't want to require a global constructor, and in cannam@62: // order to initialize the TLS on first use we need to use a local static variable (in getKey()). cannam@62: // Each template instance will get a separate such local static variable, fulfilling our need. cannam@62: cannam@62: public: cannam@62: ThreadLocalPtr() = default; cannam@62: constexpr ThreadLocalPtr(decltype(nullptr)) {} cannam@62: // Allow initialization to nullptr without a global constructor. cannam@62: cannam@62: inline ThreadLocalPtr& operator=(T* val) { cannam@62: pthread_setspecific(getKey(), val); cannam@62: return *this; cannam@62: } cannam@62: cannam@62: inline operator T*() const { cannam@62: return get(); cannam@62: } cannam@62: cannam@62: inline T& operator*() const { cannam@62: return *get(); cannam@62: } cannam@62: cannam@62: inline T* operator->() const { cannam@62: return get(); cannam@62: } cannam@62: cannam@62: private: cannam@62: inline T* get() const { cannam@62: return reinterpret_cast(pthread_getspecific(getKey())); cannam@62: } cannam@62: cannam@62: inline static pthread_key_t getKey() { cannam@62: static pthread_key_t key = createKey(); cannam@62: return key; cannam@62: } cannam@62: cannam@62: static pthread_key_t createKey() { cannam@62: pthread_key_t key; cannam@62: pthread_key_create(&key, 0); cannam@62: return key; cannam@62: } cannam@62: }; cannam@62: cannam@62: } // namespace _ (private) cannam@62: cannam@62: #elif __GNUC__ cannam@62: cannam@62: #define KJ_THREADLOCAL_PTR(type) static __thread type* cannam@62: // GCC's __thread is lighter-weight than thread_local and is good enough for our purposes. cannam@62: cannam@62: #else cannam@62: cannam@62: #define KJ_THREADLOCAL_PTR(type) static thread_local type* cannam@62: cannam@62: #endif // KJ_USE_PTHREAD_TLS cannam@62: cannam@62: } // namespace kj cannam@62: cannam@62: #endif // KJ_THREADLOCAL_H_