1 |
root |
1.1 |
/* |
2 |
|
|
* Copyright 2007 Marc Alexander Lehmann <libev@schmorp.de> |
3 |
|
|
* |
4 |
|
|
* Redistribution and use in source and binary forms, with or without |
5 |
|
|
* modification, are permitted provided that the following conditions |
6 |
|
|
* are met: |
7 |
|
|
* |
8 |
|
|
* 1. Redistributions of source code must retain the above copyright |
9 |
|
|
* notice, this list of conditions and the following disclaimer. |
10 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
11 |
|
|
* notice, this list of conditions and the following disclaimer in the |
12 |
|
|
* documentation and/or other materials provided with the distribution. |
13 |
|
|
* 3. The name of the author may not be used to endorse or promote products |
14 |
|
|
* derived from this software without specific prior written permission. |
15 |
|
|
* |
16 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 |
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 |
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 |
|
|
*/ |
27 |
|
|
|
28 |
|
|
#include <sys/types.h> |
29 |
|
|
#include <sys/time.h> |
30 |
|
|
#include <poll.h> |
31 |
|
|
#include <port.h> |
32 |
|
|
#include <string.h> |
33 |
|
|
#include <errno.h> |
34 |
|
|
|
35 |
|
|
static void |
36 |
|
|
port_modify (EV_P_ int fd, int oev, int nev) |
37 |
|
|
{ |
38 |
|
|
/* we need to reassociate no matter what, as closes are |
39 |
|
|
* once more silently being discarded. |
40 |
|
|
*/ |
41 |
|
|
if (!nev) |
42 |
|
|
{ |
43 |
|
|
if (oev) |
44 |
|
|
port_dissociate (port_fd, PORT_SOURCE_FD, fd); |
45 |
|
|
} |
46 |
|
|
else if (0 > |
47 |
|
|
port_associate ( |
48 |
|
|
port_fd, PORT_SOURCE_FD, fd, |
49 |
|
|
(nev & EV_READ ? POLLIN : 0) |
50 |
|
|
| (nev & EV_WRITE ? POLLOUT : 0), |
51 |
|
|
0 |
52 |
|
|
) |
53 |
|
|
) |
54 |
|
|
{ |
55 |
|
|
if (errno == EBADFD) |
56 |
|
|
fd_kill (EV_A_ fd); |
57 |
|
|
else |
58 |
|
|
syserr ("(libev) port_associate"); |
59 |
|
|
} |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
static void |
63 |
|
|
port_poll (EV_P_ ev_tstamp timeout) |
64 |
|
|
{ |
65 |
|
|
int res, i; |
66 |
|
|
struct timespec ts; |
67 |
|
|
uint_t nget = 1; |
68 |
|
|
|
69 |
|
|
ts.tv_sec = (time_t)timeout; |
70 |
|
|
ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9; |
71 |
|
|
res = port_getn (port_fd, port_events, port_eventmax, &nget, &ts); |
72 |
|
|
|
73 |
|
|
if (res < 0) |
74 |
|
|
{ |
75 |
|
|
if (errno != EINTR && errno != ETIME) |
76 |
|
|
syserr ("(libev) port_getn"); |
77 |
|
|
|
78 |
|
|
return; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
for (i = 0; i < nget; ++i) |
82 |
|
|
{ |
83 |
|
|
if (port_events [i].portev_source == PORT_SOURCE_FD) |
84 |
|
|
{ |
85 |
|
|
int fd = port_events [i].portev_object; |
86 |
|
|
|
87 |
|
|
fd_event ( |
88 |
|
|
EV_A_ |
89 |
|
|
fd, |
90 |
|
|
(port_events [i].portev_events & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
91 |
|
|
| (port_events [i].portev_events & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
92 |
|
|
); |
93 |
|
|
|
94 |
|
|
anfds [fd].events = 0; /* event received == disassociated */ |
95 |
|
|
fd_change (EV_A_ fd); /* need to reify later */ |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
if (expect_false (nget == port_eventmax)) |
100 |
|
|
{ |
101 |
|
|
ev_free (port_events); |
102 |
|
|
port_eventmax = array_roundsize (port_event_t, port_eventmax << 1); |
103 |
|
|
port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax); |
104 |
|
|
} |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
static int |
108 |
|
|
port_init (EV_P_ int flags) |
109 |
|
|
{ |
110 |
|
|
/* Initalize the kernel queue */ |
111 |
|
|
if ((port_fd = port_create ()) < 0) |
112 |
|
|
return 0; |
113 |
|
|
|
114 |
|
|
fcntl (port_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */ |
115 |
|
|
|
116 |
|
|
method_fudge = 1e-3; /* needed to compensate for port_getn returning early */ |
117 |
|
|
method_modify = port_modify; |
118 |
|
|
method_poll = port_poll; |
119 |
|
|
|
120 |
|
|
port_eventmax = 64; /* intiial number of events receivable per poll */ |
121 |
|
|
port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax); |
122 |
|
|
|
123 |
|
|
return EVMETHOD_PORT; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
static void |
127 |
|
|
port_destroy (EV_P) |
128 |
|
|
{ |
129 |
|
|
close (port_fd); |
130 |
|
|
|
131 |
|
|
ev_free (port_events); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
static void |
135 |
|
|
port_fork (EV_P) |
136 |
|
|
{ |
137 |
|
|
close (port_fd); |
138 |
|
|
|
139 |
|
|
while ((port_fd = port_create ()) < 0) |
140 |
|
|
syserr ("(libev) port"); |
141 |
|
|
|
142 |
|
|
fcntl (port_fd, F_SETFD, FD_CLOEXEC); |
143 |
|
|
|
144 |
|
|
/* re-register interest in fds */ |
145 |
|
|
fd_rearm_all (EV_A); |
146 |
|
|
} |
147 |
|
|
|