Version 0.7.9
[urcu.git] / urcu / tls-compat.h
1 #ifndef _URCU_TLS_COMPAT_H
2 #define _URCU_TLS_COMPAT_H
3
4 /*
5 * urcu/tls-compat.h
6 *
7 * Userspace RCU library - Thread-Local Storage Compatibility Header
8 *
9 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <stdlib.h>
27 #include <urcu/config.h>
28 #include <urcu/compiler.h>
29 #include <urcu/arch.h>
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 #ifdef CONFIG_RCU_TLS /* Based on ax_tls.m4 */
36
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 *
47 * Another exmaple:
48 * typedef void (*call_rcu_flavor)(struct rcu_head *, XXXX);
49 * DECLARE_URCU_TLS(call_rcu_flavor, p_call_rcu);
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.
62 *
63 * Moreover, URCU_TLS variables should not be touched from signal
64 * handlers setup with with sigaltstack(2).
65 */
66
67 # define DECLARE_URCU_TLS(type, name) \
68 CONFIG_RCU_TLS type name
69
70 # define DEFINE_URCU_TLS(type, name) \
71 CONFIG_RCU_TLS type name
72
73 # define __DEFINE_URCU_TLS_GLOBAL(type, name) \
74 CONFIG_RCU_TLS type name
75
76 # define URCU_TLS(name) (name)
77
78 #else /* #ifndef CONFIG_RCU_TLS */
79
80 /*
81 * The *_1() macros ensure macro parameters are expanded.
82 *
83 * __DEFINE_URCU_TLS_GLOBAL and __URCU_TLS_CALL exist for the sole
84 * purpose of notifying applications compiled against non-fixed 0.7 and
85 * 0.8 userspace RCU headers and using multiple flavors concurrently to
86 * recompile against fixed userspace RCU headers.
87 */
88
89 # include <pthread.h>
90
91 struct urcu_tls {
92 pthread_key_t key;
93 pthread_mutex_t init_mutex;
94 int init_done;
95 };
96
97 # define DECLARE_URCU_TLS_1(type, name) \
98 type *__tls_access2_ ## name(void)
99
100 # define DECLARE_URCU_TLS(type, name) \
101 DECLARE_URCU_TLS_1(type, name)
102
103 /*
104 * Note: we don't free memory at process exit, since it will be dealt
105 * with by the OS.
106 */
107 # define __URCU_TLS_CALL_1(name) \
108 __tls_access2_ ## name
109
110 # define __URCU_TLS_CALL(name) \
111 __URCU_TLS_CALL_1(name)
112
113 # define DEFINE_URCU_TLS_1(type, name) \
114 type *__tls_access2_ ## name(void) \
115 { \
116 static struct urcu_tls __tls_ ## name = { \
117 .init_mutex = PTHREAD_MUTEX_INITIALIZER,\
118 .init_done = 0, \
119 }; \
120 void *__tls_p; \
121 if (!__tls_ ## name.init_done) { \
122 /* Mutex to protect concurrent init */ \
123 pthread_mutex_lock(&__tls_ ## name.init_mutex); \
124 if (!__tls_ ## name.init_done) { \
125 (void) pthread_key_create(&__tls_ ## name.key, \
126 free); \
127 cmm_smp_wmb(); /* create key before write init_done */ \
128 __tls_ ## name.init_done = 1; \
129 } \
130 pthread_mutex_unlock(&__tls_ ## name.init_mutex); \
131 } \
132 cmm_smp_rmb(); /* read init_done before getting key */ \
133 __tls_p = pthread_getspecific(__tls_ ## name.key); \
134 if (caa_unlikely(__tls_p == NULL)) { \
135 __tls_p = calloc(1, sizeof(type)); \
136 (void) pthread_setspecific(__tls_ ## name.key, \
137 __tls_p); \
138 } \
139 return __tls_p; \
140 }
141
142 /*
143 * Define with and without macro expansion to handle erroneous callers.
144 * Trigger an abort() if the caller application uses the clashing symbol
145 * if a weak symbol is overridden.
146 */
147 # define __DEFINE_URCU_TLS_GLOBAL(type, name) \
148 DEFINE_URCU_TLS_1(type, name) \
149 int __urcu_tls_symbol_refcount_ ## name __attribute__((weak)); \
150 static __attribute__((constructor)) \
151 void __urcu_tls_inc_refcount_ ## name(void) \
152 { \
153 __urcu_tls_symbol_refcount_ ## name++; \
154 } \
155 type *__tls_access_ ## name(void) \
156 { \
157 if (__urcu_tls_symbol_refcount_ ## name > 1) { \
158 fprintf(stderr, "Error: Userspace RCU symbol clash for multiple concurrent flavors. Please upgrade liburcu libraries and headers, then recompile your application.\n"); \
159 abort(); \
160 } \
161 return __URCU_TLS_CALL(name)(); \
162 }
163
164 # define DEFINE_URCU_TLS(type, name) \
165 DEFINE_URCU_TLS_1(type, name)
166
167 # define URCU_TLS_1(name) (*__tls_access2_ ## name())
168
169 # define URCU_TLS(name) URCU_TLS_1(name)
170
171 #endif /* #else #ifndef CONFIG_RCU_TLS */
172
173 #ifdef __cplusplus
174 }
175 #endif
176
177 #endif /* _URCU_TLS_COMPAT_H */
This page took 0.033345 seconds and 4 git commands to generate.