Version 0.12.0
[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
35#ifdef CONFIG_RCU_TLS /* Based on ax_tls.m4 */
36
c85e62f3
LJ
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 *
ffa11a18 47 * Another example:
c85e62f3
LJ
48 * typedef void (*call_rcu_flavor)(struct rcu_head *, XXXX);
49 * DECLARE_URCU_TLS(call_rcu_flavor, p_call_rcu);
fa320ad0
LJ
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.
9948a988
MD
62 *
63 * Moreover, URCU_TLS variables should not be touched from signal
64 * handlers setup with with sigaltstack(2).
c85e62f3
LJ
65 */
66
4d0d66bb 67# define DECLARE_URCU_TLS(type, name) \
3db1417f 68 CONFIG_RCU_TLS type name
4d0d66bb
MD
69
70# define DEFINE_URCU_TLS(type, name) \
3db1417f 71 CONFIG_RCU_TLS type name
4d0d66bb 72
cb1f3ebf
MD
73# define DEFINE_URCU_TLS_INIT(type, name, init) \
74 CONFIG_RCU_TLS type name = (init)
75
3db1417f 76# define URCU_TLS(name) (name)
4d0d66bb
MD
77
78#else /* #ifndef CONFIG_RCU_TLS */
79
ae62f05a
MD
80/*
81 * The *_1() macros ensure macro parameters are expanded.
82 */
83
4d0d66bb
MD
84# include <pthread.h>
85
86struct urcu_tls {
87 pthread_key_t key;
88 pthread_mutex_t init_mutex;
89 int init_done;
90};
91
ae62f05a 92# define DECLARE_URCU_TLS_1(type, name) \
4d0d66bb 93 type *__tls_access_ ## name(void)
ae62f05a
MD
94# define DECLARE_URCU_TLS(type, name) \
95 DECLARE_URCU_TLS_1(type, name)
4d0d66bb
MD
96
97/*
98 * Note: we don't free memory at process exit, since it will be dealt
99 * with by the OS.
100 */
cb1f3ebf 101# define DEFINE_URCU_TLS_INIT_1(type, name, do_init) \
4d0d66bb
MD
102 type *__tls_access_ ## name(void) \
103 { \
104 static struct urcu_tls __tls_ ## name = { \
105 .init_mutex = PTHREAD_MUTEX_INITIALIZER,\
106 .init_done = 0, \
107 }; \
cb1f3ebf 108 __typeof__(type) *__tls_p; \
4d0d66bb
MD
109 if (!__tls_ ## name.init_done) { \
110 /* Mutex to protect concurrent init */ \
111 pthread_mutex_lock(&__tls_ ## name.init_mutex); \
112 if (!__tls_ ## name.init_done) { \
113 (void) pthread_key_create(&__tls_ ## name.key, \
114 free); \
115 cmm_smp_wmb(); /* create key before write init_done */ \
116 __tls_ ## name.init_done = 1; \
117 } \
118 pthread_mutex_unlock(&__tls_ ## name.init_mutex); \
119 } \
120 cmm_smp_rmb(); /* read init_done before getting key */ \
121 __tls_p = pthread_getspecific(__tls_ ## name.key); \
122 if (caa_unlikely(__tls_p == NULL)) { \
123 __tls_p = calloc(1, sizeof(type)); \
cb1f3ebf 124 do_init \
4d0d66bb
MD
125 (void) pthread_setspecific(__tls_ ## name.key, \
126 __tls_p); \
127 } \
128 return __tls_p; \
129 }
130
cb1f3ebf
MD
131# define _URCU_TLS_INIT(init) \
132 *__tls_p = (init);
133
134# define DEFINE_URCU_TLS_INIT(type, name, init) \
135 DEFINE_URCU_TLS_INIT_1(type, name, _URCU_TLS_INIT(init))
136
ae62f05a 137# define DEFINE_URCU_TLS(type, name) \
cb1f3ebf 138 DEFINE_URCU_TLS_INIT_1(type, name, /* empty */)
ae62f05a
MD
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.0428269999999999 seconds and 4 git commands to generate.