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 |
static void |
40 |
port_modify (EV_P_ int fd, int oev, int nev) |
41 |
{ |
42 |
/* we need to reassociate no matter what, as closes are |
43 |
* once more silently being discarded. |
44 |
*/ |
45 |
if (!nev) |
46 |
{ |
47 |
if (oev) |
48 |
port_dissociate (backend_fd, PORT_SOURCE_FD, fd); |
49 |
} |
50 |
else if (0 > |
51 |
port_associate ( |
52 |
backend_fd, PORT_SOURCE_FD, fd, |
53 |
(nev & EV_READ ? POLLIN : 0) |
54 |
| (nev & EV_WRITE ? POLLOUT : 0), |
55 |
0 |
56 |
) |
57 |
) |
58 |
{ |
59 |
if (errno == EBADFD) |
60 |
fd_kill (EV_A_ fd); |
61 |
else |
62 |
syserr ("(libev) port_associate"); |
63 |
} |
64 |
} |
65 |
|
66 |
static void |
67 |
port_poll (EV_P_ ev_tstamp timeout) |
68 |
{ |
69 |
int res, i; |
70 |
struct timespec ts; |
71 |
uint_t nget = 1; |
72 |
|
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); |
76 |
|
77 |
if (res < 0) |
78 |
{ |
79 |
if (errno != EINTR && errno != ETIME) |
80 |
syserr ("(libev) port_getn"); |
81 |
|
82 |
return; |
83 |
} |
84 |
|
85 |
for (i = 0; i < nget; ++i) |
86 |
{ |
87 |
if (port_events [i].portev_source == PORT_SOURCE_FD) |
88 |
{ |
89 |
int fd = port_events [i].portev_object; |
90 |
|
91 |
fd_event ( |
92 |
EV_A_ |
93 |
fd, |
94 |
(port_events [i].portev_events & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
95 |
| (port_events [i].portev_events & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
96 |
); |
97 |
|
98 |
anfds [fd].events = 0; /* event received == disassociated */ |
99 |
fd_change (EV_A_ fd); /* need to reify later */ |
100 |
} |
101 |
} |
102 |
|
103 |
if (expect_false (nget == port_eventmax)) |
104 |
{ |
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); |
108 |
} |
109 |
} |
110 |
|
111 |
int inline_size |
112 |
port_init (EV_P_ int flags) |
113 |
{ |
114 |
/* Initalize the kernel queue */ |
115 |
if ((backend_fd = port_create ()) < 0) |
116 |
return 0; |
117 |
|
118 |
fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */ |
119 |
|
120 |
backend_fudge = 1e-3; /* needed to compensate for port_getn returning early */ |
121 |
backend_modify = port_modify; |
122 |
backend_poll = port_poll; |
123 |
|
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); |
126 |
|
127 |
return EVBACKEND_PORT; |
128 |
} |
129 |
|
130 |
void inline_size |
131 |
port_destroy (EV_P) |
132 |
{ |
133 |
ev_free (port_events); |
134 |
} |
135 |
|
136 |
void inline_size |
137 |
port_fork (EV_P) |
138 |
{ |
139 |
close (backend_fd); |
140 |
|
141 |
while ((backend_fd = port_create ()) < 0) |
142 |
syserr ("(libev) port"); |
143 |
|
144 |
fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |
145 |
|
146 |
/* re-register interest in fds */ |
147 |
fd_rearm_all (EV_A); |
148 |
} |
149 |
|