--- libev/ev_poll.c 2008/10/29 06:32:48 1.27 +++ libev/ev_poll.c 2019/07/07 06:00:32 1.45 @@ -1,19 +1,19 @@ /* * libev poll fd activity backend * - * Copyright (c) 2007,2008 Marc Alexander Lehmann + * Copyright (c) 2007,2008,2009,2010,2011,2016,2019 Marc Alexander Lehmann * All rights reserved. * * Redistribution and use in source and binary forms, with or without modifica- * tion, are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * + * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO @@ -39,11 +39,14 @@ #include -void inline_size -pollidx_init (int *base, int count) +inline_size +void +array_needsize_pollidx (int *base, int offset, int count) { - /* consider using memset (.., -1, ...), which is pratically guarenteed - * to work on all systems implementing poll */ + /* using memset (.., -1, ...) is tempting, we we try + * to be ultraportable + */ + base += offset; while (count--) *base++ = -1; } @@ -56,14 +59,14 @@ if (oev == nev) return; - array_needsize (int, pollidxs, pollidxmax, fd + 1, pollidx_init); + array_needsize (int, pollidxs, pollidxmax, fd + 1, array_needsize_pollidx); idx = pollidxs [fd]; if (idx < 0) /* need to allocate a new pollfd */ { pollidxs [fd] = idx = pollcnt++; - array_needsize (struct pollfd, polls, pollmax, pollcnt, EMPTY2); + array_needsize (struct pollfd, polls, pollmax, pollcnt, array_needsize_noinit); polls [idx].fd = fd; } @@ -77,7 +80,7 @@ { pollidxs [fd] = -1; - if (expect_true (idx < --pollcnt)) + if (ecb_expect_true (idx < --pollcnt)) { polls [idx] = polls [pollcnt]; pollidxs [polls [idx].fd] = idx; @@ -89,9 +92,13 @@ poll_poll (EV_P_ ev_tstamp timeout) { struct pollfd *p; - int res = poll (polls, pollcnt, (int)ceil (timeout * 1000.)); + int res; + + EV_RELEASE_CB; + res = poll (polls, pollcnt, timeout * 1e3 + 0.9999); + EV_ACQUIRE_CB; - if (expect_false (res < 0)) + if (ecb_expect_false (res < 0)) { if (errno == EBADF) fd_ebadf (EV_A); @@ -102,28 +109,36 @@ } else for (p = polls; res; ++p) - if (expect_false (p->revents)) /* this expect is debatable */ - { - --res; + { + assert (("libev: poll returned illegal result, broken BSD kernel?", p < polls + pollcnt)); - if (expect_false (p->revents & POLLNVAL)) - fd_kill (EV_A_ p->fd); - else - fd_event ( - EV_A_ - p->fd, - (p->revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) - | (p->revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) - ); - } + if (ecb_expect_false (p->revents)) /* this expect is debatable */ + { + --res; + + if (ecb_expect_false (p->revents & POLLNVAL)) + { + assert (("libev: poll found invalid fd in poll set", 0)); + fd_kill (EV_A_ p->fd); + } + else + fd_event ( + EV_A_ + p->fd, + (p->revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) + | (p->revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) + ); + } + } } -int inline_size +inline_size +int poll_init (EV_P_ int flags) { - backend_fudge = 0.; /* posix says this is zero */ - backend_modify = poll_modify; - backend_poll = poll_poll; + backend_mintime = 1e-3; + backend_modify = poll_modify; + backend_poll = poll_poll; pollidxs = 0; pollidxmax = 0; polls = 0; pollmax = 0; pollcnt = 0; @@ -131,7 +146,8 @@ return EVBACKEND_POLL; } -void inline_size +inline_size +void poll_destroy (EV_P) { ev_free (pollidxs);