2020-11-28 12:01:51 -08:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-07 22:11:56 -05:00
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2020-11-28 12:01:51 -08:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-27 17:18:44 -08: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-11-28 12:01:51 -08:00
|
|
|
│ │
|
2020-12-27 17:18:44 -08: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-11-28 12:01:51 -08:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-05-23 15:06:11 -07:00
|
|
|
#include "libc/calls/internal.h"
|
2022-08-15 15:18:37 -07:00
|
|
|
#include "libc/calls/state.internal.h"
|
2022-04-14 23:39:48 -07:00
|
|
|
#include "libc/calls/struct/timeval.h"
|
2023-09-21 07:30:39 -07:00
|
|
|
#include "libc/limits.h"
|
2024-08-04 12:52:25 -07:00
|
|
|
#include "libc/macros.h"
|
2022-04-14 23:39:48 -07:00
|
|
|
#include "libc/sock/select.h"
|
|
|
|
#include "libc/sock/sock.h"
|
2022-08-13 13:11:56 -07:00
|
|
|
#include "libc/sock/struct/pollfd.h"
|
|
|
|
#include "libc/sock/struct/pollfd.internal.h"
|
2022-04-14 23:39:48 -07:00
|
|
|
#include "libc/sysv/consts/poll.h"
|
2021-08-26 15:59:55 -07:00
|
|
|
#include "libc/sysv/errfuns.h"
|
2023-05-09 23:35:10 -07:00
|
|
|
#ifdef __x86_64__
|
|
|
|
|
2024-09-01 19:29:47 -07:00
|
|
|
// <sync libc/sysv/consts.sh>
|
|
|
|
#define POLLERR_ 0x0001 // implied in events
|
|
|
|
#define POLLHUP_ 0x0002 // implied in events
|
|
|
|
#define POLLNVAL_ 0x0004 // implied in events
|
|
|
|
#define POLLIN_ 0x0300
|
|
|
|
#define POLLRDNORM_ 0x0100
|
|
|
|
#define POLLRDBAND_ 0x0200
|
|
|
|
#define POLLOUT_ 0x0010
|
|
|
|
#define POLLWRNORM_ 0x0010
|
|
|
|
#define POLLWRBAND_ 0x0020 // MSDN undocumented
|
|
|
|
#define POLLPRI_ 0x0400 // MSDN unsupported
|
|
|
|
// </sync libc/sysv/consts.sh>
|
|
|
|
|
2022-04-14 23:39:48 -07:00
|
|
|
int sys_select_nt(int nfds, fd_set *readfds, fd_set *writefds,
|
2024-10-07 15:29:01 -07:00
|
|
|
fd_set *exceptfds, const struct timespec *timeout,
|
2022-08-15 15:18:37 -07:00
|
|
|
const sigset_t *sigmask) {
|
2024-09-01 19:29:47 -07:00
|
|
|
int pfds = 0;
|
2020-11-28 12:01:51 -08:00
|
|
|
|
2022-04-14 23:39:48 -07:00
|
|
|
// convert bitsets to pollfd
|
2024-09-01 19:29:47 -07:00
|
|
|
struct pollfd fds[128];
|
|
|
|
for (int fd = 0; fd < nfds; ++fd) {
|
|
|
|
int events = 0;
|
|
|
|
if (readfds && FD_ISSET(fd, readfds))
|
|
|
|
events |= POLLIN_;
|
|
|
|
if (writefds && FD_ISSET(fd, writefds))
|
|
|
|
events |= POLLOUT_;
|
|
|
|
if (exceptfds && FD_ISSET(fd, exceptfds))
|
|
|
|
events |= POLLPRI_;
|
2022-04-14 23:39:48 -07:00
|
|
|
if (events) {
|
2024-09-01 19:29:47 -07:00
|
|
|
if (pfds == ARRAYLEN(fds))
|
|
|
|
return e2big();
|
|
|
|
fds[pfds].fd = fd;
|
|
|
|
fds[pfds].events = events;
|
|
|
|
fds[pfds].revents = 0;
|
|
|
|
++pfds;
|
2020-11-28 12:01:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 23:39:48 -07:00
|
|
|
// call our nt poll implementation
|
2024-10-07 15:29:01 -07:00
|
|
|
int fdcount = sys_poll_nt(fds, pfds, timeout, sigmask);
|
2024-09-01 19:29:47 -07:00
|
|
|
if (fdcount == -1)
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 10:38:00 -07:00
|
|
|
return -1;
|
2022-04-14 23:39:48 -07:00
|
|
|
|
|
|
|
// convert pollfd back to bitsets
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 10:38:00 -07:00
|
|
|
if (readfds)
|
|
|
|
FD_ZERO(readfds);
|
|
|
|
if (writefds)
|
|
|
|
FD_ZERO(writefds);
|
|
|
|
if (exceptfds)
|
|
|
|
FD_ZERO(exceptfds);
|
2023-09-21 07:30:39 -07:00
|
|
|
int bits = 0;
|
2024-09-01 19:29:47 -07:00
|
|
|
for (int i = 0; i < pfds; ++i) {
|
|
|
|
if (fds[i].revents & (POLLIN_ | POLLHUP_ | POLLERR_ | POLLNVAL_)) {
|
2023-09-21 07:30:39 -07:00
|
|
|
if (readfds) {
|
|
|
|
FD_SET(fds[i].fd, readfds);
|
|
|
|
++bits;
|
|
|
|
}
|
|
|
|
}
|
2024-09-01 19:29:47 -07:00
|
|
|
if (fds[i].revents & POLLOUT_) {
|
2023-09-21 07:30:39 -07:00
|
|
|
if (writefds) {
|
|
|
|
FD_SET(fds[i].fd, writefds);
|
|
|
|
++bits;
|
|
|
|
}
|
|
|
|
}
|
2024-09-01 19:29:47 -07:00
|
|
|
if (fds[i].revents & POLLPRI_) {
|
2023-09-21 07:30:39 -07:00
|
|
|
if (exceptfds) {
|
|
|
|
FD_SET(fds[i].fd, exceptfds);
|
|
|
|
++bits;
|
|
|
|
}
|
|
|
|
}
|
2020-11-28 12:01:51 -08:00
|
|
|
}
|
|
|
|
|
2023-09-21 07:30:39 -07:00
|
|
|
return bits;
|
2020-11-28 12:01:51 -08:00
|
|
|
}
|
2023-05-09 23:35:10 -07:00
|
|
|
|
|
|
|
#endif /* __x86_64__ */
|