Mesa (main): c11: Implement thread_local in c11/threads.h

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Jun 15 18:05:01 UTC 2022


Module: Mesa
Branch: main
Commit: 6e33ef2bb9d096bdac40678e5cf5dda3e98fef7b
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=6e33ef2bb9d096bdac40678e5cf5dda3e98fef7b

Author: Yonggang Luo <luoyonggang at gmail.com>
Date:   Sat Apr  9 22:33:44 2022 +0800

c11: Implement thread_local in c11/threads.h

Use thread_local when possible

Signed-off-by: Yonggang Luo <luoyonggang at gmail.com>
Reviewed-by: Jesse Natalie <jenatali at microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15087>

---

 src/c11/threads.h                             | 26 ++++++++++++++++++++++++++
 src/gallium/frontends/nine/nine_dump.c        |  3 ++-
 src/gallium/winsys/svga/drm/vmw_screen_svga.c |  3 ++-
 src/util/u_thread.h                           |  8 +++-----
 4 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/src/c11/threads.h b/src/c11/threads.h
index 23495cd197c..a9d45e006dd 100644
--- a/src/c11/threads.h
+++ b/src/c11/threads.h
@@ -26,6 +26,32 @@
 
 /*---------------------------- macros ---------------------------*/
 
+#ifndef _Thread_local
+#  if defined(__cplusplus)
+     /* C++11 doesn't need `_Thread_local` keyword or macro */
+#  elif !defined(__STDC_NO_THREADS__)
+     /* threads are optional in C11, _Thread_local present in this condition */
+#  elif defined(_MSC_VER)
+#    define _Thread_local __declspec(thread)
+#  elif defined(__GNUC__)
+#    define _Thread_local __thread
+#  else
+     /* Leave _Thread_local undefined so that use of _Thread_local would not promote
+      * to a non-thread-local global variable
+      */
+#  endif
+#endif
+
+#if !defined(__cplusplus)
+   /*
+    * C11 thread_local() macro
+    * C++11 and above already have thread_local keyword
+    */
+#  ifndef thread_local
+#    define thread_local _Thread_local
+#  endif
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/src/gallium/frontends/nine/nine_dump.c b/src/gallium/frontends/nine/nine_dump.c
index 85ee266defb..26faed8a2dd 100644
--- a/src/gallium/frontends/nine/nine_dump.c
+++ b/src/gallium/frontends/nine/nine_dump.c
@@ -3,6 +3,7 @@
 #include "nine_pipe.h"
 
 #include <stdio.h>
+#include "c11/threads.h"
 #include "util/u_memory.h"
 #include "util/u_math.h"
 
@@ -10,7 +11,7 @@
 
 #if defined(DEBUG) || !defined(NDEBUG)
 
-static char __thread tls[128];
+static char thread_local tls[128];
 
 const char *nine_D3DDEVTYPE_to_str(D3DDEVTYPE type)
 {
diff --git a/src/gallium/winsys/svga/drm/vmw_screen_svga.c b/src/gallium/winsys/svga/drm/vmw_screen_svga.c
index 547d6ea6d4c..98b38322917 100644
--- a/src/gallium/winsys/svga/drm/vmw_screen_svga.c
+++ b/src/gallium/winsys/svga/drm/vmw_screen_svga.c
@@ -40,6 +40,7 @@
 #include "svga_cmd.h"
 #include "svga3d_caps.h"
 
+#include "c11/threads.h"
 #include "util/os_file.h"
 #include "util/u_inlines.h"
 #include "util/u_math.h"
@@ -123,7 +124,7 @@ typedef __attribute__((aligned(32))) struct MKSGuestStatInfoEntry {
    } stat;
 } MKSGuestStatInfoEntry;
 
-static __thread struct svga_winsys_stats_timeframe *mksstat_tls_global = NULL;
+static thread_local struct svga_winsys_stats_timeframe *mksstat_tls_global = NULL;
 
 #define ALIGN(x, power_of_two) (((x) + (power_of_two) - 1) & ~((power_of_two) - 1))
 
diff --git a/src/util/u_thread.h b/src/util/u_thread.h
index 7d50e5ca87a..15fadfae19a 100644
--- a/src/util/u_thread.h
+++ b/src/util/u_thread.h
@@ -79,13 +79,11 @@
  * expensive pthread_getspecific() or its equivalent).
  */
 #ifdef USE_ELF_TLS
-#ifdef _MSC_VER
-#define __THREAD_INITIAL_EXEC __declspec(thread)
-#elif defined(__GLIBC__)
-#define __THREAD_INITIAL_EXEC __thread __attribute__((tls_model("initial-exec")))
+#if defined(__GLIBC__)
+#define __THREAD_INITIAL_EXEC thread_local __attribute__((tls_model("initial-exec")))
 #define REALLY_INITIAL_EXEC
 #else
-#define __THREAD_INITIAL_EXEC __thread
+#define __THREAD_INITIAL_EXEC thread_local
 #endif
 #endif
 



More information about the mesa-commit mailing list