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