Quality RTOS & Embedded Software

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


The empty for loop in list.c

Posted by fraza077 on September 11, 2015

I guess a lot of you have seen this your code get stuck here before:

for( pxIterator = ( ListItem_t * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext ) /lint !e826 !e740 The mini list structure is used as the list end to save RAM. This is checked and valid. */ { / There is nothing to do here, just iterating to the wanted insertion position. */ }

After having bundled through all this once again today, I found my problem (I was in a critical section due to an accidental Find/Replace having greater scope than I thought), but it struck me that there really weren't enough checks in this section, despite the comment claim that "There is nothing to do here".

In my case, for whatever reason, it was adding a duplicate element to the list. Is this something one could use an assert for? Inside the for loop:

configASSERT(pxNewListItem != pxIterator);

Also, in case anything else goes wrong, why not ensure the list only gets run through once? For example:

volatile xListItem *start = ( xListItem * ) &( pxList->xListEnd ); volatile uint32 RunThroughCnt = 0; for( pxIterator = ( xListItem * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext ) { if (pxIterator == start) { RunThroughCnt++; configASSERT(RunThroughCnt <= 1); } configASSERT(pxNewListItem != pxIterator);


I imagine those of you who know the inner workings of FreeRTOS better than I do might know some reasons why these wouldn't work? If not, is there anything else that can be done to make this safer?

Just staying in the for loop forever is annoying, all the info I get is that the watchdog kicked in, And I don't have a clue where the code was when that happened.



The empty for loop in list.c

Posted by rtel on September 11, 2015

Things inside the kernel are kept as lean as possible, especially code that runs frequently, and especially especially code that is in a loop. Adding these checks would be useful, I agree, but aren't done because it would make the code slower. Instead we have added asserts at the points in the code that can lead to you getting stuck in the loop. For example, the most common cause (bar far) is people setting incorrect interrupt priorities - that is - using FreeRTOS API functions from an interrupt that has a priority higher than the priority masked by critical sections - so we have an assert that traps that happening. That way the assert should be hit before the code ever reaches the loop, and it is hit at the point the cause is detected, rather than the symptom.


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

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

Latest News

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

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

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

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