Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <093e58cf-fa4d-1d42-5955-aab5a294fb5e@embedded-brains.de>
Date: Fri, 21 Jan 2022 11:09:01 +0100
From: Sebastian Huber <sebastian.huber@...edded-brains.de>
To: libc-coord@...ts.openwall.com
Subject: Constructors/destructors for thread-local objects?

Hello,

first some background information, Newlib currently uses a huge object 
of type struct _reent to store thread-specific data.

The structure contains for example 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 even if it 
does not use it. This is an issue for lower end targets and applications 
which need to qualify the software according to safety standards (for 
example ECSS-E-ST-40C, ECSS-Q-ST-80C, IEC 61508,
ISO 26262, DO-178, DO-330, DO-333).

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;

Now, there is a problem with the clean up of resources when a thread is 
deleted. There is currently no standard way for constructors and 
destructors of thread-local objects in C.

For C++ a translation unit and thread specific __tls_guard variable is 
used to construct an object on demand.  The __cxa_thread_atexit() 
support is used to register destructors.  The code for this is generated 
by the compiler.

We could also use POSIX keys to register destructors.

I would like to add something similar to the .init_array and .fini_array 
at least for Newlib.  Would .tls_init_array and .tls_fini_array good names?

extern void (*__tls_init_array_start []) (void);
extern void (*__tls_init_array_end []) (void);

extern void (*__tls_fini_array_start []) (void);
extern void (*__tls_fini_array_end []) (void);

Do we need .tls_preinit_array?

It is unlikely that another C library will use this, but anyway, I would 
like to use some names which could be used elsewhere as well.

-- 
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.huber@...edded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.