1 #ifndef _URCU_TLS_COMPAT_H
2 #define _URCU_TLS_COMPAT_H
7 * Userspace RCU library - Thread-Local Storage Compatibility Header
9 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
27 #include <urcu/config.h>
28 #include <urcu/compiler.h>
29 #include <urcu/arch.h>
35 #ifdef CONFIG_RCU_TLS /* Based on ax_tls.m4 */
38 * Hint: How to define/declare TLS variables of compound types
39 * such as array or function pointers?
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:
44 * typedef int my_int_array_type[4];
45 * DEFINE_URCU_TLS(my_int_array_type, var_name);
48 * typedef void (*call_rcu_flavor)(struct rcu_head *, XXXX);
49 * DECLARE_URCU_TLS(call_rcu_flavor, p_call_rcu);
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.
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.
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.
63 * Moreover, URCU_TLS variables should not be touched from signal
64 * handlers setup with with sigaltstack(2).
67 # define DECLARE_URCU_TLS(type, name) \
68 CONFIG_RCU_TLS type name
70 # define DEFINE_URCU_TLS(type, name) \
71 CONFIG_RCU_TLS type name
73 # define URCU_TLS(name) (name)
75 #else /* #ifndef CONFIG_RCU_TLS */
81 pthread_mutex_t init_mutex
;
85 # define DECLARE_URCU_TLS(type, name) \
86 type *__tls_access_ ## name(void)
89 * Note: we don't free memory at process exit, since it will be dealt
92 # define DEFINE_URCU_TLS(type, name) \
93 type *__tls_access_ ## name(void) \
95 static struct urcu_tls __tls_ ## name = { \
96 .init_mutex = PTHREAD_MUTEX_INITIALIZER,\
100 if (!__tls_ ## name.init_done) { \
101 /* Mutex to protect concurrent init */ \
102 pthread_mutex_lock(&__tls_ ## name.init_mutex); \
103 if (!__tls_ ## name.init_done) { \
104 (void) pthread_key_create(&__tls_ ## name.key, \
106 cmm_smp_wmb(); /* create key before write init_done */ \
107 __tls_ ## name.init_done = 1; \
109 pthread_mutex_unlock(&__tls_ ## name.init_mutex); \
111 cmm_smp_rmb(); /* read init_done before getting key */ \
112 __tls_p = pthread_getspecific(__tls_ ## name.key); \
113 if (caa_unlikely(__tls_p == NULL)) { \
114 __tls_p = calloc(1, sizeof(type)); \
115 (void) pthread_setspecific(__tls_ ## name.key, \
121 # define URCU_TLS(name) (*__tls_access_ ## name())
123 #endif /* #else #ifndef CONFIG_RCU_TLS */
129 #endif /* _URCU_TLS_COMPAT_H */