Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

Check if queue is empty!

Posted by ulmus on 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?

RE: Check if queue is empty!

Posted by Richard Damon on February 28, 2012
uxQueueMessagesWaiting will tell you how many items are currently in the queue, 0 means it is empty.

RE: Check if queue is empty!

Posted by ulmus on 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 :(
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...

RE: Check if queue is empty!

Posted by Richard on 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.

RE: Check if queue is empty!

Posted by ulmus on 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 :)

best regards


RE: Check if queue is empty!

Posted by Richard on 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 :)”


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.

RE: Check if queue is empty!

Posted by ulmus on February 29, 2012
Thanx Richard!
Its great to see it:) What about ...FromISR() ? ;)

RE: Check if queue is empty!

Posted by ulmus on 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....

RE: Check if queue is empty!

Posted by Richard Damon on 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.

RE: Check if queue is empty!

Posted by ulmus on February 29, 2012
QueueGet? What is it?

RE: Check if queue is empty!

Posted by Richard Damon on February 29, 2012
xQueueReceive

RE: Check if queue is empty!

Posted by Kevin on 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.

RE: Check if queue is empty!

Posted by Paul Coleman on April 13, 2012
Yes that function is documented in the manual I have which is September 2011 (V1.2.0)


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




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists