Add nstime_ns_since which obtains the duration since the input time.
This commit is contained in:
parent
cafe9a3158
commit
310af725b0
@ -35,6 +35,7 @@ void nstime_isubtract(nstime_t *time, uint64_t subtrahend);
|
||||
void nstime_imultiply(nstime_t *time, uint64_t multiplier);
|
||||
void nstime_idivide(nstime_t *time, uint64_t divisor);
|
||||
uint64_t nstime_divide(const nstime_t *time, const nstime_t *divisor);
|
||||
uint64_t nstime_ns_since(const nstime_t *past);
|
||||
|
||||
typedef bool (nstime_monotonic_t)(void);
|
||||
extern nstime_monotonic_t *JET_MUTABLE nstime_monotonic;
|
||||
|
13
src/nstime.c
13
src/nstime.c
@ -158,6 +158,19 @@ nstime_divide(const nstime_t *time, const nstime_t *divisor) {
|
||||
return time->ns / divisor->ns;
|
||||
}
|
||||
|
||||
/* Returns time since *past, w/o updating *past. */
|
||||
uint64_t
|
||||
nstime_ns_since(const nstime_t *past) {
|
||||
nstime_assert_initialized(past);
|
||||
|
||||
nstime_t now;
|
||||
nstime_copy(&now, past);
|
||||
nstime_update(&now);
|
||||
|
||||
assert(nstime_compare(&now, past) >= 0);
|
||||
return now.ns - past->ns;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
# define NSTIME_MONOTONIC true
|
||||
static void
|
||||
|
@ -201,6 +201,33 @@ TEST_BEGIN(test_nstime_divide) {
|
||||
}
|
||||
TEST_END
|
||||
|
||||
void
|
||||
test_nstime_since_once(nstime_t *t) {
|
||||
nstime_t old_t;
|
||||
nstime_copy(&old_t, t);
|
||||
|
||||
uint64_t ns_since = nstime_ns_since(t);
|
||||
nstime_update(t);
|
||||
|
||||
nstime_t new_t;
|
||||
nstime_copy(&new_t, t);
|
||||
nstime_subtract(&new_t, &old_t);
|
||||
|
||||
expect_u64_ge(nstime_ns(&new_t), ns_since,
|
||||
"Incorrect time since result");
|
||||
}
|
||||
|
||||
TEST_BEGIN(test_nstime_ns_since) {
|
||||
nstime_t t;
|
||||
|
||||
nstime_init_update(&t);
|
||||
for (uint64_t i = 0; i < 10000; i++) {
|
||||
/* Keeps updating t and verifies ns_since is valid. */
|
||||
test_nstime_since_once(&t);
|
||||
}
|
||||
}
|
||||
TEST_END
|
||||
|
||||
TEST_BEGIN(test_nstime_monotonic) {
|
||||
nstime_monotonic();
|
||||
}
|
||||
@ -220,5 +247,6 @@ main(void) {
|
||||
test_nstime_imultiply,
|
||||
test_nstime_idivide,
|
||||
test_nstime_divide,
|
||||
test_nstime_ns_since,
|
||||
test_nstime_monotonic);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user