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