Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Priority inversion and solution under SylixOS

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. Concept

When a high-priority thread accesses a shared resource through a semaphore mechanism, the semaphore is occupied by a low-priority thread, and this low-priority thread is preempted by other medium-priority threads when accessing the shared resource. as a result, high-priority threads are blocked by many threads with lower priority, which is called priority inversion.

Priority inversion will cause low-priority tasks to run before high-priority tasks, which will lead to uncontrollable phenomena in real-time systems. therefore, priority inversion is unacceptable in real-time systems.

two。 Resolve priority inversion

The reason why the high priority task can not be executed is that the low priority task takes up resources, while the low priority task can not get CPU and can not release resources, so the principle to solve the priority inversion is to let the low priority task execute as soon as possible and release resources.

Priority inversion under SylixOS includes priority ceiling policy (priority ceiling) and priority inheritance strategy (priority inheritance), and mutex semaphores under SylixOS support both of these strategies.

2.1 priority ceiling

Priority ceiling policy refers to raising the priority of tasks that occupy a resource to the highest priority of all tasks that may access the resource (this priority is called the priority ceiling of the resource).

2.2 priority inheritance

When it is found that a high-priority task is blocked by a low-priority task occupying resources, the priority of the low-priority task is raised to the priority of the highest-priority task waiting for the resources it occupies.

3. Mutex solves priority reversal 3.1 mutex attribute block

When a mutex is created, it is necessary to create a mutex attribute block. The structure of the mutex attribute block under SylixOS is shown in listing 3.1.

Program listing 3.1 Mutual exclusion attribute block structure

Typedef struct {int PMUTEXATTR_iIsEnable; / * whether this attribute block is valid * / int PMUTEXATTR_iType; / * mutex type * / int PMUTEXATTR_iPrioceiling; / * ceiling priority * / unsigned long PMUTEXATTR_ulOption / * algorithm type * /} pthread_mutexattr_t

When a mutex is initialized, the default attribute is used if it is not initialized for the mutex attribute block, and the SylixOS mutex default attribute block is shown in listing 3.2 of the program.

Listing 3.2 default mutex attribute block

Static const pthread_mutexattr_t _ G_pmutexattrDefault = {1, PTHREAD_MUTEX_DEFAULT, / * allow recursive calls to * / PTHREAD_MUTEX_CEILING, (LW_OPTION_INHERIT_PRIORITY | LW_OPTION_WAIT_PRIORITY) / * PTHREAD_PRIO_NONE * /}

As can be seen from the default mutex default attribute block, the default solution for priority inversion under SylixOS is priority inheritance strategy.

3.2 increase priority

A low-priority task occupies a resource, and when a high-priority task requests a resource, it attempts to increase the priority of the low-priority task, and the _ EventPrioTryBoost function is called when the resource is requested to try to increase the task priority.

# include VOID _ EventPrioTryBoost (PLW_CLASS_EVENT pevent, PLW_CLASS_TCB ptcbCur)

Function _ EventPrioTryBoost prototype analysis:

Parameter pevent is the event control block to which the resource belongs

The parameter ptcbCur is the current task control block.

According to the attribute settings of the mutex attribute block, this function determines whether to choose the priority inheritance policy or the ceiling policy (the priority inheritance policy is used by default under SylixOS), and calls the _ SchedSetPrio function to change the priority of the task.

3.3 recovery priority

When a low-priority task releases a mutex, a call to EventPrioTryResume attempts to restore the priority of the task.

# include VOID _ EventPrioTryResume (PLW_CLASS_EVENT pevent, PLW_CLASS_TCB ptcbCur)

Function _ EventPrioTryResume prototype analysis:

Parameter pevent is the event control block to which the resource belongs

The parameter ptcbCur is the current task control block.

When a low-priority task deletes a mutex, it attempts to restore the task priority, as shown in listing 3.3.

Listing 3.3 attempts to restore priority

If (ptcb) {ucPriority = (UINT8) pevent- > EVENT_ulMaxCounter; / * get the original thread priority * / / * owner priority has changed, restore priority * / if (! LW_PRIO_IS_EQU (ucPriority, ptcb- > TCB_ucPriority)) {_ SchedSetPrio (ptcb, ucPriority);}} 4. Priority setting function

When using mutexes, try to raise or restore the priority of the task and call the SchedSetPrio function to set the priority.

# include VOID _ SchedSetPrio (PLW_CLASS_TCB ptcb, UINT8 ucPriority)

Function _ SchedSetPrio prototype analysis:

Parameter ptcb is the current task control block

The parameter ucPriority is the priority to be set.

The _ SchedSetPrio function changes the priority of the task, and the implementation process is shown in figure 4.1.

Figure 4.1 priority setting process

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report