In any multithreaded design I find myself often implementing this sort of pattern:
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(). 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
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);
vTaskDelay(5 / portTICKPERIOD_MS);
xQueueSend(gOperationQueue, newOp, 10 / portTICK_PERIOD_MS);
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
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).
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).
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.
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.