2 * Copyright 2007 Marc Alexander Lehmann <libev@schmorp.de>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/types.h>
34 port_modify (EV_P_ int fd, int oev, int nev)
36 /* we need to reassociate no matter what, as closes are
37 * once more silently being discarded.
42 port_dissociate (backend_fd, PORT_SOURCE_FD, fd);
46 backend_fd, PORT_SOURCE_FD, fd,
47 (nev & EV_READ ? POLLIN : 0)
48 | (nev & EV_WRITE ? POLLOUT : 0),
56 syserr ("(libev) port_associate");
61 port_poll (EV_P_ ev_tstamp timeout)
67 ts.tv_sec = (time_t)timeout;
68 ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9;
69 res = port_getn (backend_fd, port_events, port_eventmax, &nget, &ts);
73 if (errno != EINTR && errno != ETIME)
74 syserr ("(libev) port_getn");
79 for (i = 0; i < nget; ++i)
81 if (port_events [i].portev_source == PORT_SOURCE_FD)
83 int fd = port_events [i].portev_object;
88 (port_events [i].portev_events & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
89 | (port_events [i].portev_events & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
92 anfds [fd].events = 0; /* event received == disassociated */
93 fd_change (EV_A_ fd); /* need to reify later */
97 if (expect_false (nget == port_eventmax))
99 ev_free (port_events);
100 port_eventmax = array_roundsize (port_event_t, port_eventmax << 1);
101 port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax);
106 port_init (EV_P_ int flags)
108 /* Initalize the kernel queue */
109 if ((backend_fd = port_create ()) < 0)
112 fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
114 backend_fudge = 1e-3; /* needed to compensate for port_getn returning early */
115 backend_modify = port_modify;
116 backend_poll = port_poll;
118 port_eventmax = 64; /* intiial number of events receivable per poll */
119 port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax);
121 return EVBACKEND_PORT;
129 ev_free (port_events);
137 while ((backend_fd = port_create ()) < 0)
138 syserr ("(libev) port");
140 fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
142 /* re-register interest in fds */