epoll源码分析(一)
主要数据结构
这里讨论的是linux2.6的eventpoll.c文件里面的代码, 所以讲的代码基本都是这个文件的, 其他个别不在里面的代码我已经写出了路径.
这里写的代码后面的描述会用到的数据结构和函数, 这里可以先跳过函数, 只了解一下重要结构体的元素就行, 后面讲解的时侯再回头看.
struct eventpoll
{
spinlock_t lock; // 自旋锁
struct mutex mtx; // 防止使用时被删除
wait_queue_head_t wq; // sys_epoll_wait() 使用的等待队列
wait_queue_head_t poll_wait; // file->poll() 使用的等待队列
struct list_head rdllist; // 事件满足条件的链表, 事件就绪, 准备在epoll_wait时写入用户空间
struct rb_root rbr;
struct epitem *ovflist; // 将事件到达的fd进行连接, 并发送至用户空间
struct user_struct *user;
};
struct epitem
{
struct rb_node rbn;
struct list_head rdllink; // 事件的就绪队列
struct epoll_filed ffd;
int nwait;
struct list_ead pwqlist; // poll 等待队列
struct eventpoll *ep; // 属于哪个结构体
struct list_head fllink; // 链接fd对应的file链表
struct epoll_event event; // 事件
};
struct epoll_filefd
{
struct file *file;
int fd;
};
static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
{
next->prev = new;
new->prev = prev;
new->next = next;
prev->next = new;
}
static inline void list_add_tail