Only do one event at a time to a callback.

This commit is contained in:
Nils O. Selåsdal
2013-11-09 11:06:19 +01:00
parent a4c6b65d5f
commit 6b26812cf3
3 changed files with 38 additions and 27 deletions
+19 -10
View File
@@ -162,7 +162,7 @@ static int iomux_epoll_run(struct IOMux *mux_, struct timeval *timeout)
rc = epoll_wait(mux->epoll_fd, mux->pending_events, EPOLL_WAIT_EVENTS, timeout_ms);
while (rc == -1 && errno == EINTR)
} while (rc == -1 && errno == EINTR);
assert(rc >= 0);
if(rc < 0)
@@ -177,18 +177,27 @@ static int iomux_epoll_run(struct IOMux *mux_, struct timeval *timeout)
for (i = 0; i < rc; i++) {
struct IOMuxFD *fd = mux->pending_events[i].data.ptr;
if (fd != NULL) { //calling _unregister from a callback could have removed it
unsigned int events;
unsigned int events;
events = epoll_2_mux_events(mux->pending_events[i].events);
events = epoll_2_mux_events(mux->pending_events[i].events);
//we want to deliver only one event at a time to simplify
//application programming.
fd = mux->pending_events[i].data.ptr;
if (events & MUX_EV_READ && fd != NULL) {
assert(fd->callback != NULL);
fd->callback(mux_, fd, events);
//be sure not to use fd after the callback, it might been removed.
mux->pending_events[i].data.ptr = NULL; //clear from pending list
event_cnt++;
fd->callback(mux_, fd, MUX_EV_READ);
}
//now re-check, in case the IOMuxFD was removed
fd = mux->pending_events[i].data.ptr;
if (events & MUX_EV_WRITE && fd != NULL) {
assert(fd->callback != NULL);
fd->callback(mux_, fd, MUX_EV_WRITE);
}
event_cnt += events != 0;
}
mux->pending = 0;