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.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
36 port_modify (EV_P_ int fd, int oev, int nev)
38 /* we need to reassociate no matter what, as closes are
39 * once more silently being discarded.
44 port_dissociate (port_fd, PORT_SOURCE_FD, fd);
48 port_fd, PORT_SOURCE_FD, fd,
49 (nev & EV_READ ? POLLIN : 0)
50 | (nev & EV_WRITE ? POLLOUT : 0),
58 syserr ("(libev) port_associate");
63 port_poll (EV_P_ ev_tstamp timeout)
69 ts.tv_sec = (time_t)timeout;
70 ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9;
71 res = port_getn (port_fd, port_events, port_eventmax, &nget, &ts);
75 if (errno != EINTR && errno != ETIME)
76 syserr ("(libev) port_getn");
81 for (i = 0; i < nget; ++i)
83 if (port_events [i].portev_source == PORT_SOURCE_FD)
85 int fd = port_events [i].portev_object;
90 (port_events [i].portev_events & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
91 | (port_events [i].portev_events & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
94 anfds [fd].events = 0; /* event received == disassociated */
95 fd_change (EV_A_ fd); /* need to reify later */
99 if (expect_false (nget == port_eventmax))
101 ev_free (port_events);
102 port_eventmax = array_roundsize (port_event_t, port_eventmax << 1);
103 port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax);
108 port_init (EV_P_ int flags)
110 /* Initalize the kernel queue */
111 if ((port_fd = port_create ()) < 0)
114 fcntl (port_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
116 method_fudge = 1e-3; /* needed to compensate for port_getn returning early */
117 method_modify = port_modify;
118 method_poll = port_poll;
120 port_eventmax = 64; /* intiial number of events receivable per poll */
121 port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax);
123 return EVMETHOD_PORT;
131 ev_free (port_events);
139 while ((port_fd = port_create ()) < 0)
140 syserr ("(libev) port");
142 fcntl (port_fd, F_SETFD, FD_CLOEXEC);
144 /* re-register interest in fds */