| 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 | /* |
| 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 | * |
| 47 | * Another exmaple: |
| 48 | * typedef void (*call_rcu_flavor)(struct rcu_head *, XXXX); |
| 49 | * DECLARE_URCU_TLS(call_rcu_flavor, p_call_rcu); |
| 50 | */ |
| 51 | |
| 52 | # define DECLARE_URCU_TLS(type, name) \ |
| 53 | CONFIG_RCU_TLS type name |
| 54 | |
| 55 | # define DEFINE_URCU_TLS(type, name) \ |
| 56 | CONFIG_RCU_TLS type name |
| 57 | |
| 58 | # define URCU_TLS(name) (name) |
| 59 | |
| 60 | #else /* #ifndef CONFIG_RCU_TLS */ |
| 61 | |
| 62 | # include <pthread.h> |
| 63 | |
| 64 | struct urcu_tls { |
| 65 | pthread_key_t key; |
| 66 | pthread_mutex_t init_mutex; |
| 67 | int init_done; |
| 68 | }; |
| 69 | |
| 70 | # define DECLARE_URCU_TLS(type, name) \ |
| 71 | type *__tls_access_ ## name(void) |
| 72 | |
| 73 | /* |
| 74 | * Note: we don't free memory at process exit, since it will be dealt |
| 75 | * with by the OS. |
| 76 | */ |
| 77 | # define DEFINE_URCU_TLS(type, name) \ |
| 78 | type *__tls_access_ ## name(void) \ |
| 79 | { \ |
| 80 | static struct urcu_tls __tls_ ## name = { \ |
| 81 | .init_mutex = PTHREAD_MUTEX_INITIALIZER,\ |
| 82 | .init_done = 0, \ |
| 83 | }; \ |
| 84 | void *__tls_p; \ |
| 85 | if (!__tls_ ## name.init_done) { \ |
| 86 | /* Mutex to protect concurrent init */ \ |
| 87 | pthread_mutex_lock(&__tls_ ## name.init_mutex); \ |
| 88 | if (!__tls_ ## name.init_done) { \ |
| 89 | (void) pthread_key_create(&__tls_ ## name.key, \ |
| 90 | free); \ |
| 91 | cmm_smp_wmb(); /* create key before write init_done */ \ |
| 92 | __tls_ ## name.init_done = 1; \ |
| 93 | } \ |
| 94 | pthread_mutex_unlock(&__tls_ ## name.init_mutex); \ |
| 95 | } \ |
| 96 | cmm_smp_rmb(); /* read init_done before getting key */ \ |
| 97 | __tls_p = pthread_getspecific(__tls_ ## name.key); \ |
| 98 | if (caa_unlikely(__tls_p == NULL)) { \ |
| 99 | __tls_p = calloc(1, sizeof(type)); \ |
| 100 | (void) pthread_setspecific(__tls_ ## name.key, \ |
| 101 | __tls_p); \ |
| 102 | } \ |
| 103 | return __tls_p; \ |
| 104 | } |
| 105 | |
| 106 | # define URCU_TLS(name) (*__tls_access_ ## name()) |
| 107 | |
| 108 | #endif /* #else #ifndef CONFIG_RCU_TLS */ |
| 109 | |
| 110 | #ifdef __cplusplus |
| 111 | } |
| 112 | #endif |
| 113 | |
| 114 | #endif /* _URCU_TLS_COMPAT_H */ |