jpayne@69: // Copyright (c) 2014, Jason Choy jpayne@69: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors jpayne@69: // Licensed under the MIT License: jpayne@69: // jpayne@69: // Permission is hereby granted, free of charge, to any person obtaining a copy jpayne@69: // of this software and associated documentation files (the "Software"), to deal jpayne@69: // in the Software without restriction, including without limitation the rights jpayne@69: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell jpayne@69: // copies of the Software, and to permit persons to whom the Software is jpayne@69: // furnished to do so, subject to the following conditions: jpayne@69: // jpayne@69: // The above copyright notice and this permission notice shall be included in jpayne@69: // all copies or substantial portions of the Software. jpayne@69: // jpayne@69: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR jpayne@69: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, jpayne@69: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE jpayne@69: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER jpayne@69: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, jpayne@69: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN jpayne@69: // THE SOFTWARE. jpayne@69: jpayne@69: #pragma once jpayne@69: jpayne@69: // This file declares a macro `KJ_THREADLOCAL_PTR` for declaring thread-local pointer-typed jpayne@69: // variables. Use like: jpayne@69: // KJ_THREADLOCAL_PTR(MyType) foo = nullptr; jpayne@69: // This is equivalent to: jpayne@69: // thread_local MyType* foo = nullptr; jpayne@69: // This can only be used at the global scope. jpayne@69: // jpayne@69: // AVOID USING THIS. Use of thread-locals is discouraged because they often have many of the same jpayne@69: // properties as singletons: http://www.object-oriented-security.org/lets-argue/singletons jpayne@69: // jpayne@69: // Also, thread-locals tend to be hostile to event-driven code, which can be particularly jpayne@69: // surprising when using fibers (all fibers in the same thread will share the same threadlocals, jpayne@69: // even though they do not share a stack). jpayne@69: // jpayne@69: // That said, thread-locals are sometimes needed for runtime logistics in the KJ framework. For jpayne@69: // example, the current exception callback and current EventLoop are stored as thread-local jpayne@69: // pointers. Since KJ only ever needs to store pointers, not values, we avoid the question of jpayne@69: // whether these values' destructors need to be run, and we avoid the need for heap allocation. jpayne@69: jpayne@69: #include "common.h" jpayne@69: jpayne@69: KJ_BEGIN_HEADER jpayne@69: jpayne@69: namespace kj { jpayne@69: jpayne@69: #if __GNUC__ jpayne@69: jpayne@69: #define KJ_THREADLOCAL_PTR(type) static __thread type* jpayne@69: // GCC's __thread is lighter-weight than thread_local and is good enough for our purposes. jpayne@69: // jpayne@69: // TODO(cleanup): The above comment was written many years ago. Is it still true? Shouldn't the jpayne@69: // compiler be smart enough to optimize a thread_local of POD type? jpayne@69: jpayne@69: #else jpayne@69: jpayne@69: #define KJ_THREADLOCAL_PTR(type) static thread_local type* jpayne@69: jpayne@69: #endif // KJ_USE_PTHREAD_TLS jpayne@69: jpayne@69: } // namespace kj jpayne@69: jpayne@69: KJ_END_HEADER