等待完成(completion)
想等待某件處理中的事件完成的話,可以使用這個方法。用法很簡單,先準備好completion變數,然後呼叫要等待的函式即可。處理結束,會自動呼叫通報完成的函式,並返回等待的函式。由於過程中會sleep因此不能用在interrupt context。定義在<linux/completion.h>
初始化
void init_completion(struct completion *x);
進入睡眠
void fastcall_sched wait_for_completion(struct completion *x);
呼叫此函式後,就會讓user context進入睡眠,在等待對象完成之前都不會醒來。其變化還有用signal喚醒的wait_for_completion_interruptible()以及可以指定等待時間的wait_for_completion_timeout()。
int fastcall_sched wait_for_completion_interruptible(struct completion *x);
unsigned long fastcall_sched wait_for_completion_timeout(struct completion *x,unsigned long timeout);
喚醒
void fastcall completion(struct completion *x);
也有通報完成、同時中止kernel thread的函式
void completion_and_exit(struct completion *x);
範例:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h> /* current and everything */
#include <linux/kernel.h> /* printk() */
#include <linux/fs.h> /* everything... */
#include <linux/types.h> /* size_t */
#include <linux/completion.h>
MODULE_LICENSE("Dual BSD/GPL");
static int complete_major = 0;
DECLARE_COMPLETION(comp);
ssize_t complete_read (struct file *filp, char __user *buf, size_t count, loff_t *pos)
{
printk(KERN_DEBUG "process %i (%s) going to sleep\n",
current->pid, current->comm);
wait_for_completion(&comp);
printk(KERN_DEBUG "awoken %i (%s)\n", current->pid, current->comm);
return 0; /* EOF */
}
ssize_t complete_write (struct file *filp, const char __user *buf, size_t count,
loff_t *pos)
{
printk(KERN_DEBUG "process %i (%s) awakening the readers...\n",
current->pid, current->comm);
complete(&comp);
return count; /* succeed, to avoid retrial */
}
struct file_operations complete_fops = {
.owner = THIS_MODULE,
.read = complete_read,
.write = complete_write,
};
int complete_init(void)
{
int result;
/*
* Register your major, and accept a dynamic number
*/
result = register_chrdev(complete_major, "complete", &complete_fops);
if (result < 0)
return result;
if (complete_major == 0)
complete_major = result; /* dynamic */
return 0;
}
void complete_cleanup(void)
{
unregister_chrdev(complete_major, "complete");
}
module_init(complete_init);
module_exit(complete_cleanup);
這個範例很簡單,當user context進入read區域時候便會啟動睡眠,此時可以在write進行工作,當write的工作處離完成之後會發出完成通知,read裡面收到完工通知便會跳出睡眠。