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