Code Provided for a vQueueFlush function

dkure wrote on Friday, June 18, 2010:

Hello All,

I recently needed a queue flush function, and it seemed like a waste to use a while(xQueueReceive()) loop to flush the queue.

Instead I came up with the following function

void vQueueFlush( const xQueueHandle pxQueue )
    portBASE_TYPE xYield = pdFALSE;
    /* Reset the queue to default state */
    pxQueue->uxMessagesWaiting = 0;
    pxQueue->pcWriteTo         = pxQueue->pcHead;
    pxQueue->pcReadFrom        = pxQueue->pcHead + ( ( pxQueue->uxLength - 1 ) * pxQueue->uxItemSize );
    /* Clear the event list waiting to send */
    while (listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
        if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
            /* The task waiting has a higher priority than this task. */
            xYield = pdTRUE;
    if (xYield == pdTRUE) {

Now I have spotted some simlilar code by Richard Barry which also mentioned rxLocks and txLocks. I have not touched these as I am not sure what they do exactly.  I have tested the code, and it works. It also saves real time when trying to use large queues. I created a queue of 5000 elements and when using this queue free function it saved 16ms of execution on an STM32 @ 72MHz.

It would be great if this could be included in the next version of FreeRTOS.

Regards Paul

davedoors wrote on Friday, June 18, 2010:

Can you post a link to this message in the feature request tracker?

I presume for a complete implementation you would also need to clear the tasks waiting to receive.

dkure wrote on Friday, June 18, 2010:

As this is a flushing of the queue, I think that tasks that are waiting to receive still need to be blocked, waiting on data.

Its meant to flush the queue of all data, not necessarily reset the queue back to the default state.