fix: don't use C++ thread_local on MacOs
[urcu.git] / include / 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
36
37 /*
38 * Don't use C++ 'thread_local' on MacOs, the implementation is incompatible
39 * with C and will result in a link error when accessing an extern variable
40 * provided by the C library from C++ code.
41 */
42 #if defined (__cplusplus) && (__cplusplus >= 201103L) && !defined(__APPLE__)
43 # define URCU_TLS_STORAGE_CLASS thread_local
44 #elif defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
45 # define URCU_TLS_STORAGE_CLASS _Thread_local
46 #elif defined (_MSC_VER)
47 # define URCU_TLS_STORAGE_CLASS __declspec(thread)
48 #else
49 # define URCU_TLS_STORAGE_CLASS __thread
50 #endif
51
52 /*
53 * Hint: How to define/declare TLS variables of compound types
54 * such as array or function pointers?
55 *
56 * Answer: Use typedef to assign a type_name to the compound type.
57 * Example: Define a TLS variable which is an int array with len=4:
58 *
59 * typedef int my_int_array_type[4];
60 * DEFINE_URCU_TLS(my_int_array_type, var_name);
61 *
62 * Another example:
63 * typedef void (*call_rcu_flavor)(struct rcu_head *, XXXX);
64 * DECLARE_URCU_TLS(call_rcu_flavor, p_call_rcu);
65 *
66 * NOTE: URCU_TLS() is NOT async-signal-safe, you can't use it
67 * inside any function which can be called from signal handler.
68 *
69 * But if pthread_getspecific() is async-signal-safe in your
70 * platform, you can make URCU_TLS() async-signal-safe via:
71 * ensuring the first call to URCU_TLS() of a given TLS variable of
72 * all threads is called earliest from a non-signal handler function.
73 *
74 * Example: In any thread, the first call of URCU_TLS(rcu_reader)
75 * is called from rcu_register_thread(), so we can ensure all later
76 * URCU_TLS(rcu_reader) in any thread is async-signal-safe.
77 *
78 * Moreover, URCU_TLS variables should not be touched from signal
79 * handlers setup with with sigaltstack(2).
80 */
81
82 # define DECLARE_URCU_TLS(type, name) \
83 URCU_TLS_STORAGE_CLASS type name
84
85 # define DEFINE_URCU_TLS(type, name) \
86 URCU_TLS_STORAGE_CLASS type name
87
88 # define DEFINE_URCU_TLS_INIT(type, name, init) \
89 URCU_TLS_STORAGE_CLASS type name = (init)
90
91 # define URCU_TLS(name) (name)
92
93 #else /* #ifndef CONFIG_RCU_TLS */
94
95 /*
96 * The *_1() macros ensure macro parameters are expanded.
97 */
98
99 # include <pthread.h>
100
101 struct urcu_tls {
102 pthread_key_t key;
103 pthread_mutex_t init_mutex;
104 int init_done;
105 };
106
107 # define DECLARE_URCU_TLS_1(type, name) \
108 type *__tls_access_ ## name(void)
109 # define DECLARE_URCU_TLS(type, name) \
110 DECLARE_URCU_TLS_1(type, name)
111
112 /*
113 * Note: we don't free memory at process exit, since it will be dealt
114 * with by the OS.
115 */
116 # define DEFINE_URCU_TLS_INIT_1(type, name, do_init) \
117 type *__tls_access_ ## name(void) \
118 { \
119 static struct urcu_tls __tls_ ## name = { \
120 .init_mutex = PTHREAD_MUTEX_INITIALIZER,\
121 .init_done = 0, \
122 }; \
123 __typeof__(type) *__tls_p; \
124 if (!__tls_ ## name.init_done) { \
125 /* Mutex to protect concurrent init */ \
126 pthread_mutex_lock(&__tls_ ## name.init_mutex); \
127 if (!__tls_ ## name.init_done) { \
128 (void) pthread_key_create(&__tls_ ## name.key, \
129 free); \
130 cmm_smp_wmb(); /* create key before write init_done */ \
131 __tls_ ## name.init_done = 1; \
132 } \
133 pthread_mutex_unlock(&__tls_ ## name.init_mutex); \
134 } \
135 cmm_smp_rmb(); /* read init_done before getting key */ \
136 __tls_p = pthread_getspecific(__tls_ ## name.key); \
137 if (caa_unlikely(__tls_p == NULL)) { \
138 __tls_p = calloc(1, sizeof(type)); \
139 do_init \
140 (void) pthread_setspecific(__tls_ ## name.key, \
141 __tls_p); \
142 } \
143 return __tls_p; \
144 }
145
146 # define _URCU_TLS_INIT(init) \
147 *__tls_p = (init);
148
149 # define DEFINE_URCU_TLS_INIT(type, name, init) \
150 DEFINE_URCU_TLS_INIT_1(type, name, _URCU_TLS_INIT(init))
151
152 # define DEFINE_URCU_TLS(type, name) \
153 DEFINE_URCU_TLS_INIT_1(type, name, /* empty */)
154
155 # define URCU_TLS_1(name) (*__tls_access_ ## name())
156
157 # define URCU_TLS(name) URCU_TLS_1(name)
158
159 #endif /* #else #ifndef CONFIG_RCU_TLS */
160
161 #ifdef __cplusplus
162 }
163 #endif
164
165 #endif /* _URCU_TLS_COMPAT_H */
This page took 0.032893 seconds and 5 git commands to generate.