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:
Justine Tunney 2024-09-04 00:38:44 -07:00
parent 8f8145105c
commit dd8544c3bd
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
87 changed files with 939 additions and 900 deletions

View file

@ -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)

View file

@ -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);

View file

@ -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;

View file

@ -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);

View file

@ -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") */

View file

@ -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) {

View file

@ -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),

View file

@ -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") */

View file

@ -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));

View file

@ -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 (&notify_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 (&notify_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 (&notify_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 (&notify_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++) {

View file

@ -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"));
}

View file

@ -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]);

View file

@ -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);
}

View file

@ -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;
}

View file

@ -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 (&notify_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 (&notify_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 = &note_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));