porting newlib to FreeRTOS

I want to port the newlib malloc function to FreeRTOS. To ensure that the malloc functions are thread-safe, following function should be provided: __malloc_lock __malloc_unlock following hint is inside the newlib documentation: A call to <<malloc>> may call <<__malloc_lock>> recursively; that is, the sequence of calls may go <<__malloc_lock>>, <<__malloc_lock>>, <<__malloc_unlock>>, <<__malloc_unlock>>.  Any implementation of these routines must be careful to avoid causing a thread to wait for a lock that it already holds. To avoid locking out themself, i need an information which task is currently executed. Is there an easy way to implement a function like GetCurrentTCB()? If different threads works in the library at the same time, it can creates reentrancy problems. Newlib avoid this with the _impure_ptr to a _reent struct. Every task should have his own _reent structure and the _impure_ptr is moving between these structures during context switches. How can i implement this? Is it possible to implement a callback function inside the scheduler? regards markus

porting newlib to FreeRTOS

To answer the first part of your question – look at the function xTaskGetCurrentTaskHandle() in task.c.  For the second part of your question – I’m not sure your intention.  Is it to add another item into the task control block and save and restore this item automatically in the context switch?  Or is it to have the ‘callback’ called each time there is a context switch? Regards.

porting newlib to FreeRTOS

Hi Richard, thanks for your answer. Yes, that´s what i want. But the function xTaskGetCurrentTaskHandle() is not listed in the online documentation. Most of the sample FreeRTOSConfig.h have no define INCLUDE_xTaskGetCurrentTaskHandle. I have added this. For the second part of my question: My idea is to have a way to manipulate some global vars while the scheduler is inside the "critical section" and a context switch occurs. It can be implemented like vApplicationTickHook() or vApplicationIdleHook(). As a consequence the developer has a way to do some stuff while a context switch occur. For details see http://www.embedded.com/shared/printableArticle.jhtml?articleID=15201696 subsection reentrancy regards markus

porting newlib to FreeRTOS

For ARM port… I have a C++ class that supplies a recursive locking mechanism based on FreeRTOS queues (semaphore-like usage where 1 item of size 0 is used). It is not as straight-forward as you might think. The code is not directly usable in C, but could easily be modified. It is the algorithm that is important. I have modified the portRESTORE_CONTEXT() and portSAVE_CONTEXT() macros of portmacro.h to stack/unstack _impure_ptr. Later this evening I can get the code out. I can send both directly to you if you email me at ‘glen @ EmbeddedClarity.com’ (elide spaces around @).