3737/** don't call into system billions of times for no reason */
3838struct Mono_Time {
3939 uint64_t cur_time ;
40- uint64_t base_time ;
40+
4141#ifdef OS_WIN32
4242 /* protect `last_clock_update` and `last_clock_mono` from concurrent access */
4343 pthread_mutex_t last_clock_lock ;
@@ -65,6 +65,7 @@ static uint64_t current_time_monotonic_default(void *user_data)
6565 /* GetTickCount provides only a 32 bit counter, but we can't use
6666 * GetTickCount64 for backwards compatibility, so we handle wraparound
6767 * ourselves.
68+ * TODO(Green-Sky): switch to QPC, since GTC only advertises a precision of "typically" 10-16ms
6869 */
6970 const uint32_t ticks = GetTickCount ();
7071
@@ -88,7 +89,7 @@ static uint64_t current_time_monotonic_default(void *user_data)
8889#else // !OS_WIN32
8990static uint64_t timespec_to_u64 (struct timespec clock_mono )
9091{
91- return 1000ULL * clock_mono .tv_sec + (clock_mono .tv_nsec / 1000000ULL );
92+ return UINT64_C ( 1000 ) * clock_mono .tv_sec + (clock_mono .tv_nsec / UINT64_C ( 1000000 ) );
9293}
9394#ifdef __APPLE__
9495non_null ()
@@ -149,7 +150,6 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t
149150 mono_time_set_current_time_callback (mono_time , current_time_callback , user_data );
150151
151152#ifdef OS_WIN32
152-
153153 mono_time -> last_clock_mono = 0 ;
154154 mono_time -> last_clock_update = false;
155155
@@ -158,18 +158,17 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t
158158 mem_delete (mem , mono_time );
159159 return nullptr ;
160160 }
161-
162161#endif
163162
164- mono_time -> cur_time = 0 ;
165- #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
166- // Maximum reproducibility. Never return time = 0.
167- mono_time -> base_time = 1 ;
168- #else
169- // Never return time = 0 in case time() returns 0 (e.g. on microcontrollers
170- // without battery-powered RTC or ones where NTP didn't initialise it yet).
171- mono_time -> base_time = max_u64 (1 , (uint64_t )time (nullptr )) * 1000ULL - current_time_monotonic (mono_time );
172- #endif
163+ mono_time -> cur_time = 1 ;
164+ /* #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION*/
165+ /*// Maximum reproducibility. Never return time = 0.*/
166+ /* mono_time->base_time = 1;*/
167+ /* #else*/
168+ /*// Never return time = 0 in case time() returns 0 (e.g. on microcontrollers*/
169+ /*// without battery-powered RTC or ones where NTP didn't initialise it yet).*/
170+ /* mono_time->base_time = max_u64(1, (uint64_t)time(nullptr)) * 1000ULL - current_time_monotonic(mono_time);*/
171+ /* #endif*/
173172
174173 mono_time_update (mono_time );
175174
@@ -198,8 +197,7 @@ void mono_time_update(Mono_Time *mono_time)
198197 pthread_mutex_lock (& mono_time -> last_clock_lock );
199198 mono_time -> last_clock_update = true;
200199#endif
201- const uint64_t cur_time =
202- mono_time -> base_time + mono_time -> current_time_callback (mono_time -> user_data );
200+ const uint64_t cur_time = mono_time -> current_time_callback (mono_time -> user_data );
203201#ifdef OS_WIN32
204202 pthread_mutex_unlock (& mono_time -> last_clock_lock );
205203#endif
@@ -228,7 +226,7 @@ uint64_t mono_time_get_ms(const Mono_Time *mono_time)
228226
229227uint64_t mono_time_get (const Mono_Time * mono_time )
230228{
231- return mono_time_get_ms (mono_time ) / 1000ULL ;
229+ return mono_time_get_ms (mono_time ) / UINT64_C ( 1000 ) ;
232230}
233231
234232bool mono_time_is_timeout (const Mono_Time * mono_time , uint64_t timestamp , uint64_t timeout )
@@ -248,11 +246,6 @@ void mono_time_set_current_time_callback(Mono_Time *mono_time,
248246 }
249247}
250248
251- /** @brief Return current monotonic time in milliseconds (ms).
252- *
253- * The starting point is unspecified and in particular is likely not comparable
254- * to the return value of `mono_time_get_ms()`.
255- */
256249uint64_t current_time_monotonic (Mono_Time * mono_time )
257250{
258251 /* For WIN32 we don't want to change overflow state of mono_time here */
0 commit comments