第一章 poll基础知识
select() 和 poll() 系统调用的本质一样,poll() 的机制与 select() 类似,与 select() 在本质上没有多大差别,管理多个描述符也是进行轮询,根据描述符的状态进行处理,但是 poll() 没有最大文件描述符数量的限制(但是数量过大后性能也是会下降)。poll() 和 select() 同样存在一个缺点就是,包含大量文件描述符的数组被整体复制于用户态和内核的地址空间之间,而不论这些文件描述符是否就绪,它的开销随着文件描述符数量的增加而线性增大。
1.1 poll()函数介绍
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
功能:监视并等待多个文件描述符的属性变化
Poll就是监控文件是否可读的一种机制,作用与select一样。
应用程序的调用函数如下:
Poll机制会判断fds中的文件是否可读,如果可读则会立即返回,返回的值就是可读fd的数量,如果不可读,那么就进程就会休眠timeout这么长的时间,然后再来判断是否有文件可读,如果有,返回fd的数量,如果没有,则返回0.
int poll(struct pollfd *fds,nfds_t nfds, int timeout);
fds:指向一个结构体数组的第0个元素的指针,每个数组元素都是一个struct pollfd结构,用于指定测试某个给定的fd的条件
struct pollfd{
int fd; //文件描述符
short events; //等待的事件
short revents; //实际发生的事件
};
fds结构体参数说明:
- fd:每一个 pollfd 结构体指定了一个被监视的文件描述符,可以传递多个结构体,指示 poll() 监视多个文件描述符。
- events:指定监测fd的事件(输入、输出、错误),每一个事件有多个取值, poll函数的事件标志符值如下:
常量 说明
POLLIN 普通或优先级带数据可读
POLLRDNORM 普通数据可读
POLLRDBAND 优先级带数据可读
POLLPRI 高优先级数据可读
POLLOUT 普通数据可写
POLLWRNORM 普通数据可写
POLLWRBAND 优先级带数据可写
POLLERR 发生错误
POLLHUP 发生挂起
POLLNVAL 描述字不是一个打开的文件
- revents:revents 域是文件描述符的操作结果事件,内核在调用返回时设置这个域。events 域中请求的任何事件都可能在 revents 域中返回.
注意:每个结构体的 events 域是由用户来设置,告诉内核我们关注的是什么,而 revents 域是返回时内核设置的,以说明对该描述符发生了什么事件
nfds:用来指定第一个参数数组元素个数
timeout:指定等待的毫秒数,无论 I/O 是否准备好,poll() 都会返回.
返回值:
成功时,poll() 返回结构体中 revents 域不为 0 的文件描述符个数;如果在超时前没有任何事件发生,poll()返回 0;
失败时,poll() 返回 -1,并设置 errno 为下列值之一:
EBADF:一个或多个结构体中指定的文件描述符无效。
EFAULT:fds 指针指向的地址超出进程的地址空间。
EINTR:请求的事件之前产生一个信号,调用可以重新发起。
EINVAL:nfds 参数超出 PLIMIT_NOFILE 值。
ENOMEM:可用内存不足,无法完成请求。
1.2 poll例程
static int redisContextWaitReady(redisContext *c, long msec) {
struct pollfd wfd[1];
wfd[0].fd = c->fd;
wfd[0].events = POLLOUT;
if (errno == EINPROGRESS) {
int res;
if ((res = poll(wfd, 1, msec)) == -1) {
__redisSetErrorFromErrno(c, REDIS_ERR_IO, "poll(2)");
redisContextCloseFd(c);
return REDIS_ERR;
} else if (res == 0) {
errno = ETIMEDOUT;
__redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
redisContextCloseFd(c);
return REDIS_ERR;
}
if (redisCheckSocketError(c) != REDIS_OK)
return REDIS_ERR;
return REDIS_OK;
}
__redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
redisContextCloseFd(c);
return REDIS_ERR;
}
第二章 epoll基础知识
#include <sys/epoll.h>
int epoll_create(int size); //成功时返回epoll文件描述符,失败时返回-1
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); //成功时返回0,失败时返回-1
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout); //成功时返回发生事件的文件描述数,失败时返回-1
(1)epoll_create:创建保存epoll文件描述符的空间。
调用epoll_create函数时创建的文件描述符保存空间称为“epoll例程”。但要注意:size参数只是应用程序向操作系统提的建议,操作系统并不一定会生成一个大小为size的epoll例程。
(2)epoll_ctl:向空间注册并注销文件描述符。
参数epfd指定注册监视对象的epoll例程的文件描述符,op指定监视对象的添加、删除或更改等操作,有以下两种常量:
1)EPOLL_CTL_ADD:将文件描述符注册到epoll例程
2)EPOLL_CTL_DEL:从epoll例程中删除文件描述符
3)EPOLL_CTL_MOD:更改注册的文件描述符的关注事件发生情况
关于event参数的讲解
该参数是 struct epoll_event 类型的指针变量,结构体定义如下。
typedef union epoll_data
{
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
} epoll_data_t;
struct epoll_event
{
uint32_t events; /* Epoll 事件 */
epoll_data_t data; /* 用户数据 */
};
epoll_event的成员events中可以保存的常量及所指的事件类型有以下:
1) EPOLLIN:需要读取数据的情况
2) EPOLLOUT:输出缓冲为空,可以立即发送数据的情况
3) EPOLLPRI:收到OOBO数据的情况
4) EPOLLRDHUP:断开连接或半关闭的情况,这在边缘触发方式下非常有用
5) EPOLLERR:发生错误的情况
6) EPOLLET:以边缘触发的方式得到事件通知
7) EPOLLONESHOT:发生一次事件后,相应文件描述符不再收到事件通知。因此需要向epoll_ctl函数的第二个参数EPOLL_CTL_MOD,再次设置事件。
(3)epoll_wait:与select函数类似,等待文件描述符发生变化。操作系统返回epoll_event类型的结构体通知监视对象的变化。timeout函数是为毫秒为单位的等待时间,传递-1时,一直等待直到事件发生。声明足够大的epoll_event结构体数组后,传递给epoll_wait函数时,发生变化的文件符信息将被填入该数组。因此,不需要像select函数那样针对所有文件符进行循环。
#include <stdio.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <stdlib.h>
#include<arpa/inet.h>
#include <netinet/in.h> //包含sockaddr_in定义
#include <errno.h>
#include <string.h> //包含memset strncpy
int main(int argc,char* argv[]) //主函数
{
int epfd1;int result;
int server_len,client_len;
int server_sockfd,client_sockfd;
struct sockaddr_in server_address; //定义在 <netinet/in.h>
struct sockaddr_in client_address;
struct epoll_event ev1;
struct epoll_event ev[20];
int epollreturn;
int i,j,res;
int sockfd;
char ch = '0';
char buff[1024];
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_sockfd = socket(AF_INET,SOCK_STREAM,0);
server_address.sin_port = htons(8888);
server_len = sizeof(server_address);
client_len = sizeof(client_address);
result = bind(server_sockfd,(struct sockaddr*)&server_address,server_len);
if(result!=0)
{
printf("bind failed\n");
exit(1); //在stdlib.h
}
/* 调用epoll_create建立一个epoll对象 */
epfd1 = epoll_create(10000);
ev1.data.fd = server_sockfd;
ev1.events = EPOLLIN;
printf("%08x\n",EPOLLIN);
printf("%08x\n",EPOLLOUT);
printf("%08x\n",EPOLLPRI);
printf("%08x\n",EPOLLERR);
printf("%08x\n",EPOLLHUP);
printf("%08x\n",EPOLLET);
printf("%08x\n",EPOLLONESHOT);
epoll_ctl(epfd1,EPOLL_CTL_ADD,server_sockfd,&ev1);
/**
* 1. epfd: 要操作的内核事件表的文件描述符,即 epoll_create 的返回值
* 2. op:指定操作类型,操作类型有三种:
-> EPOLL_CTL_ADD:往内核事件表中注册指定fd 相关的事件
-> EPOLL_CTL_MOD:修改指定 fd 上的注册事件
-> EPOLL_CTL_DEL:删除指定 fd 的注册事件
* 3. fd:所要操作的文件描述符,也就是要内核事件表中监听的 fd
* 4. event:指定所要监听的事件类型,epoll_event 结构指针类型
*/
result = listen(server_sockfd,5);
if(result!=0)
{
printf("listen failed\n");
exit(1);
}
memset(buff,0,1024);
strncpy(buff,"this is server",14);
for(;;)
{
epollreturn = epoll_wait(epfd1,ev,20,8000);
printf("epoll return is %d\n",epollreturn);
if(epollreturn>0)
{
for(i=0;i<epollreturn;i++)
{
if(ev[i].data.fd==server_sockfd){//如果新监测到一个SOCKET用户连接到了绑定的SOCKET端口,建立新的连接。
client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address, &client_len);//没有计算client_len的值,会导致accept返回-1
printf("accept one client,socket:%d\n",client_sockfd);
ev1.data.fd=client_sockfd;
ev1.events=EPOLLIN;
epoll_ctl(epfd1,EPOLL_CTL_ADD,client_sockfd,&ev1);
//ev1.data.fd=client_sockfd;
//ev1.events=EPOLLOUT;
//epoll_ctl(epfd1,EPOLL_CTL_ADD,client_sockfd,&ev1); //注册
//EPOLLIN :表示对应的文件描述符可以读
}else if(ev[i].events&EPOLLIN){ //如果是已经连接的用户,收到数据,那么进行读入。
sockfd = ev[i].data.fd;
if (sockfd < 0) {
printf("EPOLLIN,sockfd < 0\n");
continue;
}
printf("epoll recv\n");
res = recv(sockfd,&ch,1,0);
if (res < 0) {
if (errno == ECONNRESET) {
// close(sockfd);
ev[i].data.fd = -1;
printf("EPOLLIN,res<0,errno == ECONNRESET\n");
} else {
printf("EPOLLIN,recv error,res <0\n");
}
} else if (res == 0) {
// close(sockfd); //个测试发现关闭socket,epoll队列中就不再监视这个socket了,似乎不需要删除监视
ev[i].data.fd = -1;
printf("EPOLLIN,res == 0\n");
ev1.data.fd=sockfd;
ev1.events=EPOLLIN;
epoll_ctl(epfd1,EPOLL_CTL_DEL,sockfd,&ev1);
} else {
printf("EPOLLIN,receive one char %c,socket is %d\n",ch,sockfd);
}
// ev1.data.fd=sockfd;
// ev1.events=EPOLLOUT;
// epoll_ctl(epfd1,EPOLL_CTL_MOD,sockfd,&ev1);
/**/
}
/* else if(ev[i].events&EPOLLOUT) { // 监测数据发送的原理是,对端调用recv,通知到服务器端,通知epoll,这个socket有数据要发。
sockfd = ev[i].data.fd;
res = send(sockfd,buff,102,0);
if(res==-1) {
printf("send error,res is %d\n",res);
close(sockfd);
ev1.data.fd=sockfd;
ev1.events=EPOLLOUT;
epoll_ctl(epfd1,EPOLL_CTL_DEL,sockfd,&ev1);
}
ev1.data.fd=sockfd; //设置用于读操作的文件描述符
ev1.events=EPOLLIN; //设置用于注测的读操作事件
epoll_ctl(epfd1,EPOLL_CTL_MOD,sockfd,&ev1); //修改sockfd上要处理的事件为EPOLIN
}*/
}
}
}
return 0;
}
第三章 libevent中epoll
3.1 初始化结构体epoll_event
struct epollop *epollop;
struct epollop {
struct epoll_event *events;
int nevents;
int epfd;
#ifdef USING_TIMERFD
int timerfd;
#endif
};
3.2 epoll_create创建保存epoll文件描述符的空间
epfd = epoll_create(32000)
3.3 申请一 个sizeof(struct epollop)大小的内存
epollop = mm_calloc(1, sizeof(struct epollop))
epoll.c源码
/*
* Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
* Copyright 2007-2012 Niels Provos, Nick Mathewson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "event2/event-config.h"
#include "evconfig-private.h"
#ifdef EVENT__HAVE_EPOLL
#include <stdint.h>
#include <sys/types.h>
#include <sys/resource.h>
#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
#include <sys/epoll.h>
#include <signal.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#ifdef EVENT__HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef EVENT__HAVE_SYS_TIMERFD_H
#include <sys/timerfd.h>
#endif
#include "event-internal.h"
#include "evsignal-internal.h"
#include "event2/thread.h"
#include "evthread-internal.h"
#include "log-internal.h"
#include "evmap-internal.h"
#include "changelist-internal.h"
#include "time-internal.h"
/* Since Linux 2.6.17, epoll is able to report about peer half-closed connection
using special EPOLLRDHUP flag on a read event.
*/
#if !defined(EPOLLRDHUP)
#define EPOLLRDHUP 0
#define EARLY_CLOSE_IF_HAVE_RDHUP 0
#else
#define EARLY_CLOSE_IF_HAVE_RDHUP EV_FEATURE_EARLY_CLOSE
#endif
#include "epolltable-internal.h"
#if defined(EVENT__HAVE_SYS_TIMERFD_H) && \
defined(EVENT__HAVE_TIMERFD_CREATE) && \
defined(HAVE_POSIX_MONOTONIC) && defined(TFD_NONBLOCK) && \
defined(TFD_CLOEXEC)
/* Note that we only use timerfd if TFD_NONBLOCK and TFD_CLOEXEC are available
and working. This means that we can't support it on 2.6.25 (where timerfd
was introduced) or 2.6.26, since 2.6.27 introduced those flags.
*/
#define USING_TIMERFD
#endif
struct epollop {
struct epoll_event *events;
int nevents;
int epfd;
#ifdef USING_TIMERFD
int timerfd;
#endif
};
static void *epoll_init(struct event_base *);
static int epoll_dispatch(struct event_base *, struct timeval *);
static void epoll_dealloc(struct event_base *);
static const struct eventop epollops_changelist = {
"epoll (with changelist)",
epoll_init,
event_changelist_add_,
event_changelist_del_,
epoll_dispatch,
epoll_dealloc,
1, /* need reinit */
EV_FEATURE_ET|EV_FEATURE_O1| EARLY_CLOSE_IF_HAVE_RDHUP,
EVENT_CHANGELIST_FDINFO_SIZE
};
static int epoll_nochangelist_add(struct event_base *base, evutil_socket_t fd,
short old, short events, void *p);
static int epoll_nochangelist_del(struct event_base *base, evutil_socket_t fd,
short old, short events, void *p);
const struct eventop epollops = {
"epoll",
epoll_init,
epoll_nochangelist_add,
epoll_nochangelist_del,
epoll_dispatch,
epoll_dealloc,
1, /* need reinit */
EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_EARLY_CLOSE,
0
};
#define INITIAL_NEVENT 32
#define MAX_NEVENT 4096
/* On Linux kernels at least up to 2.6.24.4, epoll can't handle timeout
* values bigger than (LONG_MAX - 999ULL)/HZ. HZ in the wild can be
* as big as 1000, and LONG_MAX can be as small as (1<<31)-1, so the
* largest number of msec we can support here is 2147482. Let's
* round that down by 47 seconds.
*/
#define MAX_EPOLL_TIMEOUT_MSEC (35*60*1000)
static void *
epoll_init(struct event_base *base)
{
int epfd = -1;
struct epollop *epollop;
#ifdef EVENT__HAVE_EPOLL_CREATE1
/* First, try the shiny new epoll_create1 interface, if we have it. */
epfd = epoll_create1(EPOLL_CLOEXEC);
#endif
if (epfd == -1) {
/* Initialize the kernel queue using the old interface. (The
size field is ignored since 2.6.8.) */
if ((epfd = epoll_create(32000)) == -1) {
if (errno != ENOSYS)
event_warn("epoll_create");
return (NULL);
}
evutil_make_socket_closeonexec(epfd);
}
if (!(epollop = mm_calloc(1, sizeof(struct epollop)))) {
close(epfd);
return (NULL);
}
epollop->epfd = epfd;
/* Initialize fields */
epollop->events = mm_calloc(INITIAL_NEVENT, sizeof(struct epoll_event));
if (epollop->events == NULL) {
mm_free(epollop);
close(epfd);
return (NULL);
}
epollop->nevents = INITIAL_NEVENT;
if ((base->flags & EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST) != 0 ||
((base->flags & EVENT_BASE_FLAG_IGNORE_ENV) == 0 &&
evutil_getenv_("EVENT_EPOLL_USE_CHANGELIST") != NULL)) {
base->evsel = &epollops_changelist;
}
#ifdef USING_TIMERFD
/*
The epoll interface ordinarily gives us one-millisecond precision,
so on Linux it makes perfect sense to use the CLOCK_MONOTONIC_COARSE
timer. But when the user has set the new PRECISE_TIMER flag for an
event_base, we can try to use timerfd to give them finer granularity.
*/
if ((base->flags & EVENT_BASE_FLAG_PRECISE_TIMER) &&
base->monotonic_timer.monotonic_clock == CLOCK_MONOTONIC) {
int fd;
fd = epollop->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
if (epollop->timerfd >= 0) {
struct epoll_event epev;
memset(&epev, 0, sizeof(epev));
epev.data.fd = epollop->timerfd;
epev.events = EPOLLIN;
if (epoll_ctl(epollop->epfd, EPOLL_CTL_ADD, fd, &epev) < 0) {
event_warn("epoll_ctl(timerfd)");
close(fd);
epollop->timerfd = -1;
}
} else {
if (errno != EINVAL && errno != ENOSYS) {
/* These errors probably mean that we were
* compiled with timerfd/TFD_* support, but
* we're running on a kernel that lacks those.
*/
event_warn("timerfd_create");
}
epollop->timerfd = -1;
}
} else {
epollop->timerfd = -1;
}
#endif
evsig_init_(base);
return (epollop);
}
static const char *
change_to_string(int change)
{
change &= (EV_CHANGE_ADD|EV_CHANGE_DEL);
if (change == EV_CHANGE_ADD) {
return "add";
} else if (change == EV_CHANGE_DEL) {
return "del";
} else if (change == 0) {
return "none";
} else {
return "???";
}
}
static const char *
epoll_op_to_string(int op)
{
return op == EPOLL_CTL_ADD?"ADD":
op == EPOLL_CTL_DEL?"DEL":
op == EPOLL_CTL_MOD?"MOD":
"???";
}
#define PRINT_CHANGES(op, events, ch, status) \
"Epoll %s(%d) on fd %d " status ". " \
"Old events were %d; " \
"read change was %d (%s); " \
"write change was %d (%s); " \
"close change was %d (%s)", \
epoll_op_to_string(op), \
events, \
ch->fd, \
ch->old_events, \
ch->read_change, \
change_to_string(ch->read_change), \
ch->write_change, \
change_to_string(ch->write_change), \
ch->close_change, \
change_to_string(ch->close_change)
static int
epoll_apply_one_change(struct event_base *base,
struct epollop *epollop,
const struct event_change *ch)
{
struct epoll_event epev;
int op, events = 0;
int idx;
idx = EPOLL_OP_TABLE_INDEX(ch);
op = epoll_op_table[idx].op;
events = epoll_op_table[idx].events;
if (!events) {
EVUTIL_ASSERT(op == 0);
return 0;
}
if ((ch->read_change|ch->write_change) & EV_CHANGE_ET)
events |= EPOLLET;
memset(&epev, 0, sizeof(epev));
epev.data.fd = ch->fd;
epev.events = events;
if (epoll_ctl(epollop->epfd, op, ch->fd, &epev) == 0) {
event_debug((PRINT_CHANGES(op, epev.events, ch, "okay")));
return 0;
}
switch (op) {
case EPOLL_CTL_MOD:
if (errno == ENOENT) {
/* If a MOD operation fails with ENOENT, the
* fd was probably closed and re-opened. We
* should retry the operation as an ADD.
*/
if (epoll_ctl(epollop->epfd, EPOLL_CTL_ADD, ch->fd, &epev) == -1) {
event_warn("Epoll MOD(%d) on %d retried as ADD; that failed too",
(int)epev.events, ch->fd);
return -1;
} else {
event_debug(("Epoll MOD(%d) on %d retried as ADD; succeeded.",
(int)epev.events,
ch->fd));
return 0;
}
}
break;
case EPOLL_CTL_ADD:
if (errno == EEXIST) {
/* If an ADD operation fails with EEXIST,
* either the operation was redundant (as with a
* precautionary add), or we ran into a fun
* kernel bug where using dup*() to duplicate the
* same file into the same fd gives you the same epitem
* rather than a fresh one. For the second case,
* we must retry with MOD. */
if (epoll_ctl(epollop->epfd, EPOLL_CTL_MOD, ch->fd, &epev) == -1) {
event_warn("Epoll ADD(%d) on %d retried as MOD; that failed too",
(int)epev.events, ch->fd);
return -1;
} else {
event_debug(("Epoll ADD(%d) on %d retried as MOD; succeeded.",
(int)epev.events,
ch->fd));
return 0;
}
}
break;
case EPOLL_CTL_DEL:
if (errno == ENOENT || errno == EBADF || errno == EPERM) {
/* If a delete fails with one of these errors,
* that's fine too: we closed the fd before we
* got around to calling epoll_dispatch. */
event_debug(("Epoll DEL(%d) on fd %d gave %s: DEL was unnecessary.",
(int)epev.events,
ch->fd,
strerror(errno)));
return 0;
}
break;
default:
break;
}
event_warn(PRINT_CHANGES(op, epev.events, ch, "failed"));
return -1;
}
static int
epoll_apply_changes(struct event_base *base)
{
struct event_changelist *changelist = &base->changelist;
struct epollop *epollop = base->evbase;
struct event_change *ch;
int r = 0;
int i;
for (i = 0; i < changelist->n_changes; ++i) {
ch = &changelist->changes[i];
if (epoll_apply_one_change(base, epollop, ch) < 0)
r = -1;
}
return (r);
}
static int
epoll_nochangelist_add(struct event_base *base, evutil_socket_t fd,
short old, short events, void *p)
{
struct event_change ch;
ch.fd = fd;
ch.old_events = old;
ch.read_change = ch.write_change = ch.close_change = 0;
if (events & EV_WRITE)
ch.write_change = EV_CHANGE_ADD |
(events & EV_ET);
if (events & EV_READ)
ch.read_change = EV_CHANGE_ADD |
(events & EV_ET);
if (events & EV_CLOSED)
ch.close_change = EV_CHANGE_ADD |
(events & EV_ET);
return epoll_apply_one_change(base, base->evbase, &ch);
}
static int
epoll_nochangelist_del(struct event_base *base, evutil_socket_t fd,
short old, short events, void *p)
{
struct event_change ch;
ch.fd = fd;
ch.old_events = old;
ch.read_change = ch.write_change = ch.close_change = 0;
if (events & EV_WRITE)
ch.write_change = EV_CHANGE_DEL |
(events & EV_ET);
if (events & EV_READ)
ch.read_change = EV_CHANGE_DEL |
(events & EV_ET);
if (events & EV_CLOSED)
ch.close_change = EV_CHANGE_DEL |
(events & EV_ET);
return epoll_apply_one_change(base, base->evbase, &ch);
}
static int
epoll_dispatch(struct event_base *base, struct timeval *tv)
{
struct epollop *epollop = base->evbase;
struct epoll_event *events = epollop->events;
int i, res;
long timeout = -1;
#ifdef USING_TIMERFD
if (epollop->timerfd >= 0) {
struct itimerspec is;
is.it_interval.tv_sec = 0;
is.it_interval.tv_nsec = 0;
if (tv == NULL) {
/* No timeout; disarm the timer. */
is.it_value.tv_sec = 0;
is.it_value.tv_nsec = 0;
} else {
if (tv->tv_sec == 0 && tv->tv_usec == 0) {
/* we need to exit immediately; timerfd can't
* do that. */
timeout = 0;
}
is.it_value.tv_sec = tv->tv_sec;
is.it_value.tv_nsec = tv->tv_usec * 1000;
}
/* TODO: we could avoid unnecessary syscalls here by only
calling timerfd_settime when the top timeout changes, or
when we're called with a different timeval.
*/
if (timerfd_settime(epollop->timerfd, 0, &is, NULL) < 0) {
event_warn("timerfd_settime");
}
} else
#endif
if (tv != NULL) {
timeout = evutil_tv_to_msec_(tv);
if (timeout < 0 || timeout > MAX_EPOLL_TIMEOUT_MSEC) {
/* Linux kernels can wait forever if the timeout is
* too big; see comment on MAX_EPOLL_TIMEOUT_MSEC. */
timeout = MAX_EPOLL_TIMEOUT_MSEC;
}
}
epoll_apply_changes(base);
event_changelist_remove_all_(&base->changelist, base);
EVBASE_RELEASE_LOCK(base, th_base_lock);
res = epoll_wait(epollop->epfd, events, epollop->nevents, timeout);
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
if (res == -1) {
if (errno != EINTR) {
event_warn("epoll_wait");
return (-1);
}
return (0);
}
event_debug(("%s: epoll_wait reports %d", __func__, res));
EVUTIL_ASSERT(res <= epollop->nevents);
for (i = 0; i < res; i++) {
int what = events[i].events;
short ev = 0;
#ifdef USING_TIMERFD
if (events[i].data.fd == epollop->timerfd)
continue;
#endif
if (what & (EPOLLHUP|EPOLLERR)) {
ev = EV_READ | EV_WRITE;
} else {
if (what & EPOLLIN)
ev |= EV_READ;
if (what & EPOLLOUT)
ev |= EV_WRITE;
if (what & EPOLLRDHUP)
ev |= EV_CLOSED;
}
if (!ev)
continue;
evmap_io_active_(base, events[i].data.fd, ev | EV_ET);
}
if (res == epollop->nevents && epollop->nevents < MAX_NEVENT) {
/* We used all of the event space this time. We should
be ready for more events next time. */
int new_nevents = epollop->nevents * 2;
struct epoll_event *new_events;
new_events = mm_realloc(epollop->events,
new_nevents * sizeof(struct epoll_event));
if (new_events) {
epollop->events = new_events;
epollop->nevents = new_nevents;
}
}
return (0);
}
static void
epoll_dealloc(struct event_base *base)
{
struct epollop *epollop = base->evbase;
evsig_dealloc_(base);
if (epollop->events)
mm_free(epollop->events);
if (epollop->epfd >= 0)
close(epollop->epfd);
#ifdef USING_TIMERFD
if (epollop->timerfd >= 0)
close(epollop->timerfd);
#endif
memset(epollop, 0, sizeof(struct epollop));
mm_free(epollop);
}
#endif /* EVENT__HAVE_EPOLL */