include/fetch/backend_poll.c

filter: Statementstmt 40.1% 71 / 177 Branchbran 25.7% 35 / 136 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 33.8% 106 / 313 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/fetch/ft_h2.h ↑ Summary xs/fetch.xs »
1 /* Hyperman::Event::Poll - the portable poll(2) readiness backend, and the
2 * backend dispatcher (hm_backend_create). This is the portability floor:
3 * timers are a deadline list checked against a
4 * monotonic clock, signals use a self-pipe. Always available.
5 *
6 * Native Windows lacks poll(2) and this whole file; there hm_backend_create
7 * and the select() backend live in backend_select.c instead. */
8  
9 #ifndef _WIN32
10  
11 #include "ft_backend.h"
12 #include "ft_win.h" /* ft_monotonic: clock_gettime here, QPC on Windows */
13  
14 #include <poll.h>
15 #include <signal.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <errno.h>
21  
22 typedef struct {
23 double when; /* monotonic deadline */
24 double interval; /* 0 = one-shot */
25 void *udata;
26 } hm_ptimer;
27  
28 typedef struct {
29 struct pollfd *pfds;
30 int npfds, cap;
31 int idx[HM_MAXFD]; /* fd -> pfds index, or -1 */
32 unsigned char oneshot[HM_MAXFD]; /* HM_EV_* bits to self-clear */
33 hm_ptimer *timers;
34 int ntimers, tcap;
35 int sigpipe[2]; /* self-pipe, -1 until used */
36 } hm_poll_state;
37  
38 /* self-pipe globals: async-signal-safe handler writes one byte */
39 static volatile sig_atomic_t hm_poll_sigflag[64];
40 static int hm_poll_pipe_w = -1;
41  
42 0 static void hm_poll_handler(int signo) {
43 0 ▶ if (signo > 0 && signo < 64) hm_poll_sigflag[signo] = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (signo > 0 && signo < 64) hm_poll_sigflag[signo] = 1; 0 0 0
if (signo > 0 && signo < 64) hm_poll_sigflag[signo] = 1; 0 0 0
44 0 ▶ if (hm_poll_pipe_w >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_poll_pipe_w >= 0) { 0 0 0
45 0 char b = 1;
46 0 ssize_t r = write(hm_poll_pipe_w, &b, 1);
47 (void)r;
48 }
49 0 }
50  
51 #define hm_poll_now() ft_monotonic()
52  
53 0 static short hm_poll_events(int mask) {
54 0 short ev = 0;
55 0 ▶ if (mask & HM_EV_READ) ev |= POLLIN;
Branch: 1 branch
BranchTrueFalseTotal
if (mask & HM_EV_READ) ev |= POLLIN; 0 0 0
56 0 ▶ if (mask & HM_EV_WRITE) ev |= POLLOUT;
Branch: 1 branch
BranchTrueFalseTotal
if (mask & HM_EV_WRITE) ev |= POLLOUT; 0 0 0
57 0 return ev;
58 }
59  
60 0 static int hm_poll_mask_of(short events) {
61 0 int m = 0;
62 0 ▶ if (events & POLLIN) m |= HM_EV_READ;
Branch: 1 branch
BranchTrueFalseTotal
if (events & POLLIN) m |= HM_EV_READ; 0 0 0
63 0 ▶ if (events & POLLOUT) m |= HM_EV_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if (events & POLLOUT) m |= HM_EV_WRITE; 0 0 0
64 0 return m;
65 }
66  
67 0 static int pl_add_io(hm_backend *be, int fd, int mask, int oneshot) {
68 0 hm_poll_state *st = (hm_poll_state *)be->state;
69 int i;
70 0 ▶ if (fd < 0 || fd >= HM_MAXFD) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 || fd >= HM_MAXFD) return -1; 0 0 0
if (fd < 0 || fd >= HM_MAXFD) return -1; 0 0 0
71 0 i = st->idx[fd];
72 0 ▶ if (i < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < 0) { 0 0 0
73 0 ▶ if (st->npfds == st->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->npfds == st->cap) { 0 0 0
74 0 ▶ st->cap = st->cap ? st->cap * 2 : 64;
Branch: 1 branch
BranchTrueFalseTotal
st->cap = st->cap ? st->cap * 2 : 64; 0 0 0
75 0 st->pfds = (struct pollfd *)realloc(st->pfds,
76 0 st->cap * sizeof(struct pollfd));
77 }
78 0 i = st->npfds++;
79 0 st->pfds[i].fd = fd;
80 0 st->pfds[i].events = 0;
81 0 st->idx[fd] = i;
82 }
83 0 st->pfds[i].events |= hm_poll_events(mask);
84 0 ▶ if (oneshot) st->oneshot[fd] |= (unsigned char)mask;
Branch: 1 branch
BranchTrueFalseTotal
if (oneshot) st->oneshot[fd] |= (unsigned char)mask; 0 0 0
85 0 else st->oneshot[fd] &= (unsigned char)~mask;
86 0 return 0;
87 }
88  
89 0 static int pl_modify_io(hm_backend *be, int fd, int mask, int oneshot) {
90 0 return pl_add_io(be, fd, mask, oneshot);
91 }
92  
93 0 static int pl_remove_io(hm_backend *be, int fd, int mask) {
94 0 hm_poll_state *st = (hm_poll_state *)be->state;
95 int i;
96 0 ▶ if (fd < 0 || fd >= HM_MAXFD) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 || fd >= HM_MAXFD) return -1; 0 0 0
if (fd < 0 || fd >= HM_MAXFD) return -1; 0 0 0
97 0 i = st->idx[fd];
98 0 ▶ if (i < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i < 0) return 0; 0 0 0
99 0 st->pfds[i].events &= ~hm_poll_events(mask);
100 0 st->oneshot[fd] &= (unsigned char)~mask;
101 0 ▶ if (st->pfds[i].events == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->pfds[i].events == 0) { 0 0 0
102 0 int last = st->npfds - 1;
103 0 ▶ if (i != last) {
Branch: 1 branch
BranchTrueFalseTotal
if (i != last) { 0 0 0
104 0 st->pfds[i] = st->pfds[last];
105 0 st->idx[st->pfds[i].fd] = i;
106 }
107 0 st->npfds--;
108 0 st->idx[fd] = -1;
109 }
110 0 return 0;
111 }
112  
113 1 static int pl_add_timer(hm_backend *be, double secs, int oneshot, void *udata) {
114 1 hm_poll_state *st = (hm_poll_state *)be->state;
115 1 ▶ if (st->ntimers == st->tcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->ntimers == st->tcap) { 1 0 1
116 1 ▶ st->tcap = st->tcap ? st->tcap * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
st->tcap = st->tcap ? st->tcap * 2 : 16; 0 1 1
117 1 st->timers = (hm_ptimer *)realloc(st->timers,
118 1 st->tcap * sizeof(hm_ptimer));
119 }
120 1 st->timers[st->ntimers].when = hm_poll_now() + secs;
121 1 ▶ st->timers[st->ntimers].interval = oneshot ? 0.0 : secs;
Branch: 1 branch
BranchTrueFalseTotal
st->timers[st->ntimers].interval = oneshot ? 0.0 : secs; 0 1 1
122 1 st->timers[st->ntimers].udata = udata;
123 1 st->ntimers++;
124 1 return 0;
125 }
126  
127 0 static int pl_del_timer(hm_backend *be, void *udata) {
128 0 hm_poll_state *st = (hm_poll_state *)be->state;
129 int i;
130 0 ▶ for (i = 0; i < st->ntimers; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < st->ntimers; i++) { 0 0 0
131 0 ▶ if (st->timers[i].udata == udata) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->timers[i].udata == udata) { 0 0 0
132 0 st->timers[i] = st->timers[--st->ntimers];
133 0 return 0;
134 }
135 }
136 0 return -1;
137 }
138  
139 0 static int pl_add_signal(hm_backend *be, int signo) {
140 0 hm_poll_state *st = (hm_poll_state *)be->state;
141 struct sigaction sa;
142 0 ▶ if (st->sigpipe[0] < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->sigpipe[0] < 0) { 0 0 0
143 0 ▶ if (pipe(st->sigpipe) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (pipe(st->sigpipe) < 0) return -1; 0 0 0
144 0 fcntl(st->sigpipe[0], F_SETFL, O_NONBLOCK);
145 0 fcntl(st->sigpipe[1], F_SETFL, O_NONBLOCK);
146 0 hm_poll_pipe_w = st->sigpipe[1];
147 0 pl_add_io(be, st->sigpipe[0], HM_EV_READ, 0);
148 }
149 0 memset(&sa, 0, sizeof(sa));
150 0 sa.sa_handler = hm_poll_handler;
151 0 sigemptyset(&sa.sa_mask);
152 0 return sigaction(signo, &sa, NULL);
153 }
154  
155 1 static int pl_wait(hm_backend *be, hm_event *out, int max, double timeout) {
156 1 hm_poll_state *st = (hm_poll_state *)be->state;
157 1 double now = hm_poll_now();
158 1 int tmo_ms, n, i, nev = 0;
159  
160 /* clamp the poll timeout to the nearest timer deadline */
161 {
162 1 double tmo = timeout;
163 2 ▶ for (i = 0; i < st->ntimers; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < st->ntimers; i++) { 1 1 2
164 1 double d = st->timers[i].when - now;
165 1 ▶ if (d < 0) d = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (d < 0) d = 0; 0 1 1
166 1 ▶ if (tmo < 0 || d < tmo) tmo = d;
Branch: 2 branches
BranchTrueFalseTotal
if (tmo < 0 || d < tmo) tmo = d; 0 1 1
if (tmo < 0 || d < tmo) tmo = d; 0 0 0
167 }
168 1 ▶ tmo_ms = tmo < 0 ? -1 : (int)(tmo * 1000) + 1;
Branch: 1 branch
BranchTrueFalseTotal
tmo_ms = tmo < 0 ? -1 : (int)(tmo * 1000) + 1; 0 1 1
169 }
170  
171 1 n = poll(st->pfds, (nfds_t)st->npfds, tmo_ms);
172 1 ▶ if (n < 0 && errno != EINTR) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 && errno != EINTR) return -1; 0 1 1
if (n < 0 && errno != EINTR) return -1; 0 0 0
173  
174 /* io + signal readiness */
175 1 ▶ for (i = 0; i < st->npfds && nev < max; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < st->npfds && nev < max; i++) { 0 1 1
for (i = 0; i < st->npfds && nev < max; i++) { 0 0 0
176 0 short rev = st->pfds[i].revents;
177 0 int fd = st->pfds[i].fd;
178 0 ▶ if (!rev) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!rev) continue; 0 0 0
179  
180 0 ▶ if (st->sigpipe[0] >= 0 && fd == st->sigpipe[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (st->sigpipe[0] >= 0 && fd == st->sigpipe[0]) { 0 0 0
if (st->sigpipe[0] >= 0 && fd == st->sigpipe[0]) { 0 0 0
181 char buf[64];
182 int s;
183 0 ▶ while (read(st->sigpipe[0], buf, sizeof(buf)) > 0) { }
Branch: 1 branch
BranchTrueFalseTotal
while (read(st->sigpipe[0], buf, sizeof(buf)) > 0) { } 0 0 0
184 0 ▶ for (s = 1; s < 64 && nev < max; s++) {
Branch: 2 branches
BranchTrueFalseTotal
for (s = 1; s < 64 && nev < max; s++) { 0 0 0
for (s = 1; s < 64 && nev < max; s++) { 0 0 0
185 0 ▶ if (hm_poll_sigflag[s]) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_poll_sigflag[s]) { 0 0 0
186 0 hm_poll_sigflag[s] = 0;
187 0 out[nev].kind = HM_EV_SIGNAL;
188 0 out[nev].fd = s;
189 0 out[nev].udata = NULL;
190 0 nev++;
191 }
192 }
193 0 continue;
194 }
195  
196 {
197 0 int had = hm_poll_mask_of(st->pfds[i].events);
198 0 int got = 0;
199 0 ▶ if (rev & (POLLIN | POLLHUP | POLLERR | POLLNVAL)) got |= HM_EV_READ;
Branch: 1 branch
BranchTrueFalseTotal
if (rev & (POLLIN | POLLHUP | POLLERR | POLLNVAL)) got |= HM_EV_READ; 0 0 0
200 0 ▶ if (rev & (POLLOUT | POLLHUP | POLLERR | POLLNVAL)) got |= HM_EV_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if (rev & (POLLOUT | POLLHUP | POLLERR | POLLNVAL)) got |= HM_EV_WRITE; 0 0 0
201 0 got &= had;
202 0 ▶ if (!got) got = had; /* error on an fd: wake whatever watches it */
Branch: 1 branch
BranchTrueFalseTotal
if (!got) got = had; /* error on an fd: wake whatever watches it */ 0 0 0
203 0 ▶ if (got & HM_EV_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (got & HM_EV_READ) { 0 0 0
204 0 out[nev].kind = HM_EV_READ;
205 0 out[nev].fd = fd;
206 0 out[nev].udata = NULL;
207 0 nev++;
208 }
209 0 ▶ if ((got & HM_EV_WRITE) && nev < max) {
Branch: 2 branches
BranchTrueFalseTotal
if ((got & HM_EV_WRITE) && nev < max) { 0 0 0
if ((got & HM_EV_WRITE) && nev < max) { 0 0 0
210 0 out[nev].kind = HM_EV_WRITE;
211 0 out[nev].fd = fd;
212 0 out[nev].udata = NULL;
213 0 nev++;
214 }
215 0 ▶ if (st->oneshot[fd] & got) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->oneshot[fd] & got) { 0 0 0
216 0 pl_remove_io(be, fd, st->oneshot[fd] & got);
217 0 i--; /* entry may have been swapped away; recheck this slot */
218 }
219 }
220 }
221  
222 /* expired timers */
223 1 now = hm_poll_now();
224 2 ▶ for (i = 0; i < st->ntimers && nev < max; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < st->ntimers && nev < max; i++) { 1 1 2
for (i = 0; i < st->ntimers && nev < max; i++) { 1 0 1
225 1 ▶ if (st->timers[i].when <= now) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->timers[i].when <= now) { 1 0 1
226 1 out[nev].kind = HM_EV_TIMER;
227 1 out[nev].fd = -1;
228 1 out[nev].udata = st->timers[i].udata;
229 1 nev++;
230 1 ▶ if (st->timers[i].interval > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->timers[i].interval > 0) { 0 1 1
231 0 st->timers[i].when += st->timers[i].interval;
232 } else {
233 1 st->timers[i] = st->timers[--st->ntimers];
234 1 i--;
235 }
236 }
237 }
238  
239 1 return nev;
240 }
241  
242 1 static void pl_destroy(hm_backend *be) {
243 1 hm_poll_state *st = (hm_poll_state *)be->state;
244 1 ▶ if (st) {
Branch: 1 branch
BranchTrueFalseTotal
if (st) { 1 0 1
245 /* be->foreign: inherited across a fork; the pipe is the parent's */
246 1 ▶ if (st->sigpipe[0] >= 0 && !be->foreign) {
Branch: 2 branches
BranchTrueFalseTotal
if (st->sigpipe[0] >= 0 && !be->foreign) { 0 1 1
if (st->sigpipe[0] >= 0 && !be->foreign) { 0 0 0
247 0 close(st->sigpipe[0]); close(st->sigpipe[1]);
248 }
249 1 ▶ if (hm_poll_pipe_w == st->sigpipe[1]) hm_poll_pipe_w = -1;
Branch: 1 branch
BranchTrueFalseTotal
if (hm_poll_pipe_w == st->sigpipe[1]) hm_poll_pipe_w = -1; 1 0 1
250 1 free(st->pfds);
251 1 free(st->timers);
252 1 free(st);
253 }
254 1 free(be);
255 1 }
256  
257 1 hm_backend *hm_backend_poll_new(void) {
258 1 hm_backend *be = (hm_backend *)calloc(1, sizeof(hm_backend));
259 1 hm_poll_state *st = (hm_poll_state *)calloc(1, sizeof(hm_poll_state));
260 int i;
261 1 ▶ if (!be || !st) { free(be); free(st); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!be || !st) { free(be); free(st); return NULL; } 1 0 1
if (!be || !st) { free(be); free(st); return NULL; } 0 1 1
262 65537 ▶ for (i = 0; i < HM_MAXFD; i++) st->idx[i] = -1;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < HM_MAXFD; i++) st->idx[i] = -1; 65536 1 65537
263 1 st->sigpipe[0] = st->sigpipe[1] = -1;
264 1 be->name = "poll";
265 1 be->state = st;
266 1 be->add_io = pl_add_io;
267 1 be->modify_io = pl_modify_io;
268 1 be->remove_io = pl_remove_io;
269 1 be->add_timer = pl_add_timer;
270 1 be->del_timer = pl_del_timer;
271 1 be->add_signal = pl_add_signal;
272 1 be->wait_ev = pl_wait;
273 1 be->destroy = pl_destroy;
274 1 return be;
275 }
276  
277 0 int hm_backend_poll_available(void) { return 1; }
278  
279 /* ---- backend selection: kqueue > epoll > poll, HYPERMAN_BACKEND to force */
280  
281 50 hm_backend *hm_backend_create(const char *name) {
282 50 ▶ if (name && *name) {
Branch: 2 branches
BranchTrueFalseTotal
if (name && *name) { 7 43 50
if (name && *name) { 7 0 7
283 7 ▶ if (strcmp(name, "kqueue") == 0) return hm_backend_kqueue_new();
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(name, "kqueue") == 0) return hm_backend_kqueue_new(); 1 6 7
284 6 ▶ if (strcmp(name, "io_uring") == 0) return hm_backend_iouring_new();
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(name, "io_uring") == 0) return hm_backend_iouring_new(); 1 5 6
285 5 ▶ if (strcmp(name, "epoll") == 0) return hm_backend_epoll_new();
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(name, "epoll") == 0) return hm_backend_epoll_new(); 4 1 5
286 1 ▶ if (strcmp(name, "poll") == 0) return hm_backend_poll_new();
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(name, "poll") == 0) return hm_backend_poll_new(); 1 0 1
287 0 return NULL;
288 }
289 /* auto: kqueue > epoll > poll; io_uring stays opt-in until benchmarked */
290 {
291 hm_backend *be;
292 43 ▶ if ((be = hm_backend_kqueue_new())) return be;
Branch: 1 branch
BranchTrueFalseTotal
if ((be = hm_backend_kqueue_new())) return be; 0 43 43
293 43 ▶ if ((be = hm_backend_iouring_new())) return be;
Branch: 1 branch
BranchTrueFalseTotal
if ((be = hm_backend_iouring_new())) return be; 0 43 43
294 43 ▶ if ((be = hm_backend_epoll_new())) return be;
Branch: 1 branch
BranchTrueFalseTotal
if ((be = hm_backend_epoll_new())) return be; 43 0 43
295 0 return hm_backend_poll_new();
296 }
297 }
298  
299 #endif /* !_WIN32 */
« include/fetch/ft_h2.h ↑ Summary xs/fetch.xs »