Behaviour of nested calls to xSemaphoreTake with mutex in same task

pugglewuggle wrote on Tuesday, December 22, 2015:

If a task has already obtained a mutex will a calling xSemphoreTake on the same mutex in the same task (for example, in a function being called) succeed or fail?

rtel wrote on Tuesday, December 22, 2015:

A standard mutex can only be taken once, so nested calls to attempt to take a standard mutex will fail. This is quite easy for you to test, or determine from the code.

If you want the possibility to obtain the same mutex twice (at once) then you will instead need to create a recursive mutex.

pugglewuggle wrote on Wednesday, March 02, 2016:

To be clear, do I need to give that mutex the same number of times I take it?

richard_damon wrote on Wednesday, March 02, 2016:

If it is a non-recursive mutex, if you already have it, you shouldn’t call take again.

If it is a recursive mutex, you need to give it as many times as you took it.

Note, recursive mutexes use xSemaphoreTakeRecursive/xSemaphoreGiveRecursive instead of the simple xSemaphoreTask and xSemaphoreGive.