c11: Implement thread_local in c11/threads.h

Use thread_local when possible

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
Reviewed-by: Jesse Natalie <jenatali@microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15087>
This commit is contained in:
Yonggang Luo 2022-04-09 22:33:44 +08:00 committed by Marge Bot
parent 09cd5c9bdb
commit 6e33ef2bb9
4 changed files with 33 additions and 7 deletions

View file

@ -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

View file

@ -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)
{

View file

@ -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))

View file

@ -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