gpt4 book ai didi

c - EPOLLERR 和 EPOLLHUP 的真正含义是什么以及如何处理它们?

转载 作者:IT王子 更新时间:2023-10-29 06:02:47 25 4
gpt4 key购买 nike

我正在阅读 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.

最佳答案

so why set AE_WRITABE when EPOLLERR or EPOLLHUP event happened?

这大概是为了简化调用 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 事件时也是如此;由于没有针对异常情况的单独事件标志,处理循环不需要对它们进行特殊处理。

关于c - EPOLLERR 和 EPOLLHUP 的真正含义是什么以及如何处理它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30529883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com