【发布时间】:2015-04-13 09:45:06
【问题描述】:
我们知道锁定自旋锁会禁用相关处理器上的抢占。所以现在,假设执行的内核代码调用了一个使进程休眠的函数。尽管抢占停用,Linux内核是否会将处理器交给另一个线程?
【问题讨论】:
标签: linux multithreading linux-kernel linux-device-driver device-driver
我们知道锁定自旋锁会禁用相关处理器上的抢占。所以现在,假设执行的内核代码调用了一个使进程休眠的函数。尽管抢占停用,Linux内核是否会将处理器交给另一个线程?
【问题讨论】:
标签: linux multithreading linux-kernel linux-device-driver device-driver
这取决于。有一系列cond_resched() 函数正在检查是否设置了抢占位并因此禁用重新调度:
/*
* Returns true when we need to resched and can (barring IRQ state).
*/
static __always_inline bool should_resched(void)
{
return unlikely(!preempt_count() && tif_need_resched());
}
int __sched _cond_resched(void)
{
if (should_resched()) {
__cond_resched();
return 1;
}
return 0;
}
但并非所有内核例程都这样做。 IE。互斥锁直接调用schedule_preempt_disabled(),它忽略了原子性检查。在这种情况下,schedule() 会尝试将 CPU 分配给另一个任务,但会抱怨“原子时调度”。
【讨论】: