Skip to content

Commit 746c59c

Browse files
danielfl-iarcarlescufi
authored andcommitted
arch: kernel: lib: toolchain: Standardize TLS keyword
Up until now, the `__thread` keyword has been used for declaring variables as Thread local storage. However, `__thread` is a GNU specific keyword which thus limits compatibility with other toolchains (for instance IAR). This PR intoduces a new macro `Z_THREAD_LOCAL` which expands to the corresponding C11, C23 or C++11 standard keyword based on the standard that is specified during compilation, else it uses the old `__thread` keyword. Signed-off-by: Daniel Flodin <daniel.flodin@iar.com>
1 parent 62c0fc3 commit 746c59c

File tree

11 files changed

+37
-19
lines changed

11 files changed

+37
-19
lines changed

arch/riscv/core/thread.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/*
1616
* Per-thread (TLS) variable indicating whether execution is in user mode.
1717
*/
18-
__thread uint8_t is_user_mode;
18+
Z_THREAD_LOCAL uint8_t is_user_mode;
1919
#endif
2020

2121
void arch_new_thread(struct k_thread *thread, k_thread_stack_t *stack,

arch/xtensa/core/thread.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
2222
/*
2323
* Per-thread (TLS) variable indicating whether execution is in user mode.
2424
*/
25-
__thread uint32_t is_user_mode;
25+
Z_THREAD_LOCAL uint32_t is_user_mode;
2626
#endif
2727

2828
#endif /* CONFIG_USERSPACE */

doc/kernel/services/other/thread_local_storage.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ making a system call.
2929
Declaring and Using Thread Local Variables
3030
******************************************
3131

32-
The keyword ``__thread`` can be used to declare thread local variables.
32+
The macro ``Z_THREAD_LOCAL`` can be used to declare thread local variables.
3333

3434
For example, to declare a thread local variable in header files:
3535

3636
.. code-block:: c
3737
38-
extern __thread int i;
38+
extern Z_THREAD_LOCAL int i;
3939
4040
And to declare the actual variable in source files:
4141

4242
.. code-block:: c
4343
44-
__thread int i;
44+
Z_THREAD_LOCAL int i;
4545
4646
Keyword ``static`` can also be used to limit the variable within a source file:
4747

4848
.. code-block:: c
4949
50-
static __thread int j;
50+
static Z_THREAD_LOCAL int j;
5151
5252
Using the thread local variable is the same as using other variable, for example:
5353

include/zephyr/toolchain/common.h

+18
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@
3939
#endif
4040
#endif
4141

42+
/*
43+
* Thread local variables are declared with different keywords depending on
44+
* which C/C++ standard that is used. C++11 and C23 uses "thread_local" whilst
45+
* C11 uses "_Thread_local". Previously the GNU "__thread" keyword was used
46+
* which is the same in both gcc and g++.
47+
*/
48+
#ifndef Z_THREAD_LOCAL
49+
#if defined(__cplusplus) && (__cplusplus) >= 201103L /* C++11 */
50+
#define Z_THREAD_LOCAL thread_local
51+
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__) >= 202311L /* C23 */
52+
#define Z_THREAD_LOCAL thread_local
53+
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__) >= 201112L /* C11 */
54+
#define Z_THREAD_LOCAL _Thread_local
55+
#else /* Default back to old behavior which used the GNU keyword. */
56+
#define Z_THREAD_LOCAL __thread
57+
#endif
58+
#endif /* Z_THREAD_LOCAL */
59+
4260
/*
4361
* Generate a reference to an external symbol.
4462
* The reference indicates to the linker that the symbol is required

kernel/compiler_stack_protect.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void _StackCheckHandler(void)
4747
* The canary value gets initialized in z_cstart().
4848
*/
4949
#ifdef CONFIG_STACK_CANARIES_TLS
50-
__thread volatile uintptr_t __stack_chk_guard;
50+
Z_THREAD_LOCAL volatile uintptr_t __stack_chk_guard;
5151
#elif CONFIG_USERSPACE
5252
K_APP_DMEM(z_libc_partition) volatile uintptr_t __stack_chk_guard;
5353
#else

