queue flush

nobody wrote on Monday, September 05, 2005:

Hello.

Is there any smart way to implement a xQueueFlush to empty a queue other than

Enter critical
while(not empty)
  get queue item and delete
exit critical

BR. Lars

rtel wrote on Thursday, September 08, 2005:

Sorry for the delay in responding  but I have been out of the office for the last 4 days.

Items are queued by copy.  They are copied into the buffer allocated to the queue when the queue is created.  It is not strictly necessary therefore to delete the queued item, unless your application implementation (rather than the scheduler implementation) dictates this.  For example, if the queued item is a pointer that points to dynamically allocated memory, and losing the queued item would mean there was no way of referring to the memory in order to free it again.

A queue can be emptied by simply resetting its structure members back to their starting condition, and in so going indicating that the queue is empty.  I think the following code should achieve this, but have not tested this:

------
pxQueue->uxMessagesWaiting = 0;
pxQueue->pcWriteTo = pxQueue->pcHead;
pxQueue->pcReadFrom = pxQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );
pxQueue->xRxLock = queueUNLOCKED;
pxQueue->xTxLock = queueUNLOCKED;

vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
------

Look at the function xQueueCreate() in queue.c for uxQueueLength and uxItemSize definitions.

You would also have to ensure that no tasks were blocked waiting on queue events (items to be posted onto the queue, or space to become available on the queue).

Regards.

jschiavoni wrote on Monday, November 24, 2008:

Hi,

Also I need to flush a queue, and I need to know if this implementation of locks is fine:

taskENTER_CRITICAL();
{

pxQueue->uxMessagesWaiting = 0;
pxQueue->pcWriteTo = pxQueue->pcHead;
pxQueue->pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength = uxQueueLength - 1 ) * pxQueue->uxItemSize );

pxQueue->xRxLock = queueUNLOCKED;
pxQueue->xTxLock = queueUNLOCKED;

vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );

}taskEXIT_CRITICAL();

Regards
Juan