| 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 |
|
| #include "hyperman.h" |
| 7 |
|
| |
| 8 |
|
| /* poll(2) is POSIX. On Windows the readiness floor is backend_wsapoll.c |
| 9 |
|
| * (WSAPoll), and this file contributes only the dispatcher at the bottom - |
| 10 |
|
| * which is why that half sits outside the guard. */ |
| 11 |
|
| #ifndef _WIN32 |
| 12 |
|
| |
| 13 |
|
| #include <poll.h> |
| 14 |
|
| #include <signal.h> |
| 15 |
|
| #include <string.h> |
| 16 |
|
| #include <stdlib.h> |
| 17 |
|
| #include <unistd.h> |
| 18 |
|
| #include <fcntl.h> |
| 19 |
|
| #include <errno.h> |
| 20 |
|
| |
| 21 |
|
| typedef struct { |
| 22 |
|
| double when; /* monotonic deadline */ |
| 23 |
|
| double interval; /* 0 = one-shot */ |
| 24 |
|
| void *udata; |
| 25 |
|
| } hm_ptimer; |
| 26 |
|
| |
| 27 |
|
| typedef struct { |
| 28 |
|
| struct pollfd *pfds; |
| 29 |
|
| int npfds, cap; |
| 30 |
|
| int idx[HM_MAXFD]; /* fd -> pfds index, or -1 */ |
| 31 |
|
| unsigned char oneshot[HM_MAXFD]; /* HM_EV_* bits to self-clear */ |
| 32 |
|
| hm_ptimer *timers; |
| 33 |
|
| int ntimers, tcap; |
| 34 |
|
| int sigpipe[2]; /* self-pipe, -1 until used */ |
| 35 |
|
| } hm_poll_state; |
| 36 |
|
| |
| 37 |
|
| /* self-pipe globals: async-signal-safe handler writes one byte */ |
| 38 |
|
| static volatile sig_atomic_t hm_poll_sigflag[64]; |
| 39 |
|
| static int hm_poll_pipe_w = -1; |
| 40 |
|
| |
| 41 |
2 |
| static void hm_poll_handler(int signo) { |
| 42 |
2 |
| int saved = errno; /* a handler must leave errno as it found it */ |
| 43 |
|
| /* |
| 44 |
|
| # uncoverable branch false class:ignore_covered_err |
| 45 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 46 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 47 |
2 |
▶ | if (signo > 0 && signo < 64) hm_poll_sigflag[signo] = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (signo > 0 && signo < 64) hm_poll_sigflag[signo] = 1; |
2 |
0 |
2 |
| if (signo > 0 && signo < 64) hm_poll_sigflag[signo] = 1; |
2 |
0 |
2 |
|
| 48 |
|
| /* |
| 49 |
|
| # uncoverable branch false class:ignore_covered_err |
| 50 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 51 |
2 |
▶ | if (hm_poll_pipe_w >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_poll_pipe_w >= 0) { |
2 |
0 |
2 |
|
| 52 |
2 |
| char b = 1; |
| 53 |
2 |
| ssize_t r = write(hm_poll_pipe_w, &b, 1); |
| 54 |
|
| (void)r; |
| 55 |
|
| } |
| 56 |
2 |
| errno = saved; |
| 57 |
2 |
| } |
| 58 |
|
| |
| 59 |
126 |
| static double hm_poll_now(void) { |
| 60 |
|
| struct timespec ts; |
| 61 |
126 |
| clock_gettime(CLOCK_MONOTONIC, &ts); |
| 62 |
126 |
| return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9; |
| 63 |
|
| } |
| 64 |
|
| |
| 65 |
43 |
| static short hm_poll_events(int mask) { |
| 66 |
43 |
| short ev = 0; |
| 67 |
|
| /* |
| 68 |
|
| # uncoverable branch false class:ignore_covered_err |
| 69 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 70 |
43 |
▶ | if (mask & HM_EV_READ) ev |= POLLIN; |
Branch: 1 branch
| Branch | True | False | Total |
| if (mask & HM_EV_READ) ev |= POLLIN; |
41 |
2 |
43 |
|
| 71 |
|
| /* |
| 72 |
|
| # uncoverable branch true class:ignore_covered_err |
| 73 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 74 |
43 |
▶ | if (mask & HM_EV_WRITE) ev |= POLLOUT; |
Branch: 1 branch
| Branch | True | False | Total |
| if (mask & HM_EV_WRITE) ev |= POLLOUT; |
2 |
41 |
43 |
|
| 75 |
43 |
| return ev; |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
34 |
| static int hm_poll_mask_of(short events) { |
| 79 |
34 |
| int m = 0; |
| 80 |
|
| /* |
| 81 |
|
| # uncoverable branch false class:ignore_covered_err |
| 82 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 83 |
34 |
▶ | if (events & POLLIN) m |= HM_EV_READ; |
Branch: 1 branch
| Branch | True | False | Total |
| if (events & POLLIN) m |= HM_EV_READ; |
34 |
0 |
34 |
|
| 84 |
|
| /* |
| 85 |
|
| # uncoverable branch true class:ignore_covered_err |
| 86 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 87 |
34 |
▶ | if (events & POLLOUT) m |= HM_EV_WRITE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (events & POLLOUT) m |= HM_EV_WRITE; |
0 |
34 |
34 |
|
| 88 |
34 |
| return m; |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
24 |
| static int pl_add_io(hm_backend *be, int fd, int mask, int oneshot) { |
| 92 |
24 |
| hm_poll_state *st = (hm_poll_state *)be->state; |
| 93 |
|
| int i; |
| 94 |
|
| /* |
| 95 |
|
| # uncoverable branch true class:ignore_covered_err |
| 96 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 97 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 98 |
24 |
▶ | if (fd < 0 || fd >= HM_MAXFD) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (fd < 0 || fd >= HM_MAXFD) return -1; |
24 |
0 |
24 |
| if (fd < 0 || fd >= HM_MAXFD) return -1; |
0 |
24 |
24 |
|
| 99 |
24 |
| i = st->idx[fd]; |
| 100 |
|
| /* |
| 101 |
|
| # uncoverable branch false class:ignore_covered_err |
| 102 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 103 |
24 |
▶ | if (i < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < 0) { |
23 |
1 |
24 |
|
| 104 |
23 |
▶ | if (st->npfds == st->cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->npfds == st->cap) { |
6 |
17 |
23 |
|
| 105 |
6 |
▶ | st->cap = st->cap ? st->cap * 2 : 64; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| st->cap = st->cap ? st->cap * 2 : 64; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
0 |
6 |
6 |
|
| 106 |
6 |
| st->pfds = (struct pollfd *)hm_xrealloc(st->pfds, |
| 107 |
6 |
| st->cap * sizeof(struct pollfd)); |
| 108 |
|
| } |
| 109 |
23 |
| i = st->npfds++; |
| 110 |
23 |
| st->pfds[i].fd = fd; |
| 111 |
23 |
| st->pfds[i].events = 0; |
| 112 |
23 |
| st->idx[fd] = i; |
| 113 |
|
| } |
| 114 |
24 |
| st->pfds[i].events |= hm_poll_events(mask); |
| 115 |
24 |
▶ | if (oneshot) st->oneshot[fd] |= (unsigned char)mask; |
Branch: 1 branch
| Branch | True | False | Total |
| if (oneshot) st->oneshot[fd] |= (unsigned char)mask; |
4 |
20 |
24 |
|
| 116 |
20 |
| else st->oneshot[fd] &= (unsigned char)~mask; |
| 117 |
24 |
| return 0; |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
|
| /* |
| 121 |
|
| # uncoverable statement |
| 122 |
|
| # uncoverable subroutine */ |
| 123 |
0 |
| static int pl_modify_io(hm_backend *be, int fd, int mask, int oneshot) { |
| 124 |
0 |
| return pl_add_io(be, fd, mask, oneshot); /* # uncoverable statement */ |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
19 |
| static int pl_remove_io(hm_backend *be, int fd, int mask) { |
| 128 |
19 |
| hm_poll_state *st = (hm_poll_state *)be->state; |
| 129 |
|
| int i; |
| 130 |
|
| /* |
| 131 |
|
| # uncoverable branch true class:ignore_covered_err |
| 132 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 133 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 134 |
19 |
▶ | if (fd < 0 || fd >= HM_MAXFD) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (fd < 0 || fd >= HM_MAXFD) return -1; |
19 |
0 |
19 |
| if (fd < 0 || fd >= HM_MAXFD) return -1; |
0 |
19 |
19 |
|
| 135 |
19 |
| i = st->idx[fd]; |
| 136 |
|
| /* |
| 137 |
|
| # uncoverable branch true |
| 138 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 139 |
19 |
▶ | if (i < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < 0) return 0; |
0 |
19 |
19 |
|
| 140 |
19 |
| st->pfds[i].events &= ~hm_poll_events(mask); |
| 141 |
19 |
| st->oneshot[fd] &= (unsigned char)~mask; |
| 142 |
|
| /* |
| 143 |
|
| # uncoverable branch false class:ignore_covered_err |
| 144 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 145 |
19 |
▶ | if (st->pfds[i].events == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->pfds[i].events == 0) { |
18 |
1 |
19 |
|
| 146 |
18 |
| int last = st->npfds - 1; |
| 147 |
18 |
▶ | if (i != last) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i != last) { |
5 |
13 |
18 |
|
| 148 |
5 |
| st->pfds[i] = st->pfds[last]; |
| 149 |
5 |
| st->idx[st->pfds[i].fd] = i; |
| 150 |
|
| } |
| 151 |
18 |
| st->npfds--; |
| 152 |
18 |
| st->idx[fd] = -1; |
| 153 |
|
| } |
| 154 |
19 |
| return 0; |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
24 |
| static int pl_add_timer(hm_backend *be, double secs, int oneshot, void *udata) { |
| 158 |
24 |
| hm_poll_state *st = (hm_poll_state *)be->state; |
| 159 |
24 |
▶ | if (st->ntimers == st->tcap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->ntimers == st->tcap) { |
7 |
17 |
24 |
|
| 160 |
7 |
▶ | st->tcap = st->tcap ? st->tcap * 2 : 16; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| st->tcap = st->tcap ? st->tcap * 2 : 16; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
0 |
7 |
7 |
|
| 161 |
7 |
| st->timers = (hm_ptimer *)hm_xrealloc(st->timers, |
| 162 |
7 |
| st->tcap * sizeof(hm_ptimer)); |
| 163 |
|
| } |
| 164 |
24 |
| st->timers[st->ntimers].when = hm_poll_now() + secs; |
| 165 |
24 |
▶ | st->timers[st->ntimers].interval = oneshot ? 0.0 : secs; |
Branch: 1 branch
| Branch | True | False | Total |
| st->timers[st->ntimers].interval = oneshot ? 0.0 : secs; |
2 |
22 |
24 |
|
| 166 |
24 |
| st->timers[st->ntimers].udata = udata; |
| 167 |
24 |
| st->ntimers++; |
| 168 |
24 |
| return 0; |
| 169 |
|
| } |
| 170 |
|
| |
| 171 |
|
| /* |
| 172 |
|
| # uncoverable statement |
| 173 |
|
| # uncoverable subroutine */ |
| 174 |
0 |
| static int pl_del_timer(hm_backend *be, void *udata) { |
| 175 |
0 |
| hm_poll_state *st = (hm_poll_state *)be->state; /* # uncoverable statement */ |
| 176 |
|
| int i; |
| 177 |
|
| /* |
| 178 |
|
| # uncoverable statement |
| 179 |
|
| # uncoverable branch true |
| 180 |
|
| # uncoverable branch false |
| 181 |
|
| # uncoverable condition left count:1 |
| 182 |
|
| # uncoverable condition right count:1 */ |
| 183 |
0 |
▶ | for (i = 0; i < st->ntimers; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < st->ntimers; i++) { |
0 |
0 |
0 |
|
| 184 |
|
| /* |
| 185 |
|
| # uncoverable statement |
| 186 |
|
| # uncoverable branch true |
| 187 |
|
| # uncoverable branch false |
| 188 |
|
| # uncoverable condition left count:1 |
| 189 |
|
| # uncoverable condition right count:1 */ |
| 190 |
0 |
▶ | if (st->timers[i].udata == udata) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->timers[i].udata == udata) { |
0 |
0 |
0 |
|
| 191 |
0 |
| st->timers[i] = st->timers[--st->ntimers]; /* # uncoverable statement */ |
| 192 |
0 |
| return 0; /* # uncoverable statement */ |
| 193 |
|
| } |
| 194 |
|
| } |
| 195 |
0 |
| return -1; /* # uncoverable statement */ |
| 196 |
|
| } |
| 197 |
|
| |
| 198 |
6 |
| static int pl_add_signal(hm_backend *be, int signo) { |
| 199 |
6 |
| hm_poll_state *st = (hm_poll_state *)be->state; |
| 200 |
|
| struct sigaction sa; |
| 201 |
6 |
▶ | if (st->sigpipe[0] < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->sigpipe[0] < 0) { |
2 |
4 |
6 |
|
| 202 |
|
| /* |
| 203 |
|
| # uncoverable branch true |
| 204 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 205 |
2 |
▶ | if (pipe(st->sigpipe) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pipe(st->sigpipe) < 0) return -1; |
0 |
2 |
2 |
|
| 206 |
2 |
| fcntl(st->sigpipe[0], F_SETFL, O_NONBLOCK); |
| 207 |
2 |
| fcntl(st->sigpipe[1], F_SETFL, O_NONBLOCK); |
| 208 |
2 |
| hm_poll_pipe_w = st->sigpipe[1]; |
| 209 |
2 |
| pl_add_io(be, st->sigpipe[0], HM_EV_READ, 0); |
| 210 |
|
| } |
| 211 |
6 |
| memset(&sa, 0, sizeof(sa)); |
| 212 |
6 |
| sa.sa_handler = hm_poll_handler; |
| 213 |
6 |
| sigemptyset(&sa.sa_mask); |
| 214 |
6 |
| return sigaction(signo, &sa, NULL); |
| 215 |
|
| } |
| 216 |
|
| |
| 217 |
51 |
| static int pl_wait(hm_backend *be, hm_event *out, int max, double timeout) { |
| 218 |
51 |
| hm_poll_state *st = (hm_poll_state *)be->state; |
| 219 |
51 |
| double now = hm_poll_now(); |
| 220 |
51 |
| int tmo_ms, n, i, nev = 0; |
| 221 |
|
| |
| 222 |
|
| /* clamp the poll timeout to the nearest timer deadline */ |
| 223 |
|
| { |
| 224 |
51 |
| double tmo = timeout; |
| 225 |
153 |
▶ | for (i = 0; i < st->ntimers; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < st->ntimers; i++) { |
102 |
51 |
153 |
|
| 226 |
102 |
| double d = st->timers[i].when - now; |
| 227 |
102 |
▶ | if (d < 0) d = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (d < 0) d = 0; |
8 |
94 |
102 |
|
| 228 |
102 |
▶ | if (tmo < 0 || d < tmo) tmo = d; |
Branch: 2 branches
| Branch | True | False | Total |
| if (tmo < 0 || d < tmo) tmo = d; |
54 |
48 |
102 |
| if (tmo < 0 || d < tmo) tmo = d; |
47 |
7 |
54 |
|
| 229 |
|
| } |
| 230 |
51 |
▶ | tmo_ms = tmo < 0 ? -1 : (int)(tmo * 1000) + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| tmo_ms = tmo < 0 ? -1 : (int)(tmo * 1000) + 1; |
3 |
48 |
51 |
|
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
51 |
| n = poll(st->pfds, (nfds_t)st->npfds, tmo_ms); |
| 234 |
|
| /* |
| 235 |
|
| # uncoverable branch true class:ignore_covered_err |
| 236 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 237 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 238 |
51 |
▶ | if (n < 0 && errno != EINTR) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n < 0 && errno != EINTR) return -1; |
2 |
49 |
51 |
| if (n < 0 && errno != EINTR) return -1; |
0 |
2 |
2 |
|
| 239 |
|
| |
| 240 |
|
| /* io + signal readiness. Not walked after EINTR or a timeout: revents |
| 241 |
|
| * is then whatever the previous call left, or never-written memory for |
| 242 |
|
| * an entry realloc'd in since, and a stale bit dispatched a readiness |
| 243 |
|
| * nobody had. The timers below still run. */ |
| 244 |
183 |
▶ | for (i = 0; n > 0 && i < st->npfds && nev < max; i++) { /* # uncoverable condition right count:3 class:ignore_covered_err */ |
Branch: 3 branches
| Branch | True | False | Total |
| for (i = 0; n > 0 && i < st->npfds && nev < max; i++) { /* # uncoverable condition right count:3 class:ignore_covered_err */ |
168 |
15 |
183 |
| for (i = 0; n > 0 && i < st->npfds && nev < max; i++) { /* # uncoverable condition right count:3 class:ignore_covered_err */ |
132 |
36 |
168 |
| for (i = 0; n > 0 && i < st->npfds && nev < max; i++) { /* # uncoverable condition right count:3 class:ignore_covered_err */ |
132 |
0 |
132 |
|
| 245 |
132 |
| short rev = st->pfds[i].revents; |
| 246 |
132 |
| int fd = st->pfds[i].fd; |
| 247 |
132 |
▶ | if (!rev) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rev) continue; |
96 |
36 |
132 |
|
| 248 |
|
| |
| 249 |
36 |
▶ | if (st->sigpipe[0] >= 0 && fd == st->sigpipe[0]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (st->sigpipe[0] >= 0 && fd == st->sigpipe[0]) { |
33 |
3 |
36 |
| if (st->sigpipe[0] >= 0 && fd == st->sigpipe[0]) { |
2 |
31 |
33 |
|
| 250 |
|
| char buf[64]; |
| 251 |
|
| int s; |
| 252 |
4 |
▶ | while (read(st->sigpipe[0], buf, sizeof(buf)) > 0) { } |
Branch: 1 branch
| Branch | True | False | Total |
| while (read(st->sigpipe[0], buf, sizeof(buf)) > 0) { } |
2 |
2 |
4 |
|
| 253 |
128 |
▶ | for (s = 1; s < 64 && nev < max; s++) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| for (s = 1; s < 64 && nev < max; s++) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
126 |
2 |
128 |
| for (s = 1; s < 64 && nev < max; s++) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
126 |
0 |
126 |
|
| 254 |
126 |
▶ | if (hm_poll_sigflag[s]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_poll_sigflag[s]) { |
2 |
124 |
126 |
|
| 255 |
2 |
| hm_poll_sigflag[s] = 0; |
| 256 |
2 |
| out[nev].kind = HM_EV_SIGNAL; |
| 257 |
2 |
| out[nev].fd = s; |
| 258 |
2 |
| out[nev].udata = NULL; |
| 259 |
2 |
| nev++; |
| 260 |
|
| } |
| 261 |
|
| } |
| 262 |
2 |
| continue; |
| 263 |
|
| } |
| 264 |
|
| |
| 265 |
|
| { |
| 266 |
34 |
| int had = hm_poll_mask_of(st->pfds[i].events); |
| 267 |
34 |
| int got = 0; |
| 268 |
|
| /* |
| 269 |
|
| # uncoverable branch false class:ignore_covered_err |
| 270 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 271 |
34 |
▶ | if (rev & (POLLIN | POLLHUP | POLLERR | POLLNVAL)) got |= HM_EV_READ; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rev & (POLLIN | POLLHUP | POLLERR | POLLNVAL)) got |= HM_EV_READ; |
34 |
0 |
34 |
|
| 272 |
|
| /* |
| 273 |
|
| # uncoverable branch true class:ignore_covered_err |
| 274 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 275 |
34 |
▶ | if (rev & (POLLOUT | POLLHUP | POLLERR | POLLNVAL)) got |= HM_EV_WRITE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rev & (POLLOUT | POLLHUP | POLLERR | POLLNVAL)) got |= HM_EV_WRITE; |
0 |
34 |
34 |
|
| 276 |
34 |
| got &= had; |
| 277 |
|
| /* |
| 278 |
|
| # uncoverable branch true class:ignore_covered_err |
| 279 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 280 |
34 |
▶ | if (!got) got = had; /* error on an fd: wake whatever watches it */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!got) got = had; /* error on an fd: wake whatever watches it */ |
0 |
34 |
34 |
|
| 281 |
|
| /* |
| 282 |
|
| # uncoverable branch false class:ignore_covered_err |
| 283 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 284 |
34 |
▶ | if (got & HM_EV_READ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (got & HM_EV_READ) { |
34 |
0 |
34 |
|
| 285 |
34 |
| out[nev].kind = HM_EV_READ; |
| 286 |
34 |
| out[nev].fd = fd; |
| 287 |
34 |
| out[nev].udata = NULL; |
| 288 |
34 |
| nev++; |
| 289 |
|
| } |
| 290 |
|
| /* |
| 291 |
|
| # uncoverable branch true class:ignore_covered_err |
| 292 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 293 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 294 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 295 |
34 |
▶ | if ((got & HM_EV_WRITE) && nev < max) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((got & HM_EV_WRITE) && nev < max) { |
0 |
34 |
34 |
| if ((got & HM_EV_WRITE) && nev < max) { |
0 |
0 |
0 |
|
| 296 |
0 |
| out[nev].kind = HM_EV_WRITE; /* # uncoverable statement class:ignore_covered_err */ |
| 297 |
0 |
| out[nev].fd = fd; /* # uncoverable statement class:ignore_covered_err */ |
| 298 |
0 |
| out[nev].udata = NULL; /* # uncoverable statement class:ignore_covered_err */ |
| 299 |
0 |
| nev++; /* # uncoverable statement class:ignore_covered_err */ |
| 300 |
|
| } |
| 301 |
34 |
▶ | if (st->oneshot[fd] & got) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->oneshot[fd] & got) { |
3 |
31 |
34 |
|
| 302 |
3 |
| pl_remove_io(be, fd, st->oneshot[fd] & got); |
| 303 |
3 |
| i--; /* entry may have been swapped away; recheck this slot */ |
| 304 |
|
| } |
| 305 |
|
| } |
| 306 |
|
| } |
| 307 |
|
| |
| 308 |
|
| /* expired timers */ |
| 309 |
51 |
| now = hm_poll_now(); |
| 310 |
153 |
▶ | for (i = 0; i < st->ntimers && nev < max; i++) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < st->ntimers && nev < max; i++) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
102 |
51 |
153 |
| for (i = 0; i < st->ntimers && nev < max; i++) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
102 |
0 |
102 |
|
| 311 |
102 |
▶ | if (st->timers[i].when <= now) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->timers[i].when <= now) { |
17 |
85 |
102 |
|
| 312 |
17 |
| out[nev].kind = HM_EV_TIMER; |
| 313 |
17 |
| out[nev].fd = -1; |
| 314 |
17 |
| out[nev].udata = st->timers[i].udata; |
| 315 |
17 |
| nev++; |
| 316 |
17 |
▶ | if (st->timers[i].interval > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->timers[i].interval > 0) { |
1 |
16 |
17 |
|
| 317 |
1 |
| st->timers[i].when += st->timers[i].interval; |
| 318 |
|
| } else { |
| 319 |
16 |
| st->timers[i] = st->timers[--st->ntimers]; |
| 320 |
16 |
| i--; |
| 321 |
|
| } |
| 322 |
|
| } |
| 323 |
|
| } |
| 324 |
|
| |
| 325 |
51 |
| return nev; |
| 326 |
|
| } |
| 327 |
|
| |
| 328 |
17 |
| static void pl_destroy(hm_backend *be) { |
| 329 |
17 |
| hm_poll_state *st = (hm_poll_state *)be->state; |
| 330 |
|
| /* |
| 331 |
|
| # uncoverable branch false class:ignore_covered_err |
| 332 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 333 |
17 |
▶ | if (st) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st) { |
17 |
0 |
17 |
|
| 334 |
|
| /* be->foreign: inherited across a fork; the pipe is the parent's */ |
| 335 |
17 |
▶ | if (st->sigpipe[0] >= 0 && !be->foreign) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (st->sigpipe[0] >= 0 && !be->foreign) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
2 |
15 |
17 |
| if (st->sigpipe[0] >= 0 && !be->foreign) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
2 |
0 |
2 |
|
| 336 |
2 |
| close(st->sigpipe[0]); close(st->sigpipe[1]); |
| 337 |
|
| } |
| 338 |
|
| /* |
| 339 |
|
| # uncoverable branch false class:ignore_covered_err |
| 340 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 341 |
17 |
▶ | if (hm_poll_pipe_w == st->sigpipe[1]) hm_poll_pipe_w = -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_poll_pipe_w == st->sigpipe[1]) hm_poll_pipe_w = -1; |
17 |
0 |
17 |
|
| 342 |
17 |
| free(st->pfds); |
| 343 |
17 |
| free(st->timers); |
| 344 |
17 |
| free(st); |
| 345 |
|
| } |
| 346 |
17 |
| free(be); |
| 347 |
17 |
| } |
| 348 |
|
| |
| 349 |
17 |
| hm_backend *hm_backend_poll_new(void) { |
| 350 |
17 |
| hm_backend *be = (hm_backend *)hm_xcalloc(1, sizeof(hm_backend)); |
| 351 |
17 |
| hm_poll_state *st = (hm_poll_state *)hm_xcalloc(1, sizeof(hm_poll_state)); |
| 352 |
|
| int i; |
| 353 |
|
| /* |
| 354 |
|
| # uncoverable branch true class:ignore_covered_err |
| 355 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 356 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 357 |
17 |
▶ | if (!be || !st) { free(be); free(st); return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!be || !st) { free(be); free(st); return NULL; } |
17 |
0 |
17 |
| if (!be || !st) { free(be); free(st); return NULL; } |
0 |
17 |
17 |
|
| 358 |
1114129 |
▶ | for (i = 0; i < HM_MAXFD; i++) st->idx[i] = -1; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < HM_MAXFD; i++) st->idx[i] = -1; |
1114112 |
17 |
1114129 |
|
| 359 |
17 |
| st->sigpipe[0] = st->sigpipe[1] = -1; |
| 360 |
17 |
| be->name = "poll"; |
| 361 |
17 |
| be->state = st; |
| 362 |
17 |
| be->add_io = pl_add_io; |
| 363 |
17 |
| be->modify_io = pl_modify_io; |
| 364 |
17 |
| be->remove_io = pl_remove_io; |
| 365 |
17 |
| be->add_timer = pl_add_timer; |
| 366 |
17 |
| be->del_timer = pl_del_timer; |
| 367 |
17 |
| be->add_signal = pl_add_signal; |
| 368 |
17 |
| be->wait = pl_wait; |
| 369 |
17 |
| be->destroy = pl_destroy; |
| 370 |
17 |
| return be; |
| 371 |
|
| } |
| 372 |
|
| |
| 373 |
5 |
| int hm_backend_poll_available(void) { return 1; } |
| 374 |
|
| |
| 375 |
|
| #else /* _WIN32: no poll(2) */ |
| 376 |
|
| |
| 377 |
|
| hm_backend *hm_backend_poll_new(void) { return 0; } |
| 378 |
|
| int hm_backend_poll_available(void) { return 0; } |
| 379 |
|
| |
| 380 |
|
| #endif /* !_WIN32 */ |
| 381 |
|
| |
| 382 |
|
| /* ---- backend selection: kqueue > epoll > wsapoll > poll ---------------- */ |
| 383 |
|
| |
| 384 |
177 |
| hm_backend *hm_backend_create(const char *name) { |
| 385 |
177 |
▶ | if (name && *name) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (name && *name) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
27 |
150 |
177 |
| if (name && *name) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
27 |
0 |
27 |
|
| 386 |
27 |
▶ | if (strcmp(name, "kqueue") == 0) return hm_backend_kqueue_new(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(name, "kqueue") == 0) return hm_backend_kqueue_new(); |
1 |
26 |
27 |
|
| 387 |
26 |
▶ | if (strcmp(name, "io_uring") == 0) return hm_backend_iouring_new(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(name, "io_uring") == 0) return hm_backend_iouring_new(); |
1 |
25 |
26 |
|
| 388 |
25 |
▶ | if (strcmp(name, "epoll") == 0) return hm_backend_epoll_new(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(name, "epoll") == 0) return hm_backend_epoll_new(); |
6 |
19 |
25 |
|
| 389 |
19 |
▶ | if (strcmp(name, "wsapoll") == 0) return hm_backend_wsapoll_new(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(name, "wsapoll") == 0) return hm_backend_wsapoll_new(); |
1 |
18 |
19 |
|
| 390 |
18 |
▶ | if (strcmp(name, "poll") == 0) return hm_backend_poll_new(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(name, "poll") == 0) return hm_backend_poll_new(); |
17 |
1 |
18 |
|
| 391 |
1 |
| return NULL; |
| 392 |
|
| } |
| 393 |
|
| /* auto: kqueue > io_uring > epoll > wsapoll > poll. io_uring IS chosen |
| 394 |
|
| * wherever liburing was found at build time (it beat epoll by 48% at a |
| 395 |
|
| * thousand connections); HYPERMAN_BACKEND=epoll opts out. Each _new |
| 396 |
|
| * returns NULL where its platform is absent, so exactly one of |
| 397 |
|
| * wsapoll/poll can ever answer. */ |
| 398 |
|
| { |
| 399 |
|
| hm_backend *be; |
| 400 |
|
| /* |
| 401 |
|
| # uncoverable branch false class:ignore_covered_err |
| 402 |
|
| # uncoverable branch true class:ignore_covered_err |
| 403 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 404 |
150 |
▶ | if ((be = hm_backend_kqueue_new())) return be; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((be = hm_backend_kqueue_new())) return be; |
0 |
150 |
150 |
|
| 405 |
|
| /* |
| 406 |
|
| # uncoverable statement class:ignore_covered_err |
| 407 |
|
| # uncoverable branch true class:ignore_covered_err |
| 408 |
|
| # uncoverable branch false class:ignore_covered_err |
| 409 |
|
| # uncoverable condition left count:1 */ |
| 410 |
150 |
▶ | if ((be = hm_backend_iouring_new())) return be; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((be = hm_backend_iouring_new())) return be; |
0 |
150 |
150 |
|
| 411 |
|
| /* |
| 412 |
|
| # uncoverable statement class:ignore_covered_err |
| 413 |
|
| # uncoverable branch true class:ignore_covered_err |
| 414 |
|
| # uncoverable branch false class:ignore_covered_err |
| 415 |
|
| # uncoverable condition right count:1 */ |
| 416 |
150 |
▶ | if ((be = hm_backend_epoll_new())) return be; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((be = hm_backend_epoll_new())) return be; |
150 |
0 |
150 |
|
| 417 |
|
| /* |
| 418 |
|
| # uncoverable statement |
| 419 |
|
| # uncoverable branch true |
| 420 |
|
| # uncoverable branch false |
| 421 |
|
| # uncoverable condition left count:1 |
| 422 |
|
| # uncoverable condition right count:1 */ |
| 423 |
0 |
▶ | if ((be = hm_backend_wsapoll_new())) return be; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((be = hm_backend_wsapoll_new())) return be; |
0 |
0 |
0 |
|
| 424 |
0 |
| return hm_backend_poll_new(); /* # uncoverable statement */ |
| 425 |
|
| } |
| 426 |
|
| } |