Check if queue is empty!

ulmus71 wrote on Tuesday, February 28, 2012:

I must check if wueue is empty, becasue i have to turn off hardware, but i must be sure that all is received by this hardware. Now i can only do queuepick to check if it has something or not, but it does receive. I want only check if it is empty or not to wait   for queue is empty if i want to turn off hardware.
P.S. it is thermal printer, in other tasks i send into queue some data, another task just sends it to printer, but printing takes time to empty queue. In some place i want to turn off printer power but i have to be sure that all data from queue is received by printer.
Is there simple macro e.g. IsQueueEmpty() without doing unnecesasary receive like in QueuePick?

richard_damon wrote on Tuesday, February 28, 2012:

uxQueueMessagesWaiting will tell you how many items are currently in the queue, 0 means it is empty.

ulmus71 wrote on Tuesday, February 28, 2012:

Thanx richard_damon,
yes I know it, i even found in queue.h:
xQueueIsQueueEmptyFromISR
but version without ‘FromISR’ is only in queue.c file :frowning:
No one of above functions are not in API and it is why i am asking. I want to know for sure that i can use function and in next freertos version it will be, becasue if it is not in API then name of that function may change.
In queue.c there is function to chack if queue is empty outside ISR and From ISR. If it would be in API then i would be very happy…

rtel wrote on Tuesday, February 28, 2012:

You can also call xQueuePeek() with a block time of zero.  It will not remove any items from the queue.  You might also consider calling xQueueReceive() with a block time of zero, if there is something in the queue I presume you want to process it, and if there isn’t it xQueueReceive() will return pdFALSE.

Regards.

ulmus71 wrote on Tuesday, February 28, 2012:

Hi Richard!
I dont want to process this queue. I want only to know if it is empty already. I can do xQueuePick but it is a waste of time to do receiving if i dont want to know results. Of course i have to prepare receiving buffer for pick… next wasting of time…

I thought that there is something that i can use to check if queue is empty.
I think however  the best choice is to use uxQueueMessagesWaiting (in queue.h) All these functions use the same uxMessagesWaiting
it is used by prvIsQueueEmpty and xQueueIsQueueEmptyFromISR and  uxQueueMessagesWaiting and uxQueueMessagesWaitingFromISR and maybe one or two more.

Hmmm, last question:
can i use

‘uxQueueMessagesWaiting’
and
‘uxQueueMessagesWaitingFromISR’

without worring that it dissapear in future releases of FreeRTOS? Or maybe you plan to add that functions to API on the website? That would be great to have it documented :slight_smile:

best regards

rtel wrote on Wednesday, February 29, 2012:

can i use

‘uxQueueMessagesWaiting’
and
‘uxQueueMessagesWaitingFromISR’

without worring that it dissapear in future releases of FreeRTOS? Or maybe you plan to add that functions to API on the website? That would be great to have it documented :slight_smile:

uxQueueMessagesWaiting is already documented in the API reference available on the website.

http://www.freertos.org/a00018.html#ucQueueMessagesWaiting

I need to do an audit to see if there are any functions that are not documented, which there could be.

Regards.

ulmus71 wrote on Wednesday, February 29, 2012:

Thanx Richard!
Its great to see it:) What about …FromISR() ? :wink:

ulmus71 wrote on Wednesday, February 29, 2012:

Hi again…

Remember folks not to use loop like this:

	while(uxQueueMessagesWaiting(xTransmitQueue)>0);

I know that it has critical region and it disables interrupt, but… why

	while(uxQueueMessagesWaitingFromISR(xTransmitQueue)>0);

does the same? I mean, why my app hangs when i use that loop without entering critical region and without dissabling interrupts? It just checks variable, nothing more, and it that case it doesnt matter that i am calling uxQueueMessagesWaitingFromISR not from ISR, i checked that that variable ‘uxMessagesWaiting’ in queue struct is ‘volatile’ then it should work, i think.

In the end i have to use

	while(uxQueueMessagesWaiting(xTransmitQueue)>0) vTaskDelay(1);

to get this loop working, and even better in this case is using:

	while(uxQueueMessagesWaitingFromISR(xTransmitQueue)>0) vTaskDelay(1);

definition of queue struct is in the queue.c file then the only way of  checking only that volatile variable is to use
uxQueueMessagesWaitingFromISR(), but, in this case, why i have to use vTaskDelay(1) in ‘while’ loop since i dont disable interrupts??? i dont know….

richard_damon wrote on Wednesday, February 29, 2012:

uxQueMessagesWaiting has a short critical section as it needs to access two pointers, and wants to make sure that nobody can add or remove messages while it is performing its calculation. The returned result may not be how many messages are still in the queue, but will be a count of how many messages were in the queue at one point during the operation of the function.

As to using while loops to poll for the message. that is just bad practice. The issue is that while you are looping on this test, no lower priority tasks will get to run. If a lower priority task needs to run to put something into the queue, it will never happen.

If you want to wait for data, use a QueueGet, or a QueuePeek if you don’t want to remove the data. Then the task will be suspended until the queue has data and automatically resumed.

ulmus71 wrote on Wednesday, February 29, 2012:

QueueGet? What is it?

richard_damon wrote on Wednesday, February 29, 2012:

xQueueReceive

fazjaxton wrote on Thursday, April 12, 2012:

An audit would be nice.  There are some functions (e.g. uxQueueMessagesWaitingFromISR) that are not documented in the API section of the website.

I was thinking about getting the Reference Manual:  http://shop.freertos.org/FreeRTOS_Reference_Manual_PDF_p/pdf-reference-manual.htm  Is it up to date?  Thanks.

paulgcoleman wrote on Friday, April 13, 2012:

Yes that function is documented in the manual I have which is September 2011 (V1.2.0)