fix: use urcu-tls compat with c++ compiler
[urcu.git] / include / urcu / tls-compat.h
index 8ac1ea0615ddc2eb91ee51e9e15b89bd094d4c81..431319d608518af222045f9467dfb8081dfaaa8c 100644 (file)
 extern "C" {
 #endif
 
-#ifdef CONFIG_RCU_TLS  /* Based on ax_tls.m4 */
+#ifdef CONFIG_RCU_TLS
+
+/*
+ * Default to '__thread' on all C and C++ compilers except MSVC. While C11 has
+ * '_Thread_local' and C++11 has 'thread_local', only '__thread' seems to have
+ * a compatible implementation when linking public extern symbols across
+ * language boundaries.
+ *
+ * For more details, see 'https://gcc.gnu.org/onlinedocs/gcc/Thread-Local.html'.
+ */
+#if defined(_MSC_VER)
+# define URCU_TLS_STORAGE_CLASS        __declspec(thread)
+#else
+# define URCU_TLS_STORAGE_CLASS        __thread
+#endif
 
 /*
  * Hint: How to define/declare TLS variables of compound types
@@ -65,10 +79,13 @@ extern "C" {
  */
 
 # define DECLARE_URCU_TLS(type, name)  \
-       CONFIG_RCU_TLS type name
+       URCU_TLS_STORAGE_CLASS type name
 
 # define DEFINE_URCU_TLS(type, name)   \
-       CONFIG_RCU_TLS type name
+       URCU_TLS_STORAGE_CLASS type name
+
+# define DEFINE_URCU_TLS_INIT(type, name, init)        \
+       URCU_TLS_STORAGE_CLASS type name = (init)
 
 # define URCU_TLS(name)                (name)
 
@@ -95,14 +112,15 @@ struct urcu_tls {
  * Note: we don't free memory at process exit, since it will be dealt
  * with by the OS.
  */
-# define DEFINE_URCU_TLS_1(type, name)                         \
+# define DEFINE_URCU_TLS_INIT_1(type, name, do_init)           \
        type *__tls_access_ ## name(void)                       \
        {                                                       \
                static struct urcu_tls __tls_ ## name = {       \
+                       .key = 0,                               \
                        .init_mutex = PTHREAD_MUTEX_INITIALIZER,\
                        .init_done = 0,                         \
                };                                              \
-               void *__tls_p;                                  \
+               __typeof__(type) *__tls_p;                      \
                if (!__tls_ ## name.init_done) {                \
                        /* Mutex to protect concurrent init */  \
                        pthread_mutex_lock(&__tls_ ## name.init_mutex); \
@@ -115,17 +133,24 @@ struct urcu_tls {
                        pthread_mutex_unlock(&__tls_ ## name.init_mutex); \
                }                                               \
                cmm_smp_rmb();  /* read init_done before getting key */ \
-               __tls_p = pthread_getspecific(__tls_ ## name.key); \
+               __tls_p = (__typeof__(type) *) pthread_getspecific(__tls_ ## name.key); \
                if (caa_unlikely(__tls_p == NULL)) {            \
-                       __tls_p = calloc(1, sizeof(type));      \
+                       __tls_p = (__typeof__(type) *) calloc(1, sizeof(type)); \
+                       do_init                                 \
                        (void) pthread_setspecific(__tls_ ## name.key,  \
                                __tls_p);                       \
                }                                               \
                return __tls_p;                                 \
        }
 
+# define _URCU_TLS_INIT(init)                                  \
+       *__tls_p = (init);
+
+# define DEFINE_URCU_TLS_INIT(type, name, init)                        \
+       DEFINE_URCU_TLS_INIT_1(type, name, _URCU_TLS_INIT(init))
+
 # define DEFINE_URCU_TLS(type, name)                           \
-       DEFINE_URCU_TLS_1(type, name)
+       DEFINE_URCU_TLS_INIT_1(type, name, /* empty */)
 
 # define URCU_TLS_1(name)      (*__tls_access_ ## name())
 
This page took 0.024648 seconds and 4 git commands to generate.