Skip to content

Commit 1f34bcb

Browse files
Green-Skyiphydf
authored andcommitted
refactor: remove base_time from mono_clock
The unix time offset is not used anywhere and was causing issues on windows.
1 parent 6a90ddf commit 1f34bcb

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

toxcore/mono_time.c

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
/** don't call into system billions of times for no reason */
4040
struct Mono_Time {
4141
uint64_t cur_time;
42-
uint64_t base_time;
42+
4343
#ifdef OS_WIN32
4444
/* protect `last_clock_update` and `last_clock_mono` from concurrent access */
4545
pthread_mutex_t last_clock_lock;
@@ -67,6 +67,7 @@ static uint64_t current_time_monotonic_default(void *user_data)
6767
/* GetTickCount provides only a 32 bit counter, but we can't use
6868
* GetTickCount64 for backwards compatibility, so we handle wraparound
6969
* ourselves.
70+
* TODO(Green-Sky): switch to QPC, since GTC only advertises a precision of "typically" 10-16ms
7071
*/
7172
const uint32_t ticks = GetTickCount();
7273

@@ -90,7 +91,7 @@ static uint64_t current_time_monotonic_default(void *user_data)
9091
#else // !OS_WIN32
9192
static uint64_t timespec_to_u64(struct timespec clock_mono)
9293
{
93-
return 1000ULL * clock_mono.tv_sec + (clock_mono.tv_nsec / 1000000ULL);
94+
return UINT64_C(1000) * clock_mono.tv_sec + (clock_mono.tv_nsec / UINT64_C(1000000));
9495
}
9596
#ifdef __APPLE__
9697
non_null()
@@ -151,7 +152,6 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t
151152
mono_time_set_current_time_callback(mono_time, current_time_callback, user_data);
152153

153154
#ifdef OS_WIN32
154-
155155
mono_time->last_clock_mono = 0;
156156
mono_time->last_clock_update = false;
157157

@@ -160,18 +160,10 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t
160160
mem_delete(mem, mono_time);
161161
return nullptr;
162162
}
163-
164163
#endif
165164

166-
mono_time->cur_time = 0;
167-
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
168165
// Maximum reproducibility. Never return time = 0.
169-
mono_time->base_time = 1;
170-
#else
171-
// Never return time = 0 in case time() returns 0 (e.g. on microcontrollers
172-
// without battery-powered RTC or ones where NTP didn't initialise it yet).
173-
mono_time->base_time = max_u64(1, (uint64_t)time(nullptr)) * 1000ULL - current_time_monotonic(mono_time);
174-
#endif
166+
mono_time->cur_time = 1;
175167

176168
mono_time_update(mono_time);
177169

@@ -200,8 +192,7 @@ void mono_time_update(Mono_Time *mono_time)
200192
pthread_mutex_lock(&mono_time->last_clock_lock);
201193
mono_time->last_clock_update = true;
202194
#endif
203-
const uint64_t cur_time =
204-
mono_time->base_time + mono_time->current_time_callback(mono_time->user_data);
195+
const uint64_t cur_time = mono_time->current_time_callback(mono_time->user_data);
205196
#ifdef OS_WIN32
206197
pthread_mutex_unlock(&mono_time->last_clock_lock);
207198
#endif
@@ -230,7 +221,7 @@ uint64_t mono_time_get_ms(const Mono_Time *mono_time)
230221

231222
uint64_t mono_time_get(const Mono_Time *mono_time)
232223
{
233-
return mono_time_get_ms(mono_time) / 1000ULL;
224+
return mono_time_get_ms(mono_time) / UINT64_C(1000);
234225
}
235226

236227
bool mono_time_is_timeout(const Mono_Time *mono_time, uint64_t timestamp, uint64_t timeout)
@@ -250,11 +241,6 @@ void mono_time_set_current_time_callback(Mono_Time *mono_time,
250241
}
251242
}
252243

253-
/** @brief Return current monotonic time in milliseconds (ms).
254-
*
255-
* The starting point is unspecified and in particular is likely not comparable
256-
* to the return value of `mono_time_get_ms()`.
257-
*/
258244
uint64_t current_time_monotonic(Mono_Time *mono_time)
259245
{
260246
/* For WIN32 we don't want to change overflow state of mono_time here */

toxcore/mono_time.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ void mono_time_update(Mono_Time *mono_time);
6363

6464
/** @brief Return current monotonic time in milliseconds (ms).
6565
*
66-
* The starting point is UNIX epoch as measured by `time()` in `mono_time_new()`.
66+
* The starting point is implementation defined.
6767
*/
6868
non_null()
6969
uint64_t mono_time_get_ms(const Mono_Time *mono_time);
7070

7171
/** @brief Return a monotonically increasing time in seconds.
7272
*
73-
* The starting point is UNIX epoch as measured by `time()` in `mono_time_new()`.
73+
* The starting point is implementation defined.
7474
*/
7575
non_null()
7676
uint64_t mono_time_get(const Mono_Time *mono_time);
@@ -85,6 +85,9 @@ bool mono_time_is_timeout(const Mono_Time *mono_time, uint64_t timestamp, uint64
8585
*
8686
* The starting point is unspecified and in particular is likely not comparable
8787
* to the return value of `mono_time_get_ms()`.
88+
*
89+
* TODO(Green-Sky): Get rid of this from the api.
90+
* The only user seems to be net_crypt and for no good reason.
8891
*/
8992
non_null()
9093
uint64_t current_time_monotonic(Mono_Time *mono_time);

0 commit comments

Comments
 (0)