Skip to content

Commit 5f91cb5

Browse files
committed
Add Thread Local Storage (TLS) support using Picolibc functions
This patch provides definitions of the general TLS support macros in terms of the Picolibc TLS support functions. Picolibc is normally configured to use TLS internally for all variables that are intended to be task-local, so these changes are necessary for picolibc to work correctly with FreeRTOS. The picolibc helper functions rely on elements within the linker script to arrange the TLS data in memory and define some symbols. Applications wanting to use this mechanism will need changes in their linker script when migrating to picolibc. Signed-off-by: Keith Packard <[email protected]>
1 parent 48ad473 commit 5f91cb5

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

.github/lexicon.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,7 @@ mclk
10611061
mconfigintcoresw
10621062
mcr
10631063
mcu
1064+
md
10641065
mddr
10651066
mder
10661067
mdh
@@ -1342,6 +1343,7 @@ phy
13421343
phya
13431344
pic
13441345
picnt
1346+
picolibc
13451347
pien
13461348
piir
13471349
pimr

include/FreeRTOS.h

+39
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,45 @@
102102
#endif
103103
#endif /* if ( configUSE_NEWLIB_REENTRANT == 1 ) */
104104

105+
#if ( configUSE_PICOLIBC_TLS == 1 )
106+
107+
/* Use picolibc TLS support to allocate space for __thread variables,
108+
* initialize them at thread creation and set the TLS context at
109+
* thread switch time.
110+
*
111+
* See the picolibc TLS docs:
112+
* https://github.com/picolibc/picolibc/blob/main/doc/tls.md
113+
* for additional information. */
114+
#include <picotls.h>
115+
116+
#define configUSE_C_RUNTIME_TLS_SUPPORT 1
117+
118+
#define configTLS_BLOCK_TYPE void *
119+
120+
/* Allocate thread local storage block off the end of the
121+
* stack. The _tls_size() function returns the size (in
122+
* bytes) of the total TLS area used by the application */
123+
#if ( portSTACK_GROWTH < 0 )
124+
#define configINIT_TLS_BLOCK( xTLSBlock ) \
125+
do { \
126+
pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) - _tls_size() ); \
127+
xTLSBlock = pxTopOfStack; \
128+
_init_tls( xTLSBlock ); \
129+
} while( 0 )
130+
#else /* portSTACK_GROWTH */
131+
#define configINIT_TLS_BLOCK( xTLSBlock ) \
132+
do { \
133+
xTLSBlock = pxTopOfStack; \
134+
pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) + _tls_size() ); \
135+
_init_tls( xTLSBlock ); \
136+
} while( 0 )
137+
#endif /* portSTACK_GROWTH */
138+
139+
#define configSET_TLS_BLOCK( xTLSBlock ) _set_tls( xTLSBlock )
140+
141+
#define configDEINIT_TLS_BLOCK( xTLSBlock )
142+
#endif /* if ( configUSE_PICOLIBC_TLS == 1 ) */
143+
105144
#ifndef configUSE_C_RUNTIME_TLS_SUPPORT
106145
#define configUSE_C_RUNTIME_TLS_SUPPORT 0
107146
#endif

0 commit comments

Comments
 (0)