From 5d1c5d25160fdc182011713085c04c7c5860a312 Mon Sep 17 00:00:00 2001 From: iphydf Date: Mon, 28 Mar 2022 21:52:43 +0000 Subject: [PATCH] refactor: Store time in Mono_Time in milliseconds. Conversion to seconds happens in `mono_time_get`, and a new function `mono_time_get_ms` allows code to retrieve monotonic time in milliseconds. --- .../docker/tox-bootstrapd.sha256 | 2 +- toxcore/mono_time.c | 31 +++++++++++-------- toxcore/mono_time.h | 19 +++++++++--- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index 36581325990..b71392f075f 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -cfa2c61f92df59419af678ef9d6fcd4f5609a505235d77a54c1a4ec66f1c10af /usr/local/bin/tox-bootstrapd +46107403fe7f6d00b8371d17797ee48d8cc2fd67ffc13f28728bc2cb0df3d3f3 /usr/local/bin/tox-bootstrapd diff --git a/toxcore/mono_time.c b/toxcore/mono_time.c index eac3f686b49..aaa8608c0d9 100644 --- a/toxcore/mono_time.c +++ b/toxcore/mono_time.c @@ -161,9 +161,9 @@ Mono_Time *mono_time_new(void) // had a chance to set the callback. // TODO(iphydf): Put mono time callback into Tox_Options with accessors only // in tox_private.h. - mono_time->base_time = 0; + mono_time->base_time = 1; // Never return time = 0. #else - mono_time->base_time = (uint64_t)time(nullptr) - (current_time_monotonic(mono_time) / 1000ULL); + mono_time->base_time = (uint64_t)time(nullptr) * 1000ULL - current_time_monotonic(mono_time); mono_time_update(mono_time); #endif @@ -185,14 +185,13 @@ void mono_time_free(Mono_Time *mono_time) void mono_time_update(Mono_Time *mono_time) { - uint64_t cur_time = 0; #ifdef OS_WIN32 /* we actually want to update the overflow state of mono_time here */ pthread_mutex_lock(&mono_time->last_clock_lock); mono_time->last_clock_update = true; #endif - cur_time = mono_time->current_time_callback(mono_time, mono_time->user_data) / 1000ULL; - cur_time += mono_time->base_time; + const uint64_t cur_time = + mono_time->base_time + mono_time->current_time_callback(mono_time, mono_time->user_data); #ifdef OS_WIN32 pthread_mutex_unlock(&mono_time->last_clock_lock); #endif @@ -202,17 +201,22 @@ void mono_time_update(Mono_Time *mono_time) pthread_rwlock_unlock(mono_time->time_update_lock); } -uint64_t mono_time_get(const Mono_Time *mono_time) +uint64_t mono_time_get_ms(const Mono_Time *mono_time) { -#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION +#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION // Fuzzing is only single thread for now, no locking needed */ - return mono_time->cur_time; -#else pthread_rwlock_rdlock(mono_time->time_update_lock); +#endif const uint64_t cur_time = mono_time->cur_time; +#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION pthread_rwlock_unlock(mono_time->time_update_lock); - return cur_time; #endif + return cur_time; +} + +uint64_t mono_time_get(const Mono_Time *mono_time) +{ + return mono_time_get_ms(mono_time) / 1000ULL; } bool mono_time_is_timeout(const Mono_Time *mono_time, uint64_t timestamp, uint64_t timeout) @@ -232,9 +236,10 @@ void mono_time_set_current_time_callback(Mono_Time *mono_time, } } -/** - * Return current monotonic time in milliseconds (ms). The starting point is - * unspecified. +/** @brief Return current monotonic time in milliseconds (ms). + * + * The starting point is unspecified and in particular is likely not comparable + * to the return value of `mono_time_get_ms()`. */ uint64_t current_time_monotonic(Mono_Time *mono_time) { diff --git a/toxcore/mono_time.h b/toxcore/mono_time.h index 9336ee16540..893d697b836 100644 --- a/toxcore/mono_time.h +++ b/toxcore/mono_time.h @@ -57,8 +57,16 @@ void mono_time_free(Mono_Time *mono_time); non_null() void mono_time_update(Mono_Time *mono_time); -/** - * Return unix time since epoch in seconds. +/** @brief Return current monotonic time in milliseconds (ms). + * + * The starting point is UNIX epoch as measured by `time()` in `mono_time_new()`. + */ +non_null() +uint64_t mono_time_get_ms(const Mono_Time *mono_time); + +/** @brief Return a monotonically increasing time in seconds. + * + * The starting point is UNIX epoch as measured by `time()` in `mono_time_new()`. */ non_null() uint64_t mono_time_get(const Mono_Time *mono_time); @@ -69,9 +77,10 @@ uint64_t mono_time_get(const Mono_Time *mono_time); non_null() bool mono_time_is_timeout(const Mono_Time *mono_time, uint64_t timestamp, uint64_t timeout); -/** - * Return current monotonic time in milliseconds (ms). The starting point is - * unspecified. +/** @brief Return current monotonic time in milliseconds (ms). + * + * The starting point is unspecified and in particular is likely not comparable + * to the return value of `mono_time_get_ms()`. */ non_null() uint64_t current_time_monotonic(Mono_Time *mono_time);