threadlocal.h (2863B)
1 // Copyright (c) 2014, Jason Choy <jjwchoy@gmail.com> 2 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors 3 // Licensed under the MIT License: 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 #pragma once 24 25 // This file declares a macro `KJ_THREADLOCAL_PTR` for declaring thread-local pointer-typed 26 // variables. Use like: 27 // KJ_THREADLOCAL_PTR(MyType) foo = nullptr; 28 // This is equivalent to: 29 // thread_local MyType* foo = nullptr; 30 // This can only be used at the global scope. 31 // 32 // AVOID USING THIS. Use of thread-locals is discouraged because they often have many of the same 33 // properties as singletons: http://www.object-oriented-security.org/lets-argue/singletons 34 // 35 // Also, thread-locals tend to be hostile to event-driven code, which can be particularly 36 // surprising when using fibers (all fibers in the same thread will share the same threadlocals, 37 // even though they do not share a stack). 38 // 39 // That said, thread-locals are sometimes needed for runtime logistics in the KJ framework. For 40 // example, the current exception callback and current EventLoop are stored as thread-local 41 // pointers. Since KJ only ever needs to store pointers, not values, we avoid the question of 42 // whether these values' destructors need to be run, and we avoid the need for heap allocation. 43 44 #include "common.h" 45 46 KJ_BEGIN_HEADER 47 48 namespace kj { 49 50 #if __GNUC__ 51 52 #define KJ_THREADLOCAL_PTR(type) static __thread type* 53 // GCC's __thread is lighter-weight than thread_local and is good enough for our purposes. 54 // 55 // TODO(cleanup): The above comment was written many years ago. Is it still true? Shouldn't the 56 // compiler be smart enough to optimize a thread_local of POD type? 57 58 #else 59 60 #define KJ_THREADLOCAL_PTR(type) static thread_local type* 61 62 #endif // KJ_USE_PTHREAD_TLS 63 64 } // namespace kj 65 66 KJ_END_HEADER