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


Best practice for determining busy status of a task processing a queue

Posted by liteyear on November 7, 2016

In any multithreaded design I find myself often implementing this sort of pattern:

~~~ void mytask() { while(1) { waitonqueue(requestQ, &theOperation); raiseflag(busy);




void requestoperation(newOperation) { addto_queue(requestQ, newOperation); } ~~~

This works pretty well when only one other task is requesting operations and waiting on the results. When you have multiple tasks doing the same however, you quickly run into edge cases where for example the task is between wait_on_queue() and raise_flag() or lower_flag() and wait_on_queue(). In both those cases, it is difficult to determine whether it is safe to assume the task is idle.

For example, imagine the implementation of a "wait_for_operation_to_complete()" function in another task. If you call request_operation() and then wait on len(queue) == 0 && !busy, you could return early because the condition is true in between wait_on_queue() and raise_flag().

In the pthreads world I've successfully modified this pattern to only pop the operation off the queue after processing it. That way len(queue) == 0 is a good indication of idleness. If I were to replicate this in FreeRTOS it might look like this:

~~~ QueueHandle_t gOperationQueue = NULL;

void my_task(void *pvParameters) { gOperationQueue = xQueueCreate(3, sizeof(sOperation));

    xQueuePeek(gOperationQueue, &theOp, portMAX_DELAY);


    xQueueReceive(gOperationQueue, &theOp, portMAX_DELAY);


void requestoperation(newOp) { while(!gOperationQueue) vTaskDelay(5 / portTICKPERIOD_MS);

xQueueSend(gOperationQueue, newOp, 10 / portTICK_PERIOD_MS);


bool taskisidle(void) { return uxQueueMessagesWaiting(gOperationQueue) == 0; } ~~~

This is neat because it removes the busy semaphore (although you'd still need to add a mutex if you want concurrent access to variables that the task can update), but I wonder if this is best practice, given that the xQueuePeek and xQueueReceive functions don't seem to hint at this sort of usage (eg. the &theOp argument is required for both, even though it is useless in the xQueueReceive call).

How do others tend to implement this sort of pattern? It seems like such a common paradigm with not a lot written about it (that I've seen).

Best practice for determining busy status of a task processing a queue

Posted by richard_damon on November 7, 2016

My question is what do you need with the busy flag? If the task doing this processing should only have one request in it at a time, that sounds like you want a mutex guarding the access to the task. I would then use a pair of semaphores, one to trigger to that task that a request is pending (and using a shared memory buffer to send the request, alternatively you could use a queue to transfer it) and a second one the task uses to signal the results are ready.

More often, I see something sort of like what you are describing as part of a device driver, where the calling task is going to be held up until the operation is completed, in which case you don't need a seperate task, but that it runs within the 'calling task' (with a mutex so only one task can be actualy within the driver).

Best practice for determining busy status of a task processing a queue

Posted by liteyear on November 8, 2016

Fair question. Let me be a bit clearer about the purpose of the busy flag. It's different to the two scenarios you describe.

In a couple of recent examples the task was a file system task. Operations were load/save/delete/list type things. Imagine that periodically a few operations get queued up - mount disk, get free space, list files, etc, which keep the internal state up to date if anything changes. Then imagine there are two other tasks that occasionally want to know what the current file list is. They shouldn't request their own "list file" operation and wait for that to finish because there will already be such an operation in the queue if necessary. It's not enough to mutex the file list because the file system task could be busy doing other things first. What they need to know is that the file system task has finished all its operations - that is, it's not busy and is up to date.

There are other scenarios - say the initialisation task asks the file system task to do a bunch of things nice and early during startup, and then launches tasks A and B. At some point both task A and B need some information about the state of the file system, but neither of them made the request for that initialisation to occur, so they can't just check if their own operations have completed. What they should do is check if file system task is busy. If not, then they know the initialisation operations have completed and they're right to extract the state details.

So it's really a "have you got anything on your plate at the moment?" type flag.

[ 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