Hi,
I have a question on the correct usage of queues in Zephyr.
I have a firmware program that uses multiple threads, and as it has developed over the years multiple threads can write to a single flash buffer, before triggering it to be written. Fairly early on I separated the actual writing to the flash into it's own thread, so multiple threads could not attempt to write at the same time. The buffer for the flash block is protected by a mutex, so multiple threads CANNOT write to the buffer before it is completed, which has worked fine and I have never noticed any issues. I have multiple functions that will write to the flash buffer, depending on what they are adding (data reading / mqtt message / information) and they are all protected by the mutex. As they are pretty much only copying some memory to another part of memory along with some minimal operations, I don't think it is necessarily an issue as it won't take much time to process.
I have come to a point where I am refactoring a lot of code, and am wondering whether I should be doing this differently. I initially thought of work queues, but after looking into it, it seems like this only really works if the function of the work is always the same. I am not sure if there is a nice way to actually pass through a buffer to write. I have seen this example where you pass it through your own structure with the work item as one of the elements, and get the container of it in the the work function, but that seems such a hacky / convoluted way to do it.
void print_error(struct k_work *item)
{
struct device_info *the_device =
CONTAINER_OF(item, struct device_info, work);
printk("Got error on device %s\n", the_device->name);
}
Then I also found message queues, which I guess makes more sense, as you can pass through specific buffers to save, and the consumer thread can deal with the type of detail to add to the flash.
I realise there may be an even better way to do this that I haven't found, but the question is, what is the correct way to do this?
Work queue, Message Queue, or stick with the Mutex protection?
Thanks,
Damien