Fix library compatibility
[urcu.git] / 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 /* Based on ax_tls.m4 */
36
37 # define DECLARE_URCU_TLS(type, name) \
38 CONFIG_RCU_TLS type name
39
40 # define DEFINE_URCU_TLS(type, name) \
41 CONFIG_RCU_TLS type name
42
43 # define URCU_TLS(name) (name)
44
45 #else /* #ifndef CONFIG_RCU_TLS */
46
47 # include <pthread.h>
48
49 struct urcu_tls {
50 pthread_key_t key;
51 pthread_mutex_t init_mutex;
52 int init_done;
53 };
54
55 # define DECLARE_URCU_TLS(type, name) \
56 type *__tls_access_ ## name(void)
57
58 /*
59 * Note: we don't free memory at process exit, since it will be dealt
60 * with by the OS.
61 */
62 # define DEFINE_URCU_TLS(type, name) \
63 type *__tls_access_ ## name(void) \
64 { \
65 static struct urcu_tls __tls_ ## name = { \
66 .init_mutex = PTHREAD_MUTEX_INITIALIZER,\
67 .init_done = 0, \
68 }; \
69 void *__tls_p; \
70 if (!__tls_ ## name.init_done) { \
71 /* Mutex to protect concurrent init */ \
72 pthread_mutex_lock(&__tls_ ## name.init_mutex); \
73 if (!__tls_ ## name.init_done) { \
74 (void) pthread_key_create(&__tls_ ## name.key, \
75 free); \
76 cmm_smp_wmb(); /* create key before write init_done */ \
77 __tls_ ## name.init_done = 1; \
78 } \
79 pthread_mutex_unlock(&__tls_ ## name.init_mutex); \
80 } \
81 cmm_smp_rmb(); /* read init_done before getting key */ \
82 __tls_p = pthread_getspecific(__tls_ ## name.key); \
83 if (caa_unlikely(__tls_p == NULL)) { \
84 __tls_p = calloc(1, sizeof(type)); \
85 (void) pthread_setspecific(__tls_ ## name.key, \
86 __tls_p); \
87 } \
88 return __tls_p; \
89 }
90
91 # define URCU_TLS(name) (*__tls_access_ ## name())
92
93 #endif /* #else #ifndef CONFIG_RCU_TLS */
94
95 #ifdef __cplusplus
96 }
97 #endif
98
99 #endif /* _URCU_TLS_COMPAT_H */
This page took 0.03308 seconds and 4 git commands to generate.