The empty for loop in list.c

fraza077 wrote on Friday, 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.

Cheers,

Zac

rtel wrote on Friday, 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.

Regards.