Skip to content

Commit 4abaaa1

Browse files
author
Keith Packard
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 0b20405 commit 4abaaa1

File tree

3 files changed

+82
-0
lines changed

3 files changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@
7676

7777
#endif /* if ( configUSE_NEWLIB_REENTRANT == 1 ) */
7878

79+
/* Must be defaulted before configUSE_PICOLIBC_TLS is used below. */
80+
#ifndef configUSE_PICOLIBC_TLS
81+
#define configUSE_PICOLIBC_TLS 0
82+
#endif
83+
84+
#if ( configUSE_PICOLIBC_TLS == 1 )
85+
86+
#include "picolibc-freertos.h"
87+
88+
#endif /* if ( configUSE_PICOLIBC_TLS == 1 ) */
89+
7990
#ifndef configUSE_C_RUNTIME_TLS_SUPPORT
8091
#define configUSE_C_RUNTIME_TLS_SUPPORT 0
8192
#endif

include/picolibc-freertos.h

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* FreeRTOS Kernel <DEVELOPMENT BRANCH>
3+
* Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* SPDX-License-Identifier: MIT
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
8+
* this software and associated documentation files (the "Software"), to deal in
9+
* the Software without restriction, including without limitation the rights to
10+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11+
* the Software, and to permit persons to whom the Software is furnished to do so,
12+
* subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
*
24+
* https://www.FreeRTOS.org
25+
* https://github.com/FreeRTOS
26+
*
27+
*/
28+
29+
#ifndef INC_PICOLIBC_FREERTOS_H
30+
#define INC_PICOLIBC_FREERTOS_H
31+
32+
/* Use picolibc TLS support to allocate space for __thread variables,
33+
* initialize them at thread creation and set the TLS context at
34+
* thread switch time.
35+
*
36+
* See the picolibc TLS docs:
37+
* https://github.com/picolibc/picolibc/blob/main/doc/tls.md
38+
* for additional information. */
39+
40+
#include <picotls.h>
41+
42+
#define configUSE_C_RUNTIME_TLS_SUPPORT 1
43+
44+
#define configTLS_BLOCK_TYPE void *
45+
46+
/* Allocate thread local storage block off the end of the
47+
* stack. The _tls_size() function returns the size (in
48+
* bytes) of the total TLS area used by the application */
49+
#if ( portSTACK_GROWTH < 0 )
50+
#define configINIT_TLS_BLOCK( xTLSBlock, pxTopOfStack ) \
51+
do { \
52+
pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) - _tls_size() ); \
53+
xTLSBlock = pxTopOfStack; \
54+
_init_tls( xTLSBlock ); \
55+
} while( 0 )
56+
#else /* portSTACK_GROWTH */
57+
#define configINIT_TLS_BLOCK( xTLSBlock, pxTopOfStack ) \
58+
do { \
59+
xTLSBlock = pxTopOfStack; \
60+
pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) + _tls_size() ); \
61+
_init_tls( xTLSBlock ); \
62+
} while( 0 )
63+
#endif /* portSTACK_GROWTH */
64+
65+
#define configSET_TLS_BLOCK( xTLSBlock ) _set_tls( xTLSBlock )
66+
67+
#define configDEINIT_TLS_BLOCK( xTLSBlock )
68+
69+
#endif /* INC_PICOLIBC_FREERTOS_H */

0 commit comments

Comments
 (0)