工作队列

来源:互联网 发布:软件制作软件 编辑:程序博客网 时间:2024/05/17 22:16
 #define INIT_WORK(_work, _func, _data)

work工作队列增加一个工作任务,该任务就是func

work_struct是用来处理中断的。比如在中断里面要做很多事,但是比较耗时,这时就可以把耗时的工作放到工作队列

container_of宏
根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针

#include <linux/kernel.h>/**  * container_of - cast a member of a structure out to the containing structure  * @ptr:        the pointer to the member.  * @type:       the type of the container struct this is embedded in.  * @member:     the name of the member within the struct.  * */ #define container_of(ptr, type, member) ({ \     const typeof( ((type *)0)->member ) *__mptr = (ptr); \     (type *)( (char *)__mptr - offsetof(type, member) );})#include <linux/stddef.h>#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  • 使用typeof获取变量member的类型
  • 定义临时变量__mptr
  • 将变量ptr指针赋给__mptr
  • __mptr转换为char*型指针
  • (char *)__mptr - offsetof(type, member) )求出结构体起始地址
  • 将结构体转换为type*型

( (TYPE *)0 ) 将零转型为TYPE类型指针。结构以内存空间首地址0作为起始地址,则成员地址自然为偏移地址

1 0
原创粉丝点击