Optimizing Task Handling with vTaskSuspend/vTaskResume in FreeRTOS

Hello,

I have a small task in FreeRTOS that functions as a Finite State Machine (FSM). When it reaches the “DONE” state, the task suspends itself using vTaskSuspend(), and it is resumed by another task using vTaskResume() when needed.

My Current Setup:

  • FSM runs until the “DONE” state, where it calls vTaskSuspend().
  • Another task triggers it back using vTaskResume().

This setup works fine, but I’m wondering if there are more efficient alternatives to vTaskSuspend() and vTaskResume() for such small tasks, without restructuring the FSM or using blocking loops. Would using task notifications or another mechanism be more optimal?

Any advice or suggestions would be appreciated!

Is it possible for the other task to call vTaskResume() before the FSM task calls vTaskSuspend()? If not, then using suspend and resume is probably ok. If so, you will have a race condition because the “resume” operation isn’t latched - the FSM task has no way of knowing it has already been called and it should start again. Using a counting semaphore (i.e. give the FSM task takes the semaphore instead of suspending, the other task gives the semaphore instead of resuming in the FSM task) or equivalent with task notifications will work just as well and prevent the race condition because the actions are latched (remembered).

1 Like