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