Newlib reentrancy

Gedare Bloom gedare at rtems.org
Mon Apr 22 13:46:27 UTC 2013


On Mon, Apr 22, 2013 at 6:54 AM, Sebastian Huber
<sebastian.huber at embedded-brains.de> wrote:
> On 04/22/2013 12:09 PM, Chris Johns wrote:
>>
>> Sebastian Huber wrote:
>>>
>>> Hello,
>>>
>>> Newlib stores all global state in a so called reentrancy structure, e.g.
>>> errno, stdin, random number state, etc. In RTEMS every thread can have
>>> its own reentrancy structure (a reference it is part of the thread
>>> control block). In Newlib there are two mechanisms to access the
>>> executing thread's reentrancy structure
>>>
>>> 1. via a global pointer _impure_ptr, or
>>> 2. via a function __getreent().
>>>
>>> See also:
>>>
>>> http://sourceware.org/ml/newlib/2013/msg00031.html
>>>
>>> Currently RTEMS uses option 1. The global pointer is updated during a
>>> thread dispatch in threaddispatch.c.
>>>
>>> The option 1. is unsuitable for SMP since here a global pointer cannot
>>> be used to store thread specific data. I suggest to use option 2. for
>>> RTEMS.
>>
>>
>> I agree. What is needed to define this support when building newlib ?
>
>
> RTEMS has to define __DYNAMIC_REENT__ in "newlib/libc/include/sys/config.h".
>
>
>>
>>> For the single-processor RTEMS this has the benefit that the
>>> thread dispatch code is simplified, e.g. the code block
>>>
>>> /*
>>> * Switch libc's task specific data.
>>> */
>>> if ( _Thread_libc_reent ) {
>>> executing->libc_reent = *_Thread_libc_reent;
>>> *_Thread_libc_reent = heir->libc_reent;
>>> }
>>>
>>> can be removed. The execution time of _Thread_Dispatch() is critical for
>>> the interrupt and thread switch latency.
>>>
>>> The implementation of __getreent() for RTEMS is easy:
>>>
>>> struct _reent *__getreent(void)
>>> {
>>> struct _reent *reent = _Thread_Executing->libc_reent;
>>>
>>> if ( reent == NULL ) {
>>> reent = _global_impure_ptr;
>>> }
>>>
>>> return reent;
>>> }
>>
>>
>> Does this code need locking ?
>
>
> No, but I haven't tested anything yet.
>
Seems right to me. Do we need the check for NULL though, or can we
define the invariant that _Thread_Executing->libc_reent is valid
pointer to the reent structure?

>
> --
> Sebastian Huber, embedded brains GmbH
>
> Address : Dornierstr. 4, D-82178 Puchheim, Germany
> Phone   : +49 89 189 47 41-16
> Fax     : +49 89 189 47 41-09
> E-Mail  : sebastian.huber at embedded-brains.de
> PGP     : Public key available on request.
>
> Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
> _______________________________________________
> rtems-devel mailing list
> rtems-devel at rtems.org
> http://www.rtems.org/mailman/listinfo/rtems-devel




More information about the devel mailing list