欢迎来到天天文库
浏览记录
ID:11897479
大小:38.50 KB
页数:3页
时间:2018-07-14
《linux内核completion实现分析》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Linux内核completion实现分析completion结构及初始化#define__WAITQUEUE_INITIALIZER(name,tsk){.private=tsk,.func=default_wake_function,.task_list={NULL,NULL}}#defineDECLARE_WAITQUEUE(name,tsk)wait_queue_tname=__WAITQUEUE_INITIALIZER(name,tsk)struct__wait_queue{unsignedintflags;#defineWQ_FLAG_EXCLUSIVE0x01vo
2、id*private;wait_queue_func_tfunc;structlist_headtask_list;};structcompletion{unsignedintdone;wait_queue_head_twait;};静态初始化直接使用DECLARE_COMPLETION#defineDECLARE_COMPLETION(work)structcompletionwork=COMPLETION_INITIALIZER(work)#defineCOMPLETION_INITIALIZER(work){0,__WAIT_QUEUE_HEAD_INITIALIZER((w
3、ork).wait)}staticinlinevoidinit_completion(structcompletion*x){x->done=0;init_waitqueue_head(&x->wait);}#defineINIT_COMPLETION(x)((x).done=0)动态创建structcompletionmy_completion;init_completion(&my_completion);completion实现分析completion实现主要依靠spinlock、系统调度schedule、wait_queue,还有一个标志位done。一个completion通常
4、是一个单次设备,也就是说,他在使用一次后会被丢弃。但是如果仔细处理也可以被重复使用。如果没有使用completion_all,则我们可以重复使用一个completion结构,只要那个将要触发的事件是明确而不含糊的,就不会有问题。但是如果使用了completion_all,则必须在重复使用前重新初始化,可以使用宏INIT_COMPLETION完成,此宏把done置零。因为在completion_all中done被赋值成UINT_MAX/2,所有在唤醒所有进程后done也不太可能变成0,为方便下次使用,必须重新初始化。Completion常用函数:voidfastcall__schedwa
5、it_for_completion(structcompletion*x);unsignedlongfastcall__schedwait_for_completion_timeout(structcompletion*x,unsignedlongtimeout);voidfastcallcomplete(structcompletion*x);voidfastcallcomplete_all(structcompletion*x);completion例子:#include#include#include6、nel.h>/*printk*/#include/*currentandeverything*/#include/*forcompletion*/#include/*forchardev*/staticintcompletion_major=0;ssize_tcompletion_read(structfile*filp,char__user*buf,size_tcount,loff_t*pos);ssize_tcompletion_write(structfile*filp,char__us7、er*buf,size_tcount,loff_t*pos);DECLARE_COMPLETION(my_completion);ssize_tcompletion_read(structfile*filp,char__user*buf,size_tcount,loff_t*pos){printk(KERN_ALERT"%s,process%i(%s)goingtosleep",__FUNCTION__,current->pid,curre
6、nel.h>/*printk*/#include/*currentandeverything*/#include/*forcompletion*/#include/*forchardev*/staticintcompletion_major=0;ssize_tcompletion_read(structfile*filp,char__user*buf,size_tcount,loff_t*pos);ssize_tcompletion_write(structfile*filp,char__us
7、er*buf,size_tcount,loff_t*pos);DECLARE_COMPLETION(my_completion);ssize_tcompletion_read(structfile*filp,char__user*buf,size_tcount,loff_t*pos){printk(KERN_ALERT"%s,process%i(%s)goingtosleep",__FUNCTION__,current->pid,curre
此文档下载收益归作者所有