Priority in FreeRTOS

In FreeRTOS, Is it true that if a lower priority task is holding a mutex to protect a shared resource and a higher priority task attempts to acquire the same resource, the higher priority task will be blocked until the lower priority task releases the resource. This can cause a delay in the execution of the higher priority task until the resource becomes available. Once the mutex is released by the lower priority task, the higher priority task will be allowed to access the resource.

Yes, that is correct, since the Higher Priority task has said it need the Mutex, it will be blocked until it is available (or the timeout on the take expires).

With Mutexes, the lower priority task will temporarily inherit the priority of the higher priority task, so that a middle priority task can’t keep the low priority task from completing what it needs to do to release the Mutex.

3 Likes

In the context of priority, Is it true that semaphore counting operates with a slight variation. When multiple tasks of different priorities are waiting to acquire the semaphore, the highest priority task will be unblocked and allowed to proceed once the semaphore becomes available.

This mechanism ensures that even if a lower priority task is currently holding the semaphore, a higher priority task attempting to acquire it will preempt the lower priority task. Consequently, the higher priority task gains access to the resource once it becomes available.

That’s right. Waiting resp. blocking and unblocking only happens if the semaphore count already dropped down to 0 when a task wanted to aquire/take it.

That’s only right if the semaphore was not yet counted down to 0 means no task is actually holding the semaphore. Otherwise the task attempting to aquired the semaphore is blocked regardless of its priority.

Again a (counting) semaphore is semantically and also technically different to a mutex.
Which bigger problem do you want to solve ? Is it similar to the train station example in the post mentioned before ?

1 Like

I wanted to ensure I understand correctly that mutexes support priority inheritance, while semaphores do not.

That is correct, and again, it is worth mentioning that the support for priority inheritance is very closely related to the concept of ownership, which is unique to muteces.