我有一个由中断驱动的进程填充并由kthread清空的项目队列。
问题是正在清空队列的kthread占用了我的一个核心的100%cpu。看来,当队列为空时,while()循环会不断地反复检查kthread_should_stop(),并且仅在我停止线程时才释放内核。
#DEFINE MY_BUF_SIZE 100
struct item_struct {
unsigned char buff[MY_BUF_SIZE];
struct list_head list;
};
struct item_struct itemList;
struct task_struct *forward_items_thread;
void forward_items(void * args) {
while (!kthread_should_stop()){
struct item_struct *item, *tmpItem;
list_for_each_entry_safe(item, tmpItem, &itemList.list, list)
{
my_send_items(item);
list_del(&item->list);
kfree(item);
}
}
}
void setup_item_forwarding(void){
int err;
INIT_LIST_HEAD(&itemList.list);
forward_items_thread = kthread_create(forward_items);
if (IS_ERR(forward_items_thread)) {
printk("kthread create failed");
err = PTR_ERR(forward_items_thread);
goto err_return;
}
err = wake_up_process(forward_items_thread);
if (err == 1) {
printk("forward_items_thread has woken.\n");
}
else {
printk("forward_items_thread failed to wake.\n");
}
err_return:
//do cleanup
}
我不想使用睡眠/延迟,因为使用它的过程对〜1ms的时间敏感。
如何使kthread处理排队的项目而又不占用CPU太多时间?
答案 0 :(得分:1)
您可以在使线程可中断之后使用schedule()
。
void forward_items(void * args) {
while (!kthread_should_stop()){
struct item_struct *item, *tmpItem;
// set interruptible
set_current_state(TASK_INTERRUPTIBLE);
// do I have anything to do ?
if(my_func_no_task_to_do())
{
schedule();
}
list_for_each_entry_safe(item, tmpItem, &itemList.list, list)
{
my_send_items(item);
list_del(&item->list);
kfree(item);
}
}
}