如何正确唤醒中断处理程序中的进程 [英] How correctly wake up process inside interrupt handlers
问题描述
简而言之,在 read
方法中,我检查变量是否为0,如果是,则将当前进程置于睡眠状态:
Briefly, in a read
method i check if a variable is 0 and if it's i put the current process to sleep:
static ssize_t soc2e_read(struct file *filp, char __user *buf,
size_t count, loff_t * ppos)
{
...
struct soc2e_dev *soc2e = (struct soc2e_dev *)filp->private_data;
if (soc2e->bytes == 0)
{
if (wait_event_interruptible(soc2e->wlist, (soc2e->bytes > 0)))
return -ERESTARTSYS;
}
...
}
我必须在中断处理程序中唤醒进程:
I must wake up the process in an interrupt handler:
static irqreturn_t soc2e_irq_handler(int irq, void *dev)
{
...
struct soc2e_dev *soc2e = dev;
...
soc2e->bytes += read_bytes;
wake_up_interruptible(&soc2e->wlist);
return IRQ_HANDLED;
}
我认为(并已验证)这可能是原子性问题.如果中断发生在 read
方法中的 if(soc2e-> bytes == 0)
和 wait_event_interruptible
的调用之间.也许直到下一个中断才会唤醒该进程.解决此问题的最佳方法是什么?
I think (and also verified) that here could be a problem of atomicity. What happen if interrupt comes between if (soc2e->bytes == 0)
in read
method and the call to wait_event_interruptible
. Maybe the process won't be waked up until next interrupt. What is the best way to resolve this issue?
推荐答案
wait_event_interruptible
宏在避免描述种族方面已经非常小心了.实际上,您不需要对 bytes
成员进行初始检查-您只需在您的 read
方法中编写:
The wait_event_interruptible
macro is already pretty careful about avoiding the race you describe. In fact, you don't need the initial check of your bytes
member -- you could just write in your read
method:
if (wait_event_interruptible(soc2e->wlist, soc2e->bytes > 0))
return -ERESTARTSYS;
因为 wait_event_interruptible()
如果条件为真(或者在进入睡眠状态时变为真),则实际上不会进入睡眠状态.
because wait_event_interruptible()
will not actually go to sleep if the condition is true (or becomes true while it's in the middle of going to sleep).
这篇关于如何正确唤醒中断处理程序中的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!