freertos-addons icon indicating copy to clipboard operation
freertos-addons copied to clipboard

Why does Tasklet require a Mutex?

Open srobertjames opened this issue 1 year ago • 3 comments

We're evaluating the freertos-c++ lib for possible usage. Can you explain why Tasklet involves a Mutex? That doesn't seem to be the typical usage pattern in the underlying FreeRTOS C implementation.

srobertjames avatar Sep 21 '23 06:09 srobertjames

It's to protect against race conditions that might occur if/when the Tasklet is deleted. You can see an example usage in https://github.com/michaelbecker/freertos-addons/blob/master/Linux/Demo/Linux_g%2B%2B_tasklets/main.cpp

michaelbecker avatar Sep 21 '23 14:09 michaelbecker

Thanks. By "deleted" do you mean "deleted from RAM via delete operator"? Or "deleted from FreeRTOS Task List"?

srobertjames avatar Sep 21 '23 15:09 srobertjames

Both. If you are using the C++ wrappers and you decide you want to permanently delete a Tasklet object, then you should be calling the delete operator, either directly or indirectly (like when a local object goes out of scope). Just make sure you include a call to CheckForSafeDelete() in your destructor to prevent any weird race conditions. In that code I've leveraging the semaphore/mutex again to guarantee FreeRTOS doesn't try to run your tasklet code after you've deleted it.

michaelbecker avatar Sep 21 '23 17:09 michaelbecker