It is mentioned in multiple freeRTOS documentations that Mutexes should not be used from an interrupt (ex: https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html). I do understand why we shouldn’t “take” a mutex in an interrupt (blocking call/priority inheritance logic) but I am not clear why we shouldn’t be able to “give” a mutex from an interrupt?
Today if I can’t “give” a mutex in an ISR, I’d have to create what seem to be a redundant synchronization semaphore btw the calling task and the ISR and implement mutual exclusion within the task.
The key point on this is that the mutexes have special code optimized for use in resource management to provide exclusive access (mutex = mutual exclusion).
As such, the usage pattern for a mutex should always be:
Take the mutex (then if successful)
Use the resource
Release the mutex.
As such, the mutex should always be given back by the task that took it. If anyone else releases it, then the possible sequence could be
Process A takes the Mutex
Process A uses the Resource
Someone else releases the mutex
Process B takes the Mutex
Process A and B both try to use the resource !!! Bad things might happen.
If there really is a need for someone else to be able to give the flag back, then you should be using a semaphore (and carefully think if you are creating a problem).
Additionally mutexes are associated with tasks because they include a (task) priority inheritance mechanism, which is out of context with in interrupt.
It sounds to be that what you really should be using is a Binary Semaphore, not a mutex. While they both use SemaphoreHandle_t in FreeRTOS, they have different use cases. A binary semaphore is simply a semaphore with a count of one. A mutex is used specifically for mutual exclusion. It is a perfectly normal usecase for an interrupt to give a semaphore to a binary semaphore. It’s not normal for an interrupt to unlock a mutex.
Even the termininology between binary semaphores and mutexes are different. Mutexes are generally said to be “locked” and “unlocked”, not “given” and “taken” as in a semaphore. So you should be thinking about them in a different way.