Skip to content

Commit 6569bca

Browse files
committed
TSRM: Replace TSRM_TLS by C11’s thread_local
1 parent 18184a9 commit 6569bca

File tree

8 files changed

+33
-18
lines changed

8 files changed

+33
-18
lines changed

TSRM/TSRM.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ static pthread_key_t tls_key;
112112
# define tsrm_tls_get() pthread_getspecific(tls_key)
113113
#endif
114114

115-
TSRM_TLS bool in_main_thread = false;
116-
TSRM_TLS bool is_thread_shutdown = false;
115+
thread_local bool in_main_thread = false;
116+
thread_local bool is_thread_shutdown = false;
117117

118118
/* Startup TSRM (call once for the entire process) */
119119
TSRM_API bool tsrm_startup(int expected_threads, int expected_resources, int debug_level, const char *debug_filename)

TSRM/TSRM.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
#include <stdint.h>
2424
#include <stdbool.h>
25+
#if __STDC_NO_THREADS__
26+
# define thread_local _Thread_local
27+
#else
28+
# include <threads.h>
29+
#endif
2530

2631
#ifdef TSRM_WIN32
2732
# ifdef TSRM_EXPORTS
@@ -142,12 +147,6 @@ TSRM_API bool tsrm_is_shutdown(void);
142147
TSRM_API const char *tsrm_api_name(void);
143148
TSRM_API bool tsrm_is_managed_thread(void);
144149

145-
#ifdef TSRM_WIN32
146-
# define TSRM_TLS __declspec(thread)
147-
#else
148-
# define TSRM_TLS __thread
149-
#endif
150-
151150
#ifndef __has_attribute
152151
# define __has_attribute(x) 0
153152
#endif
@@ -175,10 +174,10 @@ TSRM_API bool tsrm_is_managed_thread(void);
175174
#define TSRMG_BULK_STATIC(id, type) ((type) (*((void ***) TSRMLS_CACHE))[TSRM_UNSHUFFLE_RSRC_ID(id)])
176175
#define TSRMG_FAST_STATIC(offset, type, element) (TSRMG_FAST_BULK_STATIC(offset, type)->element)
177176
#define TSRMG_FAST_BULK_STATIC(offset, type) ((type) (((char*) TSRMLS_CACHE)+(offset)))
178-
#define TSRMLS_MAIN_CACHE_EXTERN() extern TSRM_TLS void *TSRMLS_CACHE TSRM_TLS_MODEL_ATTR;
179-
#define TSRMLS_MAIN_CACHE_DEFINE() TSRM_TLS void *TSRMLS_CACHE TSRM_TLS_MODEL_ATTR = NULL;
180-
#define TSRMLS_CACHE_EXTERN() extern TSRM_TLS void *TSRMLS_CACHE;
181-
#define TSRMLS_CACHE_DEFINE() TSRM_TLS void *TSRMLS_CACHE = NULL;
177+
#define TSRMLS_MAIN_CACHE_EXTERN() extern thread_local void *TSRMLS_CACHE TSRM_TLS_MODEL_ATTR;
178+
#define TSRMLS_MAIN_CACHE_DEFINE() thread_local void *TSRMLS_CACHE TSRM_TLS_MODEL_ATTR = NULL;
179+
#define TSRMLS_CACHE_EXTERN() extern thread_local void *TSRMLS_CACHE;
180+
#define TSRMLS_CACHE_DEFINE() thread_local void *TSRMLS_CACHE = NULL;
182181
#define TSRMLS_CACHE_UPDATE() TSRMLS_CACHE = tsrm_get_ls_cache()
183182
#define TSRMLS_CACHE _tsrm_ls_cache
184183

UPGRADING.INTERNALS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ PHP 8.6 INTERNALS UPGRADE NOTES
2020
. The misnamed ZVAL_IS_NULL() has been removed. Use Z_ISNULL() instead.
2121
. New zend_class_entry.ce_flags2 and zend_function.fn_flags2 fields were
2222
added, given the primary flags were running out of bits.
23+
. The TSRM_TLS macro has been removed. Use C11's thread_local instead.
2324

2425
========================
2526
2. Build system changes

Zend/zend_types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ typedef ZEND_RESULT_CODE zend_result;
7777
#endif
7878

7979
#ifdef ZTS
80-
#define ZEND_TLS static TSRM_TLS
81-
#define ZEND_EXT_TLS TSRM_TLS
80+
#define ZEND_TLS static thread_local
81+
#define ZEND_EXT_TLS thread_local
8282
#else
8383
#define ZEND_TLS static
8484
#define ZEND_EXT_TLS

ext/opcache/jit/tls/testing/def-vars.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* Declare a few additional TLS variables to fill any surplus space,
22
* so _tsrm_ls_cache is allocated in the dynamic section. */
33

4-
#define DEF_VAR(prefix, num) __thread void* prefix##num
4+
#define DEF_VAR(prefix, num) thread_local void* prefix##num
55
#define DEF_VARS(prefix) \
66
DEF_VAR(prefix, 0000); \
77
DEF_VAR(prefix, 0001); \

ext/opcache/jit/tls/testing/def.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
#include <unistd.h>
55
#include <stdio.h>
66
#include <stdarg.h>
7+
#if __STDC_NO_THREADS__
8+
# define thread_local _Thread_local
9+
#else
10+
# include <threads.h>
11+
#endif
712

813
#ifdef NO_SURPLUS
914
# include "def-vars.h"
1015
DEF_VARS(def);
1116
#endif
1217

13-
__thread void* _tsrm_ls_cache;
18+
thread_local void* _tsrm_ls_cache;
1419

1520
size_t tsrm_get_ls_cache_tcb_offset(void) {
1621
return 0;

ext/opcache/jit/tls/testing/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#include <stdio.h>
22
#include <dlfcn.h>
3+
#if __STDC_NO_THREADS__
4+
# define thread_local _Thread_local
5+
#else
6+
# include <threads.h>
7+
#endif
38

49
#ifdef NO_SURPLUS
510
# include "def-vars.h"
611
DEF_VARS(main);
712
#endif
813

9-
__thread int some_tls_var;
14+
thread_local int some_tls_var;
1015

1116
#ifndef DL_DECL
1217
int decl(void);

ext/opcache/jit/tls/testing/user.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11

22
/* _tsrm_ls_cache is used / inspected here */
33

4+
#if __STDC_NO_THREADS__
5+
# define thread_local _Thread_local
6+
#else
7+
# include <threads.h>
8+
#endif
49
#include "../zend_jit_tls.h"
510

6-
extern __thread void* _tsrm_ls_cache;
11+
extern thread_local void* _tsrm_ls_cache;
712

813
int test(void)
914
{

0 commit comments

Comments
 (0)