edwards3 wrote on Thursday, May 26, 2011:
Suppose I´m writing a task supervisor, and it discovers a problem with a task holding a mutex, or a MutexTake timesout,
Would the easiest thing not to be to re-write the task so it does not contain that bug?
then i´d need the mutex released and the task holding it doesn’t do it.
If I vTaskDelete(mutexHolder); does the mutex get released?
No, it will not release the semaphore. It will remove the task being deleted from any block list if the task is waiting for a semaphore, but that is all. If the supervisor task can detect that there is a problem with the sempahore then it can release the semaphore before deleting the task. Probably best to do it that way round, although it would also probably work the other way around too (delete the task then release the semaphore).
And in a similar case, suppose I decide to suspend a task for the time being, and part of the task involves taking a mutex:
- Does the suspend cause the mutex to be released?
Of course not, how could that be a logical thing to do? If it held a semaphore when it was suspended, when it was unsuspended it must also hold the semaphore, otherwise you will get into all sorts of problems if the semaphore is used for mutual exclusion. The task will not know when it is being suspended and unsuspended, or that that had happened, so won’t know it no longer has the semaphore. I don’t think there is a way for the task doing the unsuspending to give the semaphore back to the task being unsuspended. A semaphore can only be taken by a task, not passed to another task.
- Can I check with the task to see if the mutex is taken before I suspend (or delete) it? Or do I have to add my own flag?
I don’t think there is an easy way of checking, although you can add in a mechanism to the semaphore itself to see which task holds it. I think that information is held in the semaphore anyway (it is in a mutex semaphore I think) so you could add a simple query function. Alternatively, have the task set a flag as it takes the semaphore, then just check the flag.