1 |
/* |
2 |
* libev solaris event port backend |
3 |
* |
4 |
* Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
5 |
* All rights reserved. |
6 |
* |
7 |
* Redistribution and use in source and binary forms, with or without |
8 |
* modification, are permitted provided that the following conditions are |
9 |
* met: |
10 |
* |
11 |
* * Redistributions of source code must retain the above copyright |
12 |
* notice, this list of conditions and the following disclaimer. |
13 |
* |
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. |
18 |
* |
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. |
30 |
*/ |
31 |
|
32 |
#include <sys/types.h> |
33 |
#include <sys/time.h> |
34 |
#include <poll.h> |
35 |
#include <port.h> |
36 |
#include <string.h> |
37 |
#include <errno.h> |
38 |
|
39 |
void inline_speed |
40 |
port_associate_and_check (EV_P_ int fd, int ev) |
41 |
{ |
42 |
if (0 > |
43 |
port_associate ( |
44 |
backend_fd, PORT_SOURCE_FD, fd, |
45 |
(ev & EV_READ ? POLLIN : 0) |
46 |
| (ev & EV_WRITE ? POLLOUT : 0), |
47 |
0 |
48 |
) |
49 |
) |
50 |
{ |
51 |
if (errno == EBADFD) |
52 |
fd_kill (EV_A_ fd); |
53 |
else |
54 |
syserr ("(libev) port_associate"); |
55 |
} |
56 |
} |
57 |
|
58 |
static void |
59 |
port_modify (EV_P_ int fd, int oev, int nev) |
60 |
{ |
61 |
/* we need to reassociate no matter what, as closes are |
62 |
* once more silently being discarded. |
63 |
*/ |
64 |
if (!nev) |
65 |
{ |
66 |
if (oev) |
67 |
port_dissociate (backend_fd, PORT_SOURCE_FD, fd); |
68 |
} |
69 |
else |
70 |
port_associate_and_check (EV_A_ fd, nev); |
71 |
} |
72 |
|
73 |
static void |
74 |
port_poll (EV_P_ ev_tstamp timeout) |
75 |
{ |
76 |
int res, i; |
77 |
struct timespec ts; |
78 |
uint_t nget = 1; |
79 |
|
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); |
83 |
|
84 |
if (res < 0) |
85 |
{ |
86 |
if (errno != EINTR && errno != ETIME) |
87 |
syserr ("(libev) port_getn"); |
88 |
|
89 |
return; |
90 |
} |
91 |
|
92 |
for (i = 0; i < nget; ++i) |
93 |
{ |
94 |
if (port_events [i].portev_source == PORT_SOURCE_FD) |
95 |
{ |
96 |
int fd = port_events [i].portev_object; |
97 |
|
98 |
fd_event ( |
99 |
EV_A_ |
100 |
fd, |
101 |
(port_events [i].portev_events & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
102 |
| (port_events [i].portev_events & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
103 |
); |
104 |
|
105 |
port_associate_and_check (EV_A_ fd, anfds [fd].events); |
106 |
} |
107 |
} |
108 |
|
109 |
if (expect_false (nget == port_eventmax)) |
110 |
{ |
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); |
114 |
} |
115 |
} |
116 |
|
117 |
int inline_size |
118 |
port_init (EV_P_ int flags) |
119 |
{ |
120 |
/* Initalize the kernel queue */ |
121 |
if ((backend_fd = port_create ()) < 0) |
122 |
return 0; |
123 |
|
124 |
fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */ |
125 |
|
126 |
backend_fudge = 1e-3; /* needed to compensate for port_getn returning early */ |
127 |
backend_modify = port_modify; |
128 |
backend_poll = port_poll; |
129 |
|
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); |
132 |
|
133 |
return EVBACKEND_PORT; |
134 |
} |
135 |
|
136 |
void inline_size |
137 |
port_destroy (EV_P) |
138 |
{ |
139 |
ev_free (port_events); |
140 |
} |
141 |
|
142 |
void inline_size |
143 |
port_fork (EV_P) |
144 |
{ |
145 |
close (backend_fd); |
146 |
|
147 |
while ((backend_fd = port_create ()) < 0) |
148 |
syserr ("(libev) port"); |
149 |
|
150 |
fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |
151 |
|
152 |
/* re-register interest in fds */ |
153 |
fd_rearm_all (EV_A); |
154 |
} |
155 |
|