Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


porting newlib to FreeRTOS

Posted by Markus B. on June 21, 2007
I want to port the newlib malloc function to FreeRTOS. To ensure that the malloc functions are thread-safe, following function should be provided:


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?


RE: porting newlib to FreeRTOS

Posted by Richard on June 21, 2007
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?


RE: porting newlib to FreeRTOS

Posted by Markus B. on June 21, 2007
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


RE: porting newlib to FreeRTOS

Posted by Glen B. on June 21, 2007
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 @).

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS v10.2.0 is available for immediate download. MIT licensed, and including RISC-V and ARMv8-M (Cortex-M33) demos.

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Cadence Tensilica Cortes

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers





STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner