#4560 closed project (fixed)
Use thread-local storage for Newlib reentrancy objects
Reported by: | Sebastian Huber | Owned by: | Sebastian Huber |
---|---|---|---|
Priority: | normal | Milestone: | 6.1 |
Component: | tool/newlib | Version: | 6 |
Severity: | normal | Keywords: | SoC, newlib, executive, large, qualification |
Cc: | Blocked By: | #4672 | |
Blocking: |
Description (last modified by Gedare Bloom)
Problem
The state of the art architectures supported by RTEMS have all efficient support for thread-local storage (MIPS has issues with thread-local storage, however, is MIPS state of the art?).
Newlib currently uses a huge object of type struct _reent
to store thread-specific data. This object is returned by __getreent()
. It is related to the __DYNAMIC_REENT__
Newlib configuration option which is always defined for RTEMS.
The reentrancy structure contains errno
and also the standard input, output, and error file streams. This means that if an application only uses errno
it has a dependency on the file stream support event if it does not use it. This is an issue for lower end targets and the pre-qualification of RTEMS.
Solution
One approach to disentangle the dependencies introduced by struct _reent
is to get rid of this structure and replace the individual members of the structure with thread-local objects. For example, instead of
struct _reent { int _errno; __FILE *_stdin; __FILE *_stdout; __FILE *_stderr; };
use
_Thread_local int _errno; _Thread_local __FILE *_stdin; _Thread_local __FILE *_stdout; _Thread_local __FILE *_stderr;
Newlib already has access macros for the struct _reent
members, for example:
#define _REENT_SIGNGAM(ptr) ((ptr)->_new._reent._gamma_signgam) #define _REENT_RAND_NEXT(ptr) ((ptr)->_new._reent._rand_next) #define _REENT_RAND48_SEED(ptr) ((ptr)->_new._reent._r48._seed) #define _REENT_RAND48_MULT(ptr) ((ptr)->_new._reent._r48._mult) #define _REENT_RAND48_ADD(ptr) ((ptr)->_new._reent._r48._add)
How-to Implement
The member access macros are incomplete. The first step is to use the Newlib configuration for RTEMS as is and rename all struct _reent
members, for example add an TEMPORARY
prefix to all member names, _errno
to TEMPORARY_errno
. Then add member access macros until Newlib builds again. Install this Newlib and check that RTEMS and libbsd compiles. Run the RTEMS and libbsd test suites to check for regressions.
In a second step to this for the _REENT_SMALL
configuration of Newlib.
The third step is to add a new Newlib configuration option, for example _REENT_THREAD_LOCAL
which turns the struct _reent
members into thread-local objects with corresponding "member" access macros. Define _REENT
to NULL
.
Skills
C and assembly
Difficulty
This is a large (350-hour) project of hard difficulty.
Attachments (2)
Change History (24)
comment:1 Changed on 12/03/21 at 17:29:59 by Joel Sherrill
comment:2 Changed on 12/03/21 at 19:20:33 by Sebastian Huber
I am not sure what the Newlib maintainer say if we try to add a third reentrancy approach. I guess for backward compatibility they want to keep the existing methods anyway.
comment:3 Changed on 12/03/21 at 20:07:52 by Joel Sherrill
Keywords: | SoC newlib added |
---|
comment:4 Changed on 12/06/21 at 08:19:37 by Chris Johns
I would like to see libdl
support for TLS added to RTEMS.
comment:7 Changed on 02/04/22 at 18:54:45 by Gedare Bloom
Keywords: | executive added |
---|
comment:8 Changed on 02/25/22 at 18:27:59 by Gedare Bloom
Description: | modified (diff) |
---|---|
Keywords: | large added |
comment:9 Changed on 06/29/22 at 09:04:57 by Sebastian Huber
Milestone: | 7.1 → 6.1 |
---|---|
Version: | 7 → 6 |
comment:10 Changed on 06/29/22 at 09:13:44 by Sebastian Huber
Blocked By: | 4672 added |
---|
comment:13 Changed on 07/21/22 at 08:25:04 by Sebastian Huber <sebastian.huber@…>
comment:14 Changed on 07/21/22 at 08:25:05 by Sebastian Huber <sebastian.huber@…>
comment:15 Changed on 07/21/22 at 08:25:06 by Sebastian Huber <sebastian.huber@…>
Changed on 08/08/22 at 11:00:15 by Sebastian Huber
Attachment: | gcc-10-RTEMS-Use-local-exec-TLS-model-by-default.patch added |
---|
Changed on 08/08/22 at 11:00:32 by Sebastian Huber
Attachment: | gcc-12-RTEMS-Use-local-exec-TLS-model-by-default.patch added |
---|
comment:16 Changed on 08/08/22 at 18:23:20 by Sebastian Huber <sebastian.huber@…>
comment:17 Changed on 09/09/22 at 05:32:55 by Sebastian Huber <sebastian.huber@…>
comment:20 Changed on 11/30/22 at 08:59:07 by Sebastian Huber
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Yes, I recently were able to integrate the final patch for GCC 13. A follow up activity is #4765.
comment:21 Changed on 04/26/23 at 05:14:33 by Sebastian Huber <sebastian.huber@…>
comment:22 Changed on 10/11/23 at 09:29:56 by Sebastian Huber
Keywords: | qualification added |
---|
One issue with this is that TLS is not supported on all architectures. We would either have to have two methods (current and TLS) to do this or fix TLS everywhere.