Skip to content

Commit 770f0f0

Browse files
committed
Add Thread Local Storage (TLS) support using Picolibc functions
Thread Local Storage (TLS) offers a platform-native mechanism for managing per-task variables that doesn't rely on custom support in FreeRTOS for things like errno. Picolibc uses TLS internally for all variables that are intended to be task-local. Because of the TLS architecture, only variables which are actually used end up getting allocate space in the TLS block. Contrast this with the newlib 'struct _reent' block which holds all possible task-local values, even for APIs not used by the application. Picolibc also has TLS helper functions that provide the necessary platform-specific code which makes the FreeRTOS changes platform independent. This patch uses those hooks instead of providing the platform specific code in FreeRTOS itself. 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 eeebde9 commit 770f0f0

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-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

+37
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,43 @@
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 ) do { \
125+
pxTopOfStack = ( StackType_t *) ( ( ( portPOINTER_SIZE_TYPE) pxTopOfStack) - _tls_size() ); \
126+
xTLSBlock = pxTopOfStack; \
127+
_init_tls( xTLSBlock ); \
128+
} while(0)
129+
#else /* portSTACK_GROWTH */
130+
#define configINIT_TLS_BLOCK( xTLSBlock ) do { \
131+
xTLSBlock = pxTopOfStack; \
132+
pxTopOfStack = ( StackType_t *) ( ( ( portPOINTER_SIZE_TYPE) pxTopOfStack) + _tls_size() ); \
133+
_init_tls(xTLSBlock); \
134+
} while(0)
135+
#endif /* portSTACK_GROWTH */
136+
137+
#define configSET_TLS_BLOCK( xTLSBlock ) _set_tls( xTLSBlock )
138+
139+
#define configDEINIT_TLS_BLOCK( xTLSBlock )
140+
#endif
141+
105142
#ifndef configUSE_C_RUNTIME_TLS_SUPPORT
106143
#define configUSE_C_RUNTIME_TLS_SUPPORT 0
107144
#endif

0 commit comments

Comments
 (0)