kthread helpers --- a good choice to replace complex completion interfaces

/* The kthread interface provides you free access to a built-in exit synchronization mechanism implemented using the completion interface. You may directly call kthread_stop() to ask the corresponding task to exit. And make a neat call to kthread_should_stop() to check whether it ought to end in your task. NOTE: To be completely understand this section you can first refer previous 'synchronizing using completion function' */ #include <linux/wait.h> #include <linux/kthread.h> #include <linux/module.h> #include <linux/sched.h> MODULE_LICENSE("Dual BSD/GPL"); static DECLARE_WAIT_QUEUE_HEAD(my_thread_wait); /* Assistant thread */ static int my_thread(void *unused) { DECLARE_WAITQUEUE(wait, current); /* continue work if no other thread has invoked kthread_stop() */ add_wait_queue(&my_thread_wait, &wait); while (!kthread_should_stop()) { /* Relinquish processor until event occurs */ set_current_state(TASK_INTERRUPTIBLE); schedule(); /* do your task */ } set_current_state(TASK_RUNNING); remove_wait_queue(&my_thread_wait, &wait); return 0; } struct task_struct *my_task; static int __init mydrv_init(void) { my_task = kthread_create(my_thread, NULL, "%s", "my_thread"); if (my_task) wake_up_process(my_task); printk(" Hi, my dear!/n"); return 0; } static void __exit mydrv_release(void) { kthread_stop(my_task); printk("Bye, my girl!/n Cruel world!/n"); } module_init(mydrv_init); module_exit(mydrv_release);

 

 

你可能感兴趣的:(kthread helpers --- a good choice to replace complex completion interfaces)