Permalink
Browse files

Issue #333 fix fileEvent validity when a already removed fileEvent re…

…use inside aeProessEvents
  • Loading branch information...
1 parent cb598cd commit 001ca06e165195455a0b5632264b003dbc3cf398 @finaldie committed Feb 20, 2012
Showing with 20 additions and 0 deletions.
  1. +17 −0 src/ae.c
  2. +3 −0 src/ae.h
View
17 src/ae.c
@@ -67,6 +67,8 @@ aeEventLoop *aeCreateEventLoop(int setsize) {
eventLoop->stop = 0;
eventLoop->maxfd = -1;
eventLoop->beforesleep = NULL;
+ eventLoop->epoch = 1;
+ eventLoop->processing = 0;
if (aeApiCreate(eventLoop) == -1) goto err;
/* Events with mask == AE_NONE are not set. So let's initialize the
* vector with it. */
@@ -103,6 +105,10 @@ int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
if (aeApiAddEvent(eventLoop, fd, mask) == -1)
return AE_ERR;
fe->mask |= mask;
+ if (eventLoop->processing)
+ fe->filter = eventLoop->epoch;
+ else
+ fe->filter = eventLoop->epoch - 1;
if (mask & AE_READABLE) fe->rfileProc = proc;
if (mask & AE_WRITABLE) fe->wfileProc = proc;
fe->clientData = clientData;
@@ -338,9 +344,18 @@ int aeProcessEvents(aeEventLoop *eventLoop, int flags)
}
}
+ /* note 1. the eventLoop->processing set to 1 at loop start
+ * set to 0 when loop end
+ * 2. evnetLoop->epoch will increment by 1 when loop end
+ * 3. we skip the file process when fe->filter == eventLoop->epoch,
+ * since we set fe->filter to eventLoop->epoch when create a new fileEvent
+ * inside aeProcessEvents */
+ eventLoop->processing = 1;
numevents = aeApiPoll(eventLoop, tvp);
for (j = 0; j < numevents; j++) {
aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd];
+ if (fe->filter == eventLoop->epoch) continue;
+
int mask = eventLoop->fired[j].mask;
int fd = eventLoop->fired[j].fd;
int rfired = 0;
@@ -358,6 +373,8 @@ int aeProcessEvents(aeEventLoop *eventLoop, int flags)
}
processed++;
}
+ eventLoop->processing = 0;
+ eventLoop->epoch++;
}
/* Check time events */
if (flags & AE_TIME_EVENTS)
View
3 src/ae.h
@@ -61,6 +61,7 @@ typedef void aeBeforeSleepProc(struct aeEventLoop *eventLoop);
/* File event structure */
typedef struct aeFileEvent {
int mask; /* one of AE_(READABLE|WRITABLE) */
+ int filter; /* set it to current eventLoop->epoch when fired in current loop */
aeFileProc *rfileProc;
aeFileProc *wfileProc;
void *clientData;
@@ -92,6 +93,8 @@ typedef struct aeEventLoop {
aeFiredEvent *fired; /* Fired events */
aeTimeEvent *timeEventHead;
int stop;
+ int epoch; /* Start from 1, incremented by 1 when every loop end */
+ int processing; /* Set it to 1 when we are inside the aeProcessEvents() or set it to 0 */
void *apidata; /* This is used for polling API specific data */
aeBeforeSleepProc *beforesleep;
} aeEventLoop;

0 comments on commit 001ca06

Please sign in to comment.