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:
Justine Tunney 2023-10-08 05:36:18 -07:00
parent af7cb3c82f
commit 791f79fcb3
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
382 changed files with 4008 additions and 4511 deletions

View file

@ -13,9 +13,12 @@
#define PT_NOCANCEL 8
#define PT_MASKED 16
#define PT_INCANCEL 32
#define PT_POLLING 64 // windows only
#define PT_INSEMAPHORE 128 // windows only
#define PT_OPENBSD_KLUDGE 128 // openbsd only
#define PT_RESTARTABLE 64
#define PT_OPENBSD_KLUDGE 128
#define PT_BLOCKER_CPU ((_Atomic(int) *)-0)
#define PT_BLOCKER_SEM ((_Atomic(int) *)-1)
#define PT_BLOCKER_IO ((_Atomic(int) *)-2)
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
@ -76,48 +79,49 @@ enum PosixThreadStatus {
#define POSIXTHREAD_CONTAINER(e) DLL_CONTAINER(struct PosixThread, list, e)
struct PosixThread {
int pt_flags; // 0x00: see PT_* constants
_Atomic(int) cancelled; // 0x04: thread has bad beliefs
_Atomic(enum PosixThreadStatus) status;
_Atomic(int) ptid; // transitions 0 → tid
void *(*start)(void *); // creation callback
void *arg; // start's parameter
void *rc; // start's return value
char *tls; // bottom of tls allocation
struct CosmoTib *tib; // middle of tls allocation
struct Dll list; // list of threads
_Atomic(_Atomic(int) *) pt_futex;
intptr_t semaphore;
intptr_t iohandle;
void *ioverlap;
jmp_buf exiter;
pthread_attr_t attr;
int abort_errno;
struct _pthread_cleanup_buffer *cleanup;
int pt_flags; // 0x00: see PT_* constants
_Atomic(int) pt_canceled; // 0x04: thread has bad beliefs
_Atomic(enum PosixThreadStatus) pt_status;
_Atomic(int) ptid; // transitions 0 → tid
void *(*pt_start)(void *); // creation callback
void *pt_arg; // start's parameter
void *pt_rc; // start's return value
char *pt_tls; // bottom of tls allocation
struct CosmoTib *tib; // middle of tls allocation
struct Dll list; // list of threads
struct _pthread_cleanup_buffer *pt_cleanup;
_Atomic(_Atomic(int) *) pt_blocker;
_Atomic(int) pt_futex;
int64_t pt_semaphore;
intptr_t pt_iohandle;
void *pt_ioverlap;
jmp_buf pt_exiter;
pthread_attr_t pt_attr;
};
typedef void (*atfork_f)(void);
extern struct Dll *_pthread_list;
extern pthread_spinlock_t _pthread_lock;
extern struct PosixThread _pthread_static;
extern _Atomic(pthread_key_dtor) _pthread_key_dtor[PTHREAD_KEYS_MAX];
void _pthread_decimate(void);
int _pthread_tid(struct PosixThread *);
void _pthread_unkey(struct CosmoTib *);
void _pthread_unwind(struct PosixThread *);
int _pthread_reschedule(struct PosixThread *);
intptr_t _pthread_syshand(struct PosixThread *);
int _pthread_atfork(atfork_f, atfork_f, atfork_f);
int _pthread_reschedule(struct PosixThread *);
int _pthread_setschedparam_freebsd(int, int, const struct sched_param *);
void _pthread_free(struct PosixThread *, bool);
void _pthread_zombify(struct PosixThread *);
void _pthread_onfork_prepare(void);
void _pthread_onfork_parent(void);
void _pthread_onfork_child(void);
int _pthread_tid(struct PosixThread *);
intptr_t _pthread_syshand(struct PosixThread *);
long _pthread_cancel_ack(void);
void _pthread_decimate(void);
void _pthread_free(struct PosixThread *, bool);
void _pthread_lock(void);
void _pthread_onfork_child(void);
void _pthread_onfork_parent(void);
void _pthread_onfork_prepare(void);
void _pthread_ungarbage(void);
void _pthread_unkey(struct CosmoTib *);
void _pthread_unlock(void);
void _pthread_unwind(struct PosixThread *);
void _pthread_zombify(struct PosixThread *);
__funline pureconst struct PosixThread *_pthread_self(void) {
return (struct PosixThread *)__get_tls()->tib_pthread;