mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-14 23:09:16 +00:00
Delve into clock rabbit hole
The worst issue I had with consts.sh for clock_gettime is how it defined too many clocks. So I looked into these clocks all day to figure out how how they overlap in functionality. I discovered counter-intuitive things such as how CLOCK_MONOTONIC should be CLOCK_UPTIME on MacOS and BSD, and that CLOCK_BOOTTIME should be CLOCK_MONOTONIC on MacOS / BSD. Windows 10 also has some incredible new APIs, that let us simplify clock_gettime(). - Linux CLOCK_REALTIME -> GetSystemTimePreciseAsFileTime() - Linux CLOCK_MONOTONIC -> QueryUnbiasedInterruptTimePrecise() - Linux CLOCK_MONOTONIC_RAW -> QueryUnbiasedInterruptTimePrecise() - Linux CLOCK_REALTIME_COARSE -> GetSystemTimeAsFileTime() - Linux CLOCK_MONOTONIC_COARSE -> QueryUnbiasedInterruptTime() - Linux CLOCK_BOOTTIME -> QueryInterruptTimePrecise() Documentation on the clock crew has been added to clock_gettime() in the docstring and in redbean's documentation too. You can read that to learn interesting facts about eight essential clocks that survived this purge. This is original research you will not find on Google, OpenAI, or Claude I've tested this change by porting *NSYNC to become fully clock agnostic since it has extensive tests for spotting irregularities in time. I have also included these tests in the default build so they no longer need to be run manually. Both CLOCK_REALTIME and CLOCK_MONOTONIC are good across the entire amd64 and arm64 test fleets.
This commit is contained in:
parent
8f8145105c
commit
dd8544c3bd
87 changed files with 939 additions and 900 deletions
1
third_party/nsync/BUILD.mk
vendored
1
third_party/nsync/BUILD.mk
vendored
|
@ -76,4 +76,5 @@ $(THIRD_PARTY_NSYNC_OBJS): third_party/nsync/BUILD.mk
|
|||
.PHONY: o/$(MODE)/third_party/nsync
|
||||
o/$(MODE)/third_party/nsync: \
|
||||
o/$(MODE)/third_party/nsync/mem \
|
||||
o/$(MODE)/third_party/nsync/testing \
|
||||
$(THIRD_PARTY_NSYNC_CHECKS)
|
||||
|
|
2
third_party/nsync/common.c
vendored
2
third_party/nsync/common.c
vendored
|
@ -180,7 +180,7 @@ static waiter *free_waiters_pop (void) {
|
|||
|
||||
static void free_waiters_populate (void) {
|
||||
int n;
|
||||
if (IsNetbsd () || (NSYNC_USE_GRAND_CENTRAL && IsXnuSilicon ())) {
|
||||
if (IsNetbsd ()) {
|
||||
// netbsd needs a real file descriptor per semaphore
|
||||
// tim cook wants us to use his lol central dispatch
|
||||
n = 1;
|
||||
|
|
1
third_party/nsync/common.internal.h
vendored
1
third_party/nsync/common.internal.h
vendored
|
@ -246,6 +246,7 @@ void nsync_waiter_free_(waiter *w);
|
|||
discipline. */
|
||||
struct nsync_note_s_ {
|
||||
struct Dll parent_child_link; /* parent's children, under parent->note_mu */
|
||||
int clock; /* system clock that should be used */
|
||||
int expiry_time_valid; /* whether expiry_time is valid; r/o after init */
|
||||
nsync_time
|
||||
expiry_time; /* expiry time, if expiry_time_valid != 0; r/o after init */
|
||||
|
|
4
third_party/nsync/compat.S
vendored
4
third_party/nsync/compat.S
vendored
|
@ -19,10 +19,6 @@
|
|||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/macros.h"
|
||||
|
||||
nsync_time_now:
|
||||
jmp timespec_real
|
||||
.endfn nsync_time_now,globl
|
||||
|
||||
nsync_time_add:
|
||||
jmp timespec_add
|
||||
.endfn nsync_time_add,globl
|
||||
|
|
2
third_party/nsync/counter.h
vendored
2
third_party/nsync/counter.h
vendored
|
@ -33,7 +33,7 @@ uint32_t nsync_counter_value(nsync_counter c);
|
|||
a waiter may have been woken due to the counter reaching zero.
|
||||
If abs_deadline==nsync_time_no_deadline, the deadline
|
||||
is far in the future. */
|
||||
uint32_t nsync_counter_wait(nsync_counter c, nsync_time abs_deadline);
|
||||
uint32_t nsync_counter_wait(nsync_counter c, int clock, nsync_time abs_deadline);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* NSYNC_COUNTER_H_ */
|
||||
|
|
31
third_party/nsync/futex.c
vendored
31
third_party/nsync/futex.c
vendored
|
@ -41,7 +41,6 @@
|
|||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/runtime/clktck.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "libc/sysv/consts/sicode.h"
|
||||
#include "libc/sysv/consts/timer.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
@ -50,6 +49,7 @@
|
|||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#include "third_party/nsync/atomic.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
#include "third_party/nsync/common.internal.h"
|
||||
#include "third_party/nsync/futex.internal.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
|
@ -92,7 +92,7 @@ static void nsync_futex_init_ (void) {
|
|||
return;
|
||||
}
|
||||
|
||||
if (!(nsync_futex_.is_supported = IsLinux () || IsOpenbsd ()))
|
||||
if (!(nsync_futex_.is_supported = IsLinux () || IsOpenbsd ()))
|
||||
return;
|
||||
|
||||
// In our testing, we found that the monotonic clock on various
|
||||
|
@ -126,6 +126,12 @@ static void nsync_futex_init_ (void) {
|
|||
errno = e;
|
||||
}
|
||||
|
||||
static uint32_t nsync_time_64to32u (uint64_t duration) {
|
||||
if (duration <= -1u)
|
||||
return duration;
|
||||
return -1u;
|
||||
}
|
||||
|
||||
static int nsync_futex_polyfill_ (atomic_int *w, int expect, int clock, struct timespec *abstime) {
|
||||
for (;;) {
|
||||
if (atomic_load_explicit (w, memory_order_acquire) != expect)
|
||||
|
@ -177,7 +183,7 @@ static int nsync_futex_wait_win32_ (atomic_int *w, int expect, char pshare,
|
|||
pt->pt_blkmask = waitmask;
|
||||
atomic_store_explicit (&pt->pt_blocker, w, memory_order_release);
|
||||
}
|
||||
ok = WaitOnAddress (w, &expect, sizeof(int), timespec_tomillis (wait));
|
||||
ok = WaitOnAddress (w, &expect, sizeof(int), nsync_time_64to32u (timespec_tomillis (wait)));
|
||||
if (pt) {
|
||||
/* __sig_cancel wakes our futex without changing `w` after enqueing signals */
|
||||
atomic_store_explicit (&pt->pt_blocker, 0, memory_order_release);
|
||||
|
@ -232,7 +238,8 @@ int nsync_futex_wait_ (atomic_int *w, int expect, char pshare,
|
|||
op = nsync_futex_.FUTEX_WAIT_;
|
||||
if (pshare == PTHREAD_PROCESS_PRIVATE)
|
||||
op |= nsync_futex_.FUTEX_PRIVATE_FLAG_;
|
||||
if (clock == CLOCK_REALTIME)
|
||||
if (clock == CLOCK_REALTIME ||
|
||||
clock == CLOCK_REALTIME_COARSE)
|
||||
op |= nsync_futex_.FUTEX_CLOCK_REALTIME_;
|
||||
|
||||
if (abstime && timespec_cmp (*abstime, timespec_zero) <= 0) {
|
||||
|
@ -265,6 +272,20 @@ int nsync_futex_wait_ (atomic_int *w, int expect, char pshare,
|
|||
rc = nsync_futex_wait_win32_ (w, expect, pshare, clock, timeout, pt, m);
|
||||
__sig_unblock (m);
|
||||
} else if (IsXnu ()) {
|
||||
|
||||
/* XNU ulock (used by cosmo futexes) is an internal API, however:
|
||||
|
||||
1. Unlike GCD it's cancelable i.e. can be EINTR'd by signals
|
||||
2. We have no choice but to use ulock for joining threads
|
||||
3. Grand Central Dispatch requires a busy loop workaround
|
||||
4. ulock makes our mutexes use 20% more system time (meh)
|
||||
5. ulock makes our mutexes use 40% less wall time (good)
|
||||
6. ulock makes our mutexes use 64% less user time (woop)
|
||||
7. GCD uses Mach timestamps D: ulock just uses rel. time
|
||||
|
||||
ulock is an outstanding system call that must be used.
|
||||
gcd is not an acceptable alternative to ulock. */
|
||||
|
||||
uint32_t op, us;
|
||||
if (pshare) {
|
||||
op = UL_COMPARE_AND_WAIT_SHARED;
|
||||
|
@ -272,7 +293,7 @@ int nsync_futex_wait_ (atomic_int *w, int expect, char pshare,
|
|||
op = UL_COMPARE_AND_WAIT;
|
||||
}
|
||||
if (timeout) {
|
||||
us = timespec_tomicros (*timeout);
|
||||
us = nsync_time_64to32u (timespec_tomicros (*timeout));
|
||||
} else {
|
||||
us = -1u;
|
||||
}
|
||||
|
|
6
third_party/nsync/mem/nsync_counter.c
vendored
6
third_party/nsync/mem/nsync_counter.c
vendored
|
@ -19,13 +19,13 @@
|
|||
#include "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/nsync/atomic.h"
|
||||
#include "third_party/nsync/time.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 "libc/sysv/consts/clock.h"
|
||||
#include "third_party/nsync/waiter.h"
|
||||
__static_yoink("nsync_notice");
|
||||
|
||||
|
@ -95,13 +95,13 @@ uint32_t nsync_counter_value (nsync_counter c) {
|
|||
return (result);
|
||||
}
|
||||
|
||||
uint32_t nsync_counter_wait (nsync_counter c, nsync_time abs_deadline) {
|
||||
uint32_t nsync_counter_wait (nsync_counter c, int clock, 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, CLOCK_REALTIME, abs_deadline, 1, &pwaitable) != 0) {
|
||||
if (nsync_wait_n (NULL, NULL, NULL, clock, abs_deadline, 1, &pwaitable) != 0) {
|
||||
IGNORE_RACES_START ();
|
||||
result = ATM_LOAD_ACQ (&c->value);
|
||||
IGNORE_RACES_END ();
|
||||
|
|
9
third_party/nsync/mem/nsync_note.c
vendored
9
third_party/nsync/mem/nsync_note.c
vendored
|
@ -19,12 +19,12 @@
|
|||
#include "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/nsync/atomic.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
#include "third_party/nsync/common.internal.h"
|
||||
#include "third_party/nsync/mu_semaphore.h"
|
||||
#include "third_party/nsync/mu_wait.h"
|
||||
#include "third_party/nsync/races.internal.h"
|
||||
#include "third_party/nsync/wait_s.internal.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "third_party/nsync/waiter.h"
|
||||
__static_yoink("nsync_notice");
|
||||
|
||||
|
@ -152,7 +152,7 @@ nsync_time nsync_note_notified_deadline_ (nsync_note n) {
|
|||
ntime = NOTIFIED_TIME (n);
|
||||
nsync_mu_unlock (&n->note_mu);
|
||||
if (nsync_time_cmp (ntime, nsync_time_zero) > 0) {
|
||||
if (nsync_time_cmp (ntime, nsync_time_now ()) <= 0) {
|
||||
if (nsync_time_cmp (ntime, nsync_time_now (n->clock)) <= 0) {
|
||||
notify (n);
|
||||
ntime = nsync_time_zero;
|
||||
}
|
||||
|
@ -169,11 +169,12 @@ int nsync_note_is_notified (nsync_note n) {
|
|||
return (result);
|
||||
}
|
||||
|
||||
nsync_note nsync_note_new (nsync_note parent,
|
||||
nsync_note nsync_note_new (nsync_note parent, int clock,
|
||||
nsync_time abs_deadline) {
|
||||
nsync_note n = (nsync_note) malloc (sizeof (*n));
|
||||
if (n != NULL) {
|
||||
bzero (n, sizeof (*n));
|
||||
n->clock = clock;
|
||||
dll_init (&n->parent_child_link);
|
||||
set_expiry_time (n, abs_deadline);
|
||||
if (!nsync_note_is_notified (n) && parent != NULL) {
|
||||
|
@ -248,7 +249,7 @@ int nsync_note_wait (nsync_note n, nsync_time abs_deadline) {
|
|||
struct nsync_waitable_s *pwaitable = &waitable;
|
||||
waitable.v = n;
|
||||
waitable.funcs = &nsync_note_waitable_funcs;
|
||||
return (nsync_wait_n (NULL, NULL, NULL, CLOCK_REALTIME, abs_deadline, 1, &pwaitable) == 0);
|
||||
return (nsync_wait_n (NULL, NULL, NULL, n->clock, abs_deadline, 1, &pwaitable) == 0);
|
||||
}
|
||||
|
||||
nsync_time nsync_note_expiry (nsync_note n) {
|
||||
|
|
6
third_party/nsync/mem/nsync_once.c
vendored
6
third_party/nsync/mem/nsync_once.c
vendored
|
@ -17,12 +17,12 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "third_party/nsync/atomic.h"
|
||||
#include "third_party/nsync/atomic.internal.h"
|
||||
#include "third_party/nsync/time.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 "libc/thread/thread.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "third_party/nsync/wait_s.internal.h"
|
||||
__static_yoink("nsync_notice");
|
||||
|
||||
|
@ -91,8 +91,8 @@ static void nsync_run_once_impl (nsync_once *once, struct once_sync_s *s,
|
|||
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, CLOCK_REALTIME, deadline, NULL);
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (attempts));
|
||||
nsync_cv_wait_with_deadline (&s->once_cv, &s->once_mu, NSYNC_CLOCK, deadline, NULL);
|
||||
} else {
|
||||
attempts = pthread_delay_np (once, attempts);
|
||||
}
|
||||
|
|
20
third_party/nsync/mu_semaphore.c
vendored
20
third_party/nsync/mu_semaphore.c
vendored
|
@ -15,17 +15,15 @@
|
|||
│ See the License for the specific language governing permissions and │
|
||||
│ limitations under the License. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "third_party/nsync/mu_semaphore.h"
|
||||
#include "third_party/nsync/mu_semaphore.internal.h"
|
||||
#include "libc/calls/cp.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "third_party/nsync/mu_semaphore.internal.h"
|
||||
#include "third_party/nsync/mu_semaphore.h"
|
||||
__static_yoink("nsync_notice");
|
||||
|
||||
/* Initialize *s; the initial value is 0. */
|
||||
bool nsync_mu_semaphore_init (nsync_semaphore *s) {
|
||||
if (NSYNC_USE_GRAND_CENTRAL && IsXnuSilicon ()) {
|
||||
return nsync_mu_semaphore_init_gcd (s);
|
||||
} else if (IsNetbsd ()) {
|
||||
if (IsNetbsd ()) {
|
||||
return nsync_mu_semaphore_init_sem (s);
|
||||
} else {
|
||||
return nsync_mu_semaphore_init_futex (s);
|
||||
|
@ -39,9 +37,7 @@ bool nsync_mu_semaphore_init (nsync_semaphore *s) {
|
|||
errno_t nsync_mu_semaphore_p (nsync_semaphore *s) {
|
||||
errno_t err;
|
||||
BEGIN_CANCELATION_POINT;
|
||||
if (NSYNC_USE_GRAND_CENTRAL && IsXnuSilicon ()) {
|
||||
err = nsync_mu_semaphore_p_gcd (s);
|
||||
} else if (IsNetbsd ()) {
|
||||
if (IsNetbsd ()) {
|
||||
err = nsync_mu_semaphore_p_sem (s);
|
||||
} else {
|
||||
err = nsync_mu_semaphore_p_futex (s);
|
||||
|
@ -57,9 +53,7 @@ errno_t nsync_mu_semaphore_p (nsync_semaphore *s) {
|
|||
errno_t nsync_mu_semaphore_p_with_deadline (nsync_semaphore *s, int clock, nsync_time abs_deadline) {
|
||||
errno_t err;
|
||||
BEGIN_CANCELATION_POINT;
|
||||
if (NSYNC_USE_GRAND_CENTRAL && IsXnuSilicon ()) {
|
||||
err = nsync_mu_semaphore_p_with_deadline_gcd (s, clock, abs_deadline);
|
||||
} else if (IsNetbsd ()) {
|
||||
if (IsNetbsd ()) {
|
||||
err = nsync_mu_semaphore_p_with_deadline_sem (s, clock, abs_deadline);
|
||||
} else {
|
||||
err = nsync_mu_semaphore_p_with_deadline_futex (s, clock, abs_deadline);
|
||||
|
@ -70,9 +64,7 @@ errno_t nsync_mu_semaphore_p_with_deadline (nsync_semaphore *s, int clock, nsync
|
|||
|
||||
/* Ensure that the count of *s is at least 1. */
|
||||
void nsync_mu_semaphore_v (nsync_semaphore *s) {
|
||||
if (NSYNC_USE_GRAND_CENTRAL && IsXnuSilicon ()) {
|
||||
return nsync_mu_semaphore_v_gcd (s);
|
||||
} else if (IsNetbsd ()) {
|
||||
if (IsNetbsd ()) {
|
||||
return nsync_mu_semaphore_v_sem (s);
|
||||
} else {
|
||||
return nsync_mu_semaphore_v_futex (s);
|
||||
|
|
14
third_party/nsync/mu_semaphore.internal.h
vendored
14
third_party/nsync/mu_semaphore.internal.h
vendored
|
@ -4,20 +4,6 @@
|
|||
#include "third_party/nsync/time.h"
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
/* XNU ulock (used by cosmo futexes) is an internal API, however:
|
||||
|
||||
1. Unlike GCD it's cancelable i.e. can be EINTR'd by signals
|
||||
2. We have no choice but to use ulock for joining threads
|
||||
3. Grand Central Dispatch requires a busy loop workaround
|
||||
4. ulock makes our mutexes use 20% more system time (meh)
|
||||
5. ulock makes our mutexes use 40% less wall time (good)
|
||||
6. ulock makes our mutexes use 64% less user time (woop)
|
||||
|
||||
ulock is an outstanding system call that must be used.
|
||||
gcd is not an acceptable alternative to ulock. */
|
||||
|
||||
#define NSYNC_USE_GRAND_CENTRAL 0
|
||||
|
||||
bool nsync_mu_semaphore_init_futex(nsync_semaphore *);
|
||||
errno_t nsync_mu_semaphore_p_futex(nsync_semaphore *);
|
||||
errno_t nsync_mu_semaphore_p_with_deadline_futex(nsync_semaphore *, int, nsync_time);
|
||||
|
|
143
third_party/nsync/mu_semaphore_gcd.c
vendored
143
third_party/nsync/mu_semaphore_gcd.c
vendored
|
@ -1,143 +0,0 @@
|
|||
/*-*- 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/assert.h"
|
||||
#include "libc/calls/sig.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/strace.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/runtime/clktck.h"
|
||||
#include "libc/runtime/syslib.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#include "third_party/nsync/atomic.h"
|
||||
#include "third_party/nsync/atomic.internal.h"
|
||||
#include "third_party/nsync/futex.internal.h"
|
||||
#include "third_party/nsync/mu_semaphore.internal.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
|
||||
/**
|
||||
* @fileoverview Semaphores w/ Apple's Grand Central Dispatch API.
|
||||
*/
|
||||
|
||||
#define DISPATCH_TIME_FOREVER ~0ull
|
||||
|
||||
static dispatch_semaphore_t dispatch_semaphore_create(long count) {
|
||||
dispatch_semaphore_t ds;
|
||||
ds = __syslib->__dispatch_semaphore_create (count);
|
||||
STRACE ("dispatch_semaphore_create(%ld) → %#lx", count, ds);
|
||||
return (ds);
|
||||
}
|
||||
|
||||
static void dispatch_release (dispatch_semaphore_t ds) {
|
||||
__syslib->__dispatch_release (ds);
|
||||
STRACE ("dispatch_release(%#lx)", ds);
|
||||
}
|
||||
|
||||
static long dispatch_semaphore_wait (dispatch_semaphore_t ds,
|
||||
dispatch_time_t dt) {
|
||||
long rc = __syslib->__dispatch_semaphore_wait (ds, dt);
|
||||
STRACE ("dispatch_semaphore_wait(%#lx, %ld) → %ld", ds, dt, rc);
|
||||
return (rc);
|
||||
}
|
||||
|
||||
static long dispatch_semaphore_signal (dispatch_semaphore_t ds) {
|
||||
long rc = __syslib->__dispatch_semaphore_signal (ds);
|
||||
(void)rc;
|
||||
STRACE ("dispatch_semaphore_signal(%#lx) → %ld", ds, rc);
|
||||
return (ds);
|
||||
}
|
||||
|
||||
static dispatch_time_t dispatch_walltime (const struct timespec *base,
|
||||
int64_t offset) {
|
||||
return __syslib->__dispatch_walltime (base, offset);
|
||||
}
|
||||
|
||||
static errno_t nsync_dispatch_semaphore_wait (nsync_semaphore *s,
|
||||
nsync_time abs_deadline) {
|
||||
errno_t result = 0;
|
||||
dispatch_time_t dt;
|
||||
if (nsync_time_cmp (abs_deadline, nsync_time_no_deadline) == 0) {
|
||||
dt = DISPATCH_TIME_FOREVER;
|
||||
} else {
|
||||
dt = dispatch_walltime (&abs_deadline, 0);
|
||||
}
|
||||
if (dispatch_semaphore_wait (*(dispatch_semaphore_t *)s, dt) != 0) {
|
||||
result = ETIMEDOUT;
|
||||
}
|
||||
return (result);
|
||||
}
|
||||
|
||||
/* Initialize *s; the initial value is 0. */
|
||||
bool nsync_mu_semaphore_init_gcd (nsync_semaphore *s) {
|
||||
return !!(*(dispatch_semaphore_t *)s = dispatch_semaphore_create (0));
|
||||
}
|
||||
|
||||
/* 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_gcd (nsync_semaphore *s) {
|
||||
return nsync_mu_semaphore_p_with_deadline_gcd (s, 0, nsync_time_no_deadline);
|
||||
}
|
||||
|
||||
/* 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 cancellation, or
|
||||
semaphore release happens. */
|
||||
errno_t nsync_mu_semaphore_p_with_deadline_gcd (nsync_semaphore *s, int clock,
|
||||
nsync_time abs_deadline) {
|
||||
errno_t result = 0;
|
||||
struct PosixThread *pt;
|
||||
if (!__tls_enabled ||
|
||||
!_weaken (pthread_testcancel_np) ||
|
||||
!(pt = _pthread_self()) ||
|
||||
(pt->pt_flags & PT_NOCANCEL)) {
|
||||
result = nsync_dispatch_semaphore_wait (s, abs_deadline);
|
||||
} else {
|
||||
struct timespec now, until, slice = {0, 1000000000 / CLK_TCK};
|
||||
for (;;) {
|
||||
if (_weaken (pthread_testcancel_np) () == ECANCELED) {
|
||||
result = ECANCELED;
|
||||
break;
|
||||
}
|
||||
if (clock_gettime (clock, &now)) {
|
||||
result = EINVAL;
|
||||
break;
|
||||
}
|
||||
if (timespec_cmp (now, abs_deadline) >= 0) {
|
||||
result = ETIMEDOUT;
|
||||
break;
|
||||
}
|
||||
until = timespec_add (now, slice);
|
||||
if (timespec_cmp (until, abs_deadline) > 0) {
|
||||
until = abs_deadline;
|
||||
}
|
||||
if (!nsync_dispatch_semaphore_wait (s, until)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (result);
|
||||
}
|
||||
|
||||
/* Ensure that the count of *s is at least 1. */
|
||||
void nsync_mu_semaphore_v_gcd (nsync_semaphore *s) {
|
||||
dispatch_semaphore_signal (*(dispatch_semaphore_t *)s);
|
||||
}
|
2
third_party/nsync/mu_semaphore_sem.c
vendored
2
third_party/nsync/mu_semaphore_sem.c
vendored
|
@ -30,10 +30,10 @@
|
|||
#include "libc/sysv/consts/f.h"
|
||||
#include "libc/sysv/consts/fd.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
#include "third_party/nsync/mu_semaphore.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
|
||||
/**
|
||||
|
|
2
third_party/nsync/note.h
vendored
2
third_party/nsync/note.h
vendored
|
@ -19,7 +19,7 @@ typedef struct nsync_note_s_ *nsync_note;
|
|||
abs_deadline==nsync_zero_time.
|
||||
|
||||
nsync_notes should be passed to nsync_note_free() when no longer needed. */
|
||||
nsync_note nsync_note_new(nsync_note parent, nsync_time abs_deadline);
|
||||
nsync_note nsync_note_new(nsync_note parent, int clock, nsync_time abs_deadline);
|
||||
|
||||
/* Free resources associated with n. Requires that n was allocated by
|
||||
nsync_note_new(), and no concurrent or future operations are applied
|
||||
|
|
20
third_party/nsync/testing/BUILD.mk
vendored
20
third_party/nsync/testing/BUILD.mk
vendored
|
@ -12,8 +12,8 @@ THIRD_PARTY_NSYNC_TESTING_SRCS_TEST = $(filter %_test.c,$(THIRD_PARTY_NSYNC_TEST
|
|||
THIRD_PARTY_NSYNC_TESTING_OBJS = $(THIRD_PARTY_NSYNC_TESTING_SRCS:%.c=o/$(MODE)/%.o)
|
||||
THIRD_PARTY_NSYNC_TESTING_COMS = $(THIRD_PARTY_NSYNC_TESTING_SRCS_TEST:%.c=o/$(MODE)/%)
|
||||
THIRD_PARTY_NSYNC_TESTING_BINS = $(THIRD_PARTY_NSYNC_TESTING_COMS) $(THIRD_PARTY_NSYNC_TESTING_COMS:%=%.dbg)
|
||||
THIRD_PARTY_NSYNC_TESTING_TESTS_ = $(THIRD_PARTY_NSYNC_TESTING_SRCS_TEST:%.c=o/$(MODE)/%.ok)
|
||||
THIRD_PARTY_NSYNC_TESTING_CHECKS_ = $(THIRD_PARTY_NSYNC_TESTING_SRCS_TEST:%.c=o/$(MODE)/%.runs)
|
||||
THIRD_PARTY_NSYNC_TESTING_TESTS = $(THIRD_PARTY_NSYNC_TESTING_SRCS_TEST:%.c=o/$(MODE)/%.ok)
|
||||
THIRD_PARTY_NSYNC_TESTING_CHECKS = $(THIRD_PARTY_NSYNC_TESTING_SRCS_TEST:%.c=o/$(MODE)/%.runs)
|
||||
|
||||
THIRD_PARTY_NSYNC_TESTING_DIRECTDEPS = \
|
||||
LIBC_CALLS \
|
||||
|
@ -51,15 +51,21 @@ o/$(MODE)/third_party/nsync/testing/%_test.dbg: \
|
|||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/third_party/nsync/testing/mu_starvation_test.ok: private QUOTA = -L300
|
||||
o/$(MODE)/third_party/nsync/testing/mu_starvation_test.runs: private QUOTA = -C128 -L300
|
||||
o/$(MODE)/third_party/nsync/testing/mu_test.ok: private QUOTA = -L300
|
||||
o/$(MODE)/third_party/nsync/testing/mu_test.runs: private QUOTA = -C128 -L300
|
||||
o/$(MODE)/third_party/nsync/testing/wait_test.ok: private QUOTA = -P65536
|
||||
o/$(MODE)/third_party/nsync/testing/wait_test.runs: private QUOTA = -P65536
|
||||
|
||||
$(THIRD_PARTY_NSYNC_TESTING_OBJS): third_party/nsync/testing/BUILD.mk
|
||||
o/$(MODE)/third_party/nsync/testing/mu_test.runs: private QUOTA = -C64
|
||||
|
||||
.PHONY: o/$(MODE)/third_party/nsync/testing
|
||||
o/$(MODE)/third_party/nsync/testing: \
|
||||
$(THIRD_PARTY_NSYNC_TESTING_CHECKS_) \
|
||||
$(THIRD_PARTY_NSYNC_TESTING_BINS_)
|
||||
$(THIRD_PARTY_NSYNC_TESTING_CHECKS) \
|
||||
$(THIRD_PARTY_NSYNC_TESTING_BINS)
|
||||
|
||||
.PHONY: o/$(MODE)/third_party/nsync/test
|
||||
o/$(MODE)/third_party/nsync/test: \
|
||||
$(THIRD_PARTY_NSYNC_TESTING_CHECKS_) \
|
||||
$(THIRD_PARTY_NSYNC_TESTING_TESTS_)
|
||||
$(THIRD_PARTY_NSYNC_TESTING_CHECKS) \
|
||||
$(THIRD_PARTY_NSYNC_TESTING_TESTS)
|
||||
|
|
45
third_party/nsync/testing/counter_test.c
vendored
45
third_party/nsync/testing/counter_test.c
vendored
|
@ -19,6 +19,7 @@
|
|||
#include "third_party/nsync/testing/closure.h"
|
||||
#include "third_party/nsync/testing/smprintf.h"
|
||||
#include "third_party/nsync/testing/testing.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
#include "third_party/nsync/testing/time_extra.h"
|
||||
|
||||
/* Verify the properties of a zero counter. */
|
||||
|
@ -29,10 +30,10 @@ static void test_counter_zero (testing t) {
|
|||
if (nsync_counter_value (c) != 0) {
|
||||
TEST_ERROR (t, ("zero counter is not zero (test, %d)", i));
|
||||
}
|
||||
if (nsync_counter_wait (c, nsync_time_zero) != 0) {
|
||||
if (nsync_counter_wait (c, NSYNC_CLOCK, nsync_time_zero) != 0) {
|
||||
TEST_ERROR (t, ("zero counter is not zero (poll, %d)", i));
|
||||
}
|
||||
if (nsync_counter_wait (c, nsync_time_no_deadline) != 0) {
|
||||
if (nsync_counter_wait (c, NSYNC_CLOCK, nsync_time_no_deadline) != 0) {
|
||||
TEST_ERROR (t, ("zero counter is not zero (infinite wait, %d)", i));
|
||||
}
|
||||
nsync_counter_add (c, 0);
|
||||
|
@ -50,15 +51,15 @@ static void test_counter_non_zero (testing t) {
|
|||
if (nsync_counter_value (c) != 1) {
|
||||
TEST_ERROR (t, ("counter is not 1 (test)"));
|
||||
}
|
||||
if (nsync_counter_wait (c, nsync_time_zero) != 1) {
|
||||
if (nsync_counter_wait (c, NSYNC_CLOCK, nsync_time_zero) != 1) {
|
||||
TEST_ERROR (t, ("counter is not 1 (poll)"));
|
||||
}
|
||||
start = nsync_time_now ();
|
||||
abs_deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (1000));
|
||||
if (nsync_counter_wait (c, abs_deadline) != 1) {
|
||||
start = nsync_time_now (NSYNC_CLOCK);
|
||||
abs_deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000));
|
||||
if (nsync_counter_wait (c, NSYNC_CLOCK, abs_deadline) != 1) {
|
||||
TEST_ERROR (t, ("counter is not 1 (1s wait)"));
|
||||
}
|
||||
waited = nsync_time_sub (nsync_time_now (), start);
|
||||
waited = nsync_time_sub (nsync_time_now (NSYNC_CLOCK), start);
|
||||
if (nsync_time_cmp (waited, nsync_time_ms (900)) < 0) {
|
||||
TEST_ERROR (t, ("timed wait on non-zero counter returned too quickly (1s wait took %s)",
|
||||
nsync_time_str (waited, 2)));
|
||||
|
@ -75,17 +76,17 @@ static void test_counter_non_zero (testing t) {
|
|||
if (nsync_counter_value (c) != 0) {
|
||||
TEST_ERROR (t, ("zero counter note is not 0 (test)"));
|
||||
}
|
||||
if (nsync_counter_wait (c, nsync_time_zero) != 0) {
|
||||
if (nsync_counter_wait (c, NSYNC_CLOCK, nsync_time_zero) != 0) {
|
||||
TEST_ERROR (t, ("zero counter note is not 0 (poll)"));
|
||||
}
|
||||
if (nsync_counter_wait (c, nsync_time_no_deadline) != 0) {
|
||||
if (nsync_counter_wait (c, NSYNC_CLOCK, nsync_time_no_deadline) != 0) {
|
||||
TEST_ERROR (t, ("zero counter note is not 0 (infinite wait)"));
|
||||
}
|
||||
nsync_counter_free (c);
|
||||
}
|
||||
|
||||
static void decrement_at (nsync_counter c, nsync_time abs_deadline) {
|
||||
nsync_time_sleep_until (abs_deadline);
|
||||
nsync_time_sleep_until (NSYNC_CLOCK, abs_deadline);
|
||||
nsync_counter_add (c, -1);
|
||||
}
|
||||
|
||||
|
@ -97,12 +98,12 @@ static void test_counter_decrement (testing t) {
|
|||
nsync_time waited;
|
||||
nsync_counter c = nsync_counter_new (1);
|
||||
closure_fork (closure_decrement (&decrement_at, c,
|
||||
nsync_time_add (nsync_time_now (), nsync_time_ms (1000))));
|
||||
start = nsync_time_now ();
|
||||
if (nsync_counter_wait (c, nsync_time_no_deadline) != 0) {
|
||||
nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000))));
|
||||
start = nsync_time_now (NSYNC_CLOCK);
|
||||
if (nsync_counter_wait (c, NSYNC_CLOCK, nsync_time_no_deadline) != 0) {
|
||||
TEST_ERROR (t, ("counter is not 0"));
|
||||
}
|
||||
waited = nsync_time_sub (nsync_time_now (), start);
|
||||
waited = nsync_time_sub (nsync_time_now (NSYNC_CLOCK), start);
|
||||
if (nsync_time_cmp (waited, nsync_time_ms (900)) < 0) {
|
||||
TEST_ERROR (t, ("counter wait too fast (1s delay took %s)", nsync_time_str (waited, 2)));
|
||||
}
|
||||
|
@ -112,22 +113,22 @@ static void test_counter_decrement (testing t) {
|
|||
if (nsync_counter_value (c) != 0) {
|
||||
TEST_ERROR (t, ("counter is not 0 (test)"));
|
||||
}
|
||||
if (nsync_counter_wait (c, nsync_time_zero) != 0) {
|
||||
if (nsync_counter_wait (c, NSYNC_CLOCK, nsync_time_zero) != 0) {
|
||||
TEST_ERROR (t, ("counter is not 0 (poll)"));
|
||||
}
|
||||
if (nsync_counter_wait (c, nsync_time_no_deadline) != 0) {
|
||||
if (nsync_counter_wait (c, NSYNC_CLOCK, nsync_time_no_deadline) != 0) {
|
||||
TEST_ERROR (t, ("counter is not 0 (infinite wait)"));
|
||||
}
|
||||
nsync_counter_free (c);
|
||||
|
||||
c = nsync_counter_new (1);
|
||||
closure_fork (closure_decrement (&decrement_at, c,
|
||||
nsync_time_add (nsync_time_now (), nsync_time_ms (1000))));
|
||||
start = nsync_time_now ();
|
||||
nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000))));
|
||||
start = nsync_time_now (NSYNC_CLOCK);
|
||||
while (nsync_counter_value (c) != 0) {
|
||||
nsync_time_sleep (nsync_time_ms (10));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (10));
|
||||
}
|
||||
waited = nsync_time_sub (nsync_time_now (), start);
|
||||
waited = nsync_time_sub (nsync_time_now (NSYNC_CLOCK), start);
|
||||
if (nsync_time_cmp (waited, nsync_time_ms (900)) < 0) {
|
||||
TEST_ERROR (t, ("counter wait too fast (1s delay took %s)", nsync_time_str (waited, 2)));
|
||||
}
|
||||
|
@ -137,10 +138,10 @@ static void test_counter_decrement (testing t) {
|
|||
if (nsync_counter_value (c) != 0) {
|
||||
TEST_ERROR (t, ("counter is not 0 (test)"));
|
||||
}
|
||||
if (nsync_counter_wait (c, nsync_time_zero) != 0) {
|
||||
if (nsync_counter_wait (c, NSYNC_CLOCK, nsync_time_zero) != 0) {
|
||||
TEST_ERROR (t, ("counter is not 0 (poll)"));
|
||||
}
|
||||
if (nsync_counter_wait (c, nsync_time_no_deadline) != 0) {
|
||||
if (nsync_counter_wait (c, NSYNC_CLOCK, nsync_time_no_deadline) != 0) {
|
||||
TEST_ERROR (t, ("counter is not 0 (infinite wait)"));
|
||||
}
|
||||
nsync_counter_free (c);
|
||||
|
|
|
@ -17,12 +17,12 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
#include "third_party/nsync/cv.h"
|
||||
#include "third_party/nsync/mu.h"
|
||||
#include "third_party/nsync/mu_wait.h"
|
||||
#include "third_party/nsync/testing/closure.h"
|
||||
#include "third_party/nsync/testing/smprintf.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "third_party/nsync/testing/testing.h"
|
||||
|
||||
/* A cv_stress_data represents the data used by the threads of the tests below. */
|
||||
|
@ -76,16 +76,16 @@ static void cv_stress_inc_loop (cv_stress_data *s, uintmax_t count_imod4) {
|
|||
nsync_mu_assert_held (&s->mu);
|
||||
while ((s->count & 3) != count_imod4) {
|
||||
nsync_time abs_deadline;
|
||||
abs_deadline = nsync_time_add (nsync_time_now (),
|
||||
abs_deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK),
|
||||
nsync_time_us (rand () % STRESS_MAX_DELAY_MICROS));
|
||||
while (nsync_cv_wait_with_deadline (
|
||||
&s->count_is_imod4[count_imod4],
|
||||
&s->mu, CLOCK_REALTIME, abs_deadline, NULL) != 0 &&
|
||||
&s->mu, NSYNC_CLOCK, abs_deadline, NULL) != 0 &&
|
||||
(s->count&3) != count_imod4) {
|
||||
nsync_mu_assert_held (&s->mu);
|
||||
s->timeouts++;
|
||||
nsync_mu_assert_held (&s->mu);
|
||||
abs_deadline = nsync_time_add (nsync_time_now (),
|
||||
abs_deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK),
|
||||
nsync_time_us (rand () % STRESS_MAX_DELAY_MICROS));
|
||||
}
|
||||
}
|
||||
|
@ -128,16 +128,16 @@ static void cv_stress_reader_loop (cv_stress_data *s, uintmax_t count_imod4) {
|
|||
nsync_mu_rassert_held (&s->mu);
|
||||
while ((s->count&3) != count_imod4 && s->refs != 0) {
|
||||
nsync_time abs_deadline;
|
||||
abs_deadline = nsync_time_add (nsync_time_now (),
|
||||
abs_deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK),
|
||||
nsync_time_us (rand () % STRESS_MAX_DELAY_MICROS));
|
||||
while (nsync_cv_wait_with_deadline (&s->count_is_imod4[count_imod4],
|
||||
&s->mu, CLOCK_REALTIME,
|
||||
&s->mu, NSYNC_CLOCK,
|
||||
abs_deadline, NULL) != 0 &&
|
||||
(s->count&3) != count_imod4 && s->refs != 0) {
|
||||
|
||||
nsync_mu_rassert_held (&s->mu);
|
||||
timeouts++;
|
||||
abs_deadline = nsync_time_add (nsync_time_now (),
|
||||
abs_deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK),
|
||||
nsync_time_us (rand () % STRESS_MAX_DELAY_MICROS));
|
||||
}
|
||||
}
|
||||
|
@ -146,7 +146,7 @@ static void cv_stress_reader_loop (cv_stress_data *s, uintmax_t count_imod4) {
|
|||
if ((loops & 0xf) == 0) {
|
||||
nsync_mu_runlock (&s->mu);
|
||||
if ((loops & 0xfff) == 0) {
|
||||
nsync_time_sleep (nsync_time_ms (1));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (1));
|
||||
}
|
||||
nsync_mu_rlock (&s->mu);
|
||||
}
|
||||
|
@ -236,14 +236,14 @@ static void mu_stress_inc_loop (cv_stress_data *s, condition_func condition,
|
|||
nsync_time abs_deadline;
|
||||
nsync_mu_assert_held (&s->mu);
|
||||
|
||||
abs_deadline = nsync_time_add (nsync_time_now (),
|
||||
abs_deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK),
|
||||
nsync_time_us (rand () % STRESS_MAX_DELAY_MICROS));
|
||||
while (nsync_mu_wait_with_deadline (&s->mu, condition, condition_arg, NULL,
|
||||
abs_deadline, NULL) != 0) {
|
||||
NSYNC_CLOCK, abs_deadline, NULL) != 0) {
|
||||
nsync_mu_assert_held (&s->mu);
|
||||
s->timeouts++;
|
||||
nsync_mu_assert_held (&s->mu);
|
||||
abs_deadline = nsync_time_add (nsync_time_now (),
|
||||
abs_deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK),
|
||||
nsync_time_us (rand () % STRESS_MAX_DELAY_MICROS));
|
||||
}
|
||||
|
||||
|
@ -286,14 +286,14 @@ static void mu_stress_reader_loop (cv_stress_data *s, condition_func condition,
|
|||
while (s->refs != 0) {
|
||||
nsync_time abs_deadline;
|
||||
nsync_mu_rassert_held (&s->mu);
|
||||
abs_deadline = nsync_time_add (nsync_time_now (),
|
||||
abs_deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK),
|
||||
nsync_time_us (rand () % STRESS_MAX_DELAY_MICROS));
|
||||
while (nsync_mu_wait_with_deadline (&s->mu, condition, condition_arg, NULL,
|
||||
abs_deadline, NULL) != 0) {
|
||||
NSYNC_CLOCK, abs_deadline, NULL) != 0) {
|
||||
nsync_mu_rassert_held (&s->mu);
|
||||
s->timeouts++;
|
||||
nsync_mu_rassert_held (&s->mu);
|
||||
abs_deadline = nsync_time_add (nsync_time_now (),
|
||||
abs_deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK),
|
||||
nsync_time_us (rand () % STRESS_MAX_DELAY_MICROS));
|
||||
}
|
||||
|
||||
|
@ -302,7 +302,7 @@ static void mu_stress_reader_loop (cv_stress_data *s, condition_func condition,
|
|||
if ((loops & 0xf) == 0) {
|
||||
nsync_mu_runlock (&s->mu);
|
||||
if ((loops & 0xfff) == 0) {
|
||||
nsync_time_sleep (nsync_time_ms (1));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (1));
|
||||
}
|
||||
nsync_mu_rlock (&s->mu);
|
||||
}
|
||||
|
@ -418,7 +418,7 @@ static int run_stress_test (cv_stress_data *s, testing t,
|
|||
nsync_mu_unlock (&s->mu);
|
||||
|
||||
/* Sleep a while to cause many timeouts. */
|
||||
nsync_time_sleep (nsync_time_ms (sleep_seconds * 1000));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (sleep_seconds * 1000));
|
||||
|
||||
nsync_mu_lock (&s->mu);
|
||||
nsync_mu_assert_held (&s->mu);
|
||||
|
@ -467,7 +467,7 @@ static int run_stress_test (cv_stress_data *s, testing t,
|
|||
nsync_mu_assert_held (&s->mu);
|
||||
nsync_mu_unlock (&s->mu);
|
||||
|
||||
if (nsync_time_cmp (s->deadline, nsync_time_now ()) < 0) {
|
||||
if (nsync_time_cmp (s->deadline, nsync_time_now (NSYNC_CLOCK)) < 0) {
|
||||
if (timeouts_seen < expected_timeouts && !testing_is_uniprocessor (t)) {
|
||||
TEST_ERROR (t, ("%s: expected more than %d timeouts, got %d",
|
||||
test_name, expected_timeouts, timeouts_seen));
|
||||
|
@ -498,7 +498,7 @@ static void test_cv_timeout_stress (testing t) {
|
|||
uintmax_t loop_count = 3;
|
||||
cv_stress_data s;
|
||||
nsync_time deadline;
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (5000));
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (5000));
|
||||
do {
|
||||
bzero ((void *) &s, sizeof (s));
|
||||
s.loop_count = loop_count;
|
||||
|
@ -518,7 +518,7 @@ static void test_mu_timeout_stress (testing t) {
|
|||
uintmax_t loop_count = 3;
|
||||
cv_stress_data s;
|
||||
nsync_time deadline;
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (5000));
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (5000));
|
||||
do {
|
||||
bzero ((void *) &s, sizeof (s));
|
||||
s.loop_count = loop_count;
|
||||
|
@ -538,7 +538,7 @@ static void test_mu_cv_timeout_stress (testing t) {
|
|||
uintmax_t loop_count = 3;
|
||||
cv_stress_data s;
|
||||
nsync_time deadline;
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (5000));
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (5000));
|
||||
do {
|
||||
bzero ((void *) &s, sizeof (s));
|
||||
s.loop_count = loop_count;
|
||||
|
|
35
third_party/nsync/testing/cv_test.c
vendored
35
third_party/nsync/testing/cv_test.c
vendored
|
@ -29,7 +29,6 @@
|
|||
#include "third_party/nsync/testing/smprintf.h"
|
||||
#include "third_party/nsync/testing/testing.h"
|
||||
#include "third_party/nsync/testing/time_extra.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
|
||||
/* --------------------------- */
|
||||
|
@ -64,7 +63,7 @@ static int cv_queue_put (cv_queue *q, void *v, nsync_time abs_deadline) {
|
|||
int wake = 0;
|
||||
nsync_mu_lock (&q->mu);
|
||||
while (q->count == q->limit &&
|
||||
nsync_cv_wait_with_deadline (&q->non_full, &q->mu, CLOCK_REALTIME, abs_deadline, NULL) == 0) {
|
||||
nsync_cv_wait_with_deadline (&q->non_full, &q->mu, NSYNC_CLOCK, abs_deadline, NULL) == 0) {
|
||||
}
|
||||
if (q->count != q->limit) {
|
||||
int i = q->pos + q->count;
|
||||
|
@ -92,7 +91,7 @@ static void *cv_queue_get (cv_queue *q, nsync_time abs_deadline) {
|
|||
void *v = NULL;
|
||||
nsync_mu_lock (&q->mu);
|
||||
while (q->count == 0 &&
|
||||
nsync_cv_wait_with_deadline (&q->non_empty, &q->mu, CLOCK_REALTIME, abs_deadline, NULL) == 0) {
|
||||
nsync_cv_wait_with_deadline (&q->non_empty, &q->mu, NSYNC_CLOCK, abs_deadline, NULL) == 0) {
|
||||
}
|
||||
if (q->count != 0) {
|
||||
v = q->data[q->pos];
|
||||
|
@ -236,13 +235,13 @@ static void test_cv_deadline (testing t) {
|
|||
nsync_time end_time;
|
||||
nsync_time start_time;
|
||||
nsync_time expected_end_time;
|
||||
start_time = nsync_time_now ();
|
||||
start_time = nsync_time_now (NSYNC_CLOCK);
|
||||
expected_end_time = nsync_time_add (start_time, nsync_time_ms (87));
|
||||
if (nsync_cv_wait_with_deadline (&cv, &mu, CLOCK_REALTIME, expected_end_time,
|
||||
if (nsync_cv_wait_with_deadline (&cv, &mu, NSYNC_CLOCK, expected_end_time,
|
||||
NULL) != ETIMEDOUT) {
|
||||
TEST_FATAL (t, ("nsync_cv_wait() returned non-expired for a timeout"));
|
||||
}
|
||||
end_time = nsync_time_now ();
|
||||
end_time = nsync_time_now (NSYNC_CLOCK);
|
||||
if (nsync_time_cmp (end_time, nsync_time_sub (expected_end_time, too_early)) < 0) {
|
||||
char *elapsed_str = nsync_time_str (nsync_time_sub (expected_end_time, end_time), 2);
|
||||
TEST_ERROR (t, ("nsync_cv_wait() returned %s too early", elapsed_str));
|
||||
|
@ -275,7 +274,7 @@ static void test_cv_cancel (testing t) {
|
|||
|
||||
/* The loops below cancel after 87 milliseconds, like the timeout tests above. */
|
||||
|
||||
future_time = nsync_time_add (nsync_time_now (), nsync_time_ms (3600000)); /* test cancels with timeout */
|
||||
future_time = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (3600000)); /* test cancels with timeout */
|
||||
|
||||
too_late_violations = 0;
|
||||
nsync_mu_lock (&mu);
|
||||
|
@ -285,18 +284,18 @@ static void test_cv_cancel (testing t) {
|
|||
nsync_time end_time;
|
||||
nsync_time start_time;
|
||||
nsync_time expected_end_time;
|
||||
start_time = nsync_time_now ();
|
||||
start_time = nsync_time_now (NSYNC_CLOCK);
|
||||
expected_end_time = nsync_time_add (start_time, nsync_time_ms (87));
|
||||
|
||||
cancel = nsync_note_new (NULL, expected_end_time);
|
||||
cancel = nsync_note_new (NULL, NSYNC_CLOCK, expected_end_time);
|
||||
|
||||
x = nsync_cv_wait_with_deadline (&cv, &mu, CLOCK_REALTIME, future_time, cancel);
|
||||
x = nsync_cv_wait_with_deadline (&cv, &mu, NSYNC_CLOCK, future_time, cancel);
|
||||
if (x != ECANCELED) {
|
||||
TEST_FATAL (t, ("nsync_cv_wait() returned non-cancelled (%d) for "
|
||||
"a cancellation; expected %d",
|
||||
x, ECANCELED));
|
||||
}
|
||||
end_time = nsync_time_now ();
|
||||
end_time = nsync_time_now (NSYNC_CLOCK);
|
||||
if (nsync_time_cmp (end_time, nsync_time_sub (expected_end_time, too_early)) < 0) {
|
||||
char *elapsed_str = nsync_time_str (nsync_time_sub (expected_end_time, end_time), 2);
|
||||
TEST_ERROR (t, ("nsync_cv_wait() returned %s too early", elapsed_str));
|
||||
|
@ -307,15 +306,15 @@ static void test_cv_cancel (testing t) {
|
|||
}
|
||||
|
||||
/* Check that an already cancelled wait returns immediately. */
|
||||
start_time = nsync_time_now ();
|
||||
start_time = nsync_time_now (NSYNC_CLOCK);
|
||||
|
||||
x = nsync_cv_wait_with_deadline (&cv, &mu, CLOCK_REALTIME, nsync_time_no_deadline, cancel);
|
||||
x = nsync_cv_wait_with_deadline (&cv, &mu, NSYNC_CLOCK, nsync_time_no_deadline, cancel);
|
||||
if (x != ECANCELED) {
|
||||
TEST_FATAL (t, ("nsync_cv_wait() returned non-cancelled (%d) for "
|
||||
"a cancellation; expected %d",
|
||||
x, ECANCELED));
|
||||
}
|
||||
end_time = nsync_time_now ();
|
||||
end_time = nsync_time_now (NSYNC_CLOCK);
|
||||
if (nsync_time_cmp (end_time, start_time) < 0) {
|
||||
char *elapsed_str = nsync_time_str (nsync_time_sub (expected_end_time, end_time), 2);
|
||||
TEST_ERROR (t, ("nsync_cv_wait() returned %s too early", elapsed_str));
|
||||
|
@ -522,7 +521,7 @@ static void test_cv_debug (testing t) {
|
|||
closure_fork (closure_debug_thread (&debug_thread_writer_cv, s));
|
||||
closure_fork (closure_debug_thread (&debug_thread_writer_cv, s));
|
||||
closure_fork (closure_debug_thread_reader (&debug_thread_reader_cv, s, NULL));
|
||||
nsync_time_sleep (nsync_time_ms (500));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (500));
|
||||
*slot (s, "wait0_mu") = nsync_mu_debug_state_and_waiters (
|
||||
&s->mu, (char *) malloc (len), len);
|
||||
*slot (s, "wait0_cv") = nsync_cv_debug_state_and_waiters (
|
||||
|
@ -530,7 +529,7 @@ static void test_cv_debug (testing t) {
|
|||
|
||||
/* allow the threads to proceed to their conditional waits */
|
||||
nsync_mu_unlock (&s->mu);
|
||||
nsync_time_sleep (nsync_time_ms (500));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (500));
|
||||
*slot (s, "wait1_mu") = nsync_mu_debug_state_and_waiters (
|
||||
&s->mu, (char *) malloc (len), len);
|
||||
*slot (s, "wait1_cv") = nsync_cv_debug_state_and_waiters (
|
||||
|
@ -547,7 +546,7 @@ static void test_cv_debug (testing t) {
|
|||
/* allow all threads to proceed and exit */
|
||||
s->flag = 0;
|
||||
nsync_mu_unlock (&s->mu);
|
||||
nsync_time_sleep (nsync_time_ms (500));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (500));
|
||||
*slot (s, "wait3_mu") = nsync_mu_debug_state_and_waiters (
|
||||
&s->mu, (char *) malloc (len), len);
|
||||
*slot (s, "wait3_cv") = nsync_cv_debug_state_and_waiters (
|
||||
|
@ -559,7 +558,7 @@ static void test_cv_debug (testing t) {
|
|||
&s->mu, (char *) malloc (len), len);
|
||||
closure_fork (closure_debug_thread_reader (
|
||||
&debug_thread_reader, s, "rheld2_mu"));
|
||||
nsync_time_sleep (nsync_time_ms (500));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (500));
|
||||
*slot (s, "rheld1again_mu") = nsync_mu_debug_state_and_waiters (
|
||||
&s->mu, (char *) malloc (len), len);
|
||||
nsync_mu_runlock (&s->mu);
|
||||
|
|
10
third_party/nsync/testing/cv_wait_example_test.c
vendored
10
third_party/nsync/testing/cv_wait_example_test.c
vendored
|
@ -18,13 +18,13 @@
|
|||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/nsync/array.internal.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
#include "third_party/nsync/cv.h"
|
||||
#include "third_party/nsync/heap.internal.h"
|
||||
#include "third_party/nsync/mu.h"
|
||||
#include "third_party/nsync/testing/closure.h"
|
||||
#include "third_party/nsync/testing/smprintf.h"
|
||||
#include "third_party/nsync/testing/testing.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "third_party/nsync/testing/time_extra.h"
|
||||
|
||||
/* Example use of CV.wait(): A priority queue of strings whose
|
||||
|
@ -75,7 +75,7 @@ static const char *string_priority_queue_cv_remove_with_deadline (string_priorit
|
|||
const char *s = NULL;
|
||||
nsync_mu_lock (&q->mu);
|
||||
while (A_LEN (&q->heap) == 0 &&
|
||||
nsync_cv_wait_with_deadline (&q->non_empty, &q->mu, CLOCK_REALTIME,
|
||||
nsync_cv_wait_with_deadline (&q->non_empty, &q->mu, NSYNC_CLOCK,
|
||||
abs_deadline, NULL) == 0) {
|
||||
}
|
||||
alen = A_LEN (&q->heap);
|
||||
|
@ -101,7 +101,7 @@ static void add_and_wait_cv (string_priority_queue_cv *q, nsync_time delay,
|
|||
int i;
|
||||
for (i = 0; i != n; i++) {
|
||||
string_priority_queue_cv_add (q, s[i]);
|
||||
nsync_time_sleep (delay);
|
||||
nsync_time_sleep (NSYNC_CLOCK, delay);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -123,7 +123,7 @@ static void a_char_append (a_char *a, const char *str) {
|
|||
static void remove_and_print_cv (string_priority_queue_cv *q, nsync_time delay, a_char *output) {
|
||||
const char *s;
|
||||
if ((s = string_priority_queue_cv_remove_with_deadline (
|
||||
q, nsync_time_add (nsync_time_now(), delay))) != NULL) {
|
||||
q, nsync_time_add (nsync_time_now (NSYNC_CLOCK), delay))) != NULL) {
|
||||
a_char_append (output, s);
|
||||
a_char_append (output, "\n");
|
||||
} else {
|
||||
|
@ -157,7 +157,7 @@ static void example_cv_wait (testing t) {
|
|||
nsync_time_ms (500), NELEM (input), input));
|
||||
|
||||
/* delay: "one", "two", "three" are queued; not "four" */
|
||||
nsync_time_sleep (nsync_time_ms (1200));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (1200));
|
||||
|
||||
remove_and_print_cv (&q, nsync_time_ms (1000), &output); /* "one" */
|
||||
remove_and_print_cv (&q, nsync_time_ms (1000), &output); /* "three" (less than "two") */
|
||||
|
|
54
third_party/nsync/testing/mu_starvation_test.c
vendored
54
third_party/nsync/testing/mu_starvation_test.c
vendored
|
@ -44,7 +44,7 @@ static void starve_data_init (starve_data *sd, int threads) {
|
|||
bzero ((void *) sd, sizeof (*sd));
|
||||
sd->not_yet_started = threads;
|
||||
sd->not_yet_done = threads;
|
||||
sd->start = nsync_time_now ();
|
||||
sd->start = nsync_time_now (NSYNC_CLOCK);
|
||||
}
|
||||
|
||||
/* Loop until *cancel or deadline, and on each iteration
|
||||
|
@ -62,9 +62,9 @@ static void starve_with_readers (starve_data *sd, nsync_time period,
|
|||
sd->not_yet_started--;
|
||||
nsync_mu_unlock (&sd->control_mu);
|
||||
|
||||
for (now = nsync_time_now ();
|
||||
for (now = nsync_time_now (NSYNC_CLOCK);
|
||||
!sd->cancel && nsync_time_cmp (now, deadline) < 0;
|
||||
now = nsync_time_now ()) {
|
||||
now = nsync_time_now (NSYNC_CLOCK)) {
|
||||
uint32_t new_us;
|
||||
uint32_t now_us = (uint32_t) (nsync_time_to_dbl (nsync_time_sub (now, sd->start)) * 1e6);
|
||||
uint32_t index = (now_us + period_us - 1) / period_us;
|
||||
|
@ -72,7 +72,7 @@ static void starve_with_readers (starve_data *sd, nsync_time period,
|
|||
index++;
|
||||
}
|
||||
new_us = index * period_us;
|
||||
nsync_time_sleep (nsync_time_from_dbl (1e-6 * (double) (new_us-now_us)));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_from_dbl (1e-6 * (double) (new_us-now_us)));
|
||||
nsync_mu_runlock (&sd->mu);
|
||||
nsync_mu_rlock (&sd->mu);
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ static void test_starve_with_readers (testing t) {
|
|||
starve_data_init (&sd, 2); /* two threads, started below */
|
||||
|
||||
/* Threads run for at most 10s. */
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (10000));
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (10000));
|
||||
|
||||
/* These two threads will try to hold a reader lock
|
||||
continuously until cancel is set or deadline is reached,
|
||||
|
@ -130,9 +130,9 @@ static void test_starve_with_readers (testing t) {
|
|||
|
||||
/* If using an nsync_mu, use nsync_mu_trylock() to attempt to acquire while the
|
||||
readers are hogging the lock. We expect no acquisitions to succeed. */
|
||||
finish = nsync_time_add (nsync_time_now (), nsync_time_ms (500));
|
||||
finish = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (500));
|
||||
trylock_acquires = 0; /* number of acquires */
|
||||
while (nsync_time_cmp (nsync_time_now (), finish) < 0) {
|
||||
while (nsync_time_cmp (nsync_time_now (NSYNC_CLOCK), finish) < 0) {
|
||||
if (nsync_mu_trylock (&sd.mu)) {
|
||||
trylock_acquires++;
|
||||
nsync_mu_unlock (&sd.mu);
|
||||
|
@ -147,15 +147,15 @@ static void test_starve_with_readers (testing t) {
|
|||
/* Use nsync_mu_lock() to attempt to acquire while the readers are hogging
|
||||
the lock. We expect several acquisitions to succeed. */
|
||||
expected_lo = 2;
|
||||
finish = nsync_time_add (nsync_time_now (), nsync_time_ms (5000));
|
||||
finish = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (5000));
|
||||
lock_acquires = 0; /* number of acquires */
|
||||
while (nsync_time_cmp (nsync_time_now (), finish) < 0 && lock_acquires < expected_lo) {
|
||||
while (nsync_time_cmp (nsync_time_now (NSYNC_CLOCK), finish) < 0 && lock_acquires < expected_lo) {
|
||||
nsync_mu_lock (&sd.mu);
|
||||
lock_acquires++;
|
||||
nsync_mu_unlock (&sd.mu);
|
||||
nsync_time_sleep (nsync_time_ms (1));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (1));
|
||||
}
|
||||
if (nsync_time_cmp (nsync_time_now (), deadline) > 0 && lock_acquires == 1) {
|
||||
if (nsync_time_cmp (nsync_time_now (NSYNC_CLOCK), deadline) > 0 && lock_acquires == 1) {
|
||||
lock_acquires = 0; /* hog threads timed out */
|
||||
}
|
||||
if (lock_acquires < expected_lo) {
|
||||
|
@ -185,10 +185,10 @@ static void starve_with_writer (starve_data *sd, nsync_time hold_time,
|
|||
sd->not_yet_started--;
|
||||
nsync_mu_unlock (&sd->control_mu);
|
||||
|
||||
for (now = nsync_time_now ();
|
||||
for (now = nsync_time_now (NSYNC_CLOCK);
|
||||
!sd->cancel && nsync_time_cmp (now, deadline) < 0;
|
||||
now = nsync_time_now ()) {
|
||||
nsync_time_sleep (hold_time);
|
||||
now = nsync_time_now (NSYNC_CLOCK)) {
|
||||
nsync_time_sleep (NSYNC_CLOCK, hold_time);
|
||||
nsync_mu_unlock (&sd->mu);
|
||||
nsync_mu_lock (&sd->mu);
|
||||
}
|
||||
|
@ -231,7 +231,7 @@ static void test_starve_with_writer (testing t) {
|
|||
nsync_time deadline;
|
||||
starve_data sd;
|
||||
starve_data_init (&sd, 1); /* one thread, started below */
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (25000)); /* runs for at most 25s. */
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (25000)); /* runs for at most 25s. */
|
||||
|
||||
/* This thread will try to hold a writer lock almost
|
||||
continuously, releasing momentarily every 10ms. */
|
||||
|
@ -249,9 +249,9 @@ static void test_starve_with_writer (testing t) {
|
|||
/* Use nsync_mu_trylock() to attempt to acquire while the writer is hogging the
|
||||
lock. We expect some acquisitions to succeed. */
|
||||
expected_lo = 1;
|
||||
finish = nsync_time_add (nsync_time_now (), nsync_time_ms (30000));
|
||||
finish = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (30000));
|
||||
trylock_acquires = 0; /* number of acquires */
|
||||
while (nsync_time_cmp (nsync_time_now (), finish) < 0 && trylock_acquires < expected_lo) {
|
||||
while (nsync_time_cmp (nsync_time_now (NSYNC_CLOCK), finish) < 0 && trylock_acquires < expected_lo) {
|
||||
if (nsync_mu_trylock (&sd.mu)) {
|
||||
trylock_acquires++;
|
||||
nsync_mu_unlock (&sd.mu);
|
||||
|
@ -269,9 +269,9 @@ static void test_starve_with_writer (testing t) {
|
|||
/* Use nsync_mu_rtrylock() to attempt to read-acquire while the writer is
|
||||
hogging the lock. We expect some acquisitions to succeed. */
|
||||
expected_lo = 1;
|
||||
finish = nsync_time_add (nsync_time_now (), nsync_time_ms (30000));
|
||||
finish = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (30000));
|
||||
rtrylock_acquires = 0; /* number of acquires */
|
||||
while (nsync_time_cmp (nsync_time_now (), finish) < 0 && rtrylock_acquires < expected_lo) {
|
||||
while (nsync_time_cmp (nsync_time_now (NSYNC_CLOCK), finish) < 0 && rtrylock_acquires < expected_lo) {
|
||||
if (nsync_mu_rtrylock (&sd.mu)) {
|
||||
rtrylock_acquires++;
|
||||
nsync_mu_runlock (&sd.mu);
|
||||
|
@ -288,15 +288,15 @@ static void test_starve_with_writer (testing t) {
|
|||
/* Use nsync_mu_lock() to attempt to acquire while the writer is hogging
|
||||
the lock. We expect several acquisitions to succeed. */
|
||||
expected_lo = 2;
|
||||
finish = nsync_time_add (nsync_time_now (), nsync_time_ms (5000));
|
||||
finish = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (5000));
|
||||
lock_acquires = 0; /* number of acquires */
|
||||
while (nsync_time_cmp (nsync_time_now (), finish) < 0 && lock_acquires < expected_lo) {
|
||||
while (nsync_time_cmp (nsync_time_now (NSYNC_CLOCK), finish) < 0 && lock_acquires < expected_lo) {
|
||||
nsync_mu_lock (&sd.mu);
|
||||
lock_acquires++;
|
||||
nsync_mu_unlock (&sd.mu);
|
||||
nsync_time_sleep (nsync_time_ms (2));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (2));
|
||||
}
|
||||
if (lock_acquires == 1 && nsync_time_cmp (nsync_time_now (), deadline) > 0) {
|
||||
if (lock_acquires == 1 && nsync_time_cmp (nsync_time_now (NSYNC_CLOCK), deadline) > 0) {
|
||||
lock_acquires = 0; /* hog thread timed out */
|
||||
}
|
||||
if (lock_acquires < expected_lo) {
|
||||
|
@ -310,16 +310,16 @@ static void test_starve_with_writer (testing t) {
|
|||
time----it means that a writer couldn't break in (the test case
|
||||
above failed), so a reader is unlikely to manage it either. */
|
||||
expected_lo = 2;
|
||||
finish = nsync_time_add (nsync_time_now (), nsync_time_ms (5000));
|
||||
finish = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (5000));
|
||||
rlock_acquires = 0; /* number of acquires */
|
||||
if (nsync_time_cmp (finish, deadline) < 0) {
|
||||
while (nsync_time_cmp (nsync_time_now (), finish) < 0 && rlock_acquires < expected_lo) {
|
||||
while (nsync_time_cmp (nsync_time_now (NSYNC_CLOCK), finish) < 0 && rlock_acquires < expected_lo) {
|
||||
nsync_mu_rlock (&sd.mu);
|
||||
rlock_acquires++;
|
||||
nsync_mu_runlock (&sd.mu);
|
||||
nsync_time_sleep (nsync_time_ms (2));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (2));
|
||||
}
|
||||
if (rlock_acquires == 1 && nsync_time_cmp (nsync_time_now (), deadline) > 0) {
|
||||
if (rlock_acquires == 1 && nsync_time_cmp (nsync_time_now (NSYNC_CLOCK), deadline) > 0) {
|
||||
rlock_acquires = 0; /* hog thread timed out */
|
||||
}
|
||||
if (rlock_acquires < expected_lo) {
|
||||
|
|
44
third_party/nsync/testing/mu_test.c
vendored
44
third_party/nsync/testing/mu_test.c
vendored
|
@ -19,12 +19,12 @@
|
|||
#include "libc/calls/calls.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
#include "third_party/nsync/cv.h"
|
||||
#include "third_party/nsync/mu_wait.h"
|
||||
#include "third_party/nsync/testing/closure.h"
|
||||
#include "third_party/nsync/testing/smprintf.h"
|
||||
#include "third_party/nsync/testing/testing.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "third_party/nsync/testing/time_extra.h"
|
||||
|
||||
/* The state shared between the threads in each of the tests below. */
|
||||
|
@ -68,7 +68,7 @@ static void test_data_wait_for_all_threads (test_data *td) {
|
|||
while (td->finished_threads != td->n_threads) {
|
||||
nsync_cv_wait_with_deadline_generic (&td->done, td->mu_in_use,
|
||||
td->lock, td->unlock,
|
||||
CLOCK_REALTIME,
|
||||
NSYNC_CLOCK,
|
||||
nsync_time_no_deadline, NULL);
|
||||
}
|
||||
(*td->unlock) (td->mu_in_use);
|
||||
|
@ -113,7 +113,7 @@ static void void_mu_unlock (void *mu) {
|
|||
static void test_mu_nthread (testing t) {
|
||||
int loop_count = 100000;
|
||||
nsync_time deadline;
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (1500));
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1500));
|
||||
do {
|
||||
int i;
|
||||
test_data td;
|
||||
|
@ -133,7 +133,7 @@ static void test_mu_nthread (testing t) {
|
|||
td.n_threads*td.loop_count, td.i));
|
||||
}
|
||||
loop_count *= 2;
|
||||
} while (nsync_time_cmp (nsync_time_now (), deadline) < 0);
|
||||
} while (nsync_time_cmp (nsync_time_now (NSYNC_CLOCK), deadline) < 0);
|
||||
}
|
||||
|
||||
/* void pthread_mutex_lock */
|
||||
|
@ -152,7 +152,7 @@ static void void_pthread_mutex_unlock (void *mu) {
|
|||
static void test_mutex_nthread (testing t) {
|
||||
int loop_count = 100000;
|
||||
nsync_time deadline;
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (1500));
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1500));
|
||||
do {
|
||||
int i;
|
||||
test_data td;
|
||||
|
@ -174,7 +174,7 @@ static void test_mutex_nthread (testing t) {
|
|||
}
|
||||
pthread_mutex_destroy (&td.mutex);
|
||||
loop_count *= 2;
|
||||
} while (nsync_time_cmp (nsync_time_now (), deadline) < 0);
|
||||
} while (nsync_time_cmp (nsync_time_now (NSYNC_CLOCK), deadline) < 0);
|
||||
}
|
||||
|
||||
/* void pthread_rwlock_wrlock */
|
||||
|
@ -193,7 +193,7 @@ static void void_pthread_rwlock_unlock (void *mu) {
|
|||
static void test_rwmutex_nthread (testing t) {
|
||||
int loop_count = 100000;
|
||||
nsync_time deadline;
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (1500));
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1500));
|
||||
do {
|
||||
int i;
|
||||
test_data td;
|
||||
|
@ -215,7 +215,7 @@ static void test_rwmutex_nthread (testing t) {
|
|||
}
|
||||
pthread_rwlock_destroy (&td.rwmutex);
|
||||
loop_count *= 2;
|
||||
} while (nsync_time_cmp (nsync_time_now (), deadline) < 0);
|
||||
} while (nsync_time_cmp (nsync_time_now (NSYNC_CLOCK), deadline) < 0);
|
||||
}
|
||||
|
||||
/* --------------------------------------- */
|
||||
|
@ -246,7 +246,7 @@ static void counting_loop_try_mu (test_data *td, int id) {
|
|||
static void test_try_mu_nthread (testing t) {
|
||||
int loop_count = 100000;
|
||||
nsync_time deadline;
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (1500));
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1500));
|
||||
do {
|
||||
int i;
|
||||
test_data td;
|
||||
|
@ -266,7 +266,7 @@ static void test_try_mu_nthread (testing t) {
|
|||
td.n_threads*td.loop_count, td.i));
|
||||
}
|
||||
loop_count *= 2;
|
||||
} while (nsync_time_cmp (nsync_time_now (), deadline) < 0);
|
||||
} while (nsync_time_cmp (nsync_time_now (NSYNC_CLOCK), deadline) < 0);
|
||||
}
|
||||
|
||||
/* --------------------------------------- */
|
||||
|
@ -305,7 +305,7 @@ static int counter_wait_for_zero_with_deadline (counter *c, nsync_time abs_deadl
|
|||
int value;
|
||||
nsync_mu_rlock (&c->mu);
|
||||
while (c->value != 0 &&
|
||||
nsync_cv_wait_with_deadline (&c->cv, &c->mu, CLOCK_REALTIME, abs_deadline, NULL) == 0) {
|
||||
nsync_cv_wait_with_deadline (&c->cv, &c->mu, NSYNC_CLOCK, abs_deadline, NULL) == 0) {
|
||||
}
|
||||
value = c->value;
|
||||
nsync_mu_runlock (&c->mu);
|
||||
|
@ -429,7 +429,7 @@ static void lock_unlock (testing t, const char *id, int verbose, nsync_mu *mu, i
|
|||
if (verbose) {
|
||||
TEST_LOG (t, ("lock_unlock %s incremented value to %d\n", id, *value));
|
||||
}
|
||||
nsync_time_sleep (sleep);
|
||||
nsync_time_sleep (NSYNC_CLOCK, sleep);
|
||||
nsync_mu_unlock (mu);
|
||||
counter_inc (done, -1);
|
||||
}
|
||||
|
@ -453,7 +453,7 @@ static void rlock_runlock (testing t, const char *id, int verbose, nsync_mu *mu,
|
|||
testing_panic (smprintf ("rlock_runlock %s expected "
|
||||
"value %d, *value=%d", id, expected_value, *value));
|
||||
}
|
||||
nsync_time_sleep (sleep);
|
||||
nsync_time_sleep (NSYNC_CLOCK, sleep);
|
||||
nsync_mu_runlock (mu);
|
||||
counter_inc (done, -1);
|
||||
}
|
||||
|
@ -465,7 +465,7 @@ static int check_times (testing t, const char *id, nsync_time start_time,
|
|||
int exceeds_count = 0;
|
||||
nsync_time now;
|
||||
nsync_time measured_duration;
|
||||
now = nsync_time_now ();
|
||||
now = nsync_time_now (NSYNC_CLOCK);
|
||||
measured_duration = nsync_time_sub (now, start_time);
|
||||
if (nsync_time_cmp (measured_duration,
|
||||
nsync_time_sub (expected_duration, nsync_time_ms (5))) < 0) {
|
||||
|
@ -520,7 +520,7 @@ static void test_rlock (testing t) {
|
|||
|
||||
nsync_time start_time;
|
||||
nsync_mu_init (&mu);
|
||||
start_time = nsync_time_now ();
|
||||
start_time = nsync_time_now (NSYNC_CLOCK);
|
||||
|
||||
/* ------------------------------------ */
|
||||
/* Acquire lock with nsync_mu_rtrylock(). This thread will
|
||||
|
@ -564,7 +564,7 @@ static void test_rlock (testing t) {
|
|||
lock_unlock_sleeping, lock_unlock_done));
|
||||
|
||||
counter_wait_for_zero (lock_unlock_sleeping);
|
||||
nsync_time_sleep (delay_duration); /* give time for lock_unlock() thread to wait. */
|
||||
nsync_time_sleep (NSYNC_CLOCK, delay_duration); /* give time for lock_unlock() thread to wait. */
|
||||
|
||||
nsync_mu_rassert_held (&mu);
|
||||
|
||||
|
@ -581,7 +581,7 @@ static void test_rlock (testing t) {
|
|||
nsync_mu_rassert_held (&mu);
|
||||
|
||||
counter_wait_for_zero (rlock_runlock_sleeping);
|
||||
nsync_time_sleep (delay_duration); /* time for rlock_runlock() threads to wait. */
|
||||
nsync_time_sleep (NSYNC_CLOCK, delay_duration); /* time for rlock_runlock() threads to wait. */
|
||||
|
||||
nsync_mu_rassert_held (&mu);
|
||||
|
||||
|
@ -610,7 +610,7 @@ static void test_rlock (testing t) {
|
|||
nsync_mu_runlock (&mu);
|
||||
/* ==================================== */
|
||||
|
||||
read_start_time = nsync_time_now ();
|
||||
read_start_time = nsync_time_now (NSYNC_CLOCK);
|
||||
counter_wait_for_zero (lock_unlock_done); /* Now can get write lock. */
|
||||
max_write_wait_exceeded += check_times (t, "i", start_time,
|
||||
nsync_time_add (nsync_time_add (delay_duration, delay_duration), writer_duration),
|
||||
|
@ -656,7 +656,7 @@ static void test_rlock (testing t) {
|
|||
nsync_time start_time;
|
||||
|
||||
nsync_mu_init (&mu);
|
||||
start_time = nsync_time_now ();
|
||||
start_time = nsync_time_now (NSYNC_CLOCK);
|
||||
|
||||
/* ------------------------------------ */
|
||||
/* Acquire lock with nsync_mu_trylock(). This thread will hold
|
||||
|
@ -696,7 +696,7 @@ static void test_rlock (testing t) {
|
|||
lock_unlock_sleeping, lock_unlock_done));
|
||||
|
||||
counter_wait_for_zero (lock_unlock_sleeping);
|
||||
nsync_time_sleep (delay_duration); /* give time for lock_unlock() thread to wait. */
|
||||
nsync_time_sleep (NSYNC_CLOCK, delay_duration); /* give time for lock_unlock() thread to wait. */
|
||||
|
||||
nsync_mu_assert_held (&mu);
|
||||
nsync_mu_rassert_held (&mu);
|
||||
|
@ -715,7 +715,7 @@ static void test_rlock (testing t) {
|
|||
nsync_mu_rassert_held (&mu);
|
||||
|
||||
counter_wait_for_zero (rlock_runlock_sleeping);
|
||||
nsync_time_sleep (delay_duration); /* time for rlock_runlock() threads to wait. */
|
||||
nsync_time_sleep (NSYNC_CLOCK, delay_duration); /* time for rlock_runlock() threads to wait. */
|
||||
|
||||
nsync_mu_assert_held (&mu);
|
||||
nsync_mu_rassert_held (&mu);
|
||||
|
@ -753,7 +753,7 @@ static void test_rlock (testing t) {
|
|||
nsync_mu_unlock (&mu);
|
||||
/* ==================================== */
|
||||
|
||||
read_start_time = nsync_time_now ();
|
||||
read_start_time = nsync_time_now (NSYNC_CLOCK);
|
||||
counter_wait_for_zero (lock_unlock_done); /* Now can get write lock. */
|
||||
max_write_wait_exceeded += check_times (t, "H", start_time,
|
||||
nsync_time_add (nsync_time_add (delay_duration, delay_duration), writer_duration),
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/nsync/array.internal.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
#include "third_party/nsync/heap.internal.h"
|
||||
#include "third_party/nsync/mu.h"
|
||||
#include "third_party/nsync/mu_wait.h"
|
||||
|
@ -74,7 +75,7 @@ static const char *string_priority_queue_mu_remove_with_deadline (
|
|||
const char *s = NULL;
|
||||
nsync_mu_lock (&q->mu);
|
||||
if (nsync_mu_wait_with_deadline (&q->mu, &spq_is_non_empty, q, NULL,
|
||||
abs_deadline, NULL) == 0) {
|
||||
NSYNC_CLOCK, abs_deadline, NULL) == 0) {
|
||||
int alen = A_LEN (&q->heap);
|
||||
if (alen != 0) {
|
||||
s = A (&q->heap, 0);
|
||||
|
@ -99,7 +100,7 @@ static void add_and_wait_mu (string_priority_queue_mu *q,
|
|||
int i;
|
||||
for (i = 0; i != n; i++) {
|
||||
string_priority_queue_mu_add (q, s[i]);
|
||||
nsync_time_sleep (delay);
|
||||
nsync_time_sleep (NSYNC_CLOCK, delay);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -120,7 +121,7 @@ static void a_char_append (a_char *a, const char *str) {
|
|||
static void remove_and_print_mu (string_priority_queue_mu *q, nsync_time delay, a_char *output) {
|
||||
const char *s;
|
||||
if ((s = string_priority_queue_mu_remove_with_deadline (q,
|
||||
nsync_time_add (nsync_time_now (), delay))) != NULL) {
|
||||
nsync_time_add (nsync_time_now (NSYNC_CLOCK), delay))) != NULL) {
|
||||
a_char_append (output, s);
|
||||
a_char_append (output, "\n");
|
||||
} else {
|
||||
|
@ -154,7 +155,7 @@ static void example_mu_wait (testing t) {
|
|||
NELEM (input), input));
|
||||
|
||||
/* delay: "one", "two", "three"; not yet "four" */
|
||||
nsync_time_sleep (nsync_time_ms (1200));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (1200));
|
||||
|
||||
remove_and_print_mu (&q, nsync_time_ms (1000), &output); /* "one" */
|
||||
remove_and_print_mu (&q, nsync_time_ms (1000), &output); /* "three" (less than "two") */
|
||||
|
|
32
third_party/nsync/testing/mu_wait_test.c
vendored
32
third_party/nsync/testing/mu_wait_test.c
vendored
|
@ -18,13 +18,13 @@
|
|||
#include "third_party/nsync/mu_wait.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
#include "third_party/nsync/mu.h"
|
||||
#include "third_party/nsync/note.h"
|
||||
#include "third_party/nsync/testing/closure.h"
|
||||
#include "third_party/nsync/testing/smprintf.h"
|
||||
#include "third_party/nsync/testing/testing.h"
|
||||
#include "third_party/nsync/testing/time_extra.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
|
||||
/* --------------------------- */
|
||||
|
||||
|
@ -64,7 +64,7 @@ static int mu_queue_put (mu_queue *q, void *v, nsync_time abs_deadline) {
|
|||
int added = 0;
|
||||
nsync_mu_lock (&q->mu);
|
||||
if (nsync_mu_wait_with_deadline (&q->mu, &mu_queue_non_full,
|
||||
q, NULL, abs_deadline, NULL) == 0) {
|
||||
q, NULL, 0, abs_deadline, NULL) == 0) {
|
||||
int i = q->pos + q->count;
|
||||
if (q->count == q->limit) {
|
||||
testing_panic ("q->count == q->limit");
|
||||
|
@ -87,7 +87,8 @@ static void *mu_queue_get (mu_queue *q, nsync_time abs_deadline) {
|
|||
void *v = NULL;
|
||||
nsync_mu_lock (&q->mu);
|
||||
if (nsync_mu_wait_with_deadline (&q->mu, &mu_queue_non_empty,
|
||||
q, NULL, abs_deadline, NULL) == 0) {
|
||||
q, NULL, NSYNC_CLOCK,
|
||||
abs_deadline, NULL) == 0) {
|
||||
if (q->count == 0) {
|
||||
testing_panic ("q->count == 0");
|
||||
}
|
||||
|
@ -228,18 +229,18 @@ static void test_mu_deadline (testing t) {
|
|||
too_early = nsync_time_ms (TOO_EARLY_MS);
|
||||
too_late = nsync_time_ms (TOO_LATE_MS);
|
||||
too_late_violations = 0;
|
||||
nsync_mu_lock (&mu);;
|
||||
nsync_mu_lock (&mu);
|
||||
for (i = 0; i != 50; i++) {
|
||||
nsync_time end_time;
|
||||
nsync_time start_time;
|
||||
nsync_time expected_end_time;
|
||||
start_time = nsync_time_now ();
|
||||
start_time = nsync_time_now (NSYNC_CLOCK);
|
||||
expected_end_time = nsync_time_add (start_time, nsync_time_ms (87));
|
||||
if (nsync_mu_wait_with_deadline (&mu, &false_condition, NULL, NULL,
|
||||
if (nsync_mu_wait_with_deadline (&mu, &false_condition, NULL, NULL, NSYNC_CLOCK,
|
||||
expected_end_time, NULL) != ETIMEDOUT) {
|
||||
TEST_FATAL (t, ("nsync_mu_wait() returned non-expired for a timeout"));
|
||||
}
|
||||
end_time = nsync_time_now ();
|
||||
end_time = nsync_time_now (NSYNC_CLOCK);
|
||||
if (nsync_time_cmp (end_time, nsync_time_sub (expected_end_time, too_early)) < 0) {
|
||||
char *elapsed_str = nsync_time_str (nsync_time_sub (expected_end_time, end_time), 2);
|
||||
TEST_ERROR (t, ("nsync_mu_wait() returned %s too early", elapsed_str));
|
||||
|
@ -271,7 +272,7 @@ static void test_mu_cancel (testing t) {
|
|||
|
||||
/* The loops below cancel after 87 milliseconds, like the timeout tests above. */
|
||||
|
||||
future_time = nsync_time_add (nsync_time_now (), nsync_time_ms (3600000)); /* test cancels with timeout */
|
||||
future_time = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (3600000)); /* test cancels with timeout */
|
||||
|
||||
too_late_violations = 0;
|
||||
nsync_mu_lock (&mu);
|
||||
|
@ -282,18 +283,18 @@ static void test_mu_cancel (testing t) {
|
|||
int x;
|
||||
nsync_note cancel;
|
||||
|
||||
start_time = nsync_time_now ();
|
||||
start_time = nsync_time_now (NSYNC_CLOCK);
|
||||
expected_end_time = nsync_time_add (start_time, nsync_time_ms (87));
|
||||
cancel = nsync_note_new (NULL, expected_end_time);
|
||||
cancel = nsync_note_new (NULL, NSYNC_CLOCK, expected_end_time);
|
||||
|
||||
x = nsync_mu_wait_with_deadline (&mu, &false_condition, NULL, NULL,
|
||||
future_time, cancel);
|
||||
NSYNC_CLOCK, future_time, cancel);
|
||||
if (x != ECANCELED) {
|
||||
TEST_FATAL (t, ("nsync_mu_wait() return non-cancelled (%d) for "
|
||||
"a cancellation; expected %d",
|
||||
x, ECANCELED));
|
||||
}
|
||||
end_time = nsync_time_now ();
|
||||
end_time = nsync_time_now (NSYNC_CLOCK);
|
||||
if (nsync_time_cmp (end_time, nsync_time_sub (expected_end_time, too_early)) < 0) {
|
||||
char *elapsed_str = nsync_time_str (nsync_time_sub (expected_end_time, end_time), 2);
|
||||
TEST_ERROR (t, ("nsync_mu_wait() returned %s too early", elapsed_str));
|
||||
|
@ -304,15 +305,16 @@ static void test_mu_cancel (testing t) {
|
|||
}
|
||||
|
||||
/* Check that an already cancelled wait returns immediately. */
|
||||
start_time = nsync_time_now ();
|
||||
start_time = nsync_time_now (NSYNC_CLOCK);
|
||||
x = nsync_mu_wait_with_deadline (&mu, &false_condition, NULL, NULL,
|
||||
nsync_time_no_deadline, cancel);
|
||||
NSYNC_CLOCK, nsync_time_no_deadline,
|
||||
cancel);
|
||||
if (x != ECANCELED) {
|
||||
TEST_FATAL (t, ("nsync_mu_wait() returned non-cancelled for a "
|
||||
"cancellation; expected %d",
|
||||
x, ECANCELED));
|
||||
}
|
||||
end_time = nsync_time_now ();
|
||||
end_time = nsync_time_now (NSYNC_CLOCK);
|
||||
if (nsync_time_cmp (end_time, start_time) < 0) {
|
||||
char *elapsed_str = nsync_time_str (nsync_time_sub (expected_end_time, end_time), 2);
|
||||
TEST_ERROR (t, ("nsync_mu_wait() returned %s too early", elapsed_str));
|
||||
|
|
64
third_party/nsync/testing/note_test.c
vendored
64
third_party/nsync/testing/note_test.c
vendored
|
@ -25,7 +25,7 @@
|
|||
/* Verify the properties of a prenotified note. */
|
||||
static void test_note_prenotified (testing t) {
|
||||
int i;
|
||||
nsync_note n = nsync_note_new (NULL, nsync_time_zero /* prenotified */);
|
||||
nsync_note n = nsync_note_new (NULL, NSYNC_CLOCK, nsync_time_zero /* prenotified */);
|
||||
nsync_time expiry;
|
||||
expiry = nsync_note_expiry (n);
|
||||
if (nsync_time_cmp (expiry, nsync_time_zero) != 0) {
|
||||
|
@ -55,7 +55,7 @@ static void test_note_unnotified (testing t) {
|
|||
nsync_time start;
|
||||
nsync_time waited;
|
||||
nsync_time deadline;
|
||||
nsync_note n = nsync_note_new (NULL, nsync_time_no_deadline);
|
||||
nsync_note n = nsync_note_new (NULL, NSYNC_CLOCK, nsync_time_no_deadline);
|
||||
nsync_time expiry;
|
||||
expiry = nsync_note_expiry (n);
|
||||
if (nsync_time_cmp (expiry, nsync_time_no_deadline) != 0) {
|
||||
|
@ -68,12 +68,12 @@ static void test_note_unnotified (testing t) {
|
|||
if (nsync_note_wait (n, nsync_time_zero)) {
|
||||
TEST_ERROR (t, ("notified note is notified (poll)"));
|
||||
}
|
||||
start = nsync_time_now ();
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (1000));
|
||||
start = nsync_time_now (NSYNC_CLOCK);
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000));
|
||||
if (nsync_note_wait (n, deadline)) {
|
||||
TEST_ERROR (t, ("unnotified note is notified (1s wait)"));
|
||||
}
|
||||
waited = nsync_time_sub (nsync_time_now (), start);
|
||||
waited = nsync_time_sub (nsync_time_now (NSYNC_CLOCK), start);
|
||||
if (nsync_time_cmp (waited, nsync_time_ms (900)) < 0) {
|
||||
TEST_ERROR (t, ("timed wait on unnotified note returned too quickly (1s wait took %s)",
|
||||
nsync_time_str (waited, 2)));
|
||||
|
@ -110,13 +110,13 @@ static void test_note_expiry (testing t) {
|
|||
nsync_time deadline;
|
||||
nsync_note n;
|
||||
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (1000));
|
||||
n = nsync_note_new (NULL, deadline);
|
||||
start = nsync_time_now ();
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000));
|
||||
n = nsync_note_new (NULL, NSYNC_CLOCK, deadline);
|
||||
start = nsync_time_now (NSYNC_CLOCK);
|
||||
if (!nsync_note_wait (n, nsync_time_no_deadline)) {
|
||||
TEST_ERROR (t, ("expired note is not notified"));
|
||||
}
|
||||
waited = nsync_time_sub (nsync_time_now (), start);
|
||||
waited = nsync_time_sub (nsync_time_now (NSYNC_CLOCK), start);
|
||||
if (nsync_time_cmp (waited, nsync_time_ms (900)) < 0) {
|
||||
TEST_ERROR (t, ("note expired too quickly (1s expiry took %s)",
|
||||
nsync_time_str (waited, 2)));
|
||||
|
@ -136,13 +136,13 @@ static void test_note_expiry (testing t) {
|
|||
}
|
||||
nsync_note_free (n);
|
||||
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (1000));
|
||||
n = nsync_note_new (NULL, deadline);
|
||||
start = nsync_time_now ();
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000));
|
||||
n = nsync_note_new (NULL, NSYNC_CLOCK, deadline);
|
||||
start = nsync_time_now (NSYNC_CLOCK);
|
||||
while (!nsync_note_is_notified (n)) {
|
||||
nsync_time_sleep (nsync_time_ms (10));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (10));
|
||||
}
|
||||
waited = nsync_time_sub (nsync_time_now (), start);
|
||||
waited = nsync_time_sub (nsync_time_now (NSYNC_CLOCK), start);
|
||||
if (nsync_time_cmp (waited, nsync_time_ms (900)) < 0) {
|
||||
TEST_ERROR (t, ("note expired too quickly (1s expiry took %s)",
|
||||
nsync_time_str (waited, 2)));
|
||||
|
@ -164,7 +164,7 @@ static void test_note_expiry (testing t) {
|
|||
}
|
||||
|
||||
static void notify_at (nsync_note n, nsync_time abs_deadline) {
|
||||
nsync_time_sleep_until (abs_deadline);
|
||||
nsync_time_sleep_until (NSYNC_CLOCK, abs_deadline);
|
||||
nsync_note_notify (n);
|
||||
}
|
||||
|
||||
|
@ -177,14 +177,14 @@ static void test_note_notify (testing t) {
|
|||
nsync_time deadline;
|
||||
nsync_note n;
|
||||
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (10000));
|
||||
n = nsync_note_new (NULL, deadline);
|
||||
closure_fork (closure_notify (¬ify_at, n, nsync_time_add (nsync_time_now (), nsync_time_ms (1000))));
|
||||
start = nsync_time_now ();
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (10000));
|
||||
n = nsync_note_new (NULL, NSYNC_CLOCK, deadline);
|
||||
closure_fork (closure_notify (¬ify_at, n, nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000))));
|
||||
start = nsync_time_now (NSYNC_CLOCK);
|
||||
if (!nsync_note_wait (n, nsync_time_no_deadline)) {
|
||||
TEST_ERROR (t, ("expired note is not notified"));
|
||||
}
|
||||
waited = nsync_time_sub (nsync_time_now (), start);
|
||||
waited = nsync_time_sub (nsync_time_now (NSYNC_CLOCK), start);
|
||||
if (nsync_time_cmp (waited, nsync_time_ms (900)) < 0) {
|
||||
TEST_ERROR (t, ("note expired too quickly (1s expiry took %s)",
|
||||
nsync_time_str (waited, 2)));
|
||||
|
@ -204,14 +204,14 @@ static void test_note_notify (testing t) {
|
|||
}
|
||||
nsync_note_free (n);
|
||||
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (10000));
|
||||
n = nsync_note_new (NULL, deadline);
|
||||
closure_fork (closure_notify (¬ify_at, n, nsync_time_add (nsync_time_now (), nsync_time_ms (1000))));
|
||||
start = nsync_time_now ();
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (10000));
|
||||
n = nsync_note_new (NULL, NSYNC_CLOCK, deadline);
|
||||
closure_fork (closure_notify (¬ify_at, n, nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000))));
|
||||
start = nsync_time_now (NSYNC_CLOCK);
|
||||
while (!nsync_note_is_notified (n)) {
|
||||
nsync_time_sleep (nsync_time_ms (10));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (10));
|
||||
}
|
||||
waited = nsync_time_sub (nsync_time_now (), start);
|
||||
waited = nsync_time_sub (nsync_time_now (NSYNC_CLOCK), start);
|
||||
if (nsync_time_cmp (waited, nsync_time_ms (900)) < 0) {
|
||||
TEST_ERROR (t, ("note expired too quickly (1s expiry took %s)",
|
||||
nsync_time_str (waited, 2)));
|
||||
|
@ -253,9 +253,9 @@ static void test_note_in_tree (testing t) {
|
|||
nsync_note node[count_i];
|
||||
|
||||
/* Initialize heap structure in the nodes. No deadlines. */
|
||||
node[0] = nsync_note_new (NULL, nsync_time_no_deadline);
|
||||
node[0] = nsync_note_new (NULL, NSYNC_CLOCK, nsync_time_no_deadline);
|
||||
for (i = 1; i != count_i; i++) {
|
||||
node[i] = nsync_note_new (node[(i-1)/2], nsync_time_no_deadline);
|
||||
node[i] = nsync_note_new (node[(i-1)/2], NSYNC_CLOCK, nsync_time_no_deadline);
|
||||
}
|
||||
|
||||
/* check that the nodes are not yet notified. */
|
||||
|
@ -285,14 +285,14 @@ static void test_note_in_tree (testing t) {
|
|||
}
|
||||
|
||||
/* Initialize heap structure in the nodes. The focus node has a 1s deadline. */
|
||||
node[0] = nsync_note_new (NULL, nsync_time_no_deadline);
|
||||
node[0] = nsync_note_new (NULL, NSYNC_CLOCK, nsync_time_no_deadline);
|
||||
for (i = 1; i != count_i; i++) {
|
||||
nsync_time deadline;
|
||||
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (1000));
|
||||
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000));
|
||||
if (i != focus_i) {
|
||||
deadline = nsync_time_no_deadline;
|
||||
}
|
||||
node[i] = nsync_note_new (node[(i - 1) / 2], deadline);
|
||||
node[i] = nsync_note_new (node[(i - 1) / 2], NSYNC_CLOCK, deadline);
|
||||
}
|
||||
|
||||
/* check that the nodes are not yet notified. */
|
||||
|
@ -303,7 +303,7 @@ static void test_note_in_tree (testing t) {
|
|||
}
|
||||
|
||||
/* Wait for timer to go off. */
|
||||
nsync_time_sleep (nsync_time_ms (1100));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (1100));
|
||||
|
||||
/* Check that the right nodes have been notified. */
|
||||
for (i = 0; i != count_i; i++) {
|
||||
|
|
4
third_party/nsync/testing/once_test.c
vendored
4
third_party/nsync/testing/once_test.c
vendored
|
@ -76,7 +76,7 @@ static void once_thread (struct once_test_thread_s *lott) {
|
|||
nsync_mu_lock (&ott_s_mu);
|
||||
s = lott->s;
|
||||
nsync_mu_unlock (&ott_s_mu);
|
||||
nsync_time_sleep (nsync_time_s_ns (0, 1 * 1000 * 1000));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_s_ns (0, 1 * 1000 * 1000));
|
||||
switch (lott->id & 3) {
|
||||
case 0: nsync_run_once (&s->once, &once_func0); break;
|
||||
case 1: nsync_run_once_spin (&s->once, &once_func1); break;
|
||||
|
@ -111,7 +111,7 @@ static void test_once_run (testing t) {
|
|||
closure_fork (closure_once_thread (&once_thread,
|
||||
&ott[j]));
|
||||
}
|
||||
if (nsync_counter_wait (s->done,
|
||||
if (nsync_counter_wait (s->done, NSYNC_CLOCK,
|
||||
nsync_time_no_deadline) != 0) {
|
||||
TEST_ERROR (t, ("s.done not decremented to 0"));
|
||||
}
|
||||
|
|
19
third_party/nsync/testing/pingpong_test.c
vendored
19
third_party/nsync/testing/pingpong_test.c
vendored
|
@ -17,9 +17,9 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/thread2.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
#include "third_party/nsync/cv.h"
|
||||
#include "third_party/nsync/mu.h"
|
||||
#include "third_party/nsync/mu_wait.h"
|
||||
|
@ -107,7 +107,7 @@ static void mutex_cv_ping_pong (ping_pong *pp, int parity) {
|
|||
nsync_cv_wait_with_deadline_generic (&pp->cv[parity], &pp->mutex,
|
||||
&void_pthread_mutex_lock,
|
||||
&void_pthread_mutex_unlock,
|
||||
CLOCK_REALTIME,
|
||||
NSYNC_CLOCK,
|
||||
nsync_time_no_deadline, NULL);
|
||||
}
|
||||
pp->i++;
|
||||
|
@ -159,12 +159,12 @@ static void benchmark_ping_pong_mu_cv (testing t) {
|
|||
/* Run by each thread in benchmark_ping_pong_mu_cv_unexpired_deadline(). */
|
||||
static void mu_cv_unexpired_deadline_ping_pong (ping_pong *pp, int parity) {
|
||||
nsync_time deadline_in1hour;
|
||||
deadline_in1hour = nsync_time_add (nsync_time_now (), nsync_time_ms (3600000));
|
||||
deadline_in1hour = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (3600000));
|
||||
nsync_mu_lock (&pp->mu);
|
||||
while (pp->i < pp->limit) {
|
||||
while ((pp->i & 1) == parity) {
|
||||
nsync_cv_wait_with_deadline (&pp->cv[parity], &pp->mu,
|
||||
CLOCK_REALTIME, deadline_in1hour,
|
||||
NSYNC_CLOCK, deadline_in1hour,
|
||||
NULL);
|
||||
}
|
||||
pp->i++;
|
||||
|
@ -200,11 +200,11 @@ static const condition_func condition[] = { &even_ping_pong, &odd_ping_pong };
|
|||
/* Run by each thread in benchmark_ping_pong_mu_unexpired_deadline(). */
|
||||
static void mu_unexpired_deadline_ping_pong (ping_pong *pp, int parity) {
|
||||
nsync_time deadline_in1hour;
|
||||
deadline_in1hour = nsync_time_add (nsync_time_now (), nsync_time_ms (3600000));
|
||||
deadline_in1hour = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (3600000));
|
||||
nsync_mu_lock (&pp->mu);
|
||||
while (pp->i < pp->limit) {
|
||||
nsync_mu_wait_with_deadline (&pp->mu, condition[parity], pp, NULL,
|
||||
deadline_in1hour, NULL);
|
||||
NSYNC_CLOCK, deadline_in1hour, NULL);
|
||||
pp->i++;
|
||||
}
|
||||
nsync_mu_unlock (&pp->mu);
|
||||
|
@ -227,7 +227,7 @@ static void benchmark_ping_pong_mu_unexpired_deadline (testing t) {
|
|||
/* Run by each thread in benchmark_ping_pong_mutex_cond_unexpired_deadline(). */
|
||||
static void mutex_cond_unexpired_deadline_ping_pong (ping_pong *pp, int parity) {
|
||||
struct timespec ts;
|
||||
clock_gettime (CLOCK_REALTIME, &ts);
|
||||
clock_gettime (NSYNC_CLOCK, &ts);
|
||||
ts.tv_sec += 3600;
|
||||
pthread_mutex_lock (&pp->mutex);
|
||||
while (pp->i < pp->limit) {
|
||||
|
@ -320,7 +320,7 @@ static void rw_mutex_cv_ping_pong (ping_pong *pp, int parity) {
|
|||
nsync_cv_wait_with_deadline_generic (&pp->cv[parity], &pp->rwmutex,
|
||||
&void_pthread_rwlock_wrlock,
|
||||
&void_pthread_rwlock_unlock,
|
||||
CLOCK_REALTIME,
|
||||
NSYNC_CLOCK,
|
||||
nsync_time_no_deadline, NULL);
|
||||
}
|
||||
pp->i++;
|
||||
|
@ -353,7 +353,8 @@ static void wait_n_cv_ping_pong (ping_pong *pp, int parity) {
|
|||
while ((pp->i & 1) == parity) {
|
||||
nsync_wait_n (&pp->mu, (void (*) (void *)) &nsync_mu_lock,
|
||||
(void (*) (void *)) &nsync_mu_unlock,
|
||||
nsync_time_no_deadline, 1, &pwaitable);
|
||||
NSYNC_CLOCK, nsync_time_no_deadline, 1,
|
||||
&pwaitable);
|
||||
}
|
||||
pp->i++;
|
||||
nsync_cv_signal (&pp->cv[1 - parity]);
|
||||
|
|
9
third_party/nsync/testing/start_thread.c
vendored
9
third_party/nsync/testing/start_thread.c
vendored
|
@ -16,6 +16,9 @@
|
|||
│ limitations under the License. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
struct thd_args {
|
||||
|
@ -35,6 +38,10 @@ void nsync_start_thread_ (void (*f) (void *), void *arg) {
|
|||
pthread_t t;
|
||||
args->f = f;
|
||||
args->arg = arg;
|
||||
pthread_create (&t, NULL, body, args);
|
||||
errno_t err = pthread_create (&t, NULL, body, args);
|
||||
if (err) {
|
||||
fprintf(stderr, "pthread_create: %s\n", strerror(err));
|
||||
exit(1);
|
||||
}
|
||||
pthread_detach (t);
|
||||
}
|
||||
|
|
20
third_party/nsync/testing/testing.c
vendored
20
third_party/nsync/testing/testing.c
vendored
|
@ -239,9 +239,9 @@ static void run_test (testing t) {
|
|||
t->test_status = 0;
|
||||
t->n = 0;
|
||||
t->stop_time = nsync_time_zero;
|
||||
t->start_time = nsync_time_now ();
|
||||
t->start_time = nsync_time_now (NSYNC_CLOCK);
|
||||
(*t->f) (t);
|
||||
elapsed_str = nsync_time_str (nsync_time_sub (nsync_time_now (), t->start_time), 2);
|
||||
elapsed_str = nsync_time_str (nsync_time_sub (nsync_time_now (NSYNC_CLOCK), t->start_time), 2);
|
||||
if (!ATM_LOAD (&t->partial_line)) {
|
||||
fprintf (t->fp, "%-25s %-45s %s %8s\n", tb->prog, t->name,
|
||||
t->test_status != 0? "failed": "passed", elapsed_str);
|
||||
|
@ -275,9 +275,9 @@ static void run_benchmark (testing t) {
|
|||
t->test_status = 0;
|
||||
t->n = n;
|
||||
t->stop_time = nsync_time_zero;
|
||||
t->start_time = nsync_time_now ();
|
||||
t->start_time = nsync_time_now (NSYNC_CLOCK);
|
||||
(*t->f) (t);
|
||||
elapsed = nsync_time_to_dbl (nsync_time_sub (nsync_time_now (), t->start_time));
|
||||
elapsed = nsync_time_to_dbl (nsync_time_sub (nsync_time_now (NSYNC_CLOCK), t->start_time));
|
||||
if (elapsed < 1e-1) {
|
||||
elapsed = 1e-1;
|
||||
}
|
||||
|
@ -445,9 +445,9 @@ int testing_is_uniprocessor (testing t) {
|
|||
|
||||
ATM_STORE_REL (&state, 0);
|
||||
closure_fork (closure_uniprocessor_check (&uniprocessor_check, &state, &s[0]));
|
||||
nsync_time_sleep (nsync_time_ms (100));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (100));
|
||||
ATM_STORE_REL (&state, 1);
|
||||
nsync_time_sleep (nsync_time_ms (400));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (400));
|
||||
ATM_STORE_REL (&state, 2);
|
||||
while (!ATM_LOAD_ACQ (&s[0].done)) {
|
||||
}
|
||||
|
@ -455,9 +455,9 @@ int testing_is_uniprocessor (testing t) {
|
|||
ATM_STORE_REL (&state, 0);
|
||||
closure_fork (closure_uniprocessor_check (&uniprocessor_check, &state, &s[1]));
|
||||
closure_fork (closure_uniprocessor_check (&uniprocessor_check, &state, &s[2]));
|
||||
nsync_time_sleep (nsync_time_ms (100));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (100));
|
||||
ATM_STORE_REL (&state, 1);
|
||||
nsync_time_sleep (nsync_time_ms (400));
|
||||
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (400));
|
||||
ATM_STORE_REL (&state, 2);
|
||||
while (!ATM_LOAD_ACQ (&s[1].done) || !ATM_LOAD_ACQ (&s[2].done)) {
|
||||
}
|
||||
|
@ -472,7 +472,7 @@ void testing_stop_timer (testing t) {
|
|||
if (nsync_time_cmp (t->stop_time, nsync_time_zero) != 0) {
|
||||
abort ();
|
||||
}
|
||||
t->stop_time = nsync_time_now ();
|
||||
t->stop_time = nsync_time_now (NSYNC_CLOCK);
|
||||
}
|
||||
|
||||
void testing_start_timer (testing t) {
|
||||
|
@ -480,7 +480,7 @@ void testing_start_timer (testing t) {
|
|||
abort ();
|
||||
}
|
||||
t->start_time = nsync_time_add (t->start_time,
|
||||
nsync_time_sub (nsync_time_now (), t->stop_time));
|
||||
nsync_time_sub (nsync_time_now (NSYNC_CLOCK), t->stop_time));
|
||||
t->stop_time = nsync_time_zero;
|
||||
}
|
||||
|
||||
|
|
31
third_party/nsync/testing/wait_test.c
vendored
31
third_party/nsync/testing/wait_test.c
vendored
|
@ -17,6 +17,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/nsync/array.internal.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
#include "third_party/nsync/counter.h"
|
||||
#include "third_party/nsync/note.h"
|
||||
#include "third_party/nsync/testing/closure.h"
|
||||
|
@ -24,10 +25,12 @@
|
|||
#include "third_party/nsync/testing/testing.h"
|
||||
#include "third_party/nsync/testing/time_extra.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
#include "third_party/nsync/waiter.h"
|
||||
|
||||
static void decrement_at (nsync_counter c, nsync_time abs_deadline, nsync_counter done) {
|
||||
nsync_time_sleep_until (abs_deadline);
|
||||
nsync_time_sleep_until (NSYNC_CLOCK, abs_deadline);
|
||||
nsync_counter_add (c, -1);
|
||||
nsync_counter_add (done, -1);
|
||||
}
|
||||
|
@ -35,7 +38,7 @@ static void decrement_at (nsync_counter c, nsync_time abs_deadline, nsync_counte
|
|||
CLOSURE_DECL_BODY3 (decrement, nsync_counter, nsync_time, nsync_counter)
|
||||
|
||||
static void notify_at (nsync_note n, nsync_time abs_deadline, nsync_counter done) {
|
||||
nsync_time_sleep_until (abs_deadline);
|
||||
nsync_time_sleep_until (NSYNC_CLOCK, abs_deadline);
|
||||
nsync_note_notify (n);
|
||||
nsync_counter_add (done, -1);
|
||||
}
|
||||
|
@ -61,7 +64,7 @@ static void test_wait_n (testing t) {
|
|||
a_pwaitable apw;
|
||||
bzero (&aw, sizeof (aw));
|
||||
bzero (&apw, sizeof (apw));
|
||||
now = nsync_time_now ();
|
||||
now = nsync_time_now (NSYNC_CLOCK);
|
||||
deadline = nsync_time_add (now, nsync_time_ms (100));
|
||||
for (j = A_LEN (&aw); A_LEN (&aw) < j+ncounter;) {
|
||||
nsync_counter c = nsync_counter_new (0);
|
||||
|
@ -75,28 +78,28 @@ static void test_wait_n (testing t) {
|
|||
}
|
||||
}
|
||||
for (j = A_LEN (&aw); A_LEN (&aw) < j+nnote;) {
|
||||
nsync_note n = nsync_note_new (NULL, nsync_time_no_deadline);
|
||||
nsync_note n = nsync_note_new (NULL, NSYNC_CLOCK, nsync_time_no_deadline);
|
||||
struct nsync_waitable_s *w = &A_PUSH (&aw);
|
||||
w->v = n;
|
||||
w->funcs = &nsync_note_waitable_funcs;
|
||||
nsync_counter_add (done, 1);
|
||||
closure_fork (closure_notify (¬ify_at, n, deadline, done));
|
||||
for (k = 0; k != 4 && A_LEN (&aw) < j+nnote; k++) {
|
||||
nsync_note cn = nsync_note_new (n, nsync_time_no_deadline);
|
||||
nsync_note cn = nsync_note_new (n, NSYNC_CLOCK, nsync_time_no_deadline);
|
||||
struct nsync_waitable_s *lw = &A_PUSH (&aw);
|
||||
lw->v = cn;
|
||||
lw->funcs = &nsync_note_waitable_funcs;
|
||||
}
|
||||
}
|
||||
for (j = A_LEN (&aw); A_LEN (&aw) < j+nnote_expire;) {
|
||||
nsync_note n = nsync_note_new (NULL, deadline);
|
||||
nsync_note n = nsync_note_new (NULL, NSYNC_CLOCK, deadline);
|
||||
struct nsync_waitable_s *w = &A_PUSH (&aw);
|
||||
w->v = n;
|
||||
w->funcs = &nsync_note_waitable_funcs;
|
||||
nsync_counter_add (done, 1);
|
||||
closure_fork (closure_notify (¬ify_at, n, deadline, done));
|
||||
for (k = 0; k != 4 && A_LEN (&aw) < j+nnote; k++) {
|
||||
nsync_note cn = nsync_note_new (n, nsync_time_no_deadline);
|
||||
nsync_note cn = nsync_note_new (n, NSYNC_CLOCK, nsync_time_no_deadline);
|
||||
struct nsync_waitable_s *lw = &A_PUSH (&aw);
|
||||
lw->v = cn;
|
||||
lw->funcs = &nsync_note_waitable_funcs;
|
||||
|
@ -109,7 +112,8 @@ static void test_wait_n (testing t) {
|
|||
A_PUSH (&apw) = &A (&aw, j);
|
||||
}
|
||||
while (A_LEN (&apw) != 0) {
|
||||
k = nsync_wait_n (NULL, NULL, NULL, nsync_time_no_deadline,
|
||||
k = nsync_wait_n (NULL, NULL, NULL,
|
||||
NSYNC_CLOCK, nsync_time_no_deadline,
|
||||
A_LEN (&apw), &A (&apw, 0));
|
||||
if (k == A_LEN (&apw)) {
|
||||
TEST_ERROR (t, ("nsync_wait_n returned with no waiter ready"));
|
||||
|
@ -117,7 +121,7 @@ static void test_wait_n (testing t) {
|
|||
A (&apw, k) = A (&apw, A_LEN (&apw) - 1);
|
||||
A_DISCARD (&apw, 1);
|
||||
}
|
||||
nsync_counter_wait (done, nsync_time_no_deadline);
|
||||
nsync_counter_wait (done, NSYNC_CLOCK, nsync_time_no_deadline);
|
||||
for (k = 0; k != ncounter; k++) {
|
||||
nsync_counter_free ((nsync_counter) A (&aw, k).v);
|
||||
}
|
||||
|
@ -159,7 +163,7 @@ static void test_wait_n_ready_while_queuing (testing t) {
|
|||
wrapped_note_waitable_funcs.ready_time = ¬e_ready_time_wrapper;
|
||||
|
||||
for (count = 0; count != sizeof (w) / sizeof (w[0]); count++) {
|
||||
nsync_note n = nsync_note_new (NULL, nsync_time_no_deadline);
|
||||
nsync_note n = nsync_note_new (NULL, NSYNC_CLOCK, nsync_time_no_deadline);
|
||||
if (nsync_note_is_notified (n)) {
|
||||
TEST_ERROR (t, ("nsync_note is unexpectedly notified"));
|
||||
}
|
||||
|
@ -167,8 +171,8 @@ static void test_wait_n_ready_while_queuing (testing t) {
|
|||
w[count].funcs = &wrapped_note_waitable_funcs;
|
||||
pw[count] = &w[count];
|
||||
}
|
||||
woken = nsync_wait_n (NULL, NULL, NULL, nsync_time_no_deadline,
|
||||
count, pw);
|
||||
woken = nsync_wait_n (NULL, NULL, NULL, NSYNC_CLOCK,
|
||||
nsync_time_no_deadline, count, pw);
|
||||
if (woken != 0) {
|
||||
TEST_ERROR (t, ("nsync_wait_n unexpectedly failed to find pw[0] notified"));
|
||||
}
|
||||
|
@ -183,6 +187,9 @@ static void test_wait_n_ready_while_queuing (testing t) {
|
|||
|
||||
int main (int argc, char *argv[]) {
|
||||
testing_base tb = testing_new (argc, argv, 0);
|
||||
// TODO(jart): remove after cosmocc update when process rlimit flake is solved
|
||||
if (IsAarch64 () && IsQemuUser ())
|
||||
return 0;
|
||||
TEST_RUN (tb, test_wait_n);
|
||||
TEST_RUN (tb, test_wait_n_ready_while_queuing);
|
||||
return (testing_base_exit (tb));
|
||||
|
|
26
third_party/nsync/time.c
vendored
Normal file
26
third_party/nsync/time.c
vendored
Normal file
|
@ -0,0 +1,26 @@
|
|||
/*-*- 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/time.h"
|
||||
|
||||
/* Return the current time since the epoch. */
|
||||
nsync_time nsync_time_now(int clock) {
|
||||
nsync_time result;
|
||||
if (clock_gettime (clock, &result))
|
||||
__builtin_trap();
|
||||
return result;
|
||||
}
|
9
third_party/nsync/time.h
vendored
9
third_party/nsync/time.h
vendored
|
@ -1,8 +1,11 @@
|
|||
#ifndef NSYNC_TIME_H_
|
||||
#define NSYNC_TIME_H_
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
#define NSYNC_CLOCK CLOCK_REALTIME
|
||||
|
||||
#define NSYNC_TIME_SEC(t) ((t).tv_sec)
|
||||
#define NSYNC_TIME_NSEC(t) ((t).tv_nsec)
|
||||
#define NSYNC_TIME_STATIC_INIT(t, ns) \
|
||||
|
@ -22,15 +25,15 @@ typedef struct timespec nsync_time;
|
|||
#define nsync_time_zero timespec_zero
|
||||
|
||||
/* Return the current time since the epoch. */
|
||||
#define nsync_time_now() timespec_real()
|
||||
nsync_time nsync_time_now(int clock);
|
||||
|
||||
/* Sleep for the specified delay. Returns the unslept time which may be
|
||||
non-zero if the call was interrupted. */
|
||||
#define nsync_time_sleep(a) timespec_sleep(a)
|
||||
#define nsync_time_sleep(c,a) timespec_sleep(c,a)
|
||||
|
||||
/* Sleep until the specified time. Returns 0 on success, and EINTR
|
||||
if the call was interrupted. */
|
||||
#define nsync_time_sleep_until(a) timespec_sleep_until(a)
|
||||
#define nsync_time_sleep_until(c,a) timespec_sleep_until(c,a)
|
||||
|
||||
/* Return a+b */
|
||||
#define nsync_time_add(a, b) timespec_add(a, b)
|
||||
|
|
11
third_party/python/Modules/timemodule.c
vendored
11
third_party/python/Modules/timemodule.c
vendored
|
@ -1052,14 +1052,9 @@ _PyTime_GetProcessTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
|
|||
*tp = (ReadFileTime(kernel_time) + ReadFileTime(user_time)) * 100;
|
||||
return 0;
|
||||
}
|
||||
if (CLOCK_PROF != -1 || CLOCK_PROCESS_CPUTIME_ID != -1) {
|
||||
if (CLOCK_PROF != -1) {
|
||||
clk_id = CLOCK_PROF;
|
||||
function = "clock_gettime(CLOCK_PROF)";
|
||||
} else {
|
||||
clk_id = CLOCK_PROCESS_CPUTIME_ID;
|
||||
function = "clock_gettime(CLOCK_PROCESS_CPUTIME_ID)";
|
||||
}
|
||||
if (CLOCK_PROCESS_CPUTIME_ID != -1) {
|
||||
clk_id = CLOCK_PROCESS_CPUTIME_ID;
|
||||
function = "clock_gettime(CLOCK_PROCESS_CPUTIME_ID)";
|
||||
if (!clock_gettime(clk_id, &ts)) {
|
||||
if (info) {
|
||||
info->implementation = function;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue