Path: blob/main/crypto/krb5/src/util/verto/ev_poll.c
34907 views
/*1* libev poll fd activity backend2*3* Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without modifica-7* tion, are permitted provided that the following conditions are met:8*9* 1. Redistributions of source code must retain the above copyright notice,10* this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED17* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-18* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO19* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-20* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,21* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;22* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,23* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-24* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED25* OF THE POSSIBILITY OF SUCH DAMAGE.26*27* Alternatively, the contents of this file may be used under the terms of28* the GNU General Public License ("GPL") version 2 or any later version,29* in which case the provisions of the GPL are applicable instead of30* the above. If you wish to allow the use of your version of this file31* only under the terms of the GPL and not to allow others to use your32* version of this file under the BSD license, indicate your decision33* by deleting the provisions above and replace them with the notice34* and other provisions required by the GPL. If you do not delete the35* provisions above, a recipient may use your version of this file under36* either the BSD or the GPL.37*/3839#include <poll.h>4041void inline_size42pollidx_init (int *base, int count)43{44/* consider using memset (.., -1, ...), which is practically guaranteed45* to work on all systems implementing poll */46while (count--)47*base++ = -1;48}4950static void51poll_modify (EV_P_ int fd, int oev, int nev)52{53int idx;5455if (oev == nev)56return;5758array_needsize (int, pollidxs, pollidxmax, fd + 1, pollidx_init);5960idx = pollidxs [fd];6162if (idx < 0) /* need to allocate a new pollfd */63{64pollidxs [fd] = idx = pollcnt++;65array_needsize (struct pollfd, polls, pollmax, pollcnt, EMPTY2);66polls [idx].fd = fd;67}6869assert (polls [idx].fd == fd);7071if (nev)72polls [idx].events =73(nev & EV_READ ? POLLIN : 0)74| (nev & EV_WRITE ? POLLOUT : 0);75else /* remove pollfd */76{77pollidxs [fd] = -1;7879if (expect_true (idx < --pollcnt))80{81polls [idx] = polls [pollcnt];82pollidxs [polls [idx].fd] = idx;83}84}85}8687static void88poll_poll (EV_P_ ev_tstamp timeout)89{90struct pollfd *p;91int res;9293EV_RELEASE_CB;94res = poll (polls, pollcnt, timeout * 1e3);95EV_ACQUIRE_CB;9697if (expect_false (res < 0))98{99if (errno == EBADF)100fd_ebadf (EV_A);101else if (errno == ENOMEM && !syserr_cb)102fd_enomem (EV_A);103else if (errno != EINTR)104ev_syserr ("(libev) poll");105}106else107for (p = polls; res; ++p)108{109assert (("libev: poll() returned illegal result, broken BSD kernel?", p < polls + pollcnt));110111if (expect_false (p->revents)) /* this expect is debatable */112{113--res;114115if (expect_false (p->revents & POLLNVAL))116fd_kill (EV_A_ p->fd);117else118fd_event (119EV_A_120p->fd,121(p->revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)122| (p->revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)123);124}125}126}127128int inline_size129poll_init (EV_P_ int flags)130{131backend_mintime = 1e-3;132backend_modify = poll_modify;133backend_poll = poll_poll;134135pollidxs = 0; pollidxmax = 0;136polls = 0; pollmax = 0; pollcnt = 0;137138return EVBACKEND_POLL;139}140141void inline_size142poll_destroy (EV_P)143{144ev_free (pollidxs);145ev_free (polls);146}147148149150