2 * libev solaris event port backend
4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
40 port_modify (EV_P_ int fd, int oev, int nev)
42 /* we need to reassociate no matter what, as closes are
43 * once more silently being discarded.
48 port_dissociate (backend_fd, PORT_SOURCE_FD, fd);
52 backend_fd, PORT_SOURCE_FD, fd,
53 (nev & EV_READ ? POLLIN : 0)
54 | (nev & EV_WRITE ? POLLOUT : 0),
62 syserr ("(libev) port_associate");
67 port_poll (EV_P_ ev_tstamp timeout)
73 ts.tv_sec = (time_t)timeout;
74 ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9;
75 res = port_getn (backend_fd, port_events, port_eventmax, &nget, &ts);
79 if (errno != EINTR && errno != ETIME)
80 syserr ("(libev) port_getn");
85 for (i = 0; i < nget; ++i)
87 if (port_events [i].portev_source == PORT_SOURCE_FD)
89 int fd = port_events [i].portev_object;
94 (port_events [i].portev_events & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
95 | (port_events [i].portev_events & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
98 anfds [fd].events = 0; /* event received == disassociated */
99 fd_change (EV_A_ fd); /* need to reify later */
103 if (expect_false (nget == port_eventmax))
105 ev_free (port_events);
106 port_eventmax = array_nextsize (sizeof (port_event_t), port_eventmax, port_eventmax + 1);
107 port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax);
112 port_init (EV_P_ int flags)
114 /* Initalize the kernel queue */
115 if ((backend_fd = port_create ()) < 0)
118 fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
120 backend_fudge = 1e-3; /* needed to compensate for port_getn returning early */
121 backend_modify = port_modify;
122 backend_poll = port_poll;
124 port_eventmax = 64; /* intiial number of events receivable per poll */
125 port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax);
127 return EVBACKEND_PORT;
133 ev_free (port_events);
141 while ((backend_fd = port_create ()) < 0)
142 syserr ("(libev) port");
144 fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
146 /* re-register interest in fds */