kernel/errno.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const int _k_neg_eagain = -EAGAIN;
2727
#if defined(CONFIG_LIBC_ERRNO)
2828
/* nothing needed here */
2929
#elif defined(CONFIG_ERRNO_IN_TLS)
30-
__thread int z_errno_var;
30+
Z_THREAD_LOCAL int z_errno_var;
3131
#else
3232

3333
#ifdef CONFIG_USERSPACE

kernel/init.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ void z_bss_zero_pinned(void)
291291

292292
#ifdef CONFIG_STACK_CANARIES
293293
#ifdef CONFIG_STACK_CANARIES_TLS
294-
extern __thread volatile uintptr_t __stack_chk_guard;
294+
extern Z_THREAD_LOCAL volatile uintptr_t __stack_chk_guard;
295295
#else
296296
extern volatile uintptr_t __stack_chk_guard;
297297
#endif /* CONFIG_STACK_CANARIES_TLS */

kernel/xip.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#ifdef CONFIG_STACK_CANARIES
1414
#ifdef CONFIG_STACK_CANARIES_TLS
15-
extern __thread volatile uintptr_t __stack_chk_guard;
15+
extern Z_THREAD_LOCAL volatile uintptr_t __stack_chk_guard;
1616
#else
1717
extern volatile uintptr_t __stack_chk_guard;
1818
#endif /* CONFIG_STACK_CANARIES_TLS */

lib/os/thread_entry.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
#ifdef CONFIG_CURRENT_THREAD_USE_TLS
1616
#include <zephyr/random/random.h>
1717

18-
__thread k_tid_t z_tls_current;
18+
Z_THREAD_LOCAL k_tid_t z_tls_current;
1919
#endif
2020

2121
#ifdef CONFIG_STACK_CANARIES_TLS
22-
extern __thread volatile uintptr_t __stack_chk_guard;
22+
extern Z_THREAD_LOCAL volatile uintptr_t __stack_chk_guard;
2323
#endif /* CONFIG_STACK_CANARIES_TLS */
2424

2525
/*

tests/kernel/mem_protect/stackprot/src/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ ZTEST(stackprot, test_create_alt_thread)
143143
}
144144

145145
#ifdef CONFIG_STACK_CANARIES_TLS
146-
extern __thread volatile uintptr_t __stack_chk_guard;
146+
extern Z_THREAD_LOCAL volatile uintptr_t __stack_chk_guard;
147147
#else
148148
extern volatile uintptr_t __stack_chk_guard;
149149
#endif

tests/kernel/threads/tls/src/main.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ K_APP_BMEM(part_common) static k_tid_t tls_tid[NUM_THREADS];
5151
K_APP_BMEM(part_common) static enum test_result tls_result[NUM_THREADS];
5252

5353
/* Thread data with initialized values */
54-
static uint8_t __thread thread_data8 = STATIC_DATA8;
55-
static uint32_t __thread thread_data32 = STATIC_DATA32;
56-
static uint64_t __thread thread_data64 = STATIC_DATA64;
54+
static uint8_t Z_THREAD_LOCAL thread_data8 = STATIC_DATA8;
55+
static uint32_t Z_THREAD_LOCAL thread_data32 = STATIC_DATA32;
56+
static uint64_t Z_THREAD_LOCAL thread_data64 = STATIC_DATA64;
5757

5858
/* Zeroed thread data */
59-
static uint8_t __thread thread_bss8;
60-
static uint32_t __thread thread_bss32;
61-
static uint64_t __thread thread_bss64;
59+
static uint8_t Z_THREAD_LOCAL thread_bss8;
60+
static uint32_t Z_THREAD_LOCAL thread_bss32;
61+
static uint64_t Z_THREAD_LOCAL thread_bss64;
6262

6363
static void tls_thread_entry(void *p1, void *p2, void *p3)
6464
{

0 commit comments

Comments
 (0)