Will xSemaphoreTakeFromISR block?


My question is that, will the call to xSemaphoreTakeFromISR, if called from an ISR routine, block forever if the respective semaphore is currently held by a normal task or if the semaphore is held by a normal task then the ISR simply exists with an error status?

For example, If I have TaskA which has taken a semaphore ‘SemX’ using xSemaphoreTake, meanwhile an interrupt arrived and it wants to acquire the same semaphore ‘SemX’ using xSemaphoreTakeFromISR then will it block the system forever as ISR will be higher priority routine waiting on to take ‘SemA’ or operating system can handle such a situation.


Hi Junaid,

Because xSemaphoreTakeFromISR() is intended for execution in an ISR, it will not block. The function returns “pdFALSE if the semaphore was not successfully taken because it was not available”.

Thank you for confirming.

In general, NO “FromISR” function will block, and thus don’t take a wait time. On most processors there is no way to leave an ISR, and resume executing tasks and then go back into the ISR at a later point. If you need that sort of behavior, then you just use a minimal ISR that unblocks a (likely high priority) task that does the work. That “ISR Task” being an actual task, can do anything a regular task can do, it also can get interrupted by other interrupts.

See https://www.freertos.org/FAQ_API.html#IQRAPI and https://www.freertos.org/xSemaphoreTakeFromISR.html

Would this be a case where a queue might be better to use than a Semaphore? Since a queue isn’t blocking (as long as there are enough slots) then the ISR would never have that problem.