2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2021-01-28 23:49:15 +00:00
|
|
|
#include "libc/bits/bits.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/struct/pollfd.h"
|
|
|
|
#include "libc/nt/winsock.h"
|
|
|
|
#include "libc/sock/internal.h"
|
2020-11-28 20:01:51 +00:00
|
|
|
#include "libc/sock/yoink.inc"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/poll.h"
|
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2021-08-26 04:35:58 +00:00
|
|
|
textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint64_t ms) {
|
|
|
|
int i, got, waitfor;
|
2021-02-04 03:35:29 +00:00
|
|
|
struct sys_pollfd_nt ntfds[64];
|
2021-08-26 04:35:58 +00:00
|
|
|
if (nfds >= ARRAYLEN(ntfds)) return einval();
|
2020-06-15 14:18:57 +00:00
|
|
|
for (i = 0; i < nfds; ++i) {
|
2021-08-26 04:35:58 +00:00
|
|
|
if (fds[i].fd >= 0) {
|
|
|
|
if (!__isfdkind(fds[i].fd, kFdSocket)) return enotsock();
|
|
|
|
ntfds[i].handle = g_fds.p[fds[i].fd].handle;
|
|
|
|
ntfds[i].events = fds[i].events & (POLLPRI | POLLIN | POLLOUT);
|
|
|
|
} else {
|
|
|
|
ntfds[i].handle = -1;
|
|
|
|
ntfds[i].events = POLLIN;
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2021-01-25 21:08:05 +00:00
|
|
|
for (;;) {
|
2021-01-28 23:49:15 +00:00
|
|
|
if (cmpxchg(&__interrupted, true, false)) return eintr();
|
2021-08-26 04:35:58 +00:00
|
|
|
waitfor = MIN(1000, ms); /* for ctrl+c */
|
2021-01-25 21:08:05 +00:00
|
|
|
if ((got = WSAPoll(ntfds, nfds, waitfor)) != -1) {
|
2021-08-26 04:35:58 +00:00
|
|
|
if (!got && (ms -= waitfor) > 0) continue;
|
2021-01-25 21:08:05 +00:00
|
|
|
for (i = 0; i < nfds; ++i) {
|
2021-08-26 04:35:58 +00:00
|
|
|
fds[i].revents = ntfds[i].handle < 0 ? 0 : ntfds[i].revents;
|
2021-01-25 21:08:05 +00:00
|
|
|
}
|
|
|
|
return got;
|
|
|
|
} else {
|
|
|
|
return __winsockerr();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|