0003-Always-use-__thread-for-Thread-local-storage-except-on-MSVC.patch 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. From 2e359284497c361e3208501fc70d49b2c54dc4ef Mon Sep 17 00:00:00 2001
  2. From: Michael Jeanson <mjeanson@efficios.com>
  3. Date: Tue, 14 Sep 2021 10:41:08 -0400
  4. Subject: [PATCH] Always use '__thread' for Thread local storage except on MSVC
  5. Use the GCC extension '__thread' [1] for Thread local storage on all C
  6. and C++ compilers except MSVC.
  7. While C11 and C++11 respectively offer '_Thread_local' and
  8. 'thread_local' as potentialy faster implementations, they offer no
  9. guarantees of compatibility when used in a library interface which might
  10. be used by both C and C++ client code.
  11. [1] https://gcc.gnu.org/onlinedocs/gcc/Thread-Local.html
  12. Change-Id: If4fe8bcdbda24b21dedf382112bd5c5f836c00c8
  13. Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
  14. Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  15. [Retrieved from:
  16. https://github.com/urcu/userspace-rcu/commit/2e359284497c361e3208501fc70d49b2c54dc4ef]
  17. Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
  18. ---
  19. include/urcu/tls-compat.h | 15 +++++++--------
  20. 1 file changed, 7 insertions(+), 8 deletions(-)
  21. diff --git a/include/urcu/tls-compat.h b/include/urcu/tls-compat.h
  22. index 25cf375a..a2c94ded 100644
  23. --- a/include/urcu/tls-compat.h
  24. +++ b/include/urcu/tls-compat.h
  25. @@ -35,15 +35,14 @@ extern "C" {
  26. #ifdef CONFIG_RCU_TLS
  27. /*
  28. - * Don't use C++ 'thread_local' on MacOs, the implementation is incompatible
  29. - * with C and will result in a link error when accessing an extern variable
  30. - * provided by the C library from C++ code.
  31. + * Default to '__thread' on all C and C++ compilers except MSVC. While C11 has
  32. + * '_Thread_local' and C++11 has 'thread_local', only '__thread' seems to have
  33. + * a compatible implementation when linking public extern symbols across
  34. + * language boundaries.
  35. + *
  36. + * For more details, see 'https://gcc.gnu.org/onlinedocs/gcc/Thread-Local.html'.
  37. */
  38. -#if defined (__cplusplus) && (__cplusplus >= 201103L) && !defined(__APPLE__)
  39. -# define URCU_TLS_STORAGE_CLASS thread_local
  40. -#elif defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
  41. -# define URCU_TLS_STORAGE_CLASS _Thread_local
  42. -#elif defined (_MSC_VER)
  43. +#if defined(_MSC_VER)
  44. # define URCU_TLS_STORAGE_CLASS __declspec(thread)
  45. #else
  46. # define URCU_TLS_STORAGE_CLASS __thread