I have five tasks.
1 and 2 are vying for a resource. They make requests to 3. 3 will update them they will wait for 3 to tell them it’s safe to proceed.
3 determine who has priority on that resource and will ask 4 and 5 to do their work to make that available.
4 and 5 will listen to commands from 3, then report their success back to 3.
I could do this with getters and setters. I could have extern memory objects all over. If you think about the arrows of communication it puts 3 in the middle everyone has to know 3, and 3 everyone else… Not so bad with 6 tasks, but imagine I wanted an all seeing logging task or added 5 more to each group.
I’m considering an EventBus instead. Where 1,2,3,4,5 all sub to messages they want from EB and EB will pub to each of the tasks on event.
What is the best FreeRTOS way to do this? I’ve had ups and downs.
-
It seems like using an eventGroup for each of the tasks is nice. I can sub by sending EB my eventGroup pointer and the flags I am interested in. eventGroups limit me to 24 “data events” which is probably enough, but it’s not leaving a ton of room to grow.
-
I don’t really want 1 and 2 to have extern / direct pointer access to 3’s internal data because that’s not threadsafe… But this leads me to queues. And queues have an issue that I really don’t want a stack of data updates as they happened chronoglically, just an element size of 1 would be fine so I have lastest data when I need it - but now we’re talking 76 Bytes for the queue plus the size of the object. Times 24 possible data events, that’s 1800 bytes overhead before I’ve done any work at all and indicates I need to share these mailboxes (queue of 1 item) and that none of my tasks consume only peak.
-
I could pass a pointer from 1 or 2 to 3, but I also need to pass a mutex pointer. Now 3 is able to directly read the memory from another task, but it’s pretty important that it take control of the mutex first. In this case, every write and read is mutex protected, so long as everyone plays nice and there is no extra memory or thread safety issues. This is not without disadvantages, I wrote mutex, but those are just empty queues iirc, a binary sempaphore is probably what I want here to avoid the cost of queues. Although there is a questionable priority issue.
That’s it so far as I can see. I want to share 3’s output with 1 and 2 or 3 and 4, so without knowing who already saw the data and when, there is really no way to know it’s safe to write… right? But using read and writes semaphores shared as part of the “event” should cover this.
I think there has to be a more elegant solution.