X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/0c513fd963e79da73c7d77018523ac956a5066bd..cff78812ebbcab7601919f479447150fb7c2c9f4:/ev_poll.c?ds=sidebyside diff --git a/ev_poll.c b/ev_poll.c index 0af63f3..2995218 100644 --- a/ev_poll.c +++ b/ev_poll.c @@ -31,11 +31,6 @@ #include -static struct pollfd *polls; -static int pollmax, pollcnt; -static int *pollidxs; /* maps fds into structure indices */ -static int pollidxmax; - static void pollidx_init (int *base, int count) { @@ -44,29 +39,36 @@ pollidx_init (int *base, int count) } static void -poll_modify (int fd, int oev, int nev) +poll_modify (EV_P_ int fd, int oev, int nev) { int idx; - array_needsize (pollidxs, pollidxmax, fd + 1, pollidx_init); + + if (oev == nev) + return; + + array_needsize (int, pollidxs, pollidxmax, fd + 1, pollidx_init); idx = pollidxs [fd]; if (idx < 0) /* need to allocate a new pollfd */ { - idx = pollcnt++; - array_needsize (polls, pollmax, pollcnt, ); + pollidxs [fd] = idx = pollcnt++; + array_needsize (struct pollfd, polls, pollmax, pollcnt, EMPTY2); polls [idx].fd = fd; } + assert (polls [idx].fd == fd); + if (nev) polls [idx].events = (nev & EV_READ ? POLLIN : 0) | (nev & EV_WRITE ? POLLOUT : 0); else /* remove pollfd */ { - if (idx < pollcnt--) + pollidxs [fd] = -1; + + if (idx < --pollcnt) { - pollidxs [fd] = -1; polls [idx] = polls [pollcnt]; pollidxs [polls [idx].fd] = idx; } @@ -74,35 +76,52 @@ poll_modify (int fd, int oev, int nev) } static void -poll_poll (ev_tstamp timeout) +poll_poll (EV_P_ ev_tstamp timeout) { - int res = poll (polls, pollcnt, ceil (timeout * 1000.)); + int i; + int res = poll (polls, pollcnt, (int)ceil (timeout * 1000.)); - if (res > 0) - { - int i; - - for (i = 0; i < pollcnt; ++i) - fd_event ( - polls [i].fd, - (polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) - | (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) - ); - } - else if (res < 0) + if (res < 0) { if (errno == EBADF) - fd_ebadf (); - else if (errno == ENOMEM) - fd_enomem (); + fd_ebadf (EV_A); + else if (errno == ENOMEM && !syserr_cb) + fd_enomem (EV_A); + else if (errno != EINTR) + syserr ("(libev) poll"); + + return; } + + for (i = 0; i < pollcnt; ++i) + if (polls [i].revents & POLLNVAL) + fd_kill (EV_A_ polls [i].fd); + else + fd_event ( + EV_A_ + polls [i].fd, + (polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) + | (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) + ); } -static void -poll_init (int flags) +static int +poll_init (EV_P_ int flags) { - ev_method = EVMETHOD_POLL; method_fudge = 1e-3; /* needed to compensate for select returning early, very conservative */ method_modify = poll_modify; method_poll = poll_poll; + + pollidxs = 0; pollidxmax = 0; + polls = 0; pollmax = 0; pollcnt = 0; + + return EVBACKEND_POLL; +} + +static void +poll_destroy (EV_P) +{ + ev_free (pollidxs); + ev_free (polls); } +