我正在阅读redis源代码并在ae_epoll.c中找到以下代码:
static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
aeApiState *state = eventLoop->apidata;
int retval, numevents = 0;
retval = epoll_wait(state->epfd,state->events,eventLoop->setsize,
tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
if (retval > 0) {
int j;
numevents = retval;
for (j = 0; j < numevents; j++) {
int mask = 0;
struct epoll_event *e = state->events+j;
if (e->events & EPOLLIN) mask |= AE_READABLE;
if (e->events & EPOLLOUT) mask |= AE_WRITABLE;
/* so why set AE_WRITABE when EPOLLERR or EPOLLHUP happend? */
if (e->events & EPOLLERR) mask |= AE_WRITABLE;
if (e->events & EPOLLHUP) mask |= AE_WRITABLE;
eventLoop->fired[j].fd = e->data.fd;
eventLoop->fired[j].mask = mask;
}
}
return numevents;
}
那么为什么在EPOLLERR或EPOLLHUP事件发生时设置AE_WRITABE?我已经阅读了关于EPOLLERR和EPOLLHUP的手册页,但我无法理解。
EPOLLERR
Error condition happened on the associated file descriptor. epoll_wait(2)
will always wait for this event; it is not necessary to set it in events.
EPOLLHUP
Hang up happened on the associated file descriptor. epoll_wait(2) will always
wait for this event; it is not necessary to set it in events.
答案 0 :(得分:0)
那么为什么在EPOLLERR或EPOLLHUP事件发生时设置AE_WRITABE?
这可能是为了简化调用aeApiPoll()
的代码,例如: G。在ae.c
:aeProcessEvents()
中:
numevents = aeApiPoll(eventLoop, tvp);
for (j = 0; j < numevents; j++) {
aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd];
int mask = eventLoop->fired[j].mask;
int fd = eventLoop->fired[j].fd;
int rfired = 0;
/* note the fe->mask & mask & ... code: maybe an already processed
* event removed an element that fired and we still didn't
* processed, so we check if the event is still valid. */
if (fe->mask & mask & AE_READABLE) {
rfired = 1;
fe->rfileProc(eventLoop,fd,fe->clientData,mask);
}
if (fe->mask & mask & AE_WRITABLE) {
if (!rfired || fe->wfileProc != fe->rfileProc)
fe->wfileProc(eventLoop,fd,fe->clientData,mask);
}
processed++;
}
fe->wfileProc()
无论如何都必须处理挂起和错误情况,因为它们可以随时发生,也可以在处理AE_WRITABLE
事件时发生;由于没有针对异常条件的单独事件标志,处理循环不需要对它们进行特殊处理。