Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Store time in Mono_Time in milliseconds. #2203

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b2996d73cab7c7453dc10ccf7ad733622558de3b1ad0db824a379cf96f500379 /usr/local/bin/tox-bootstrapd
b18557c5c89ac6a06137a692418270ab08adc0544ed631545b862fca21502743 /usr/local/bin/tox-bootstrapd
29 changes: 15 additions & 14 deletions toxcore/mono_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t
// Maximum reproducibility. Never return time = 0.
mono_time->base_time = 1;
#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);
#endif

mono_time_update(mono_time);
Expand All @@ -190,14 +190,13 @@ void mono_time_free(const Memory *mem, 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->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->user_data);
#ifdef OS_WIN32
pthread_mutex_unlock(&mono_time->last_clock_lock);
#endif
Expand All @@ -211,21 +210,22 @@ void mono_time_update(Mono_Time *mono_time)
#endif
}

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
#if !defined(ESP_PLATFORM) && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
// Fuzzing is only single thread for now, no locking needed */
return mono_time->cur_time;
#else
#ifndef ESP_PLATFORM
pthread_rwlock_rdlock(mono_time->time_update_lock);
#endif
const uint64_t cur_time = mono_time->cur_time;
#ifndef ESP_PLATFORM
#if !defined(ESP_PLATFORM) && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
pthread_rwlock_unlock(mono_time->time_update_lock);
#endif
return cur_time;
#endif
}

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)
Expand All @@ -245,9 +245,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)
{
Expand Down
19 changes: 14 additions & 5 deletions toxcore/mono_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,16 @@ void mono_time_free(const Memory *mem, 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);
Expand All @@ -73,9 +81,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);
Expand Down
Loading