mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-08 12:18:31 +00:00
Fix debug mode build
This commit is contained in:
parent
dc0ea6640e
commit
be7c5e1071
9 changed files with 26 additions and 1 deletions
80
libc/calls/poll-metal.c
Normal file
80
libc/calls/poll-metal.c
Normal file
|
@ -0,0 +1,80 @@
|
|||
/*-*- 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 2021 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ 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. │
|
||||
│ │
|
||||
│ 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. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/safemacros.internal.h"
|
||||
#include "libc/nexgen32e/rdtsc.h"
|
||||
#include "libc/nexgen32e/uart.internal.h"
|
||||
#include "libc/runtime/pc.internal.h"
|
||||
#include "libc/sock/internal.h"
|
||||
#include "libc/sysv/consts/poll.h"
|
||||
|
||||
int sys_poll_metal(struct pollfd *fds, size_t nfds, unsigned timeout_ms) {
|
||||
int rc;
|
||||
size_t i;
|
||||
bool blocking;
|
||||
uint64_t start, timeout;
|
||||
if (!timeout_ms) {
|
||||
start = 0;
|
||||
timeout = 0;
|
||||
blocking = false;
|
||||
} else {
|
||||
start = rdtsc();
|
||||
timeout = timeout_ms;
|
||||
timeout *= 3; /* approx. cycles to nanoseconds */
|
||||
timeout *= 1000000;
|
||||
blocking = true;
|
||||
}
|
||||
for (rc = 0;;) {
|
||||
for (i = 0; i < nfds; ++i) {
|
||||
fds[i].revents = 0;
|
||||
if (fds[i].fd >= 0) {
|
||||
if (__isfdopen(fds[i].fd)) {
|
||||
switch (g_fds.p[fds[i].fd].kind) {
|
||||
case kFdSerial:
|
||||
if ((fds[i].events & POLLIN) &&
|
||||
(inb(g_fds.p[fds[i].fd].handle + UART_LSR) & UART_TTYDA)) {
|
||||
fds[i].revents |= POLLIN;
|
||||
}
|
||||
if ((fds[i].events & POLLOUT) &&
|
||||
(inb(g_fds.p[fds[i].fd].handle + UART_LSR) & UART_TTYTXR)) {
|
||||
fds[i].revents |= POLLOUT;
|
||||
}
|
||||
break;
|
||||
case kFdFile:
|
||||
if (fds[i].events & (POLLIN | POLLOUT)) {
|
||||
fds[i].revents |= fds[i].events & (POLLIN | POLLOUT);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fds[i].revents = POLLNVAL;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
fds[i].revents = POLLNVAL;
|
||||
}
|
||||
}
|
||||
if (fds[i].revents) ++rc;
|
||||
}
|
||||
if (rc || !blocking || unsignedsubtract(rdtsc(), start) >= timeout) {
|
||||
break;
|
||||
} else {
|
||||
__builtin_ia32_pause();
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
211
libc/calls/poll-nt.c
Normal file
211
libc/calls/poll-nt.c
Normal file
|
@ -0,0 +1,211 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ 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. │
|
||||
│ │
|
||||
│ 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. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/bits/weaken.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/sig.internal.h"
|
||||
#include "libc/calls/sigbits.h"
|
||||
#include "libc/calls/strace.internal.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/spinlock.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nt/enum/filetype.h"
|
||||
#include "libc/nt/errors.h"
|
||||
#include "libc/nt/files.h"
|
||||
#include "libc/nt/ipc.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/pollfd.h"
|
||||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/nt/winsock.h"
|
||||
#include "libc/sock/internal.h"
|
||||
#include "libc/sock/ntstdin.internal.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/poll.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
_Alignas(64) static char poll_lock;
|
||||
|
||||
/**
|
||||
* Polls on the New Technology.
|
||||
*
|
||||
* This function is used to implement poll() and select(). You may poll
|
||||
* on both sockets and files at the same time. We also poll for signals
|
||||
* while poll is polling.
|
||||
*/
|
||||
textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint64_t *ms) {
|
||||
bool ok;
|
||||
uint32_t avail;
|
||||
struct sys_pollfd_nt pipefds[8];
|
||||
struct sys_pollfd_nt sockfds[64];
|
||||
int pipeindices[ARRAYLEN(pipefds)];
|
||||
int sockindices[ARRAYLEN(sockfds)];
|
||||
int i, sn, pn, failed, gotinvals, gotpipes, gotsocks, waitfor;
|
||||
|
||||
// check for interrupts early before doing work
|
||||
if (_check_interrupts(false, g_fds.p)) return eintr();
|
||||
|
||||
// do the planning
|
||||
// we need to read static variables
|
||||
// we might need to spawn threads and open pipes
|
||||
_spinlock(&poll_lock);
|
||||
_spinlock(&__fds_lock);
|
||||
for (gotinvals = failed = sn = pn = i = 0; i < nfds; ++i) {
|
||||
if (fds[i].fd < 0) continue;
|
||||
if (__isfdopen(fds[i].fd)) {
|
||||
if (__isfdkind(fds[i].fd, kFdSocket)) {
|
||||
if (sn < ARRAYLEN(sockfds)) {
|
||||
// the magnums for POLLIN/OUT/PRI on NT include the other ones too
|
||||
// we need to clear ones like POLLNVAL or else WSAPoll shall whine
|
||||
sockindices[sn] = i;
|
||||
sockfds[sn].handle = g_fds.p[fds[i].fd].handle;
|
||||
sockfds[sn].events = fds[i].events & (POLLPRI | POLLIN | POLLOUT);
|
||||
sockfds[sn].revents = 0;
|
||||
++sn;
|
||||
} else {
|
||||
// too many socket fds
|
||||
failed = enomem();
|
||||
break;
|
||||
}
|
||||
} else if (pn < ARRAYLEN(pipefds)) {
|
||||
pipeindices[pn] = i;
|
||||
pipefds[pn].handle = g_fds.p[fds[i].fd].handle;
|
||||
pipefds[pn].events = 0;
|
||||
pipefds[pn].revents = 0;
|
||||
switch (g_fds.p[fds[i].fd].flags & O_ACCMODE) {
|
||||
case O_RDONLY:
|
||||
pipefds[pn].events = fds[i].events & POLLIN;
|
||||
break;
|
||||
case O_WRONLY:
|
||||
pipefds[pn].events = fds[i].events & POLLOUT;
|
||||
break;
|
||||
case O_RDWR:
|
||||
pipefds[pn].events = fds[i].events & (POLLIN | POLLOUT);
|
||||
break;
|
||||
default:
|
||||
unreachable;
|
||||
}
|
||||
++pn;
|
||||
} else {
|
||||
// too many non-socket fds
|
||||
failed = enomem();
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
++gotinvals;
|
||||
}
|
||||
}
|
||||
_spunlock(&__fds_lock);
|
||||
_spunlock(&poll_lock);
|
||||
if (failed) {
|
||||
// failed to create a polling solution
|
||||
return failed;
|
||||
}
|
||||
|
||||
// perform the i/o and sleeping and looping
|
||||
for (;;) {
|
||||
// see if input is available on non-sockets
|
||||
for (gotpipes = i = 0; i < pn; ++i) {
|
||||
if (pipefds[i].events & POLLOUT) {
|
||||
// we have no way of polling if a non-socket is writeable yet
|
||||
// therefore we assume that if it can happen, it shall happen
|
||||
pipefds[i].revents = POLLOUT;
|
||||
}
|
||||
if (pipefds[i].events & POLLIN) {
|
||||
if (GetFileType(pipefds[i].handle) == kNtFileTypePipe) {
|
||||
ok = PeekNamedPipe(pipefds[i].handle, 0, 0, 0, &avail, 0);
|
||||
POLLTRACE("PeekNamedPipe(%ld, 0, 0, 0, [%'u], 0) → %hhhd% m",
|
||||
pipefds[i].handle, avail, ok);
|
||||
if (ok) {
|
||||
if (avail) {
|
||||
pipefds[i].revents = POLLIN;
|
||||
}
|
||||
} else {
|
||||
pipefds[i].revents = POLLERR;
|
||||
}
|
||||
} else {
|
||||
// we have no way of polling if a non-socket is readable yet
|
||||
// therefore we assume that if it can happen it shall happen
|
||||
pipefds[i].revents = POLLIN;
|
||||
}
|
||||
}
|
||||
if (pipefds[i].revents) {
|
||||
++gotpipes;
|
||||
}
|
||||
}
|
||||
// if we haven't found any good results yet then here we
|
||||
// compute a small time slice we don't mind sleeping for
|
||||
waitfor = gotinvals || gotpipes ? 0 : MIN(__SIG_POLLING_INTERVAL_MS, *ms);
|
||||
if (sn) {
|
||||
// we need to poll the socket handles separately because
|
||||
// microsoft certainly loves to challenge us with coding
|
||||
// please note that winsock will fail if we pass zero fd
|
||||
POLLTRACE("WSAPoll(%p, %u, %'d) out of %'lu", sockfds, sn, waitfor, *ms);
|
||||
if ((gotsocks = WSAPoll(sockfds, sn, waitfor)) == -1) {
|
||||
return __winsockerr();
|
||||
}
|
||||
*ms -= waitfor;
|
||||
} else {
|
||||
gotsocks = 0;
|
||||
if (!gotinvals && !gotpipes && waitfor) {
|
||||
// if we've only got pipes and none of them are ready
|
||||
// then we'll just explicitly sleep for the time left
|
||||
POLLTRACE("SleepEx(%'d, false) out of %'lu", waitfor, *ms);
|
||||
if (SleepEx(__SIG_POLLING_INTERVAL_MS, true) == kNtWaitIoCompletion) {
|
||||
POLLTRACE("IOCP EINTR");
|
||||
} else {
|
||||
*ms -= waitfor;
|
||||
}
|
||||
}
|
||||
}
|
||||
// we gave all the sockets and all the named pipes a shot
|
||||
// if we found anything at all then it's time to end work
|
||||
if (gotinvals || gotpipes || gotsocks || *ms <= 0) {
|
||||
break;
|
||||
}
|
||||
// otherwise loop limitlessly for timeout to elapse while
|
||||
// checking for signal delivery interrupts, along the way
|
||||
if (_check_interrupts(false, g_fds.p)) {
|
||||
return eintr();
|
||||
}
|
||||
}
|
||||
|
||||
// the system call is going to succeed
|
||||
// it's now ok to start setting the output memory
|
||||
for (i = 0; i < nfds; ++i) {
|
||||
if (fds[i].fd < 0 || __isfdopen(fds[i].fd)) {
|
||||
fds[i].revents = 0;
|
||||
} else {
|
||||
fds[i].revents = POLLNVAL;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < pn; ++i) {
|
||||
fds[pipeindices[i]].revents = pipefds[i].revents;
|
||||
}
|
||||
for (i = 0; i < sn; ++i) {
|
||||
fds[sockindices[i]].revents = sockfds[i].revents;
|
||||
}
|
||||
|
||||
// and finally return
|
||||
return gotinvals + gotpipes + gotsocks;
|
||||
}
|
81
libc/calls/poll.c
Normal file
81
libc/calls/poll.c
Normal file
|
@ -0,0 +1,81 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ 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. │
|
||||
│ │
|
||||
│ 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. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/strace.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/sock/internal.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
/**
|
||||
* Waits for something to happen on multiple file descriptors at once.
|
||||
*
|
||||
* Warning: XNU has an inconsistency with other platforms. If you have
|
||||
* pollfds with fd≥0 and none of the meaningful events flags are added
|
||||
* e.g. POLLIN then XNU won't check for POLLNVAL. This matters because
|
||||
* one of the use-cases for poll() is quickly checking for open files.
|
||||
*
|
||||
* Note: Polling works best on Windows for sockets. We're able to poll
|
||||
* input on named pipes. But for anything that isn't a socket, or pipe
|
||||
* with POLLIN, (e.g. regular file) then POLLIN/POLLOUT are always set
|
||||
* into revents if they're requested, provided they were opened with a
|
||||
* mode that permits reading and/or writing.
|
||||
*
|
||||
* Note: Windows has a limit of 64 file descriptors and ENOMEM with -1
|
||||
* is returned if that limit is exceeded. In practice the limit is not
|
||||
* this low. For example, pollfds with fd<0 don't count. So the caller
|
||||
* could flip the sign bit with a short timeout, to poll a larger set.
|
||||
*
|
||||
* @param fds[𝑖].fd should be a socket, input pipe, or conosle input
|
||||
* and if it's a negative number then the entry is ignored
|
||||
* @param fds[𝑖].events flags can have POLLIN, POLLOUT, POLLPRI,
|
||||
* POLLRDNORM, POLLWRNORM, POLLRDBAND, POLLWRBAND as well as
|
||||
* POLLERR, POLLHUP, and POLLNVAL although the latter are
|
||||
* always implied (assuming fd≥0) so they're ignored here
|
||||
* @param timeout_ms if 0 means don't wait and -1 means wait forever
|
||||
* @return number of items fds whose revents field has been set to
|
||||
* nonzero to describe its events, or 0 if the timeout elapsed,
|
||||
* or -1 w/ errno
|
||||
* @return fds[𝑖].revents is always zero initializaed and then will
|
||||
* be populated with POLL{IN,OUT,PRI,HUP,ERR,NVAL} if something
|
||||
* was determined about the file descriptor
|
||||
* @asyncsignalsafe
|
||||
* @threadsafe
|
||||
* @norestart
|
||||
*/
|
||||
int poll(struct pollfd *fds, size_t nfds, int timeout_ms) {
|
||||
int rc;
|
||||
uint64_t millis;
|
||||
if (IsAsan() && !__asan_is_valid(fds, nfds * sizeof(struct pollfd))) {
|
||||
rc = efault();
|
||||
} else if (!IsWindows()) {
|
||||
if (!IsMetal()) {
|
||||
rc = sys_poll(fds, nfds, timeout_ms);
|
||||
} else {
|
||||
rc = sys_poll_metal(fds, nfds, timeout_ms);
|
||||
}
|
||||
} else {
|
||||
millis = timeout_ms;
|
||||
rc = sys_poll_nt(fds, nfds, &millis);
|
||||
}
|
||||
STRACE("poll(%p, %'lu, %'d) → %d% lm", fds, nfds, timeout_ms, rc);
|
||||
return rc;
|
||||
}
|
29
libc/calls/winsockerr.c
Normal file
29
libc/calls/winsockerr.c
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ 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. │
|
||||
│ │
|
||||
│ 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. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/errno.h"
|
||||
#include "libc/nt/winsock.h"
|
||||
#include "libc/sock/internal.h"
|
||||
|
||||
/**
|
||||
* Error return path for winsock wrappers.
|
||||
*/
|
||||
textwindows int64_t __winsockerr(void) {
|
||||
errno = __dos2errno(WSAGetLastError());
|
||||
return -1;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue