RTOS shared resource protection - mutual exclusion realizes the protection of shared resources

Mutex realizes the protection of shared resources

overview

The previous section described the problem of priority inversion that may occur when using binary semaphores to protect shared resources. The mutex semaphore described in this section is one of the strategies to improve the priority inversion problem.

Mutex is a special binary semaphore with a priority inheritance mechanism added. Its typical structure is as follows:
insert image description here

As shown in the figure above, the mutual exclusion semaphore is a transformation of the binary semaphore. Like the binary semaphore, it does not require a message buffer, and the maximum number of messages is 1. On the basis of the binary semaphore, the mutex semaphore adds the "owner" field (indicating which task the current mutex semaphore is using), and records the current occupancy of the signal through the "owner's original priority" field The priority of the amount of tasks.

How mutex semaphores reduce the effects of priority inversion

When using a mutex semaphore, when priority inversion occurs, a priority inheritance mechanism (priority inheritance) will be triggered to speed up the running of low-priority tasks, and then allow high-priority tasks to run as soon as possible.

insert image description here

First of all, high-priority task 3 cannot occupy LED light resources from task 1 in a "robbery" manner (*Because the priority of the task only determines the priority level of obtaining CPU resources, it cannot determine the allocation of non-CPU resources, For example, the LED light resource here* ), but in order to get the execution as fast as possible

Guess you like

Origin blog.csdn.net/wangyx1234/article/details/128067826