Blame ev_poll.c

Packit aac759
/*
Packit aac759
 * libev poll fd activity backend
Packit aac759
 *
Packit aac759
 * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de>
Packit aac759
 * All rights reserved.
Packit aac759
 *
Packit aac759
 * Redistribution and use in source and binary forms, with or without modifica-
Packit aac759
 * tion, are permitted provided that the following conditions are met:
Packit aac759
 *
Packit aac759
 *   1.  Redistributions of source code must retain the above copyright notice,
Packit aac759
 *       this list of conditions and the following disclaimer.
Packit aac759
 *
Packit aac759
 *   2.  Redistributions in binary form must reproduce the above copyright
Packit aac759
 *       notice, this list of conditions and the following disclaimer in the
Packit aac759
 *       documentation and/or other materials provided with the distribution.
Packit aac759
 *
Packit aac759
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
Packit aac759
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
Packit aac759
 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
Packit aac759
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
Packit aac759
 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Packit aac759
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Packit aac759
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit aac759
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
Packit aac759
 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
Packit aac759
 * OF THE POSSIBILITY OF SUCH DAMAGE.
Packit aac759
 *
Packit aac759
 * Alternatively, the contents of this file may be used under the terms of
Packit aac759
 * the GNU General Public License ("GPL") version 2 or any later version,
Packit aac759
 * in which case the provisions of the GPL are applicable instead of
Packit aac759
 * the above. If you wish to allow the use of your version of this file
Packit aac759
 * only under the terms of the GPL and not to allow others to use your
Packit aac759
 * version of this file under the BSD license, indicate your decision
Packit aac759
 * by deleting the provisions above and replace them with the notice
Packit aac759
 * and other provisions required by the GPL. If you do not delete the
Packit aac759
 * provisions above, a recipient may use your version of this file under
Packit aac759
 * either the BSD or the GPL.
Packit aac759
 */
Packit aac759
Packit aac759
#include <poll.h>
Packit aac759
Packit aac759
inline_size
Packit aac759
void
Packit aac759
pollidx_init (int *base, int count)
Packit aac759
{
Packit aac759
  /* consider using memset (.., -1, ...), which is practically guaranteed
Packit aac759
   * to work on all systems implementing poll */
Packit aac759
  while (count--)
Packit aac759
    *base++ = -1;
Packit aac759
}
Packit aac759
Packit aac759
static void
Packit aac759
poll_modify (EV_P_ int fd, int oev, int nev)
Packit aac759
{
Packit aac759
  int idx;
Packit aac759
Packit aac759
  if (oev == nev)
Packit aac759
    return;
Packit aac759
Packit aac759
  array_needsize (int, pollidxs, pollidxmax, fd + 1, pollidx_init);
Packit aac759
Packit aac759
  idx = pollidxs [fd];
Packit aac759
Packit aac759
  if (idx < 0) /* need to allocate a new pollfd */
Packit aac759
    {
Packit aac759
      pollidxs [fd] = idx = pollcnt++;
Packit aac759
      array_needsize (struct pollfd, polls, pollmax, pollcnt, EMPTY2);
Packit aac759
      polls [idx].fd = fd;
Packit aac759
    }
Packit aac759
Packit aac759
  assert (polls [idx].fd == fd);
Packit aac759
Packit aac759
  if (nev)
Packit aac759
    polls [idx].events =
Packit aac759
        (nev & EV_READ ? POLLIN : 0)
Packit aac759
        | (nev & EV_WRITE ? POLLOUT : 0);
Packit aac759
  else /* remove pollfd */
Packit aac759
    {
Packit aac759
      pollidxs [fd] = -1;
Packit aac759
Packit aac759
      if (expect_true (idx < --pollcnt))
Packit aac759
        {
Packit aac759
          polls [idx] = polls [pollcnt];
Packit aac759
          pollidxs [polls [idx].fd] = idx;
Packit aac759
        }
Packit aac759
    }
Packit aac759
}
Packit aac759
Packit aac759
static void
Packit aac759
poll_poll (EV_P_ ev_tstamp timeout)
Packit aac759
{
Packit aac759
  struct pollfd *p;
Packit aac759
  int res;
Packit aac759
  
Packit aac759
  EV_RELEASE_CB;
Packit aac759
  res = poll (polls, pollcnt, timeout * 1e3);
Packit aac759
  EV_ACQUIRE_CB;
Packit aac759
Packit aac759
  if (expect_false (res < 0))
Packit aac759
    {
Packit aac759
      if (errno == EBADF)
Packit aac759
        fd_ebadf (EV_A);
Packit aac759
      else if (errno == ENOMEM && !syserr_cb)
Packit aac759
        fd_enomem (EV_A);
Packit aac759
      else if (errno != EINTR)
Packit aac759
        ev_syserr ("(libev) poll");
Packit aac759
    }
Packit aac759
  else
Packit aac759
    for (p = polls; res; ++p)
Packit aac759
      {
Packit aac759
        assert (("libev: poll() returned illegal result, broken BSD kernel?", p < polls + pollcnt));
Packit aac759
Packit aac759
        if (expect_false (p->revents)) /* this expect is debatable */
Packit aac759
          {
Packit aac759
            --res;
Packit aac759
Packit aac759
            if (expect_false (p->revents & POLLNVAL))
Packit aac759
              fd_kill (EV_A_ p->fd);
Packit aac759
            else
Packit aac759
              fd_event (
Packit aac759
                EV_A_
Packit aac759
                p->fd,
Packit aac759
                (p->revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
Packit aac759
                | (p->revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
Packit aac759
              );
Packit aac759
          }
Packit aac759
      }
Packit aac759
}
Packit aac759
Packit aac759
inline_size
Packit aac759
int
Packit aac759
poll_init (EV_P_ int flags)
Packit aac759
{
Packit aac759
  backend_mintime = 1e-3;
Packit aac759
  backend_modify  = poll_modify;
Packit aac759
  backend_poll    = poll_poll;
Packit aac759
Packit aac759
  pollidxs = 0; pollidxmax = 0;
Packit aac759
  polls    = 0; pollmax    = 0; pollcnt = 0;
Packit aac759
Packit aac759
  return EVBACKEND_POLL;
Packit aac759
}
Packit aac759
Packit aac759
inline_size
Packit aac759
void
Packit aac759
poll_destroy (EV_P)
Packit aac759
{
Packit aac759
  ev_free (pollidxs);
Packit aac759
  ev_free (polls);
Packit aac759
}
Packit aac759