Skip to content

Commit e726b19

Browse files
committed
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.
1 parent cd34b60 commit e726b19

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b2996d73cab7c7453dc10ccf7ad733622558de3b1ad0db824a379cf96f500379 /usr/local/bin/tox-bootstrapd
1+
b18557c5c89ac6a06137a692418270ab08adc0544ed631545b862fca21502743 /usr/local/bin/tox-bootstrapd

toxcore/mono_time.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t
165165
// Maximum reproducibility. Never return time = 0.
166166
mono_time->base_time = 1;
167167
#else
168-
mono_time->base_time = (uint64_t)time(nullptr) - (current_time_monotonic(mono_time) / 1000ULL);
168+
mono_time->base_time = (uint64_t)time(nullptr) * 1000ULL - current_time_monotonic(mono_time);
169169
#endif
170170

171171
mono_time_update(mono_time);
@@ -190,14 +190,13 @@ void mono_time_free(const Memory *mem, Mono_Time *mono_time)
190190

191191
void mono_time_update(Mono_Time *mono_time)
192192
{
193-
uint64_t cur_time = 0;
194193
#ifdef OS_WIN32
195194
/* we actually want to update the overflow state of mono_time here */
196195
pthread_mutex_lock(&mono_time->last_clock_lock);
197196
mono_time->last_clock_update = true;
198197
#endif
199-
cur_time = mono_time->current_time_callback(mono_time->user_data) / 1000ULL;
200-
cur_time += mono_time->base_time;
198+
const uint64_t cur_time =
199+
mono_time->base_time + mono_time->current_time_callback(mono_time->user_data);
201200
#ifdef OS_WIN32
202201
pthread_mutex_unlock(&mono_time->last_clock_lock);
203202
#endif
@@ -211,21 +210,22 @@ void mono_time_update(Mono_Time *mono_time)
211210
#endif
212211
}
213212

214-
uint64_t mono_time_get(const Mono_Time *mono_time)
213+
uint64_t mono_time_get_ms(const Mono_Time *mono_time)
215214
{
216-
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
215+
#if !defined(ESP_PLATFORM) && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
217216
// Fuzzing is only single thread for now, no locking needed */
218-
return mono_time->cur_time;
219-
#else
220-
#ifndef ESP_PLATFORM
221217
pthread_rwlock_rdlock(mono_time->time_update_lock);
222218
#endif
223219
const uint64_t cur_time = mono_time->cur_time;
224-
#ifndef ESP_PLATFORM
220+
#if !defined(ESP_PLATFORM) && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
225221
pthread_rwlock_unlock(mono_time->time_update_lock);
226222
#endif
227223
return cur_time;
228-
#endif
224+
}
225+
226+
uint64_t mono_time_get(const Mono_Time *mono_time)
227+
{
228+
return mono_time_get_ms(mono_time) / 1000ULL;
229229
}
230230

231231
bool mono_time_is_timeout(const Mono_Time *mono_time, uint64_t timestamp, uint64_t timeout)
@@ -245,9 +245,10 @@ void mono_time_set_current_time_callback(Mono_Time *mono_time,
245245
}
246246
}
247247

248-
/**
249-
* Return current monotonic time in milliseconds (ms). The starting point is
250-
* unspecified.
248+
/** @brief Return current monotonic time in milliseconds (ms).
249+
*
250+
* The starting point is unspecified and in particular is likely not comparable
251+
* to the return value of `mono_time_get_ms()`.
251252
*/
252253
uint64_t current_time_monotonic(Mono_Time *mono_time)
253254
{

toxcore/mono_time.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,16 @@ void mono_time_free(const Memory *mem, Mono_Time *mono_time);
6161
non_null()
6262
void mono_time_update(Mono_Time *mono_time);
6363

64-
/**
65-
* Return unix time since epoch in seconds.
64+
/** @brief Return current monotonic time in milliseconds (ms).
65+
*
66+
* The starting point is UNIX epoch as measured by `time()` in `mono_time_new()`.
67+
*/
68+
non_null()
69+
uint64_t mono_time_get_ms(const Mono_Time *mono_time);
70+
71+
/** @brief Return a monotonically increasing time in seconds.
72+
*
73+
* The starting point is UNIX epoch as measured by `time()` in `mono_time_new()`.
6674
*/
6775
non_null()
6876
uint64_t mono_time_get(const Mono_Time *mono_time);
@@ -73,9 +81,10 @@ uint64_t mono_time_get(const Mono_Time *mono_time);
7381
non_null()
7482
bool mono_time_is_timeout(const Mono_Time *mono_time, uint64_t timestamp, uint64_t timeout);
7583

76-
/**
77-
* Return current monotonic time in milliseconds (ms). The starting point is
78-
* unspecified.
84+
/** @brief Return current monotonic time in milliseconds (ms).
85+
*
86+
* The starting point is unspecified and in particular is likely not comparable
87+
* to the return value of `mono_time_get_ms()`.
7988
*/
8089
non_null()
8190
uint64_t current_time_monotonic(Mono_Time *mono_time);

0 commit comments

Comments
 (0)