mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 11:37:35 +00:00
957c61cbbf
This change upgrades to GCC 12.3 and GNU binutils 2.42. The GNU linker appears to have changed things so that only a single de-duplicated str table is present in the binary, and it gets placed wherever the linker wants, regardless of what the linker script says. To cope with that we need to stop using .ident to embed licenses. As such, this change does significant work to revamp how third party licenses are defined in the codebase, using `.section .notice,"aR",@progbits`. This new GCC 12.3 toolchain has support for GNU indirect functions. It lets us support __target_clones__ for the first time. This is used for optimizing the performance of libc string functions such as strlen and friends so far on x86, by ensuring AVX systems favor a second codepath that uses VEX encoding. It shaves some latency off certain operations. It's a useful feature to have for scientific computing for the reasons explained by the test/libcxx/openmp_test.cc example which compiles for fifteen different microarchitectures. Thanks to the upgrades, it's now also possible to use newer instruction sets, such as AVX512FP16, VNNI. Cosmo now uses the %gs register on x86 by default for TLS. Doing it is helpful for any program that links `cosmo_dlopen()`. Such programs had to recompile their binaries at startup to change the TLS instructions. That's not great, since it means every page in the executable needs to be faulted. The work of rewriting TLS-related x86 opcodes, is moved to fixupobj.com instead. This is great news for MacOS x86 users, since we previously needed to morph the binary every time for that platform but now that's no longer necessary. The only platforms where we need fixup of TLS x86 opcodes at runtime are now Windows, OpenBSD, and NetBSD. On Windows we morph TLS to point deeper into the TIB, based on a TlsAlloc assignment, and on OpenBSD/NetBSD we morph %gs back into %fs since the kernels do not allow us to specify a value for the %gs register. OpenBSD users are now required to use APE Loader to run Cosmo binaries and assimilation is no longer possible. OpenBSD kernel needs to change to allow programs to specify a value for the %gs register, or it needs to stop marking executable pages loaded by the kernel as mimmutable(). This release fixes __constructor__, .ctor, .init_array, and lastly the .preinit_array so they behave the exact same way as glibc. We no longer use hex constants to define math.h symbols like M_PI.
153 lines
5.5 KiB
C
153 lines
5.5 KiB
C
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
|
|
│ vi: set noet ft=c ts=8 sw=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 "libc/intrin/dll.h"
|
|
#include "libc/mem/mem.h"
|
|
#include "libc/str/str.h"
|
|
#include "third_party/nsync/atomic.h"
|
|
#include "third_party/nsync/atomic.internal.h"
|
|
#include "third_party/nsync/common.internal.h"
|
|
#include "third_party/nsync/counter.h"
|
|
#include "third_party/nsync/mu_semaphore.h"
|
|
#include "third_party/nsync/races.internal.h"
|
|
#include "third_party/nsync/wait_s.internal.h"
|
|
#include "third_party/nsync/waiter.h"
|
|
__static_yoink("nsync_notice");
|
|
|
|
/* Internal details of nsync_counter. */
|
|
struct nsync_counter_s_ {
|
|
nsync_atomic_uint32_ waited; /* wait has been called */
|
|
nsync_mu counter_mu; /* protects fields below except reads of "value" */
|
|
nsync_atomic_uint32_ value; /* value of counter */
|
|
struct Dll *waiters; /* list of waiters */
|
|
};
|
|
|
|
nsync_counter nsync_counter_new (uint32_t value) {
|
|
nsync_counter c = (nsync_counter) malloc (sizeof (*c));
|
|
if (c != NULL) {
|
|
bzero ((void *) c, sizeof (*c));
|
|
ATM_STORE (&c->value, value);
|
|
}
|
|
return (c);
|
|
}
|
|
|
|
void nsync_counter_free (nsync_counter c) {
|
|
nsync_mu_lock (&c->counter_mu);
|
|
ASSERT (dll_is_empty (c->waiters));
|
|
nsync_mu_unlock (&c->counter_mu);
|
|
free (c);
|
|
}
|
|
|
|
uint32_t nsync_counter_add (nsync_counter c, int32_t delta) {
|
|
uint32_t value;
|
|
IGNORE_RACES_START ();
|
|
if (delta == 0) {
|
|
value = ATM_LOAD_ACQ (&c->value);
|
|
} else {
|
|
nsync_mu_lock (&c->counter_mu);
|
|
do {
|
|
value = ATM_LOAD (&c->value);
|
|
} while (!ATM_CAS_RELACQ (&c->value, value, value+delta));
|
|
value += delta;
|
|
if (delta > 0) {
|
|
/* It's illegal to increase the count from zero if
|
|
there has been a waiter. */
|
|
ASSERT (value != (uint32_t) delta || !ATM_LOAD (&c->waited));
|
|
ASSERT (value > value - delta); /* Crash on overflow. */
|
|
} else {
|
|
ASSERT (value < value - delta); /* Crash on overflow. */
|
|
}
|
|
if (value == 0) {
|
|
struct Dll *p;
|
|
while ((p = dll_first (c->waiters)) != NULL) {
|
|
struct nsync_waiter_s *nw = DLL_NSYNC_WAITER (p);
|
|
dll_remove (&c->waiters, p);
|
|
ATM_STORE_REL (&nw->waiting, 0);
|
|
nsync_mu_semaphore_v (nw->sem);
|
|
}
|
|
}
|
|
nsync_mu_unlock (&c->counter_mu);
|
|
}
|
|
IGNORE_RACES_END ();
|
|
return (value);
|
|
}
|
|
|
|
uint32_t nsync_counter_value (nsync_counter c) {
|
|
uint32_t result;
|
|
IGNORE_RACES_START ();
|
|
result = ATM_LOAD_ACQ (&c->value);
|
|
IGNORE_RACES_END ();
|
|
return (result);
|
|
}
|
|
|
|
uint32_t nsync_counter_wait (nsync_counter c, nsync_time abs_deadline) {
|
|
struct nsync_waitable_s waitable;
|
|
struct nsync_waitable_s *pwaitable = &waitable;
|
|
uint32_t result = 0;
|
|
waitable.v = c;
|
|
waitable.funcs = &nsync_counter_waitable_funcs;
|
|
if (nsync_wait_n (NULL, NULL, NULL, abs_deadline, 1, &pwaitable) != 0) {
|
|
IGNORE_RACES_START ();
|
|
result = ATM_LOAD_ACQ (&c->value);
|
|
IGNORE_RACES_END ();
|
|
}
|
|
return (result);
|
|
}
|
|
|
|
static nsync_time counter_ready_time (void *v, struct nsync_waiter_s *nw) {
|
|
nsync_counter c = (nsync_counter) v;
|
|
nsync_time r;
|
|
ATM_STORE (&c->waited, 1);
|
|
r = (ATM_LOAD_ACQ (&c->value) == 0? nsync_time_zero : nsync_time_no_deadline);
|
|
return (r);
|
|
}
|
|
|
|
static int counter_enqueue (void *v, struct nsync_waiter_s *nw) {
|
|
nsync_counter c = (nsync_counter) v;
|
|
int32_t value;
|
|
nsync_mu_lock (&c->counter_mu);
|
|
value = ATM_LOAD_ACQ (&c->value);
|
|
if (value != 0) {
|
|
dll_make_last (&c->waiters, &nw->q);
|
|
ATM_STORE (&nw->waiting, 1);
|
|
} else {
|
|
ATM_STORE (&nw->waiting, 0);
|
|
}
|
|
nsync_mu_unlock (&c->counter_mu);
|
|
return (value != 0);
|
|
}
|
|
|
|
static int counter_dequeue (void *v, struct nsync_waiter_s *nw) {
|
|
nsync_counter c = (nsync_counter) v;
|
|
int32_t value;
|
|
nsync_mu_lock (&c->counter_mu);
|
|
value = ATM_LOAD_ACQ (&c->value);
|
|
if (ATM_LOAD_ACQ (&nw->waiting) != 0) {
|
|
dll_remove (&c->waiters, &nw->q);
|
|
ATM_STORE (&nw->waiting, 0);
|
|
}
|
|
nsync_mu_unlock (&c->counter_mu);
|
|
return (value != 0);
|
|
}
|
|
|
|
const struct nsync_waitable_funcs_s nsync_counter_waitable_funcs = {
|
|
&counter_ready_time,
|
|
&counter_enqueue,
|
|
&counter_dequeue
|
|
};
|
|
|
|
|