mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-02 23:18:44 +00:00
- 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.
99 lines
4.5 KiB
C
99 lines
4.5 KiB
C
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
|
|
│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
│ Copyright 2016 Google Inc. │
|
|
│ │
|
|
│ Licensed under the Apache License, Version 2.0 (the "License"); │
|
|
│ you may not use this file except in compliance with the License. │
|
|
│ You may obtain a copy of the License at │
|
|
│ │
|
|
│ http://www.apache.org/licenses/LICENSE-2.0 │
|
|
│ │
|
|
│ Unless required by applicable law or agreed to in writing, software │
|
|
│ distributed under the License is distributed on an "AS IS" BASIS, │
|
|
│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
|
|
│ See the License for the specific language governing permissions and │
|
|
│ limitations under the License. │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "third_party/nsync/mu_semaphore.h"
|
|
#include "libc/calls/cp.internal.h"
|
|
#include "libc/dce.h"
|
|
#include "third_party/nsync/mu_semaphore.internal.h"
|
|
|
|
/* Apple's ulock (part by Cosmo futexes) is an internal API, but:
|
|
1. Unlike GCD it's cancellable, i.e. can be EINTR'd by signals
|
|
2. We currently always use ulock anyway for joining threads */
|
|
#define PREFER_GCD_OVER_ULOCK 0
|
|
|
|
asm(".ident\t\"\\n\\n\
|
|
*NSYNC (Apache 2.0)\\n\
|
|
Copyright 2016 Google, Inc.\\n\
|
|
https://github.com/google/nsync\"");
|
|
// clang-format off
|
|
|
|
/* Initialize *s; the initial value is 0. */
|
|
void nsync_mu_semaphore_init (nsync_semaphore *s) {
|
|
if (PREFER_GCD_OVER_ULOCK && IsXnuSilicon ()) {
|
|
return nsync_mu_semaphore_init_gcd (s);
|
|
} else if (IsNetbsd ()) {
|
|
return nsync_mu_semaphore_init_sem (s);
|
|
} else {
|
|
return nsync_mu_semaphore_init_futex (s);
|
|
}
|
|
}
|
|
|
|
/* Releases system resources associated with *s. */
|
|
void nsync_mu_semaphore_destroy (nsync_semaphore *s) {
|
|
if (PREFER_GCD_OVER_ULOCK && IsXnuSilicon ()) {
|
|
return nsync_mu_semaphore_destroy_gcd (s);
|
|
} else if (IsNetbsd ()) {
|
|
return nsync_mu_semaphore_destroy_sem (s);
|
|
}
|
|
}
|
|
|
|
/* Wait until the count of *s exceeds 0, and decrement it. If POSIX cancellations
|
|
are currently disabled by the thread, then this function always succeeds. When
|
|
they're enabled in MASKED mode, this function may return ECANCELED. Otherwise,
|
|
cancellation will occur by unwinding cleanup handlers pushed to the stack. */
|
|
errno_t nsync_mu_semaphore_p (nsync_semaphore *s) {
|
|
errno_t err;
|
|
BEGIN_CANCELATION_POINT;
|
|
if (PREFER_GCD_OVER_ULOCK && IsXnuSilicon ()) {
|
|
err = nsync_mu_semaphore_p_gcd (s);
|
|
} else if (IsNetbsd ()) {
|
|
err = nsync_mu_semaphore_p_sem (s);
|
|
} else {
|
|
err = nsync_mu_semaphore_p_futex (s);
|
|
}
|
|
END_CANCELATION_POINT;
|
|
return err;
|
|
}
|
|
|
|
/* Like nsync_mu_semaphore_p() this waits for the count of *s to exceed 0,
|
|
while additionally supporting a time parameter specifying at what point
|
|
in the future ETIMEDOUT should be returned, if neither cancelation, or
|
|
semaphore release happens. */
|
|
errno_t nsync_mu_semaphore_p_with_deadline (nsync_semaphore *s, nsync_time abs_deadline) {
|
|
errno_t err;
|
|
BEGIN_CANCELATION_POINT;
|
|
if (PREFER_GCD_OVER_ULOCK && IsXnuSilicon ()) {
|
|
err = nsync_mu_semaphore_p_with_deadline_gcd (s, abs_deadline);
|
|
} else if (IsNetbsd ()) {
|
|
err = nsync_mu_semaphore_p_with_deadline_sem (s, abs_deadline);
|
|
} else {
|
|
err = nsync_mu_semaphore_p_with_deadline_futex (s, abs_deadline);
|
|
}
|
|
END_CANCELATION_POINT;
|
|
return err;
|
|
}
|
|
|
|
/* Ensure that the count of *s is at least 1. */
|
|
void nsync_mu_semaphore_v (nsync_semaphore *s) {
|
|
if (PREFER_GCD_OVER_ULOCK && IsXnuSilicon ()) {
|
|
return nsync_mu_semaphore_v_gcd (s);
|
|
} else if (IsNetbsd ()) {
|
|
return nsync_mu_semaphore_v_sem (s);
|
|
} else {
|
|
return nsync_mu_semaphore_v_futex (s);
|
|
}
|
|
}
|