Skip to content

Commit

Permalink
src/cqueues: Only call kevent() once
Browse files Browse the repository at this point in the history
  • Loading branch information
daurnimator committed Jan 6, 2016
1 parent 060bd60 commit 48e30e1
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions src/cqueues.c
Original file line number Diff line number Diff line change
Expand Up @@ -598,44 +598,48 @@ static int kpoll_ctl(struct kpoll *kp, int fd, short *state, short events, void

return 0;
#else
struct kevent event;

if (*state == events)
return 0;
struct kevent changelist[2];
int nchanges = 0;

if (events & POLLIN) {
if (!(*state & POLLIN)) {
KP_SET(&event, fd, EVFILT_READ, EV_ADD, 0, 0, udata);
KP_SET(changelist+nchanges, fd, EVFILT_READ, EV_ADD, 0, 0, udata);
nchanges++;
}
} else if (*state & POLLIN) {
KP_SET(changelist+nchanges, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
nchanges++;
}

if (0 != kevent(kp->fd, &event, 1, NULL, 0, &(struct timespec){ 0, 0 }))
return errno;
if (events & POLLOUT) {
if (!(*state & POLLOUT)) {
KP_SET(changelist+nchanges, fd, EVFILT_WRITE, EV_ADD, 0, 0, udata);
nchanges++;
}
} else if (*state & POLLOUT) {
KP_SET(changelist+nchanges, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
nchanges++;
}

if (0 == nchanges)
return 0;

if (0 != kevent(kp->fd, changelist, nchanges, NULL, 0, &(struct timespec){ 0, 0 }))
return errno;

if (events & POLLIN) {
if (!(*state & POLLIN)) {
*state |= POLLIN;
}
} else if (*state & POLLIN) {
KP_SET(&event, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);

if (0 != kevent(kp->fd, &event, 1, NULL, 0, &(struct timespec){ 0, 0 }))
return errno;

*state &= ~POLLIN;
}

if (events & POLLOUT) {
if (!(*state & POLLOUT)) {
KP_SET(&event, fd, EVFILT_WRITE, EV_ADD, 0, 0, udata);

if (0 != kevent(kp->fd, &event, 1, NULL, 0, &(struct timespec){ 0, 0 }))
return errno;

*state |= POLLOUT;
}
} else if (*state & POLLOUT) {
KP_SET(&event, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);

if (0 != kevent(kp->fd, &event, 1, NULL, 0, &(struct timespec){ 0, 0 }))
return errno;

*state &= ~POLLOUT;
}

Expand Down

0 comments on commit 48e30e1

Please sign in to comment.