2022-09-11 18:02:07 +00:00
|
|
|
/*-*- 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. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-11-06 02:49:41 +00:00
|
|
|
#include "libc/calls/cp.internal.h"
|
2023-09-12 04:34:53 +00:00
|
|
|
#include "libc/errno.h"
|
2023-07-06 13:57:28 +00:00
|
|
|
#include "libc/intrin/dll.h"
|
2022-09-11 18:02:07 +00:00
|
|
|
#include "libc/str/str.h"
|
2022-11-05 01:19:05 +00:00
|
|
|
#include "libc/thread/thread.h"
|
2022-09-11 18:02:07 +00:00
|
|
|
#include "third_party/nsync/atomic.internal.h"
|
|
|
|
#include "third_party/nsync/common.internal.h"
|
|
|
|
#include "third_party/nsync/cv.h"
|
2022-09-12 01:53:52 +00:00
|
|
|
#include "third_party/nsync/races.internal.h"
|
2022-09-11 18:02:07 +00:00
|
|
|
#include "third_party/nsync/wait_s.internal.h"
|
|
|
|
#include "third_party/nsync/waiter.h"
|
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 19:12:09 +00:00
|
|
|
__static_yoink("nsync_notice");
|
2022-09-10 23:11:26 +00:00
|
|
|
|
2022-10-03 15:17:37 +00:00
|
|
|
// once we're paying the cost of nsync we might as well get the benefit
|
|
|
|
// of a better pthread_once(), since no other component pulls it in now
|
2023-07-26 20:54:49 +00:00
|
|
|
__static_yoink("nsync_run_once");
|
2022-10-03 15:17:37 +00:00
|
|
|
|
2022-09-10 23:11:26 +00:00
|
|
|
/* Initialize *cv. */
|
|
|
|
void nsync_cv_init (nsync_cv *cv) {
|
2023-07-06 13:57:28 +00:00
|
|
|
bzero ((void *) cv, sizeof (*cv));
|
2022-09-10 23:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wake the cv waiters in the circular list pointed to by
|
|
|
|
to_wake_list, which may not be NULL. If the waiter is associated with a
|
|
|
|
nsync_mu, the "wakeup" may consist of transferring the waiters to the nsync_mu's
|
|
|
|
queue. Requires that every waiter is associated with the same mutex.
|
|
|
|
all_readers indicates whether all the waiters on the list are readers. */
|
2023-07-06 13:57:28 +00:00
|
|
|
static void wake_waiters (struct Dll *to_wake_list, int all_readers) {
|
|
|
|
struct Dll *p = NULL;
|
|
|
|
struct Dll *next = NULL;
|
|
|
|
struct Dll *first_waiter = dll_first (to_wake_list);
|
2022-09-10 23:11:26 +00:00
|
|
|
struct nsync_waiter_s *first_nw = DLL_NSYNC_WAITER (first_waiter);
|
|
|
|
waiter *first_w = NULL;
|
|
|
|
nsync_mu *pmu = NULL;
|
|
|
|
if ((first_nw->flags & NSYNC_WAITER_FLAG_MUCV) != 0) {
|
|
|
|
first_w = DLL_WAITER (first_waiter);
|
|
|
|
pmu = first_w->cv_mu;
|
|
|
|
}
|
|
|
|
if (pmu != NULL) { /* waiter is associated with the nsync_mu *pmu. */
|
|
|
|
/* We will transfer elements of to_wake_list to *pmu if all of:
|
|
|
|
- some thread holds the lock, and
|
|
|
|
- *pmu's spinlock is not held, and
|
|
|
|
- either *pmu cannot be acquired in the mode of the first
|
|
|
|
waiter, or there's more than one thread on to_wake_list
|
|
|
|
and not all are readers, and
|
|
|
|
- we acquire the spinlock on the first try.
|
|
|
|
The spinlock acquisition also marks *pmu as having waiters.
|
|
|
|
The requirement that some thread holds the lock ensures
|
|
|
|
that at least one of the transferred waiters will be woken.
|
|
|
|
*/
|
|
|
|
uint32_t old_mu_word = ATM_LOAD (&pmu->word);
|
|
|
|
int first_cant_acquire = ((old_mu_word & first_w->l_type->zero_to_acquire) != 0);
|
2023-07-06 13:57:28 +00:00
|
|
|
next = dll_next (to_wake_list, first_waiter);
|
2022-09-10 23:11:26 +00:00
|
|
|
if ((old_mu_word&MU_ANY_LOCK) != 0 &&
|
|
|
|
(old_mu_word&MU_SPINLOCK) == 0 &&
|
|
|
|
(first_cant_acquire || (next != NULL && !all_readers)) &&
|
|
|
|
ATM_CAS_ACQ (&pmu->word, old_mu_word,
|
|
|
|
(old_mu_word|MU_SPINLOCK|MU_WAITING) &
|
|
|
|
~MU_ALL_FALSE)) {
|
|
|
|
|
|
|
|
uint32_t set_on_release = 0;
|
|
|
|
|
|
|
|
/* For any waiter that should be transferred, rather
|
|
|
|
than woken, move it from to_wake_list to pmu->waiters. */
|
|
|
|
int first_is_writer = first_w->l_type == nsync_writer_type_;
|
|
|
|
int transferred_a_writer = 0;
|
|
|
|
int woke_areader = 0;
|
|
|
|
/* Transfer the first waiter iff it can't acquire *pmu. */
|
|
|
|
if (first_cant_acquire) {
|
2023-07-06 13:57:28 +00:00
|
|
|
dll_remove (&to_wake_list, first_waiter);
|
|
|
|
dll_make_last (&pmu->waiters, first_waiter);
|
2022-09-10 23:11:26 +00:00
|
|
|
/* tell nsync_cv_wait_with_deadline() that we
|
|
|
|
moved the waiter to *pmu's queue. */
|
|
|
|
first_w->cv_mu = NULL;
|
|
|
|
/* first_nw.waiting is already 1, from being on
|
|
|
|
cv's waiter queue. */
|
|
|
|
transferred_a_writer = first_is_writer;
|
|
|
|
} else {
|
|
|
|
woke_areader = !first_is_writer;
|
|
|
|
}
|
|
|
|
/* Now process the other waiters. */
|
|
|
|
for (p = next; p != NULL; p = next) {
|
|
|
|
int p_is_writer;
|
|
|
|
struct nsync_waiter_s *p_nw = DLL_NSYNC_WAITER (p);
|
|
|
|
waiter *p_w = NULL;
|
|
|
|
if ((p_nw->flags & NSYNC_WAITER_FLAG_MUCV) != 0) {
|
|
|
|
p_w = DLL_WAITER (p);
|
|
|
|
}
|
2023-07-06 13:57:28 +00:00
|
|
|
next = dll_next (to_wake_list, p);
|
2022-09-10 23:11:26 +00:00
|
|
|
p_is_writer = (p_w != NULL &&
|
|
|
|
DLL_WAITER (p)->l_type == nsync_writer_type_);
|
|
|
|
/* We transfer this element if any of:
|
|
|
|
- the first waiter can't acquire *pmu, or
|
|
|
|
- the first waiter is a writer, or
|
|
|
|
- this element is a writer. */
|
|
|
|
if (p_w == NULL) {
|
|
|
|
/* wake non-native waiter */
|
|
|
|
} else if (first_cant_acquire || first_is_writer || p_is_writer) {
|
2023-07-06 13:57:28 +00:00
|
|
|
dll_remove (&to_wake_list, p);
|
|
|
|
dll_make_last (&pmu->waiters, p);
|
2022-09-10 23:11:26 +00:00
|
|
|
/* tell nsync_cv_wait_with_deadline()
|
|
|
|
that we moved the waiter to *pmu's
|
|
|
|
queue. */
|
|
|
|
p_w->cv_mu = NULL;
|
|
|
|
/* p_nw->waiting is already 1, from
|
|
|
|
being on cv's waiter queue. */
|
|
|
|
transferred_a_writer = transferred_a_writer || p_is_writer;
|
|
|
|
} else {
|
|
|
|
woke_areader = woke_areader || !p_is_writer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Claim a waiting writer if we transferred one, except if we woke readers,
|
|
|
|
in which case we want those readers to be able to acquire immediately. */
|
|
|
|
if (transferred_a_writer && !woke_areader) {
|
|
|
|
set_on_release |= MU_WRITER_WAITING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* release *pmu's spinlock (MU_WAITING was set by CAS above) */
|
|
|
|
old_mu_word = ATM_LOAD (&pmu->word);
|
|
|
|
while (!ATM_CAS_REL (&pmu->word, old_mu_word,
|
|
|
|
(old_mu_word|set_on_release) & ~MU_SPINLOCK)) {
|
|
|
|
old_mu_word = ATM_LOAD (&pmu->word);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wake any waiters we didn't manage to enqueue on the mu. */
|
2023-07-06 13:57:28 +00:00
|
|
|
for (p = dll_first (to_wake_list); p != NULL; p = next) {
|
2022-09-10 23:11:26 +00:00
|
|
|
struct nsync_waiter_s *p_nw = DLL_NSYNC_WAITER (p);
|
2023-07-06 13:57:28 +00:00
|
|
|
next = dll_next (to_wake_list, p);
|
|
|
|
dll_remove (&to_wake_list, p);
|
2022-09-10 23:11:26 +00:00
|
|
|
/* Wake the waiter. */
|
|
|
|
ATM_STORE_REL (&p_nw->waiting, 0); /* release store */
|
|
|
|
nsync_mu_semaphore_v (p_nw->sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------ */
|
|
|
|
|
|
|
|
/* Versions of nsync_mu_lock() and nsync_mu_unlock() that take "void *"
|
|
|
|
arguments, to avoid call through a function pointer of a different type,
|
|
|
|
which is undefined. */
|
|
|
|
static void void_mu_lock (void *mu) {
|
|
|
|
nsync_mu_lock ((nsync_mu *) mu);
|
|
|
|
}
|
|
|
|
static void void_mu_unlock (void *mu) {
|
|
|
|
nsync_mu_unlock ((nsync_mu *) mu);
|
|
|
|
}
|
|
|
|
|
2023-09-12 04:34:53 +00:00
|
|
|
/* Memory needed for a single nsync_cv_wait_with_deadline_generic() call. */
|
|
|
|
struct nsync_cv_wait_with_deadline_s {
|
|
|
|
nsync_cv *pcv;
|
|
|
|
int sem_outcome;
|
|
|
|
int is_reader_mu;
|
|
|
|
uint32_t old_word;
|
|
|
|
uint32_t remove_count;
|
|
|
|
void *pmu;
|
|
|
|
void (*lock) (void *);
|
|
|
|
nsync_mu *cv_mu;
|
2024-09-03 06:37:50 +00:00
|
|
|
int clock;
|
2023-09-12 04:34:53 +00:00
|
|
|
nsync_time abs_deadline;
|
|
|
|
nsync_note cancel_note;
|
2023-11-10 09:42:06 +00:00
|
|
|
waiter *w;
|
2023-09-12 04:34:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Wait until awoken or timeout, or back out of wait if the thread is being cancelled. */
|
|
|
|
static int nsync_cv_wait_with_deadline_impl_ (struct nsync_cv_wait_with_deadline_s *c) {
|
|
|
|
int outcome = 0;
|
|
|
|
int attempts = 0;
|
|
|
|
IGNORE_RACES_START ();
|
2023-11-10 09:42:06 +00:00
|
|
|
while (ATM_LOAD_ACQ (&c->w->nw.waiting) != 0) { /* acquire load */
|
2023-09-12 04:34:53 +00:00
|
|
|
if (c->sem_outcome == 0) {
|
2024-09-03 06:37:50 +00:00
|
|
|
c->sem_outcome = nsync_sem_wait_with_cancel_ (c->w, c->clock, c->abs_deadline, c->cancel_note);
|
2023-09-12 04:34:53 +00:00
|
|
|
}
|
2023-11-10 09:42:06 +00:00
|
|
|
if (c->sem_outcome != 0 && ATM_LOAD (&c->w->nw.waiting) != 0) {
|
2023-09-12 04:34:53 +00:00
|
|
|
/* A timeout or cancellation occurred, and no wakeup.
|
|
|
|
Acquire *pcv's spinlock, and confirm. */
|
|
|
|
c->old_word = nsync_spin_test_and_set_ (&c->pcv->word, CV_SPINLOCK,
|
2024-07-06 06:13:20 +00:00
|
|
|
CV_SPINLOCK, 0, c->cv_mu);
|
2023-09-12 04:34:53 +00:00
|
|
|
/* Check that w wasn't removed from the queue after we
|
|
|
|
checked above, but before we acquired the spinlock.
|
|
|
|
The test of remove_count confirms that the waiter *w
|
|
|
|
is still governed by *pcv's spinlock; otherwise, some
|
|
|
|
other thread is about to set w.waiting==0. */
|
2023-11-10 09:42:06 +00:00
|
|
|
if (ATM_LOAD (&c->w->nw.waiting) != 0) {
|
|
|
|
if (c->remove_count == ATM_LOAD (&c->w->remove_count)) {
|
2023-09-12 04:34:53 +00:00
|
|
|
uint32_t old_value;
|
|
|
|
/* still in cv waiter queue */
|
|
|
|
/* Not woken, so remove *w from cv
|
|
|
|
queue, and declare a
|
|
|
|
timeout/cancellation. */
|
|
|
|
outcome = c->sem_outcome;
|
2023-11-10 09:42:06 +00:00
|
|
|
dll_remove (&c->pcv->waiters, &c->w->nw.q);
|
2023-09-12 04:34:53 +00:00
|
|
|
do {
|
2023-11-10 09:42:06 +00:00
|
|
|
old_value = ATM_LOAD (&c->w->remove_count);
|
|
|
|
} while (!ATM_CAS (&c->w->remove_count, old_value, old_value+1));
|
2023-09-12 04:34:53 +00:00
|
|
|
if (dll_is_empty (c->pcv->waiters)) {
|
|
|
|
c->old_word &= ~(CV_NON_EMPTY);
|
|
|
|
}
|
2023-11-10 09:42:06 +00:00
|
|
|
ATM_STORE_REL (&c->w->nw.waiting, 0); /* release store */
|
2023-09-12 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Release spinlock. */
|
|
|
|
ATM_STORE_REL (&c->pcv->word, c->old_word); /* release store */
|
|
|
|
}
|
2023-11-10 09:42:06 +00:00
|
|
|
if (ATM_LOAD (&c->w->nw.waiting) != 0) {
|
2023-09-12 04:34:53 +00:00
|
|
|
/* The delay here causes this thread ultimately to
|
|
|
|
yield to another that has dequeued this thread, but
|
|
|
|
has not yet set the waiting field to zero; a
|
|
|
|
cancellation or timeout may prevent this thread
|
|
|
|
from blocking above on the semaphore. */
|
2024-07-06 06:13:20 +00:00
|
|
|
attempts = pthread_delay_np (c->cv_mu, attempts);
|
2023-09-12 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-10 09:42:06 +00:00
|
|
|
if (c->cv_mu != NULL && c->w->cv_mu == NULL) { /* waiter was moved to *pmu's queue, and woken. */
|
2023-09-12 04:34:53 +00:00
|
|
|
/* Requeue on *pmu using existing waiter struct; current thread
|
|
|
|
is the designated waker. */
|
2023-11-10 09:42:06 +00:00
|
|
|
nsync_mu_lock_slow_ (c->cv_mu, c->w, MU_DESIG_WAKER, c->w->l_type);
|
2024-08-26 19:25:50 +00:00
|
|
|
nsync_waiter_free_ (c->w);
|
2023-09-12 04:34:53 +00:00
|
|
|
} else {
|
2024-08-26 19:25:50 +00:00
|
|
|
nsync_waiter_free_ (c->w);
|
2023-09-12 04:34:53 +00:00
|
|
|
/* Traditional case: We've woken from the cv, and need to reacquire *pmu. */
|
|
|
|
if (c->is_reader_mu) {
|
|
|
|
nsync_mu_rlock (c->cv_mu);
|
|
|
|
} else {
|
|
|
|
(*c->lock) (c->pmu);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IGNORE_RACES_END ();
|
|
|
|
return (outcome);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle POSIX thread DEFERRED mode cancellation. */
|
|
|
|
static void nsync_cv_wait_with_deadline_unwind_ (void *arg) {
|
|
|
|
struct nsync_cv_wait_with_deadline_s *c;
|
|
|
|
c = (struct nsync_cv_wait_with_deadline_s *)arg;
|
|
|
|
c->sem_outcome = ECANCELED;
|
|
|
|
nsync_cv_wait_with_deadline_impl_ (c);
|
|
|
|
}
|
|
|
|
|
2022-09-10 23:11:26 +00:00
|
|
|
/* Atomically release *pmu (which must be held on entry)
|
|
|
|
and block the calling thread on *pcv. Then wait until awakened by a
|
|
|
|
call to nsync_cv_signal() or nsync_cv_broadcast() (or a spurious wakeup), or by the time
|
|
|
|
reaching abs_deadline, or by cancel_note being notified. In all cases,
|
|
|
|
reacquire *pmu, and return the reason for the call returned (0, ETIMEDOUT,
|
|
|
|
or ECANCELED). Callers should abs_deadline==nsync_time_no_deadline for no
|
|
|
|
deadline, and cancel_note==NULL for no cancellation. nsync_cv_wait_with_deadline()
|
|
|
|
should be used in a loop, as with all Mesa-style condition variables. See
|
|
|
|
examples above.
|
|
|
|
|
|
|
|
There are two reasons for using an absolute deadline, rather than a relative
|
|
|
|
timeout---these are why pthread_cond_timedwait() also uses an absolute
|
|
|
|
deadline. First, condition variable waits have to be used in a loop; with
|
|
|
|
an absolute times, the deadline does not have to be recomputed on each
|
|
|
|
iteration. Second, in most real programmes, some activity (such as an RPC
|
|
|
|
to a server, or when guaranteeing response time in a UI), there is a
|
|
|
|
deadline imposed by the specification or the caller/user; relative delays
|
|
|
|
can shift arbitrarily with scheduling delays, and so after multiple waits
|
|
|
|
might extend beyond the expected deadline. Relative delays tend to be more
|
|
|
|
convenient mostly in tests and trivial examples than they are in real
|
|
|
|
programmes. */
|
|
|
|
int nsync_cv_wait_with_deadline_generic (nsync_cv *pcv, void *pmu,
|
|
|
|
void (*lock) (void *), void (*unlock) (void *),
|
2024-09-03 06:37:50 +00:00
|
|
|
int clock, nsync_time abs_deadline,
|
2022-09-10 23:11:26 +00:00
|
|
|
nsync_note cancel_note) {
|
2023-09-12 04:34:53 +00:00
|
|
|
int outcome;
|
|
|
|
struct nsync_cv_wait_with_deadline_s c;
|
2022-09-10 23:11:26 +00:00
|
|
|
IGNORE_RACES_START ();
|
2023-09-12 04:34:53 +00:00
|
|
|
|
2023-11-10 09:42:06 +00:00
|
|
|
c.w = nsync_waiter_new_ ();
|
2024-09-03 06:37:50 +00:00
|
|
|
c.clock = clock;
|
2023-09-12 04:34:53 +00:00
|
|
|
c.abs_deadline = abs_deadline;
|
|
|
|
c.cancel_note = cancel_note;
|
|
|
|
c.cv_mu = NULL;
|
|
|
|
c.lock = lock;
|
|
|
|
c.pcv = pcv;
|
|
|
|
c.pmu = pmu;
|
|
|
|
|
2023-11-10 09:42:06 +00:00
|
|
|
ATM_STORE (&c.w->nw.waiting, 1);
|
|
|
|
c.w->cond.f = NULL; /* Not using a conditional critical section. */
|
|
|
|
c.w->cond.v = NULL;
|
|
|
|
c.w->cond.eq = NULL;
|
2022-09-10 23:11:26 +00:00
|
|
|
if (lock == &void_mu_lock ||
|
|
|
|
lock == (void (*) (void *)) &nsync_mu_lock ||
|
|
|
|
lock == (void (*) (void *)) &nsync_mu_rlock) {
|
2023-09-12 04:34:53 +00:00
|
|
|
c.cv_mu = (nsync_mu *) pmu;
|
2022-09-10 23:11:26 +00:00
|
|
|
}
|
2023-11-10 09:42:06 +00:00
|
|
|
c.w->cv_mu = c.cv_mu; /* If *pmu is an nsync_mu, record its address, else record NULL. */
|
2023-09-12 04:34:53 +00:00
|
|
|
c.is_reader_mu = 0; /* If true, an nsync_mu in reader mode. */
|
|
|
|
if (c.cv_mu == NULL) {
|
2023-11-10 09:42:06 +00:00
|
|
|
c.w->l_type = NULL;
|
2022-09-10 23:11:26 +00:00
|
|
|
} else {
|
2023-09-12 04:34:53 +00:00
|
|
|
uint32_t old_mu_word = ATM_LOAD (&c.cv_mu->word);
|
2022-09-10 23:11:26 +00:00
|
|
|
int is_writer = (old_mu_word & MU_WHELD_IF_NON_ZERO) != 0;
|
|
|
|
int is_reader = (old_mu_word & MU_RHELD_IF_NON_ZERO) != 0;
|
|
|
|
if (is_writer) {
|
|
|
|
if (is_reader) {
|
|
|
|
nsync_panic_ ("mu held in reader and writer mode simultaneously "
|
|
|
|
"on entry to nsync_cv_wait_with_deadline()\n");
|
|
|
|
}
|
2023-11-10 09:42:06 +00:00
|
|
|
c.w->l_type = nsync_writer_type_;
|
2022-09-10 23:11:26 +00:00
|
|
|
} else if (is_reader) {
|
2023-11-10 09:42:06 +00:00
|
|
|
c.w->l_type = nsync_reader_type_;
|
2023-09-12 04:34:53 +00:00
|
|
|
c.is_reader_mu = 1;
|
2022-09-10 23:11:26 +00:00
|
|
|
} else {
|
|
|
|
nsync_panic_ ("mu not held on entry to nsync_cv_wait_with_deadline()\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* acquire spinlock, set non-empty */
|
2024-07-06 06:13:20 +00:00
|
|
|
c.old_word = nsync_spin_test_and_set_ (&pcv->word, CV_SPINLOCK, CV_SPINLOCK|CV_NON_EMPTY, 0, pmu);
|
2023-11-10 09:42:06 +00:00
|
|
|
dll_make_last (&pcv->waiters, &c.w->nw.q);
|
|
|
|
c.remove_count = ATM_LOAD (&c.w->remove_count);
|
2022-09-10 23:11:26 +00:00
|
|
|
/* Release the spin lock. */
|
2023-09-12 04:34:53 +00:00
|
|
|
ATM_STORE_REL (&pcv->word, c.old_word|CV_NON_EMPTY); /* release store */
|
2022-09-10 23:11:26 +00:00
|
|
|
|
|
|
|
/* Release *pmu. */
|
2023-09-12 04:34:53 +00:00
|
|
|
if (c.is_reader_mu) {
|
|
|
|
nsync_mu_runlock (c.cv_mu);
|
2022-09-10 23:11:26 +00:00
|
|
|
} else {
|
|
|
|
(*unlock) (pmu);
|
|
|
|
}
|
|
|
|
|
2023-09-12 04:34:53 +00:00
|
|
|
/* Wait until awoken or a timeout. */
|
|
|
|
c.sem_outcome = 0;
|
|
|
|
pthread_cleanup_push (nsync_cv_wait_with_deadline_unwind_, &c);
|
|
|
|
outcome = nsync_cv_wait_with_deadline_impl_ (&c);
|
|
|
|
pthread_cleanup_pop (0);
|
2022-09-10 23:11:26 +00:00
|
|
|
IGNORE_RACES_END ();
|
|
|
|
return (outcome);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wake at least one thread if any are currently blocked on *pcv. If
|
|
|
|
the chosen thread is a reader on an nsync_mu, wake all readers and, if
|
|
|
|
possible, a writer. */
|
|
|
|
void nsync_cv_signal (nsync_cv *pcv) {
|
|
|
|
IGNORE_RACES_START ();
|
|
|
|
if ((ATM_LOAD_ACQ (&pcv->word) & CV_NON_EMPTY) != 0) { /* acquire load */
|
2023-07-06 13:57:28 +00:00
|
|
|
struct Dll *to_wake_list = NULL; /* waiters that we will wake */
|
2022-09-10 23:11:26 +00:00
|
|
|
int all_readers = 0;
|
|
|
|
/* acquire spinlock */
|
|
|
|
uint32_t old_word = nsync_spin_test_and_set_ (&pcv->word, CV_SPINLOCK,
|
2024-07-06 06:13:20 +00:00
|
|
|
CV_SPINLOCK, 0, pcv);
|
2023-07-06 13:57:28 +00:00
|
|
|
if (!dll_is_empty (pcv->waiters)) {
|
2022-09-10 23:11:26 +00:00
|
|
|
/* Point to first waiter that enqueued itself, and
|
|
|
|
detach it from all others. */
|
|
|
|
struct nsync_waiter_s *first_nw;
|
2023-07-06 13:57:28 +00:00
|
|
|
struct Dll *first = dll_first (pcv->waiters);
|
|
|
|
dll_remove (&pcv->waiters, first);
|
2022-09-10 23:11:26 +00:00
|
|
|
first_nw = DLL_NSYNC_WAITER (first);
|
|
|
|
if ((first_nw->flags & NSYNC_WAITER_FLAG_MUCV) != 0) {
|
|
|
|
uint32_t old_value;
|
2023-09-12 04:34:53 +00:00
|
|
|
do {
|
2023-07-06 13:57:28 +00:00
|
|
|
old_value = ATM_LOAD (&DLL_WAITER (first)->remove_count);
|
2022-09-10 23:11:26 +00:00
|
|
|
} while (!ATM_CAS (&DLL_WAITER (first)->remove_count,
|
|
|
|
old_value, old_value+1));
|
|
|
|
}
|
2023-07-06 13:57:28 +00:00
|
|
|
dll_make_last (&to_wake_list, first);
|
2022-09-10 23:11:26 +00:00
|
|
|
if ((first_nw->flags & NSYNC_WAITER_FLAG_MUCV) != 0 &&
|
|
|
|
DLL_WAITER (first)->l_type == nsync_reader_type_) {
|
|
|
|
int woke_writer;
|
|
|
|
/* If the first waiter is a reader, wake all readers, and
|
|
|
|
if it's possible, one writer. This allows reader-regions
|
|
|
|
to be added to a monitor without invalidating code in which
|
2023-09-12 04:34:53 +00:00
|
|
|
a client has optimized broadcast calls by converting them to
|
2022-09-10 23:11:26 +00:00
|
|
|
signal calls. In particular, we wake a writer when waking
|
|
|
|
readers because the readers will not invalidate the condition
|
|
|
|
that motivated the client to call nsync_cv_signal(). But we
|
|
|
|
wake at most one writer because the first writer may invalidate
|
|
|
|
the condition; the client is expecting only one writer to be
|
|
|
|
able make use of the wakeup, or he would have called
|
|
|
|
nsync_cv_broadcast(). */
|
2023-07-06 13:57:28 +00:00
|
|
|
struct Dll *p = NULL;
|
|
|
|
struct Dll *next = NULL;
|
2022-09-10 23:11:26 +00:00
|
|
|
all_readers = 1;
|
|
|
|
woke_writer = 0;
|
2023-07-06 13:57:28 +00:00
|
|
|
for (p = dll_first (pcv->waiters); p != NULL; p = next) {
|
2022-09-10 23:11:26 +00:00
|
|
|
struct nsync_waiter_s *p_nw = DLL_NSYNC_WAITER (p);
|
|
|
|
int should_wake;
|
2023-07-06 13:57:28 +00:00
|
|
|
next = dll_next (pcv->waiters, p);
|
2022-09-10 23:11:26 +00:00
|
|
|
should_wake = 0;
|
|
|
|
if ((p_nw->flags & NSYNC_WAITER_FLAG_MUCV) != 0 &&
|
|
|
|
DLL_WAITER (p)->l_type == nsync_reader_type_) {
|
|
|
|
should_wake = 1;
|
|
|
|
} else if (!woke_writer) {
|
|
|
|
woke_writer = 1;
|
|
|
|
all_readers = 0;
|
|
|
|
should_wake = 1;
|
|
|
|
}
|
|
|
|
if (should_wake) {
|
2023-07-06 13:57:28 +00:00
|
|
|
dll_remove (&pcv->waiters, p);
|
2022-09-10 23:11:26 +00:00
|
|
|
if ((p_nw->flags & NSYNC_WAITER_FLAG_MUCV) != 0) {
|
|
|
|
uint32_t old_value;
|
2023-09-12 04:34:53 +00:00
|
|
|
do {
|
2022-09-10 23:11:26 +00:00
|
|
|
old_value = ATM_LOAD (
|
|
|
|
&DLL_WAITER (p)->remove_count);
|
|
|
|
} while (!ATM_CAS (&DLL_WAITER (p)->remove_count,
|
|
|
|
old_value, old_value+1));
|
|
|
|
}
|
2023-07-06 13:57:28 +00:00
|
|
|
dll_make_last (&to_wake_list, p);
|
2022-09-10 23:11:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-06 13:57:28 +00:00
|
|
|
if (dll_is_empty (pcv->waiters)) {
|
2022-09-10 23:11:26 +00:00
|
|
|
old_word &= ~(CV_NON_EMPTY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Release spinlock. */
|
|
|
|
ATM_STORE_REL (&pcv->word, old_word); /* release store */
|
2023-07-06 13:57:28 +00:00
|
|
|
if (!dll_is_empty (to_wake_list)) {
|
2022-09-10 23:11:26 +00:00
|
|
|
wake_waiters (to_wake_list, all_readers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IGNORE_RACES_END ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wake all threads currently blocked on *pcv. */
|
|
|
|
void nsync_cv_broadcast (nsync_cv *pcv) {
|
|
|
|
IGNORE_RACES_START ();
|
|
|
|
if ((ATM_LOAD_ACQ (&pcv->word) & CV_NON_EMPTY) != 0) { /* acquire load */
|
2023-07-06 13:57:28 +00:00
|
|
|
struct Dll *p;
|
|
|
|
struct Dll *next;
|
2022-09-10 23:11:26 +00:00
|
|
|
int all_readers;
|
2023-07-06 13:57:28 +00:00
|
|
|
struct Dll *to_wake_list = NULL; /* waiters that we will wake */
|
2022-09-10 23:11:26 +00:00
|
|
|
/* acquire spinlock */
|
2024-07-06 06:13:20 +00:00
|
|
|
nsync_spin_test_and_set_ (&pcv->word, CV_SPINLOCK, CV_SPINLOCK, 0, pcv);
|
2022-09-10 23:11:26 +00:00
|
|
|
p = NULL;
|
|
|
|
next = NULL;
|
|
|
|
all_readers = 1;
|
|
|
|
/* Wake entire waiter list, which we leave empty. */
|
2023-07-06 13:57:28 +00:00
|
|
|
for (p = dll_first (pcv->waiters); p != NULL; p = next) {
|
2022-09-10 23:11:26 +00:00
|
|
|
struct nsync_waiter_s *p_nw = DLL_NSYNC_WAITER (p);
|
2023-07-06 13:57:28 +00:00
|
|
|
next = dll_next (pcv->waiters, p);
|
2022-09-10 23:11:26 +00:00
|
|
|
all_readers = all_readers && (p_nw->flags & NSYNC_WAITER_FLAG_MUCV) != 0 &&
|
|
|
|
(DLL_WAITER (p)->l_type == nsync_reader_type_);
|
2023-07-06 13:57:28 +00:00
|
|
|
dll_remove (&pcv->waiters, p);
|
2022-09-10 23:11:26 +00:00
|
|
|
if ((p_nw->flags & NSYNC_WAITER_FLAG_MUCV) != 0) {
|
|
|
|
uint32_t old_value;
|
2023-09-12 04:34:53 +00:00
|
|
|
do {
|
2022-09-10 23:11:26 +00:00
|
|
|
old_value = ATM_LOAD (&DLL_WAITER (p)->remove_count);
|
|
|
|
} while (!ATM_CAS (&DLL_WAITER (p)->remove_count,
|
|
|
|
old_value, old_value+1));
|
|
|
|
}
|
2023-07-06 13:57:28 +00:00
|
|
|
dll_make_last (&to_wake_list, p);
|
2022-09-10 23:11:26 +00:00
|
|
|
}
|
|
|
|
/* Release spinlock and mark queue empty. */
|
|
|
|
ATM_STORE_REL (&pcv->word, 0); /* release store */
|
2023-07-06 13:57:28 +00:00
|
|
|
if (!dll_is_empty (to_wake_list)) { /* Wake them. */
|
2022-09-10 23:11:26 +00:00
|
|
|
wake_waiters (to_wake_list, all_readers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IGNORE_RACES_END ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait with deadline, using an nsync_mu. */
|
2022-11-06 02:49:41 +00:00
|
|
|
errno_t nsync_cv_wait_with_deadline (nsync_cv *pcv, nsync_mu *pmu,
|
2024-09-03 06:37:50 +00:00
|
|
|
int clock, nsync_time abs_deadline,
|
2022-11-06 02:49:41 +00:00
|
|
|
nsync_note cancel_note) {
|
2022-09-10 23:11:26 +00:00
|
|
|
return (nsync_cv_wait_with_deadline_generic (pcv, pmu, &void_mu_lock,
|
2024-09-03 06:37:50 +00:00
|
|
|
&void_mu_unlock, clock,
|
2022-09-10 23:11:26 +00:00
|
|
|
abs_deadline, cancel_note));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Atomically release *pmu and block the caller on *pcv. Wait
|
|
|
|
until awakened by a call to nsync_cv_signal() or nsync_cv_broadcast(), or a spurious
|
|
|
|
wakeup. Then reacquires *pmu, and return. The call is equivalent to a call
|
|
|
|
to nsync_cv_wait_with_deadline() with abs_deadline==nsync_time_no_deadline, and a NULL
|
|
|
|
cancel_note. It should be used in a loop, as with all standard Mesa-style
|
2022-11-06 02:49:41 +00:00
|
|
|
condition variables. See examples above. Returns 0 normally, otherwise
|
|
|
|
ECANCELED may be returned if calling POSIX thread is cancelled only when
|
|
|
|
the PTHREAD_CANCEL_MASKED mode is in play. */
|
|
|
|
errno_t nsync_cv_wait (nsync_cv *pcv, nsync_mu *pmu) {
|
2024-09-03 06:37:50 +00:00
|
|
|
return nsync_cv_wait_with_deadline (pcv, pmu, 0, nsync_time_no_deadline, NULL);
|
2022-09-10 23:11:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-11 18:02:07 +00:00
|
|
|
static nsync_time cv_ready_time (void *v, struct nsync_waiter_s *nw) {
|
2022-09-10 23:11:26 +00:00
|
|
|
nsync_time r;
|
|
|
|
r = (nw == NULL || ATM_LOAD_ACQ (&nw->waiting) != 0? nsync_time_no_deadline : nsync_time_zero);
|
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cv_enqueue (void *v, struct nsync_waiter_s *nw) {
|
|
|
|
nsync_cv *pcv = (nsync_cv *) v;
|
|
|
|
/* acquire spinlock */
|
2024-07-06 06:13:20 +00:00
|
|
|
uint32_t old_word = nsync_spin_test_and_set_ (&pcv->word, CV_SPINLOCK, CV_SPINLOCK, 0, pcv);
|
2023-07-06 13:57:28 +00:00
|
|
|
dll_make_last (&pcv->waiters, &nw->q);
|
2022-09-10 23:11:26 +00:00
|
|
|
ATM_STORE (&nw->waiting, 1);
|
|
|
|
/* Release spinlock. */
|
|
|
|
ATM_STORE_REL (&pcv->word, old_word | CV_NON_EMPTY); /* release store */
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cv_dequeue (void *v, struct nsync_waiter_s *nw) {
|
|
|
|
nsync_cv *pcv = (nsync_cv *) v;
|
|
|
|
int was_queued = 0;
|
|
|
|
/* acquire spinlock */
|
2024-07-06 06:13:20 +00:00
|
|
|
uint32_t old_word = nsync_spin_test_and_set_ (&pcv->word, CV_SPINLOCK, CV_SPINLOCK, 0, pcv);
|
2022-09-10 23:11:26 +00:00
|
|
|
if (ATM_LOAD_ACQ (&nw->waiting) != 0) {
|
2023-07-06 13:57:28 +00:00
|
|
|
dll_remove (&pcv->waiters, &nw->q);
|
2022-09-10 23:11:26 +00:00
|
|
|
ATM_STORE (&nw->waiting, 0);
|
|
|
|
was_queued = 1;
|
|
|
|
}
|
2023-07-06 13:57:28 +00:00
|
|
|
if (dll_is_empty (pcv->waiters)) {
|
2022-09-10 23:11:26 +00:00
|
|
|
old_word &= ~(CV_NON_EMPTY);
|
|
|
|
}
|
|
|
|
/* Release spinlock. */
|
|
|
|
ATM_STORE_REL (&pcv->word, old_word); /* release store */
|
|
|
|
return (was_queued);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct nsync_waitable_funcs_s nsync_cv_waitable_funcs = {
|
|
|
|
&cv_ready_time,
|
|
|
|
&cv_enqueue,
|
|
|
|
&cv_dequeue
|
|
|
|
};
|