3939/** don't call into system billions of times for no reason */
4040struct 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
9192static 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__
9697non_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
231222uint64_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
236227bool 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- */
258244uint64_t current_time_monotonic (Mono_Time * mono_time )
259245{
260246 /* For WIN32 we don't want to change overflow state of mono_time here */
0 commit comments