cosmopolitan/third_party/nsync/mem/nsync_once.c
Justine Tunney 957c61cbbf
Release Cosmopolitan v3.3
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.
2024-02-20 13:27:59 -08:00

144 lines
5.6 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 "third_party/nsync/atomic.h"
#include "third_party/nsync/atomic.internal.h"
#include "third_party/nsync/common.internal.h"
#include "third_party/nsync/mu_semaphore.h"
#include "third_party/nsync/once.h"
#include "third_party/nsync/races.internal.h"
#include "third_party/nsync/wait_s.internal.h"
__static_yoink("nsync_notice");
/* An once_sync_s struct contains a lock, and a condition variable on which
threads may wait for an nsync_once to be initialized by another thread.
A separate struct is used only to keep nsync_once small.
A given nsync_once can be associated with any once_sync_s struct, but cannot
be associated with more than one. nsync_once instances are mapped to
once_sync_s instances by a trivial hashing scheme implemented by
NSYNC_ONCE_SYNC_().
The number of once_sync_s structs in the following array is greater than one
only to reduce the probability of contention if a great many distinct
nsync_once variables are initialized concurrently. */
static struct once_sync_s {
nsync_mu once_mu;
nsync_cv once_cv;
} once_sync[64];
/* Return a pointer to the once_sync_s struct associated with the nsync_once *p. */
#define NSYNC_ONCE_SYNC_(p) &once_sync[(((uintptr_t) (p)) / sizeof (*(p))) % \
(sizeof (once_sync) / sizeof (once_sync[0]))]
/* Implement nsync_run_once, nsync_run_once_arg, nsync_run_once_spin, or
nsync_run_once_arg_spin, chosen as described below.
If s!=NULL, s is required to point to the once_sync_s associated with *once,
and the semantics of nsync_run_once or nsync_run_once_arg are provided.
If s==NULL, the semantics of nsync_run_once_spin, or nsync_run_once_arg_spin
are provided.
If f!=NULL, the semantics of nsync_run_once or nsync_run_once_spin are
provided. Otherwise, farg is required to be non-NULL, and the semantics of
nsync_run_once_arg or nsync_run_once_arg_spin are provided. */
static void nsync_run_once_impl (nsync_once *once, struct once_sync_s *s,
void (*f) (void), void (*farg) (void *arg), void *arg) {
uint32_t o = ATM_LOAD_ACQ (once);
if (o != 2) {
unsigned attempts = 0;
if (s != NULL) {
nsync_mu_lock (&s->once_mu);
}
while (o == 0 && !ATM_CAS_ACQ (once, 0, 1)) {
o = ATM_LOAD (once);
}
if (o == 0) {
if (s != NULL) {
nsync_mu_unlock (&s->once_mu);
}
if (f != NULL) {
(*f) ();
} else {
(*farg) (arg);
}
if (s != NULL) {
nsync_mu_lock (&s->once_mu);
nsync_cv_broadcast (&s->once_cv);
}
ATM_STORE_REL (once, 2);
}
while (ATM_LOAD_ACQ (once) != 2) {
if (s != NULL) {
nsync_time deadline;
if (attempts < 50) {
attempts += 10;
}
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (attempts));
nsync_cv_wait_with_deadline (&s->once_cv, &s->once_mu, deadline, NULL);
} else {
attempts = nsync_spin_delay_ (attempts);
}
}
if (s != NULL) {
nsync_mu_unlock (&s->once_mu);
}
}
}
void nsync_run_once (nsync_once *once, void (*f) (void)) {
uint32_t o;
IGNORE_RACES_START ();
o = ATM_LOAD_ACQ (once);
if (o != 2) {
struct once_sync_s *s = NSYNC_ONCE_SYNC_ (once);
nsync_run_once_impl (once, s, f, NULL, NULL);
}
IGNORE_RACES_END ();
}
void nsync_run_once_arg (nsync_once *once, void (*farg) (void *arg), void *arg) {
uint32_t o;
IGNORE_RACES_START ();
o = ATM_LOAD_ACQ (once);
if (o != 2) {
struct once_sync_s *s = NSYNC_ONCE_SYNC_ (once);
nsync_run_once_impl (once, s, NULL, farg, arg);
}
IGNORE_RACES_END ();
}
void nsync_run_once_spin (nsync_once *once, void (*f) (void)) {
uint32_t o;
IGNORE_RACES_START ();
o = ATM_LOAD_ACQ (once);
if (o != 2) {
nsync_run_once_impl (once, NULL, f, NULL, NULL);
}
IGNORE_RACES_END ();
}
void nsync_run_once_arg_spin (nsync_once *once, void (*farg) (void *arg), void *arg) {
uint32_t o;
IGNORE_RACES_START ();
o = ATM_LOAD_ACQ (once);
if (o != 2) {
nsync_run_once_impl (once, NULL, NULL, farg, arg);
}
IGNORE_RACES_END ();
}