今天在公司使用RT-Thread时发现它的信号量会导致优先级翻转的问题。所以根据官网的提醒,尽可能的使用互斥量。
因为下一个项目是linux的肯定会用到多线程,所以赶紧分析下是否linux的信号量会出现这个问题吗?
打开我用的linux版本linux-4.15中的linux-4.15\linux-4.15\kernel\locking\semaphore.c看到了如下的代码
/* Functions for the contended case */
struct semaphore_waiter {
struct list_head list;
struct task_struct *task;
bool up;
};
struct task_struct *task;指向进入临界区的进程。然后我们看看到include\linux\sched.h中看task_struct的结构体其中有
struct task_struct {
...
int prio;
int static_prio;
int normal_prio;
unsigned int rt_priority;
...
}
说明在linux下应该是有用优先级继承或优先级天花板的,所以可以放心使用。