My program currently makes use of some of the printf-like functions from multiple threads. Reading the newlib docs indicates that I need to set the global variable "_impure_ptr" to a "struct _reent" variable for the specific thread. Other Internet searches indicate I can define __DYNAMIC_REENT__ which will cause newlib to call a function __getreent() that I can implement to get the current thread's structure.
If I go the dynamic route, I figure I can just return a pointer based on a static thread-id (since I don't use dynamic threads, and not all of my threads use these functions). The other option would be to modify THREAD_EXT_FIELDS to do something similar.
Does this sound like I'm on the right path, or is there an example or "best practice" to ensure reentrancy in the newlib functions from ChibiOS?
I'm using CodeSourcery Lite 2010q1-188 and STM32 with my project.
Thanks,
Brian
Reentrancy in newlib functions
Moderators: RoccoMarco, lbednarz, tfAteba
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: Reentrancy in newlib functions
Hi Brian,
I don't know about __getreent() but it would be possible to enhance the port layer to switch __impure_ptr at context switch time and allocate a __reent directly into the thread context. This way reentrancy would be transparent to the application layer but all threads would have a __reent, even those that do not require the library.
Note that newlib also requires mutual exclusion for its allocator, if I remember well it expects a couple of functions to implement a mutex.
Giovanni
I don't know about __getreent() but it would be possible to enhance the port layer to switch __impure_ptr at context switch time and allocate a __reent directly into the thread context. This way reentrancy would be transparent to the application layer but all threads would have a __reent, even those that do not require the library.
Note that newlib also requires mutual exclusion for its allocator, if I remember well it expects a couple of functions to implement a mutex.
Giovanni
Re: Reentrancy in newlib functions
If my understanding is correct, I'm thinking I'd have to recompile newlib in order to use the "__DYNAMIC_REENT__" option. I'd prefer to not have to do that. The other option is, as you said, to modify the port layer to adjust the global variable. Are there any hooks in ChibiOS to let me do it for my specific application without modifying the ChibiOS source code (reason being needing to use the stable version of the OS with the license exception)?
Most likely, I'm going to modify my application to simply avoid calling printf from multiple threads anyhow, as they eat up quite a bit of stack space
Thanks for your response.
Most likely, I'm going to modify my application to simply avoid calling printf from multiple threads anyhow, as they eat up quite a bit of stack space
Thanks for your response.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: Reentrancy in newlib functions
Hi Brian,
The version in the trunk has two hooks that can be used:
- THREAD_EXT_FIELDS to add a place to put the thread copy of __impure_ptr.
- THREAD_CONTEXT_SWITCH_HOOK to switch the pointer.
but probably it would be a better idea to add an option to the GCC CMx port for newlib support, the port is for GCC after all and newlib is used by most GCC distributions so it just makes sense.
You may use the hooks right now but I plan to add proper built-in support for newlib before 2.4.x.
Giovanni
The version in the trunk has two hooks that can be used:
- THREAD_EXT_FIELDS to add a place to put the thread copy of __impure_ptr.
- THREAD_CONTEXT_SWITCH_HOOK to switch the pointer.
but probably it would be a better idea to add an option to the GCC CMx port for newlib support, the port is for GCC after all and newlib is used by most GCC distributions so it just makes sense.
You may use the hooks right now but I plan to add proper built-in support for newlib before 2.4.x.
Giovanni