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 et 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
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/calls/internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/state.internal.h"
|
2024-09-12 08:18:14 +00:00
|
|
|
#include "libc/calls/struct/sigset.h"
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
#include "libc/calls/struct/sigset.internal.h"
|
2023-09-21 14:30:39 +00:00
|
|
|
#include "libc/calls/struct/timespec.h"
|
2024-09-11 01:59:06 +00:00
|
|
|
#include "libc/calls/syscall_support-nt.internal.h"
|
2023-09-21 14:30:39 +00:00
|
|
|
#include "libc/intrin/atomic.h"
|
2024-09-11 01:59:06 +00:00
|
|
|
#include "libc/intrin/fds.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.h"
|
2023-09-19 18:42:38 +00:00
|
|
|
#include "libc/nt/console.h"
|
2022-04-16 17:40:23 +00:00
|
|
|
#include "libc/nt/enum/filetype.h"
|
2024-09-11 01:59:06 +00:00
|
|
|
#include "libc/nt/enum/wait.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/nt/errors.h"
|
2022-04-16 17:40:23 +00:00
|
|
|
#include "libc/nt/files.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/nt/ipc.h"
|
|
|
|
#include "libc/nt/runtime.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/struct/pollfd.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/nt/synchronization.h"
|
2024-09-11 01:59:06 +00:00
|
|
|
#include "libc/nt/time.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/winsock.h"
|
|
|
|
#include "libc/sock/internal.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/sock/struct/pollfd.h"
|
2022-04-16 17:40:23 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
#include "libc/thread/posixthread.internal.h"
|
2023-05-09 08:56:56 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
|
2023-11-07 00:38:44 +00:00
|
|
|
#define POLL_INTERVAL_MS 10
|
|
|
|
|
2024-09-02 02:29:47 +00: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>
|
|
|
|
|
2024-09-12 08:18:14 +00:00
|
|
|
textwindows dontinline static struct timespec sys_poll_nt_now(void) {
|
2024-09-11 01:59:06 +00:00
|
|
|
uint64_t hectons;
|
|
|
|
QueryUnbiasedInterruptTimePrecise(&hectons);
|
|
|
|
return timespec_fromnanos(hectons * 100);
|
|
|
|
}
|
|
|
|
|
2024-09-12 08:18:14 +00:00
|
|
|
textwindows static uint32_t sys_poll_nt_waitms(struct timespec deadline) {
|
|
|
|
struct timespec now = sys_poll_nt_now();
|
|
|
|
if (timespec_cmp(now, deadline) < 0) {
|
|
|
|
struct timespec remain = timespec_sub(deadline, now);
|
|
|
|
int64_t millis = timespec_tomillis(remain);
|
|
|
|
uint32_t waitfor = MIN(millis, 0xffffffffu);
|
|
|
|
return MIN(waitfor, POLL_INTERVAL_MS);
|
|
|
|
} else {
|
|
|
|
return 0; // we timed out
|
2024-09-11 01:59:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 14:30:39 +00:00
|
|
|
// Polls on the New Technology.
|
|
|
|
//
|
|
|
|
// This function is used to implement poll() and select(). You may poll
|
2023-10-14 08:06:00 +00:00
|
|
|
// on sockets, files and the console at the same time. We also poll for
|
|
|
|
// both signals and posix thread cancelation, while the poll is polling
|
2024-09-12 08:18:14 +00:00
|
|
|
textwindows static int sys_poll_nt_actual(struct pollfd *fds, uint64_t nfds,
|
|
|
|
struct timespec deadline,
|
|
|
|
sigset_t waitmask) {
|
2024-09-11 01:59:06 +00:00
|
|
|
int fileindices[64];
|
|
|
|
int sockindices[64];
|
|
|
|
int64_t filehands[64];
|
|
|
|
struct PosixThread *pt;
|
|
|
|
int i, rc, ev, kind, gotsocks;
|
2022-04-15 06:39:48 +00:00
|
|
|
struct sys_pollfd_nt sockfds[64];
|
2024-09-11 01:59:06 +00:00
|
|
|
uint32_t cm, fi, wi, sn, pn, avail, waitfor, already_slept;
|
2022-04-15 06:39:48 +00:00
|
|
|
|
2024-09-11 01:59:06 +00:00
|
|
|
// ensure revents is cleared
|
|
|
|
for (i = 0; i < nfds; ++i)
|
|
|
|
fds[i].revents = 0;
|
|
|
|
|
|
|
|
// divide files from sockets
|
|
|
|
// check for invalid file descriptors
|
2022-06-09 03:01:28 +00:00
|
|
|
__fds_lock();
|
2024-09-11 01:59:06 +00:00
|
|
|
for (rc = sn = pn = i = 0; i < nfds; ++i) {
|
2022-04-15 06:39:48 +00:00
|
|
|
if (fds[i].fd < 0)
|
|
|
|
continue;
|
|
|
|
if (__isfdopen(fds[i].fd)) {
|
2024-09-11 01:59:06 +00:00
|
|
|
kind = g_fds.p[fds[i].fd].kind;
|
|
|
|
if (kind == kFdSocket) {
|
|
|
|
// we can use WSAPoll() for these fds
|
2022-04-15 06:39:48 +00:00
|
|
|
if (sn < ARRAYLEN(sockfds)) {
|
2024-09-02 02:29:47 +00:00
|
|
|
// WSAPoll whines if we pass POLLNVAL, POLLHUP, or POLLERR.
|
2022-04-15 06:39:48 +00:00
|
|
|
sockindices[sn] = i;
|
|
|
|
sockfds[sn].handle = g_fds.p[fds[i].fd].handle;
|
2024-09-02 02:29:47 +00:00
|
|
|
sockfds[sn].events =
|
|
|
|
fds[i].events & (POLLRDNORM_ | POLLRDBAND_ | POLLWRNORM_);
|
2022-04-16 17:40:23 +00:00
|
|
|
sockfds[sn].revents = 0;
|
|
|
|
++sn;
|
2022-04-15 06:39:48 +00:00
|
|
|
} else {
|
2024-09-11 01:59:06 +00:00
|
|
|
// too many sockets
|
|
|
|
rc = einval();
|
2022-04-15 06:39:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-09-11 01:59:06 +00:00
|
|
|
} else if (kind == kFdFile || kind == kFdConsole) {
|
|
|
|
// we can use WaitForMultipleObjects() for these fds
|
|
|
|
if (pn < ARRAYLEN(fileindices) - 1) { // last slot for semaphore
|
|
|
|
fileindices[pn] = i;
|
|
|
|
filehands[pn] = g_fds.p[fds[i].fd].handle;
|
|
|
|
++pn;
|
|
|
|
} else {
|
|
|
|
// too many files
|
|
|
|
rc = einval();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (kind == kFdDevNull || kind == kFdDevRandom || kind == kFdZip) {
|
|
|
|
// we can't wait on these kinds via win32
|
|
|
|
if (fds[i].events & (POLLRDNORM_ | POLLWRNORM_)) {
|
|
|
|
// the linux kernel does this irrespective of oflags
|
|
|
|
fds[i].revents = fds[i].events & (POLLRDNORM_ | POLLWRNORM_);
|
2022-04-15 06:39:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-09-11 01:59:06 +00:00
|
|
|
// unsupported file type
|
|
|
|
fds[i].revents = POLLNVAL_;
|
2022-04-15 06:39:48 +00:00
|
|
|
}
|
2021-08-26 04:35:58 +00:00
|
|
|
} else {
|
2024-09-11 01:59:06 +00:00
|
|
|
// file not open
|
|
|
|
fds[i].revents = POLLNVAL_;
|
2021-08-26 04:35:58 +00:00
|
|
|
}
|
2024-09-11 01:59:06 +00:00
|
|
|
rc += !!fds[i].revents;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-06-09 03:01:28 +00:00
|
|
|
__fds_unlock();
|
2024-09-12 08:18:14 +00:00
|
|
|
if (rc == -1)
|
2023-10-14 08:06:00 +00:00
|
|
|
return rc;
|
2022-04-15 06:39:48 +00:00
|
|
|
|
2024-09-11 01:59:06 +00:00
|
|
|
// perform poll operation
|
2021-01-25 21:08:05 +00:00
|
|
|
for (;;) {
|
2024-09-10 11:12:21 +00:00
|
|
|
|
2024-09-11 01:59:06 +00:00
|
|
|
// check input status of pipes / consoles without blocking
|
|
|
|
// this ensures any socket fds won't starve them of events
|
2024-09-12 00:13:23 +00:00
|
|
|
// we can't poll file handles, so we just mark those ready
|
2024-09-11 01:59:06 +00:00
|
|
|
for (i = 0; i < pn; ++i) {
|
|
|
|
fi = fileindices[i];
|
|
|
|
ev = fds[fi].events;
|
|
|
|
ev &= POLLRDNORM_ | POLLWRNORM_;
|
|
|
|
if ((g_fds.p[fds[fi].fd].flags & O_ACCMODE) == O_RDONLY)
|
|
|
|
ev &= ~POLLWRNORM_;
|
|
|
|
if ((g_fds.p[fds[fi].fd].flags & O_ACCMODE) == O_WRONLY)
|
|
|
|
ev &= ~POLLRDNORM_;
|
|
|
|
if ((ev & POLLWRNORM_) && !(ev & POLLRDNORM_)) {
|
|
|
|
fds[fi].revents = fds[fi].events & (POLLRDNORM_ | POLLWRNORM_);
|
|
|
|
} else if (GetFileType(filehands[i]) == kNtFileTypePipe) {
|
2024-09-12 08:18:14 +00:00
|
|
|
if (PeekNamedPipe(filehands[i], 0, 0, 0, &avail, 0)) {
|
2024-09-11 01:59:06 +00:00
|
|
|
if (avail)
|
|
|
|
fds[fi].revents = POLLRDNORM_;
|
|
|
|
} else if (GetLastError() == kNtErrorHandleEof ||
|
|
|
|
GetLastError() == kNtErrorBrokenPipe) {
|
|
|
|
fds[fi].revents = POLLHUP_;
|
|
|
|
} else {
|
|
|
|
fds[fi].revents = POLLERR_;
|
|
|
|
}
|
|
|
|
} else if (GetConsoleMode(filehands[i], &cm)) {
|
|
|
|
switch (CountConsoleInputBytes()) {
|
|
|
|
case 0:
|
|
|
|
fds[fi].revents = fds[fi].events & POLLWRNORM_;
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
fds[fi].revents = POLLHUP_;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fds[fi].revents = fds[fi].events & (POLLRDNORM_ | POLLWRNORM_);
|
|
|
|
break;
|
|
|
|
}
|
2024-09-12 00:13:23 +00:00
|
|
|
} else {
|
|
|
|
fds[fi].revents = fds[fi].events & (POLLRDNORM_ | POLLWRNORM_);
|
2024-09-11 01:59:06 +00:00
|
|
|
}
|
|
|
|
rc += !!fds[fi].revents;
|
|
|
|
}
|
|
|
|
|
2024-09-10 11:12:21 +00:00
|
|
|
// determine how long to wait
|
2024-09-12 08:18:14 +00:00
|
|
|
waitfor = sys_poll_nt_waitms(deadline);
|
2024-09-10 11:12:21 +00:00
|
|
|
|
2024-09-11 01:59:06 +00:00
|
|
|
// check for events and/or readiness on sockets
|
|
|
|
// we always do this due to issues with POLLOUT
|
|
|
|
if (sn) {
|
|
|
|
// if we need to wait, then we prefer to wait inside WSAPoll()
|
|
|
|
// this ensures network events are received in ~10µs not ~10ms
|
|
|
|
if (!rc && waitfor) {
|
2024-09-12 08:18:14 +00:00
|
|
|
if (__sigcheck(waitmask, false))
|
2024-09-11 01:59:06 +00:00
|
|
|
return -1;
|
|
|
|
already_slept = waitfor;
|
|
|
|
} else {
|
|
|
|
already_slept = 0;
|
|
|
|
}
|
|
|
|
if ((gotsocks = WSAPoll(sockfds, sn, already_slept)) == -1)
|
|
|
|
return __winsockerr();
|
|
|
|
if (gotsocks) {
|
|
|
|
for (i = 0; i < sn; ++i)
|
|
|
|
if (sockfds[i].revents) {
|
|
|
|
fds[sockindices[i]].revents = sockfds[i].revents;
|
|
|
|
++rc;
|
2024-09-05 10:17:19 +00:00
|
|
|
}
|
2024-09-11 01:59:06 +00:00
|
|
|
} else if (already_slept) {
|
2024-09-12 08:18:14 +00:00
|
|
|
if (__sigcheck(waitmask, false))
|
2024-09-11 01:59:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
already_slept = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return if we observed events
|
|
|
|
if (rc || !waitfor)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// if nothing has happened and we haven't already waited in poll()
|
|
|
|
// then we can wait on consoles, pipes, and signals simultaneously
|
|
|
|
// this ensures low latency for apps like emacs which with no sock
|
|
|
|
// here we shall actually report that something can be written too
|
|
|
|
if (!already_slept) {
|
2024-09-12 08:18:14 +00:00
|
|
|
if (__sigcheck(waitmask, false))
|
2024-09-11 01:59:06 +00:00
|
|
|
return -1;
|
|
|
|
pt = _pthread_self();
|
|
|
|
filehands[pn] = pt->pt_semaphore = CreateSemaphore(0, 0, 1, 0);
|
|
|
|
atomic_store_explicit(&pt->pt_blocker, PT_BLOCKER_SEM,
|
|
|
|
memory_order_release);
|
|
|
|
wi = WaitForMultipleObjects(pn + 1, filehands, 0, waitfor);
|
|
|
|
atomic_store_explicit(&pt->pt_blocker, 0, memory_order_release);
|
|
|
|
CloseHandle(filehands[pn]);
|
|
|
|
if (wi == -1u) {
|
|
|
|
// win32 wait failure
|
|
|
|
return __winerr();
|
|
|
|
} else if (wi == pn) {
|
|
|
|
// our semaphore was signalled
|
2024-09-12 08:18:14 +00:00
|
|
|
if (__sigcheck(waitmask, false))
|
2024-09-11 01:59:06 +00:00
|
|
|
return -1;
|
|
|
|
} else if ((wi ^ kNtWaitAbandoned) < pn) {
|
|
|
|
// this is possibly because a process or thread was killed
|
|
|
|
fds[fileindices[wi ^ kNtWaitAbandoned]].revents = POLLERR_;
|
|
|
|
++rc;
|
|
|
|
} else if (wi < pn) {
|
|
|
|
fi = fileindices[wi];
|
|
|
|
// one of the handles we polled is ready for fi/o
|
|
|
|
if (GetConsoleMode(filehands[wi], &cm)) {
|
2024-09-05 10:17:19 +00:00
|
|
|
switch (CountConsoleInputBytes()) {
|
|
|
|
case 0:
|
2024-09-11 01:59:06 +00:00
|
|
|
// it's possible there was input and it was handled by the
|
|
|
|
// ICANON reader, and therefore should not be reported yet
|
|
|
|
if (fds[fi].events & POLLWRNORM_)
|
|
|
|
fds[fi].revents = POLLWRNORM_;
|
2024-09-05 10:17:19 +00:00
|
|
|
break;
|
|
|
|
case -1:
|
2024-09-11 01:59:06 +00:00
|
|
|
fds[fi].revents = POLLHUP_;
|
2024-09-05 10:17:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
2024-09-11 01:59:06 +00:00
|
|
|
fds[fi].revents = fds[fi].events & (POLLRDNORM_ | POLLWRNORM_);
|
2024-09-05 10:17:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-09-11 01:59:06 +00:00
|
|
|
} else if (GetFileType(filehands[wi]) == kNtFileTypePipe) {
|
|
|
|
if ((fds[fi].events & POLLRDNORM_) &&
|
|
|
|
(g_fds.p[fds[fi].fd].flags & O_ACCMODE) != O_WRONLY) {
|
|
|
|
if (PeekNamedPipe(filehands[wi], 0, 0, 0, &avail, 0)) {
|
|
|
|
fds[fi].revents = fds[fi].events & (POLLRDNORM_ | POLLWRNORM_);
|
|
|
|
} else if (GetLastError() == kNtErrorHandleEof ||
|
|
|
|
GetLastError() == kNtErrorBrokenPipe) {
|
|
|
|
fds[fi].revents = POLLHUP_;
|
|
|
|
} else {
|
|
|
|
fds[fi].revents = POLLERR_;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fds[fi].revents = fds[fi].events & (POLLRDNORM_ | POLLWRNORM_);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fds[fi].revents = fds[fi].events & (POLLRDNORM_ | POLLWRNORM_);
|
2022-04-15 06:39:48 +00:00
|
|
|
}
|
2024-09-11 01:59:06 +00:00
|
|
|
rc += !!fds[fi].revents;
|
2024-09-02 02:29:47 +00:00
|
|
|
} else {
|
2024-09-11 01:59:06 +00:00
|
|
|
// should only be possible on kNtWaitTimeout or semaphore abandoned
|
|
|
|
// keep looping for events and we'll catch timeout when appropriate
|
2024-09-12 08:18:14 +00:00
|
|
|
if (__sigcheck(waitmask, false))
|
2024-09-10 11:12:21 +00:00
|
|
|
return -1;
|
2022-04-15 06:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
|
2024-09-11 01:59:06 +00:00
|
|
|
// once again, return if we observed events
|
|
|
|
if (rc)
|
2022-04-15 06:39:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-09-11 01:59:06 +00:00
|
|
|
return rc;
|
2023-10-14 08:06:00 +00:00
|
|
|
}
|
2022-08-17 06:23:34 +00:00
|
|
|
|
2024-09-12 08:18:14 +00:00
|
|
|
textwindows static int sys_poll_nt_impl(struct pollfd *fds, uint64_t nfds,
|
|
|
|
struct timespec deadline,
|
|
|
|
const sigset_t waitmask) {
|
|
|
|
uint32_t waitms;
|
|
|
|
int i, n, rc, got = 0;
|
|
|
|
|
|
|
|
// fast path
|
|
|
|
if (nfds <= 63)
|
|
|
|
return sys_poll_nt_actual(fds, nfds, deadline, waitmask);
|
|
|
|
|
|
|
|
// clumsy path
|
|
|
|
for (;;) {
|
|
|
|
for (i = 0; i < nfds; i += 64) {
|
|
|
|
n = nfds - i;
|
|
|
|
n = n > 64 ? 64 : n;
|
|
|
|
rc = sys_poll_nt_actual(fds + i, n, timespec_zero, waitmask);
|
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
|
|
|
got += rc;
|
|
|
|
}
|
|
|
|
if (got)
|
|
|
|
return got;
|
|
|
|
if (!(waitms = sys_poll_nt_waitms(deadline)))
|
|
|
|
return 0;
|
|
|
|
if (_park_norestart(waitms, waitmask) == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-14 08:06:00 +00:00
|
|
|
textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint32_t *ms,
|
|
|
|
const sigset_t *sigmask) {
|
|
|
|
int rc;
|
2024-09-12 08:18:14 +00:00
|
|
|
struct timespec now, timeout, deadline;
|
2023-10-14 08:06:00 +00:00
|
|
|
BLOCK_SIGNALS;
|
2024-09-12 08:18:14 +00:00
|
|
|
now = ms ? sys_poll_nt_now() : timespec_zero;
|
|
|
|
timeout = ms ? timespec_frommillis(*ms) : timespec_max;
|
|
|
|
deadline = timespec_add(now, timeout);
|
|
|
|
rc = sys_poll_nt_impl(fds, nfds, deadline, sigmask ? *sigmask : _SigMask);
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
ALLOW_SIGNALS;
|
2022-08-17 06:23:34 +00:00
|
|
|
return rc;
|
2022-04-15 06:39:48 +00:00
|
|
|
}
|
2023-05-09 08:56:56 +00:00
|
|
|
|
|
|
|
#endif /* __x86_64__ */
|