Commit | Line | Data |
---|---|---|
d3d3857f MJ |
1 | // SPDX-FileCopyrightText: 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2 | // | |
3 | // SPDX-License-Identifier: LGPL-2.1-or-later | |
4 | ||
4d0d66bb MD |
5 | #ifndef _URCU_TLS_COMPAT_H |
6 | #define _URCU_TLS_COMPAT_H | |
7 | ||
8 | /* | |
4d0d66bb | 9 | * Userspace RCU library - Thread-Local Storage Compatibility Header |
4d0d66bb MD |
10 | */ |
11 | ||
12 | #include <stdlib.h> | |
13 | #include <urcu/config.h> | |
14 | #include <urcu/compiler.h> | |
15 | #include <urcu/arch.h> | |
16 | ||
17 | #ifdef __cplusplus | |
18 | extern "C" { | |
19 | #endif | |
20 | ||
109267f6 MD |
21 | #ifdef CONFIG_RCU_TLS |
22 | ||
e915ab84 | 23 | /* |
2e359284 MJ |
24 | * Default to '__thread' on all C and C++ compilers except MSVC. While C11 has |
25 | * '_Thread_local' and C++11 has 'thread_local', only '__thread' seems to have | |
26 | * a compatible implementation when linking public extern symbols across | |
27 | * language boundaries. | |
28 | * | |
29 | * For more details, see 'https://gcc.gnu.org/onlinedocs/gcc/Thread-Local.html'. | |
e915ab84 | 30 | */ |
2e359284 | 31 | #if defined(_MSC_VER) |
109267f6 MD |
32 | # define URCU_TLS_STORAGE_CLASS __declspec(thread) |
33 | #else | |
34 | # define URCU_TLS_STORAGE_CLASS __thread | |
35 | #endif | |
4d0d66bb | 36 | |
c85e62f3 LJ |
37 | /* |
38 | * Hint: How to define/declare TLS variables of compound types | |
39 | * such as array or function pointers? | |
40 | * | |
41 | * Answer: Use typedef to assign a type_name to the compound type. | |
42 | * Example: Define a TLS variable which is an int array with len=4: | |
43 | * | |
44 | * typedef int my_int_array_type[4]; | |
45 | * DEFINE_URCU_TLS(my_int_array_type, var_name); | |
46 | * | |
ffa11a18 | 47 | * Another example: |
c85e62f3 LJ |
48 | * typedef void (*call_rcu_flavor)(struct rcu_head *, XXXX); |
49 | * DECLARE_URCU_TLS(call_rcu_flavor, p_call_rcu); | |
fa320ad0 LJ |
50 | * |
51 | * NOTE: URCU_TLS() is NOT async-signal-safe, you can't use it | |
52 | * inside any function which can be called from signal handler. | |
53 | * | |
54 | * But if pthread_getspecific() is async-signal-safe in your | |
55 | * platform, you can make URCU_TLS() async-signal-safe via: | |
56 | * ensuring the first call to URCU_TLS() of a given TLS variable of | |
57 | * all threads is called earliest from a non-signal handler function. | |
58 | * | |
59 | * Example: In any thread, the first call of URCU_TLS(rcu_reader) | |
60 | * is called from rcu_register_thread(), so we can ensure all later | |
61 | * URCU_TLS(rcu_reader) in any thread is async-signal-safe. | |
9948a988 MD |
62 | * |
63 | * Moreover, URCU_TLS variables should not be touched from signal | |
64 | * handlers setup with with sigaltstack(2). | |
c85e62f3 LJ |
65 | */ |
66 | ||
4d0d66bb | 67 | # define DECLARE_URCU_TLS(type, name) \ |
109267f6 | 68 | URCU_TLS_STORAGE_CLASS type name |
4d0d66bb MD |
69 | |
70 | # define DEFINE_URCU_TLS(type, name) \ | |
109267f6 | 71 | URCU_TLS_STORAGE_CLASS type name |
4d0d66bb | 72 | |
cb1f3ebf | 73 | # define DEFINE_URCU_TLS_INIT(type, name, init) \ |
109267f6 | 74 | URCU_TLS_STORAGE_CLASS type name = (init) |
cb1f3ebf | 75 | |
3db1417f | 76 | # define URCU_TLS(name) (name) |
4d0d66bb MD |
77 | |
78 | #else /* #ifndef CONFIG_RCU_TLS */ | |
79 | ||
ae62f05a MD |
80 | /* |
81 | * The *_1() macros ensure macro parameters are expanded. | |
82 | */ | |
83 | ||
4d0d66bb MD |
84 | # include <pthread.h> |
85 | ||
86 | struct urcu_tls { | |
87 | pthread_key_t key; | |
88 | pthread_mutex_t init_mutex; | |
89 | int init_done; | |
90 | }; | |
91 | ||
ae62f05a | 92 | # define DECLARE_URCU_TLS_1(type, name) \ |
4d0d66bb | 93 | type *__tls_access_ ## name(void) |
ae62f05a MD |
94 | # define DECLARE_URCU_TLS(type, name) \ |
95 | DECLARE_URCU_TLS_1(type, name) | |
4d0d66bb MD |
96 | |
97 | /* | |
98 | * Note: we don't free memory at process exit, since it will be dealt | |
99 | * with by the OS. | |
100 | */ | |
cb1f3ebf | 101 | # define DEFINE_URCU_TLS_INIT_1(type, name, do_init) \ |
4d0d66bb MD |
102 | type *__tls_access_ ## name(void) \ |
103 | { \ | |
104 | static struct urcu_tls __tls_ ## name = { \ | |
69fbb39e | 105 | .key = 0, \ |
4d0d66bb MD |
106 | .init_mutex = PTHREAD_MUTEX_INITIALIZER,\ |
107 | .init_done = 0, \ | |
108 | }; \ | |
cb1f3ebf | 109 | __typeof__(type) *__tls_p; \ |
4d0d66bb MD |
110 | if (!__tls_ ## name.init_done) { \ |
111 | /* Mutex to protect concurrent init */ \ | |
112 | pthread_mutex_lock(&__tls_ ## name.init_mutex); \ | |
113 | if (!__tls_ ## name.init_done) { \ | |
114 | (void) pthread_key_create(&__tls_ ## name.key, \ | |
115 | free); \ | |
116 | cmm_smp_wmb(); /* create key before write init_done */ \ | |
117 | __tls_ ## name.init_done = 1; \ | |
118 | } \ | |
119 | pthread_mutex_unlock(&__tls_ ## name.init_mutex); \ | |
120 | } \ | |
121 | cmm_smp_rmb(); /* read init_done before getting key */ \ | |
69fbb39e | 122 | __tls_p = (__typeof__(type) *) pthread_getspecific(__tls_ ## name.key); \ |
4d0d66bb | 123 | if (caa_unlikely(__tls_p == NULL)) { \ |
69fbb39e | 124 | __tls_p = (__typeof__(type) *) calloc(1, sizeof(type)); \ |
cb1f3ebf | 125 | do_init \ |
4d0d66bb MD |
126 | (void) pthread_setspecific(__tls_ ## name.key, \ |
127 | __tls_p); \ | |
128 | } \ | |
129 | return __tls_p; \ | |
130 | } | |
131 | ||
cb1f3ebf MD |
132 | # define _URCU_TLS_INIT(init) \ |
133 | *__tls_p = (init); | |
134 | ||
135 | # define DEFINE_URCU_TLS_INIT(type, name, init) \ | |
136 | DEFINE_URCU_TLS_INIT_1(type, name, _URCU_TLS_INIT(init)) | |
137 | ||
ae62f05a | 138 | # define DEFINE_URCU_TLS(type, name) \ |
cb1f3ebf | 139 | DEFINE_URCU_TLS_INIT_1(type, name, /* empty */) |
ae62f05a MD |
140 | |
141 | # define URCU_TLS_1(name) (*__tls_access_ ## name()) | |
142 | ||
143 | # define URCU_TLS(name) URCU_TLS_1(name) | |
4d0d66bb MD |
144 | |
145 | #endif /* #else #ifndef CONFIG_RCU_TLS */ | |
146 | ||
147 | #ifdef __cplusplus | |
148 | } | |
149 | #endif | |
150 | ||
151 | #endif /* _URCU_TLS_COMPAT_H */ |