| 1 | /* Hyperman::Event::WSAPoll - the Windows readiness backend. | ||
| 2 | * | ||
| 3 | * Same algorithm as backend_poll.c (a pollfd array with an fd->index table, | ||
| 4 | * a deadline list for timers, a self-pipe to wake the wait), against the | ||
| 5 | * three things Windows spells differently: | ||
| 6 | * | ||
| 7 | * - WSAPoll takes SOCKETs, not our fds. Our fds ARE sockets wrapped by | ||
| 8 | * _open_osfhandle (hm_win.h), so each entry stores HM_SOCK(fd) for the | ||
| 9 | * kernel and keeps our own fd alongside to hand back in the event. | ||
| 10 | * - there are no signals. A console control handler - which runs on its | ||
| 11 | * OWN thread - sets a flag and pokes the self-pipe; wait() turns that | ||
| 12 | * into the HM_EV_SIGNAL the core already handles. CTRL_C -> SIGINT, | ||
| 13 | * CTRL_CLOSE/LOGOFF/SHUTDOWN -> SIGTERM. SIGHUP/SIGUSR1/SIGUSR2 have | ||
| 14 | * no source on Windows: add_signal accepts them so configuration does | ||
| 15 | * not have to be platform-aware, and they are simply never delivered. | ||
| 16 | * - there is no pipe() worth having here (WSAPoll only polls sockets), so | ||
| 17 | * the self-pipe is a loopback TCP pair built by hand. | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include "hyperman.h" | ||
| 21 | |||
| 22 | #ifdef _WIN32 | ||
| 23 | |||
| 24 | #include "hm_win.h" | ||
| 25 | #include <string.h> | ||
| 26 | #include <stdlib.h> | ||
| 27 | #include <errno.h> | ||
| 28 | #include <signal.h> | ||
| 29 | |||
| 30 | typedef struct { | ||
| 31 | double when; /* monotonic deadline */ | ||
| 32 | double interval; /* 0 = one-shot */ | ||
| 33 | void *udata; | ||
| 34 | } hm_wstimer; | ||
| 35 | |||
| 36 | typedef struct { | ||
| 37 | WSAPOLLFD *pfds; | ||
| 38 | int *ofd; /* pfds[i] <-> our fd */ | ||
| 39 | int npfds, cap; | ||
| 40 | int idx[HM_MAXFD]; /* fd -> pfds index, or -1 */ | ||
| 41 | unsigned char oneshot[HM_MAXFD]; /* HM_EV_* bits to self-clear */ | ||
| 42 | hm_wstimer *timers; | ||
| 43 | int ntimers, tcap; | ||
| 44 | int wake_r, wake_w; /* self-pipe fds, -1 unused */ | ||
| 45 | } hm_ws_state; | ||
| 46 | |||
| 47 | /* Set by the console handler thread, drained by wait(). sig_atomic_t is | ||
| 48 | * the right width for a flag written from another thread and read here; | ||
| 49 | * the wake byte is what guarantees the read happens promptly. */ | ||
| 50 | static volatile sig_atomic_t hm_ws_sigflag[64]; | ||
| 51 | static int hm_ws_wake_w = -1; | ||
| 52 | |||
| 53 | static BOOL WINAPI hm_ws_ctrl_handler(DWORD type) { | ||
| 54 | int signo = 0; | ||
| 55 | switch (type) { | ||
| 56 | case CTRL_C_EVENT: | ||
| 57 | case CTRL_BREAK_EVENT: signo = SIGINT; break; | ||
| 58 | case CTRL_CLOSE_EVENT: | ||
| 59 | case CTRL_LOGOFF_EVENT: | ||
| 60 | case CTRL_SHUTDOWN_EVENT: signo = SIGTERM; break; | ||
| 61 | default: return FALSE; | ||
| 62 | } | ||
| 63 | if (signo > 0 && signo < 64) hm_ws_sigflag[signo] = 1; | ||
| 64 | if (hm_ws_wake_w >= 0) { | ||
| 65 | char b = 1; | ||
| 66 | (void)hm_os_send(hm_ws_wake_w, &b, 1); | ||
| 67 | } | ||
| 68 | return TRUE; /* handled: do not let the default terminate us */ | ||
| 69 | } | ||
| 70 | |||
| 71 | /* QueryPerformanceCounter is the monotonic clock here: GetTickCount64 has | ||
| 72 | * ~15ms granularity, which a sub-second timer would round away. */ | ||
| 73 | static double hm_ws_now(void) { | ||
| 74 | static LARGE_INTEGER freq; | ||
| 75 | LARGE_INTEGER c; | ||
| 76 | if (!freq.QuadPart) QueryPerformanceFrequency(&freq); | ||
| 77 | QueryPerformanceCounter(&c); | ||
| 78 | return (double)c.QuadPart / (double)freq.QuadPart; | ||
| 79 | } | ||
| 80 | |||
| 81 | /* WSAPoll wants the NORM bits; POLLIN/POLLOUT are aliases that include the | ||
| 82 | * band variants we never ask for. */ | ||
| 83 | static SHORT hm_ws_events(int mask) { | ||
| 84 | SHORT ev = 0; | ||
| 85 | if (mask & HM_EV_READ) ev |= POLLRDNORM; | ||
| 86 | if (mask & HM_EV_WRITE) ev |= POLLWRNORM; | ||
| 87 | return ev; | ||
| 88 | } | ||
| 89 | |||
| 90 | static int hm_ws_mask_of(SHORT events) { | ||
| 91 | int m = 0; | ||
| 92 | if (events & POLLRDNORM) m |= HM_EV_READ; | ||
| 93 | if (events & POLLWRNORM) m |= HM_EV_WRITE; | ||
| 94 | return m; | ||
| 95 | } | ||
| 96 | |||
| 97 | static int ws_add_io(hm_backend *be, int fd, int mask, int oneshot) { | ||
| 98 | hm_ws_state *st = (hm_ws_state *)be->state; | ||
| 99 | int i; | ||
| 100 | if (fd < 0 || fd >= HM_MAXFD) return -1; | ||
| 101 | i = st->idx[fd]; | ||
| 102 | if (i < 0) { | ||
| 103 | if (st->npfds == st->cap) { | ||
| 104 | st->cap = st->cap ? st->cap * 2 : 64; | ||
| 105 | st->pfds = (WSAPOLLFD *)hm_xrealloc(st->pfds, | ||
| 106 | st->cap * sizeof(WSAPOLLFD)); | ||
| 107 | st->ofd = (int *)hm_xrealloc(st->ofd, st->cap * sizeof(int)); | ||
| 108 | } | ||
| 109 | i = st->npfds++; | ||
| 110 | st->pfds[i].fd = HM_SOCK(fd); | ||
| 111 | st->pfds[i].events = 0; | ||
| 112 | st->pfds[i].revents = 0; | ||
| 113 | st->ofd[i] = fd; | ||
| 114 | st->idx[fd] = i; | ||
| 115 | } | ||
| 116 | st->pfds[i].events |= hm_ws_events(mask); | ||
| 117 | if (oneshot) st->oneshot[fd] |= (unsigned char)mask; | ||
| 118 | else st->oneshot[fd] &= (unsigned char)~mask; | ||
| 119 | return 0; | ||
| 120 | } | ||
| 121 | |||
| 122 | static int ws_modify_io(hm_backend *be, int fd, int mask, int oneshot) { | ||
| 123 | return ws_add_io(be, fd, mask, oneshot); | ||
| 124 | } | ||
| 125 | |||
| 126 | static int ws_remove_io(hm_backend *be, int fd, int mask) { | ||
| 127 | hm_ws_state *st = (hm_ws_state *)be->state; | ||
| 128 | int i; | ||
| 129 | if (fd < 0 || fd >= HM_MAXFD) return -1; | ||
| 130 | i = st->idx[fd]; | ||
| 131 | if (i < 0) return 0; | ||
| 132 | st->pfds[i].events &= ~hm_ws_events(mask); | ||
| 133 | st->oneshot[fd] &= (unsigned char)~mask; | ||
| 134 | if (st->pfds[i].events == 0) { | ||
| 135 | int last = st->npfds - 1; | ||
| 136 | if (i != last) { | ||
| 137 | st->pfds[i] = st->pfds[last]; | ||
| 138 | st->ofd[i] = st->ofd[last]; | ||
| 139 | st->idx[st->ofd[i]] = i; | ||
| 140 | } | ||
| 141 | st->npfds--; | ||
| 142 | st->idx[fd] = -1; | ||
| 143 | } | ||
| 144 | return 0; | ||
| 145 | } | ||
| 146 | |||
| 147 | static int ws_add_timer(hm_backend *be, double secs, int oneshot, void *udata) { | ||
| 148 | hm_ws_state *st = (hm_ws_state *)be->state; | ||
| 149 | if (st->ntimers == st->tcap) { | ||
| 150 | st->tcap = st->tcap ? st->tcap * 2 : 16; | ||
| 151 | st->timers = (hm_wstimer *)hm_xrealloc(st->timers, | ||
| 152 | st->tcap * sizeof(hm_wstimer)); | ||
| 153 | } | ||
| 154 | st->timers[st->ntimers].when = hm_ws_now() + secs; | ||
| 155 | st->timers[st->ntimers].interval = oneshot ? 0.0 : secs; | ||
| 156 | st->timers[st->ntimers].udata = udata; | ||
| 157 | st->ntimers++; | ||
| 158 | return 0; | ||
| 159 | } | ||
| 160 | |||
| 161 | static int ws_del_timer(hm_backend *be, void *udata) { | ||
| 162 | hm_ws_state *st = (hm_ws_state *)be->state; | ||
| 163 | int i; | ||
| 164 | for (i = 0; i < st->ntimers; i++) { | ||
| 165 | if (st->timers[i].udata == udata) { | ||
| 166 | st->timers[i] = st->timers[--st->ntimers]; | ||
| 167 | return 0; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | return -1; | ||
| 171 | } | ||
| 172 | |||
| 173 | static int ws_add_signal(hm_backend *be, int signo) { | ||
| 174 | hm_ws_state *st = (hm_ws_state *)be->state; | ||
| 175 | if (signo <= 0 || signo >= 64) return -1; | ||
| 176 | if (st->wake_r < 0) { | ||
| 177 | if (hm_os_selfpipe(&st->wake_r, &st->wake_w) < 0) return -1; | ||
| 178 | hm_ws_wake_w = st->wake_w; | ||
| 179 | ws_add_io(be, st->wake_r, HM_EV_READ, 0); | ||
| 180 | SetConsoleCtrlHandler(hm_ws_ctrl_handler, TRUE); | ||
| 181 | } | ||
| 182 | /* SIGINT and SIGTERM have a console source; the rest are accepted and | ||
| 183 | * never delivered, which is documented rather than pretended away. */ | ||
| 184 | return 0; | ||
| 185 | } | ||
| 186 | |||
| 187 | static int ws_wait(hm_backend *be, hm_event *out, int max, double timeout) { | ||
| 188 | hm_ws_state *st = (hm_ws_state *)be->state; | ||
| 189 | double now = hm_ws_now(); | ||
| 190 | int tmo_ms, n, i, nev = 0; | ||
| 191 | |||
| 192 | { | ||
| 193 | double tmo = timeout; | ||
| 194 | for (i = 0; i < st->ntimers; i++) { | ||
| 195 | double d = st->timers[i].when - now; | ||
| 196 | if (d < 0) d = 0; | ||
| 197 | if (tmo < 0 || d < tmo) tmo = d; | ||
| 198 | } | ||
| 199 | tmo_ms = tmo < 0 ? -1 : (int)(tmo * 1000) + 1; | ||
| 200 | } | ||
| 201 | |||
| 202 | if (st->npfds == 0) { | ||
| 203 | /* WSAPoll rejects an empty set (WSAEINVAL) where poll() would just | ||
| 204 | * sleep; do the sleeping ourselves so a timer-only loop still runs */ | ||
| 205 | Sleep(tmo_ms < 0 ? 50 : (DWORD)tmo_ms); | ||
| 206 | n = 0; | ||
| 207 | } else { | ||
| 208 | n = WSAPoll(st->pfds, (ULONG)st->npfds, tmo_ms); | ||
| 209 | if (n == SOCKET_ERROR) { | ||
| 210 | hm_os_seterrno(); | ||
| 211 | return -1; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | for (i = 0; i < st->npfds && nev < max; i++) { | ||
| 216 | SHORT rev = st->pfds[i].revents; | ||
| 217 | int fd = st->ofd[i]; | ||
| 218 | if (!rev) continue; | ||
| 219 | |||
| 220 | if (st->wake_r >= 0 && fd == st->wake_r) { | ||
| 221 | char buf[64]; | ||
| 222 | int s; | ||
| 223 | while (hm_os_recv(st->wake_r, buf, sizeof(buf)) > 0) { } | ||
| 224 | for (s = 1; s < 64 && nev < max; s++) { | ||
| 225 | if (hm_ws_sigflag[s]) { | ||
| 226 | hm_ws_sigflag[s] = 0; | ||
| 227 | out[nev].kind = HM_EV_SIGNAL; | ||
| 228 | out[nev].fd = s; | ||
| 229 | out[nev].udata = NULL; | ||
| 230 | nev++; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | continue; | ||
| 234 | } | ||
| 235 | |||
| 236 | { | ||
| 237 | int had = hm_ws_mask_of(st->pfds[i].events); | ||
| 238 | int got = 0; | ||
| 239 | if (rev & (POLLRDNORM | POLLHUP | POLLERR | POLLNVAL)) got |= HM_EV_READ; | ||
| 240 | if (rev & (POLLWRNORM | POLLERR | POLLNVAL)) got |= HM_EV_WRITE; | ||
| 241 | got &= had; | ||
| 242 | if (!got) got = had; /* error on an fd: wake whatever watches it */ | ||
| 243 | if (got & HM_EV_READ) { | ||
| 244 | out[nev].kind = HM_EV_READ; | ||
| 245 | out[nev].fd = fd; | ||
| 246 | out[nev].udata = NULL; | ||
| 247 | nev++; | ||
| 248 | } | ||
| 249 | if ((got & HM_EV_WRITE) && nev < max) { | ||
| 250 | out[nev].kind = HM_EV_WRITE; | ||
| 251 | out[nev].fd = fd; | ||
| 252 | out[nev].udata = NULL; | ||
| 253 | nev++; | ||
| 254 | } | ||
| 255 | if (st->oneshot[fd] & got) { | ||
| 256 | ws_remove_io(be, fd, st->oneshot[fd] & got); | ||
| 257 | i--; /* entry may have been swapped away; recheck this slot */ | ||
| 258 | } | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | now = hm_ws_now(); | ||
| 263 | for (i = 0; i < st->ntimers && nev < max; i++) { | ||
| 264 | if (st->timers[i].when <= now) { | ||
| 265 | out[nev].kind = HM_EV_TIMER; | ||
| 266 | out[nev].fd = -1; | ||
| 267 | out[nev].udata = st->timers[i].udata; | ||
| 268 | nev++; | ||
| 269 | if (st->timers[i].interval > 0) { | ||
| 270 | st->timers[i].when += st->timers[i].interval; | ||
| 271 | } else { | ||
| 272 | st->timers[i] = st->timers[--st->ntimers]; | ||
| 273 | i--; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | return nev; | ||
| 279 | } | ||
| 280 | |||
| 281 | static void ws_destroy(hm_backend *be) { | ||
| 282 | hm_ws_state *st = (hm_ws_state *)be->state; | ||
| 283 | if (st) { | ||
| 284 | if (st->wake_r >= 0 && !be->foreign) { | ||
| 285 | hm_os_close(st->wake_r); | ||
| 286 | hm_os_close(st->wake_w); | ||
| 287 | } | ||
| 288 | if (hm_ws_wake_w == st->wake_w) hm_ws_wake_w = -1; | ||
| 289 | free(st->pfds); | ||
| 290 | free(st->ofd); | ||
| 291 | free(st->timers); | ||
| 292 | free(st); | ||
| 293 | } | ||
| 294 | free(be); | ||
| 295 | } | ||
| 296 | |||
| 297 | hm_backend *hm_backend_wsapoll_new(void) { | ||
| 298 | hm_backend *be = (hm_backend *)hm_xcalloc(1, sizeof(hm_backend)); | ||
| 299 | hm_ws_state *st = (hm_ws_state *)hm_xcalloc(1, sizeof(hm_ws_state)); | ||
| 300 | int i; | ||
| 301 | if (!be || !st) { free(be); free(st); return NULL; } | ||
| 302 | hm_os_init(); | ||
| 303 | for (i = 0; i < HM_MAXFD; i++) st->idx[i] = -1; | ||
| 304 | st->wake_r = st->wake_w = -1; | ||
| 305 | be->name = "wsapoll"; | ||
| 306 | be->state = st; | ||
| 307 | be->add_io = ws_add_io; | ||
| 308 | be->modify_io = ws_modify_io; | ||
| 309 | be->remove_io = ws_remove_io; | ||
| 310 | be->add_timer = ws_add_timer; | ||
| 311 | be->del_timer = ws_del_timer; | ||
| 312 | be->add_signal = ws_add_signal; | ||
| 313 | be->wait = ws_wait; | ||
| 314 | be->destroy = ws_destroy; | ||
| 315 | return be; | ||
| 316 | } | ||
| 317 | |||
| 318 | int hm_backend_wsapoll_available(void) { return 1; } | ||
| 319 | |||
| 320 | #else /* !_WIN32 */ | ||
| 321 | |||
| 322 | 1 | hm_backend *hm_backend_wsapoll_new(void) { return 0; } | |
| 323 | /* | ||
| 324 | # uncoverable statement | ||
| 325 | # uncoverable subroutine */ | ||
| 326 | 0 | int hm_backend_wsapoll_available(void) { return 0; } | |
| 327 | |||
| 328 | #endif |