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_associate_and_check (EV_P_ int fd, int ev)
44 backend_fd, PORT_SOURCE_FD, fd,
45 (ev & EV_READ ? POLLIN : 0)
46 | (ev & EV_WRITE ? POLLOUT : 0),
54 syserr ("(libev) port_associate");
59 port_modify (EV_P_ int fd, int oev, int nev)
61 /* we need to reassociate no matter what, as closes are
62 * once more silently being discarded.
67 port_dissociate (backend_fd, PORT_SOURCE_FD, fd);
70 port_associate_and_check (EV_A_ fd, nev);
74 port_poll (EV_P_ ev_tstamp timeout)
80 ts.tv_sec = (time_t)timeout;
81 ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9;
82 res = port_getn (backend_fd, port_events, port_eventmax, &nget, &ts);
86 if (errno != EINTR && errno != ETIME)
87 syserr ("(libev) port_getn");
92 for (i = 0; i < nget; ++i)
94 if (port_events [i].portev_source == PORT_SOURCE_FD)
96 int fd = port_events [i].portev_object;
101 (port_events [i].portev_events & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
102 | (port_events [i].portev_events & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
105 port_associate_and_check (EV_A_ fd, anfds [fd].events);
109 if (expect_false (nget == port_eventmax))
111 ev_free (port_events);
112 port_eventmax = array_nextsize (sizeof (port_event_t), port_eventmax, port_eventmax + 1);
113 port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax);
118 port_init (EV_P_ int flags)
120 /* Initalize the kernel queue */
121 if ((backend_fd = port_create ()) < 0)
124 fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
126 backend_fudge = 1e-3; /* needed to compensate for port_getn returning early */
127 backend_modify = port_modify;
128 backend_poll = port_poll;
130 port_eventmax = 64; /* intiial number of events receivable per poll */
131 port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax);
133 return EVBACKEND_PORT;
139 ev_free (port_events);
147 while ((backend_fd = port_create ()) < 0)
148 syserr ("(libev) port");
150 fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
152 /* re-register interest in fds */