ISR to take/give binary semaphore to pause task execution?

I’m trying to pause/run execution of a task based on toggling from an interrupt (push button). I thought using a binary semaphore for this would make sense so I created my ISR to use either SemaphoreGiveFromISR() and xSemaphoreTakeFromISR() based on the value of a flag, isRunning. Within the task’s for loop I start with if(xSemaphoreTake(handle) == pdTRUE)…….. and end with xSemaphoreGive(handle). This does not work… I can see that the give starts the task running but the take doesn’t do anything (returns pdFALSE). I suspect this is because when the ISR runs the semaphore is actually held by the task and I interrupted the task in the middle of execution, therefore the ISR wouldn’t be able to take it. However, the task MUST finish its execution cycle (code within the for loop) completely or the hardware I’m working with will be left in an inconsistent state the next time the task was able to run. Because of this, even if the ISR were able to stop the task from running, it should only be able to stop it if all execution of the for loop has completed. I’m still trying to wrap my mind around this so any suggestions of a better way to do this or explanations would be great!

ISR to take/give binary semaphore to pause task execution?

You could have the task take the semaphore and then immediately give it back, thus the only point where the ISR couldn’t pause the task is that small window. The other option is just use the global variable isRunning, and if it is clear, pend on the semaphore, and if set just keep will. Then the ISR to stop the task clears isRunning, and to start running sets isRunning and gives the semaphore to restart the task if it is pending on it.

ISR to take/give binary semaphore to pause task execution?

Can you please explain the second part?

ISR to take/give binary semaphore to pause task execution?

Global variable run and semaphore seem. ISR to stop running: run = 0; ISR to resume running: run = 1; give seem; Task at the top of the loop: while(!run) wait for sema;