1 |
root |
1.1 |
/* |
2 |
root |
1.6 |
* libev solaris event port backend |
3 |
|
|
* |
4 |
|
|
* Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
5 |
|
|
* All rights reserved. |
6 |
root |
1.1 |
* |
7 |
|
|
* Redistribution and use in source and binary forms, with or without |
8 |
root |
1.6 |
* 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 |
root |
1.1 |
* |
14 |
root |
1.6 |
* * 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 |
root |
1.1 |
* |
19 |
root |
1.6 |
* 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 |
root |
1.1 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 |
root |
1.6 |
* (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 |
root |
1.1 |
*/ |
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 |
root |
1.5 |
port_dissociate (backend_fd, PORT_SOURCE_FD, fd); |
49 |
root |
1.1 |
} |
50 |
|
|
else if (0 > |
51 |
|
|
port_associate ( |
52 |
root |
1.5 |
backend_fd, PORT_SOURCE_FD, fd, |
53 |
root |
1.1 |
(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 |
root |
1.5 |
res = port_getn (backend_fd, port_events, port_eventmax, &nget, &ts); |
76 |
root |
1.1 |
|
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 |
root |
1.8 |
port_eventmax = array_nextsize (sizeof (port_event_t), port_eventmax, port_eventmax + 1); |
107 |
root |
1.1 |
port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax); |
108 |
|
|
} |
109 |
|
|
} |
110 |
|
|
|
111 |
root |
1.7 |
int inline_size |
112 |
root |
1.1 |
port_init (EV_P_ int flags) |
113 |
|
|
{ |
114 |
|
|
/* Initalize the kernel queue */ |
115 |
root |
1.5 |
if ((backend_fd = port_create ()) < 0) |
116 |
root |
1.1 |
return 0; |
117 |
|
|
|
118 |
root |
1.5 |
fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */ |
119 |
root |
1.1 |
|
120 |
root |
1.4 |
backend_fudge = 1e-3; /* needed to compensate for port_getn returning early */ |
121 |
|
|
backend_modify = port_modify; |
122 |
|
|
backend_poll = port_poll; |
123 |
root |
1.1 |
|
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 |
root |
1.3 |
return EVBACKEND_PORT; |
128 |
root |
1.1 |
} |
129 |
|
|
|
130 |
root |
1.7 |
void inline_size |
131 |
root |
1.1 |
port_destroy (EV_P) |
132 |
|
|
{ |
133 |
|
|
ev_free (port_events); |
134 |
|
|
} |
135 |
|
|
|
136 |
root |
1.7 |
void inline_size |
137 |
root |
1.1 |
port_fork (EV_P) |
138 |
|
|
{ |
139 |
root |
1.5 |
close (backend_fd); |
140 |
root |
1.1 |
|
141 |
root |
1.5 |
while ((backend_fd = port_create ()) < 0) |
142 |
root |
1.1 |
syserr ("(libev) port"); |
143 |
|
|
|
144 |
root |
1.5 |
fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |
145 |
root |
1.1 |
|
146 |
|
|
/* re-register interest in fds */ |
147 |
|
|
fd_rearm_all (EV_A); |
148 |
|
|
} |
149 |
|
|
|