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