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