mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-06 11:18:30 +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
|
@ -17,35 +17,136 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/struct/fd.internal.h"
|
||||
#include "libc/calls/struct/sigset.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/nt/enum/wait.h"
|
||||
#include "libc/nt/errors.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/iovec.h"
|
||||
#include "libc/nt/struct/overlapped.h"
|
||||
#include "libc/nt/thread.h"
|
||||
#include "libc/nt/winsock.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sock/internal.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sock/syscall_fd.internal.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#ifdef __x86_64__
|
||||
|
||||
textwindows int64_t __winsockblock(int64_t fh, unsigned eventbit, int64_t rc,
|
||||
uint32_t timeout) {
|
||||
int64_t eh;
|
||||
struct NtWsaNetworkEvents ev;
|
||||
if (rc != -1) return rc;
|
||||
if (WSAGetLastError() != EWOULDBLOCK) return __winsockerr();
|
||||
eh = WSACreateEvent();
|
||||
bzero(&ev, sizeof(ev));
|
||||
/* The proper way to reset the state of an event object used with the
|
||||
WSAEventSelect function is to pass the handle of the event object
|
||||
to the WSAEnumNetworkEvents function in the hEventObject parameter.
|
||||
This will reset the event object and adjust the status of active FD
|
||||
events on the socket in an atomic fashion. -- MSDN */
|
||||
if (WSAEventSelect(fh, eh, 1u << eventbit) != -1 &&
|
||||
WSAEnumNetworkEvents(fh, eh, &ev) != -1) {
|
||||
if (!ev.iErrorCode[eventbit]) {
|
||||
rc = 0;
|
||||
} else {
|
||||
errno = ev.iErrorCode[eventbit];
|
||||
}
|
||||
} else {
|
||||
__winsockerr();
|
||||
}
|
||||
WSACloseEvent(eh);
|
||||
return rc;
|
||||
struct WinsockBlockResources {
|
||||
int64_t handle;
|
||||
struct NtOverlapped *overlap;
|
||||
};
|
||||
|
||||
static void UnwindWinsockBlock(void *arg) {
|
||||
struct WinsockBlockResources *wbr = arg;
|
||||
uint32_t got, flags;
|
||||
CancelIoEx(wbr->handle, wbr->overlap);
|
||||
WSAGetOverlappedResult(wbr->handle, wbr->overlap, &got, true, &flags);
|
||||
WSACloseEvent(wbr->overlap->hEvent);
|
||||
}
|
||||
|
||||
static void CancelWinsockBlock(int64_t handle, struct NtOverlapped *overlap) {
|
||||
if (!CancelIoEx(handle, overlap)) {
|
||||
unassert(WSAGetLastError() == kNtErrorNotFound);
|
||||
}
|
||||
}
|
||||
|
||||
textwindows ssize_t
|
||||
__winsock_block(int64_t handle, uint32_t flags, bool nonblock,
|
||||
uint32_t srwtimeout, sigset_t wait_signal_mask,
|
||||
int StartSocketOp(int64_t handle, struct NtOverlapped *overlap,
|
||||
uint32_t *flags, void *arg),
|
||||
void *arg) {
|
||||
|
||||
int rc;
|
||||
uint64_t m;
|
||||
uint32_t status;
|
||||
uint32_t exchanged;
|
||||
bool eagained = false;
|
||||
bool eintered = false;
|
||||
bool canceled = false;
|
||||
bool olderror = errno;
|
||||
struct PosixThread *pt;
|
||||
struct NtOverlapped overlap = {.hEvent = WSACreateEvent()};
|
||||
struct WinsockBlockResources wbr = {handle, &overlap};
|
||||
|
||||
pthread_cleanup_push(UnwindWinsockBlock, &wbr);
|
||||
rc = StartSocketOp(handle, &overlap, &flags, arg);
|
||||
if (rc && WSAGetLastError() == kNtErrorIoPending) {
|
||||
BlockingOperation:
|
||||
pt = _pthread_self();
|
||||
pt->pt_iohandle = handle;
|
||||
pt->pt_ioverlap = &overlap;
|
||||
pt->pt_flags |= PT_RESTARTABLE;
|
||||
atomic_store_explicit(&pt->pt_blocker, PT_BLOCKER_IO, memory_order_release);
|
||||
m = __sig_beginwait(wait_signal_mask);
|
||||
if (nonblock) {
|
||||
CancelWinsockBlock(handle, &overlap);
|
||||
eagained = true;
|
||||
} else if (_check_cancel()) {
|
||||
CancelWinsockBlock(handle, &overlap);
|
||||
canceled = true;
|
||||
} else if (_check_signal(true)) {
|
||||
CancelWinsockBlock(handle, &overlap);
|
||||
eintered = true;
|
||||
} else {
|
||||
status = WSAWaitForMultipleEvents(1, &overlap.hEvent, 0,
|
||||
srwtimeout ? srwtimeout : -1u, 0);
|
||||
if (status == kNtWaitTimeout) {
|
||||
// rcvtimeo or sndtimeo elapsed
|
||||
CancelWinsockBlock(handle, &overlap);
|
||||
eagained = true;
|
||||
} else if (status == kNtWaitFailed) {
|
||||
// Failure should be an impossible condition, but MSDN lists
|
||||
// WSAENETDOWN and WSA_NOT_ENOUGH_MEMORY as possible errors.
|
||||
CancelWinsockBlock(handle, &overlap);
|
||||
eintered = true;
|
||||
}
|
||||
}
|
||||
__sig_finishwait(m);
|
||||
atomic_store_explicit(&pt->pt_blocker, PT_BLOCKER_CPU,
|
||||
memory_order_release);
|
||||
pt->pt_flags &= ~PT_RESTARTABLE;
|
||||
pt->pt_ioverlap = 0;
|
||||
pt->pt_iohandle = 0;
|
||||
rc = 0;
|
||||
}
|
||||
if (!rc) {
|
||||
bool32 should_wait = canceled || eagained;
|
||||
bool32 ok = WSAGetOverlappedResult(handle, &overlap, &exchanged,
|
||||
should_wait, &flags);
|
||||
if (!ok && WSAGetLastError() == kNtErrorIoIncomplete) {
|
||||
goto BlockingOperation;
|
||||
}
|
||||
rc = ok ? 0 : -1;
|
||||
}
|
||||
WSACloseEvent(overlap.hEvent);
|
||||
pthread_cleanup_pop(false);
|
||||
|
||||
if (canceled) {
|
||||
return ecanceled();
|
||||
}
|
||||
if (!rc) {
|
||||
errno = olderror;
|
||||
return exchanged;
|
||||
}
|
||||
if (eagained) {
|
||||
return eagain();
|
||||
}
|
||||
if (WSAGetLastError() == kNtErrorOperationAborted && _check_cancel()) {
|
||||
return ecanceled();
|
||||
}
|
||||
if (eintered) {
|
||||
return eintr();
|
||||
}
|
||||
return __winsockerr();
|
||||
}
|
||||
|
||||
#endif /* __x86_64__ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue