如何解决唤醒一个正在休眠的 Kthread
static struct task_struct *control = NULL;
static long call_ioctl(struct file *filp,unsigned int iocmd,unsigned long arg)
{
switch (iocmd)
{
case SETMODE:
/* get buffer data from userspace which is either 0 or 1 */
/* 0: Manual 1: Automatic*/
switch (buffer)
{
case MANUAL:
if (control)
{
kthread_stop(control);
control = NULL;
printk(KERN_ALERT "Switching to MANUAL disabling kernel thread for Automatic\n");
}
mode = MANUAL_MODE;
printk(KERN_ALERT "IN MANUAL \n");
break;
case AUTOMATIC:
if (automatic_fan_control() != 0)
{
printk(KERN_ALERT "Failed to set fan to AUTOMATIC!!! \n");
return -1;
}
break;
default:
printk(KERN_ALERT "Entered value is incorrect\n");
return -EINVAL;
}
break;
}
}
static inline int automatic_fan_control(void)
{
control = kthread_run(sense_cpu_temperature,NULL,"Temperature Thread");
if (control)
{
printk(KERN_ALERT "Kthread Created Successfully\n");
}
else
{
printk(KERN_ALERT "Failed to create kthread. \n");
control = NULL;
return -1;
}
return 0;
}
int temperature(void *arg)
{
while (!kthread_should_stop())
{
mutex_lock(&mutex);
get_temperature();
mutex_unlock(&mutex);
/* Process the temperature value */
msleep_interruptible(polling_interval);
}
return 0;
}
这是我为驱动程序编写的上述代码:
- 当我切换到自动模式时,会创建线程来获取温度并对其进行处理,一旦这样做,它将休眠 polling_interval 时间,然后在时间过去后再次处理。
- 当我切换到手动模式时,必须停止 kthread 并转到不是线程的手动模式。
- 这里我面临的问题是当切换到手动模式时,它会在 kthread 的睡眠时间完成后响应,如果提供的睡眠时间间隔很大,则切换到手动模式需要很多时间。
- 当 kthread 切换到手动模式时,有什么办法可以让它从睡眠状态中解脱出来。有点卡在这个新手司机任何帮助都会很棒
解决方法
让我们看看msleep_interruptible
的来源:
unsigned long msleep_interruptible(unsigned int msecs)
{
unsigned long timeout = msecs_to_jiffies(msecs) + 1;
while (timeout && !signal_pending(current))
timeout = schedule_timeout_interruptible(timeout);
return jiffies_to_msecs(timeout);
}
schedule_timeout_interruptible
将休眠直到超时到期,或者信号被传递,或者被显式唤醒。它以 jiffies 为单位返回剩余时间。如上所示,msleep_interruptible
如果早起且没有待处理的信号,则会重新入睡。
kthread_stop
显式唤醒线程任务,但不发送信号,因此 msleep_interruptible
将运行完成,如果提前唤醒,线程任务将重新进入睡眠状态。
在这种情况下,一个简单的解决方法是在 temperature
中替换此调用:
msleep_interruptible(polling_interval);
具有以下调用:
schedule_timeout_uninterruptible(msecs_to_jiffies(polling_interval) + 1);
那么,如果早起,它就不会再睡了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。