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