Mutex type to wait for all tasks to stop
Posted: Thu Feb 01, 2024 5:15 pm
Hello, could you recommend me a mutex/semaphore use to wait for all tasks to stop?
Each task of interest has onStart, onResume and onStop handlers and main loop which waits for a Notification. If it receives a notification to stop, it calls its onStop handler.
There are particular situations where I want to wait for all tasks to finish their onStop() function before proceeding. Like before going to sleep, or after configuration is updated and I want to ensure all tasks are terminated before starting them again (e.g. tasks with sockets will have to wait for wifi connected event bit again and don't just start right away because wifi task haven't stopped yet and that bit wasn't cleared).
I was considering Counting Semaphore, but it seems it has reversed logic to what I need - it allows to "take" whenever there is at least one "give", but I need to wait until there is certain amount of "gives".
It seems the best blocking function for this purpose would be xEventGroupWaitBits() - with least unnecessary context switches.
But I can't figure out how many event bits I can use - in FreeRTOS docs it says there is 8 bits per group if configUSE_16_BIT_TICKS is set, 24 bits otherwise, and there will be about 15 tasks. I can't find configUSE_16_BIT_TICKS in sdkconfig, but I'm not sure if its not set somewhere else.
Each task of interest has onStart, onResume and onStop handlers and main loop which waits for a Notification. If it receives a notification to stop, it calls its onStop handler.
There are particular situations where I want to wait for all tasks to finish their onStop() function before proceeding. Like before going to sleep, or after configuration is updated and I want to ensure all tasks are terminated before starting them again (e.g. tasks with sockets will have to wait for wifi connected event bit again and don't just start right away because wifi task haven't stopped yet and that bit wasn't cleared).
I was considering Counting Semaphore, but it seems it has reversed logic to what I need - it allows to "take" whenever there is at least one "give", but I need to wait until there is certain amount of "gives".
It seems the best blocking function for this purpose would be xEventGroupWaitBits() - with least unnecessary context switches.
But I can't figure out how many event bits I can use - in FreeRTOS docs it says there is 8 bits per group if configUSE_16_BIT_TICKS is set, 24 bits otherwise, and there will be about 15 tasks. I can't find configUSE_16_BIT_TICKS in sdkconfig, but I'm not sure if its not set somewhere else.