Change support vector to Windows 8+

Doing this makes binaries tinier, since we don't need to have all the
extra code for supporting a 32-bit address space. It also benefits us
because we're able to use WIN32 futexes, which makes locking simpler.

b69f3d2488 is what officially ended our
Windows 7 support. This change is merely a formalization. You can use
old versions of Cosmo now and forevermore if you need Windows 7 since
our repository is hermetic and vendors all its dependencies.

Won't fix #617
This commit is contained in:
Justine Tunney 2022-09-15 03:49:34 -07:00
parent 6c90f830d9
commit 134ffee519
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
25 changed files with 296 additions and 167 deletions

View file

@ -17,14 +17,19 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/struct/timespec.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/limits.h"
#include "libc/nt/runtime.h"
#include "libc/nt/synchronization.h"
#include "libc/sysv/consts/futex.h"
#include "libc/thread/thread.h"
#include "third_party/nsync/common.internal.h"
#include "third_party/nsync/futex.internal.h"
#include "third_party/nsync/mu_semaphore.internal.h"
// clang-format off
/* futex() polyfill w/ sched_yield() fallback */
@ -41,8 +46,18 @@ bool FUTEX_TIMEOUT_IS_ABSOLUTE;
__attribute__((__constructor__)) static void nsync_futex_init_ (void) {
int x = 0;
if (!(FUTEX_IS_SUPPORTED = IsLinux() || IsOpenbsd()))
if (NSYNC_FUTEX_WIN32 && IsWindows ()) {
FUTEX_IS_SUPPORTED = true;
FUTEX_WAIT_ = FUTEX_WAIT;
return;
}
if (!(FUTEX_IS_SUPPORTED = IsLinux () || IsOpenbsd ())) {
// we're using sched_yield() so let's
// avoid needless clock_gettime calls
FUTEX_TIMEOUT_IS_ABSOLUTE = true;
return;
}
// In our testing, we found that the monotonic clock on various
// popular systems (such as Linux, and some BSD variants) was no
@ -78,8 +93,30 @@ __attribute__((__constructor__)) static void nsync_futex_init_ (void) {
int nsync_futex_wait_ (int *p, int expect, char pshare, struct timespec *timeout) {
int rc, op;
if (FUTEX_IS_SUPPORTED) {
op = FUTEX_WAIT_;
uint32_t ms;
if (!FUTEX_IS_SUPPORTED) {
nsync_yield_ ();
if (timeout) {
return -EINTR;
} else {
return 0;
}
}
op = FUTEX_WAIT_;
if (NSYNC_FUTEX_WIN32 && IsWindows ()) {
if (timeout) {
ms = _timespec_tomillis (*timeout);
} else {
ms = -1;
}
if (WaitOnAddress (p, &expect, sizeof(int), ms)) {
rc = 0;
} else {
rc = -GetLastError ();
}
} else {
if (pshare == PTHREAD_PROCESS_PRIVATE) {
op |= FUTEX_PRIVATE_FLAG_;
}
@ -88,35 +125,45 @@ int nsync_futex_wait_ (int *p, int expect, char pshare, struct timespec *timeout
// [jart] openbsd does this without setting carry flag
rc = -rc;
}
STRACE("futex(%t, %s, %d, %s) → %s",
p, DescribeFutexOp(op), expect,
DescribeTimespec(0, timeout), DescribeFutexResult(rc));
} else {
nsync_yield_ ();
if (timeout) {
rc = -ETIMEDOUT;
} else {
rc = 0;
}
}
STRACE("futex(%t, %s, %d, %s) → %s",
p, DescribeFutexOp (op), expect,
DescribeTimespec (0, timeout),
DescribeFutexResult (rc));
return rc;
}
int nsync_futex_wake_ (int *p, int count, char pshare) {
int rc, op;
int wake (void *, int, int) asm ("_futex");
if (FUTEX_IS_SUPPORTED) {
op = FUTEX_WAKE;
ASSERT (count == 1 || count == INT_MAX);
if (!FUTEX_IS_SUPPORTED) {
nsync_yield_ ();
return 0;
}
op = FUTEX_WAKE;
if (NSYNC_FUTEX_WIN32 && IsWindows ()) {
if (count == 1) {
WakeByAddressSingle (p);
} else {
WakeByAddressAll (p);
}
rc = 0;
} else {
if (pshare == PTHREAD_PROCESS_PRIVATE) {
op |= FUTEX_PRIVATE_FLAG_;
}
rc = wake (p, op, count);
STRACE("futex(%t, %s, %d) → %s", p,
DescribeFutexOp(op),
count, DescribeFutexResult(rc));
} else {
nsync_yield_ ();
rc = 0;
}
STRACE("futex(%t, %s, %d) → %s", p,
DescribeFutexOp(op),
count, DescribeFutexResult(rc));
return rc;
}

View file

@ -23,7 +23,7 @@
/* Initialize *s; the initial value is 0. */
void nsync_mu_semaphore_init (nsync_semaphore *s) {
if (!IsWindows ())
if (NSYNC_FUTEX_WIN32 || !IsWindows ())
nsync_mu_semaphore_init_futex (s);
else
nsync_mu_semaphore_init_win32 (s);
@ -31,7 +31,7 @@ void nsync_mu_semaphore_init (nsync_semaphore *s) {
/* Wait until the count of *s exceeds 0, and decrement it. */
void nsync_mu_semaphore_p (nsync_semaphore *s) {
if (!IsWindows ())
if (NSYNC_FUTEX_WIN32 || !IsWindows ())
nsync_mu_semaphore_p_futex (s);
else
nsync_mu_semaphore_p_win32 (s);
@ -41,7 +41,7 @@ void nsync_mu_semaphore_p (nsync_semaphore *s) {
the count of *s is non-zero, in which case decrement *s and return 0;
or abs_deadline expires, in which case return ETIMEDOUT. */
int nsync_mu_semaphore_p_with_deadline (nsync_semaphore *s, nsync_time abs_deadline) {
if (!IsWindows ())
if (NSYNC_FUTEX_WIN32 || !IsWindows ())
return nsync_mu_semaphore_p_with_deadline_futex (s, abs_deadline);
else
return nsync_mu_semaphore_p_with_deadline_win32 (s, abs_deadline);
@ -49,7 +49,7 @@ int nsync_mu_semaphore_p_with_deadline (nsync_semaphore *s, nsync_time abs_deadl
/* Ensure that the count of *s is at least 1. */
void nsync_mu_semaphore_v (nsync_semaphore *s) {
if (!IsWindows ())
if (NSYNC_FUTEX_WIN32 || !IsWindows ())
nsync_mu_semaphore_v_futex (s);
else
nsync_mu_semaphore_v_win32 (s);

View file

@ -2,6 +2,11 @@
#define NSYNC_MU_SEMAPHORE_INTERNAL_H_
#include "third_party/nsync/mu_semaphore.h"
#include "third_party/nsync/time.h"
#ifndef NSYNC_FUTEX_WIN32
#define NSYNC_FUTEX_WIN32 1
#endif
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_

View file

@ -15,6 +15,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "libc/calls/state.internal.h"
#include "libc/errno.h"
#include "libc/nt/enum/wait.h"
#include "libc/nt/synchronization.h"
@ -32,14 +33,14 @@ https://github.com/google/nsync\"");
/* Initialize *s; the initial value is 0. */
void nsync_mu_semaphore_init_win32 (nsync_semaphore *s) {
int64_t *h = (int64_t *) s;
*h = CreateSemaphore(NULL, 0, 1, NULL);
*h = CreateSemaphore (&kNtIsInheritable, 0, 1, NULL);
if (!*h) notpossible;
}
/* Wait until the count of *s exceeds 0, and decrement it. */
void nsync_mu_semaphore_p_win32 (nsync_semaphore *s) {
int64_t *h = (int64_t *) s;
WaitForSingleObject(*h, -1u);
WaitForSingleObject (*h, -1u);
}
/* Wait until one of:
@ -50,7 +51,7 @@ int nsync_mu_semaphore_p_with_deadline_win32 (nsync_semaphore *s, nsync_time abs
int result;
if (nsync_time_cmp (abs_deadline, nsync_time_no_deadline) == 0) {
result = WaitForSingleObject(*h, -1u);
result = WaitForSingleObject (*h, -1u);
} else {
nsync_time now;
now = nsync_time_now ();

View file

@ -27,6 +27,7 @@ THIRD_PARTY_NSYNC_A_DIRECTDEPS = \
LIBC_INTRIN \
LIBC_NEXGEN32E \
LIBC_NT_KERNEL32 \
LIBC_NT_SYNCHRONIZATION \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \