mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-26 20:40:28 +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.
This commit is contained in:
parent
af7cb3c82f
commit
791f79fcb3
382 changed files with 4008 additions and 4511 deletions
|
@ -22,6 +22,7 @@
|
|||
#include "libc/calls/sig.internal.h"
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/calls/struct/sigset.internal.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
|
@ -41,16 +42,17 @@
|
|||
#include "libc/nt/thread.h"
|
||||
#include "libc/nt/thunk/msabi.h"
|
||||
#include "libc/nt/winsock.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sock/internal.h"
|
||||
#include "libc/sock/struct/pollfd.h"
|
||||
#include "libc/sock/struct/pollfd.internal.h"
|
||||
#include "libc/stdio/sysparam.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/poll.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/tls.h"
|
||||
|
||||
#ifdef __x86_64__
|
||||
|
||||
// Polls on the New Technology.
|
||||
|
@ -61,25 +63,22 @@
|
|||
textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint32_t *ms,
|
||||
const sigset_t *sigmask) {
|
||||
bool ok;
|
||||
uint64_t m;
|
||||
bool interrupted;
|
||||
sigset_t oldmask;
|
||||
uint64_t millis;
|
||||
uint32_t cm, avail, waitfor;
|
||||
struct sys_pollfd_nt pipefds[8];
|
||||
struct sys_pollfd_nt sockfds[64];
|
||||
int pipeindices[ARRAYLEN(pipefds)];
|
||||
int sockindices[ARRAYLEN(sockfds)];
|
||||
struct timespec started, deadline, remain, now;
|
||||
int i, rc, sn, pn, gotinvals, gotpipes, gotsocks;
|
||||
|
||||
#if IsModeDbg()
|
||||
struct timespec noearlier =
|
||||
timespec_add(timespec_real(), timespec_frommillis(ms ? *ms : -1u));
|
||||
#endif
|
||||
BLOCK_SIGNALS;
|
||||
started = timespec_real();
|
||||
deadline = timespec_add(started, timespec_frommillis(ms ? *ms : -1u));
|
||||
|
||||
// do the planning
|
||||
// we need to read static variables
|
||||
// we might need to spawn threads and open pipes
|
||||
m = atomic_exchange(&__get_tls()->tib_sigmask, -1);
|
||||
__fds_lock();
|
||||
for (gotinvals = rc = sn = pn = i = 0; i < nfds; ++i) {
|
||||
if (fds[i].fd < 0) continue;
|
||||
|
@ -114,7 +113,7 @@ textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint32_t *ms,
|
|||
pipefds[pn].events = fds[i].events & (POLLIN | POLLOUT);
|
||||
break;
|
||||
default:
|
||||
__builtin_unreachable();
|
||||
break;
|
||||
}
|
||||
++pn;
|
||||
} else {
|
||||
|
@ -127,7 +126,6 @@ textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint32_t *ms,
|
|||
}
|
||||
}
|
||||
__fds_unlock();
|
||||
atomic_store_explicit(&__get_tls()->tib_sigmask, m, memory_order_release);
|
||||
if (rc) {
|
||||
// failed to create a polling solution
|
||||
goto Finished;
|
||||
|
@ -155,8 +153,8 @@ textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint32_t *ms,
|
|||
pipefds[i].revents |= POLLERR;
|
||||
}
|
||||
} else if (GetConsoleMode(pipefds[i].handle, &cm)) {
|
||||
if (CountConsoleInputBytes(g_fds.p + fds[pipeindices[i]].fd)) {
|
||||
pipefds[i].revents |= POLLIN;
|
||||
if (CountConsoleInputBytes()) {
|
||||
pipefds[i].revents |= POLLIN; // both >0 and -1 (eof) are pollin
|
||||
}
|
||||
} else {
|
||||
// we have no way of polling if a non-socket is readable yet
|
||||
|
@ -172,31 +170,36 @@ textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint32_t *ms,
|
|||
// compute a small time slice we don't mind sleeping for
|
||||
if (sn) {
|
||||
if ((gotsocks = WSAPoll(sockfds, sn, 0)) == -1) {
|
||||
return __winsockerr();
|
||||
rc = __winsockerr();
|
||||
goto Finished;
|
||||
}
|
||||
} else {
|
||||
gotsocks = 0;
|
||||
}
|
||||
waitfor = MIN(__SIG_POLL_INTERVAL_MS, *ms);
|
||||
if (!gotinvals && !gotsocks && !gotpipes && waitfor) {
|
||||
POLLTRACE("poll() sleeping for %'d out of %'u ms", waitfor, *ms);
|
||||
struct PosixThread *pt = _pthread_self();
|
||||
pt->abort_errno = 0;
|
||||
if (sigmask) __sig_mask(SIG_SETMASK, sigmask, &oldmask);
|
||||
interrupted = _check_interrupts(0) || __pause_thread(waitfor);
|
||||
if (sigmask) __sig_mask(SIG_SETMASK, &oldmask, 0);
|
||||
if (interrupted) return -1;
|
||||
if (*ms != -1u) {
|
||||
if (waitfor < *ms) {
|
||||
*ms -= waitfor;
|
||||
} else {
|
||||
*ms = 0;
|
||||
|
||||
// add some artificial delay, which we use as an opportunity to also
|
||||
// check for pending signals, thread cancelation, etc.
|
||||
waitfor = 0;
|
||||
if (!gotinvals && !gotsocks && !gotpipes) {
|
||||
now = timespec_real();
|
||||
if (timespec_cmp(now, deadline) < 0) {
|
||||
remain = timespec_sub(deadline, now);
|
||||
millis = timespec_tomillis(remain);
|
||||
waitfor = MIN(millis, 0xffffffffu);
|
||||
waitfor = MIN(waitfor, __SIG_POLL_INTERVAL_MS);
|
||||
if (waitfor) {
|
||||
POLLTRACE("poll() sleeping for %'d out of %'lu ms", waitfor,
|
||||
timespec_tomillis(remain));
|
||||
if ((rc = _park_norestart(waitfor, sigmask ? *sigmask : 0)) == -1) {
|
||||
goto Finished; // eintr, ecanceled, etc.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
if (gotinvals || gotpipes || gotsocks || !waitfor) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -221,15 +224,7 @@ textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint32_t *ms,
|
|||
rc = gotinvals + gotpipes + gotsocks;
|
||||
|
||||
Finished:
|
||||
|
||||
#if IsModeDbg()
|
||||
struct timespec ended = timespec_real();
|
||||
if (!rc && timespec_cmp(ended, noearlier) < 0) {
|
||||
STRACE("poll() ended %'ld ns too soon!",
|
||||
timespec_tonanos(timespec_sub(noearlier, ended)));
|
||||
}
|
||||
#endif
|
||||
|
||||
ALLOW_SIGNALS;
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue