Browse Source

Rework the poll loop to be cleaner and more efficient

master
ckolivas 10 years ago
parent
commit
676bb887bb
  1. 47
      src/connector.c

47
src/connector.c

@ -280,7 +280,7 @@ void *receiver(void *arg)
/* First fd is reserved for the accepting socket */ /* First fd is reserved for the accepting socket */
fds[0].fd = ci->serverfd; fds[0].fd = ci->serverfd;
retry: rebuild_fds:
update = false; update = false;
nfds = 1; nfds = 1;
@ -299,28 +299,36 @@ retry:
ck_runlock(&ci->lock); ck_runlock(&ci->lock);
repoll: repoll:
fds[0].events = POLLIN;
fds[0].revents = 0;
while (!ci->accept) while (!ci->accept)
sleep(1); cksleep_ms(100);
ret = poll(fds, nfds, 1000); ret = poll(fds, nfds, 1000);
if (unlikely(ret < 0)) { if (unlikely(ret < 1)) {
LOGERR("Failed to poll in receiver"); LOGERR("Failed to poll in receiver");
goto out; goto out;
} }
if (!ret) {
if (update) for (i = 0; i < nfds, ret > 0; i++) {
goto retry; int fd, accepted;
goto repoll;
}
for (i = nfds - 1; i > 0; i--) {
int fd;
if (!fds[i].revents) if (!fds[i].revents)
continue; continue;
/* Reset for the next poll pass */
fds[i].events = POLLIN;
fds[i].revents = 0;
--ret;
/* Is this the listening server socket? */
if (i == 0) {
accepted = accept_client(ci);
if (unlikely(accepted < 0))
goto out;
if (accepted)
update = true;
continue;
}
client = NULL; client = NULL;
fd = fds[i].fd; fd = fds[i].fd;
@ -335,19 +343,10 @@ repoll:
update = true; update = true;
} else } else
parse_client_msg(ci, client); parse_client_msg(ci, client);
/* Reset for the next poll pass */
fds[i].events = POLLIN;
fds[i].revents = 0;
} }
/* i should be zero now allowing us to examine the accepting socket */ if (update)
if (fds[0].revents) goto rebuild_fds;
ret = accept_client(ci);
if (unlikely(ret < 0))
goto out;
if (ret > 0 || update)
goto retry;
goto repoll; goto repoll;
out: out:
return NULL; return NULL;

Loading…
Cancel
Save