avoid double thread locking of malloc/free on IAR EW for MSP430 with heap_3.c

gmenhennitt wrote on Thursday, January 30, 2014:

IAR Embedded Workbench for MSP430 (and, I would guess, other IAR products) has an option “Enable thread support in library”. If this is ticked, the library puts locks around calls to malloc() and free() to give thread safety. The developer is responsible for implementing four functions to implement the locking (see http://supp.iar.com/FilesPublic/UPDINFO/005691/arm/doc/infocenter/DLIBThreadSupport.html). When using this with FreeRTOS, it seems to be sufficient that locking calls vTaskSuspendAll() and unlocking calls xTaskResumeAll() (similar to what heap_3.c does).

However, that means that FreeRTOS’s pvPortMalloc() and vPortFree() in heap_3.c do the lock and unlock twice - once in heap_3.c and once in the IAR library. I’m not sure what the overhead of this is, or if there are any adverse effects (I doubt that there are). But it’s fairly trivial to avoid.

I’ve modified FreeRTOS.h to add a new config option configLOCK_MALLOC_FREE which causes the duplicate calls to be omitted. I then set that in FreeRTOSConfig.h to 1. Then I wrapped the calls in heap_3.c in #if’s so that they get omitted.

The attached files are diffs against FreeRTOS 7.6.0 to implement this, together with a sample file locking.c that can be added to the project to implement the locking.


gmenhennitt wrote on Thursday, January 30, 2014:


gmenhennitt wrote on Thursday, January 30, 2014:

oops, missed one

edwards3 wrote on Thursday, January 30, 2014:

The vTaskSuspend/Resume functions are designed to nest, and have very little overhead when called with a nest bigger than 0.

What advantage does using the IAR locks have over just using heap_3?

gmenhennitt wrote on Thursday, January 30, 2014:

If the developer calls directly to malloc()/free() as opposed to pvPortMalloc()/vPortFree(), they’ll bypass the locks in heap_3.c. So they’re needed for anything that doesn’t know about FreeRTOS e.g. any third party libraries, or any calls from inside the IAR libraries. They’re used for C++ new/delete too.