37
37
/** don't call into system billions of times for no reason */
38
38
struct Mono_Time {
39
39
uint64_t cur_time ;
40
- uint64_t base_time ;
40
+
41
41
#ifdef OS_WIN32
42
42
/* protect `last_clock_update` and `last_clock_mono` from concurrent access */
43
43
pthread_mutex_t last_clock_lock ;
@@ -65,6 +65,7 @@ static uint64_t current_time_monotonic_default(void *user_data)
65
65
/* GetTickCount provides only a 32 bit counter, but we can't use
66
66
* GetTickCount64 for backwards compatibility, so we handle wraparound
67
67
* ourselves.
68
+ * TODO(Green-Sky): switch to QPC, since GTC only advertises a precision of "typically" 10-16ms
68
69
*/
69
70
const uint32_t ticks = GetTickCount ();
70
71
@@ -88,7 +89,7 @@ static uint64_t current_time_monotonic_default(void *user_data)
88
89
#else // !OS_WIN32
89
90
static uint64_t timespec_to_u64 (struct timespec clock_mono )
90
91
{
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 ) );
92
93
}
93
94
#ifdef __APPLE__
94
95
non_null ()
@@ -149,7 +150,6 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t
149
150
mono_time_set_current_time_callback (mono_time , current_time_callback , user_data );
150
151
151
152
#ifdef OS_WIN32
152
-
153
153
mono_time -> last_clock_mono = 0 ;
154
154
mono_time -> last_clock_update = false;
155
155
@@ -158,18 +158,17 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t
158
158
mem_delete (mem , mono_time );
159
159
return nullptr ;
160
160
}
161
-
162
161
#endif
163
162
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*/
173
172
174
173
mono_time_update (mono_time );
175
174
@@ -198,8 +197,7 @@ void mono_time_update(Mono_Time *mono_time)
198
197
pthread_mutex_lock (& mono_time -> last_clock_lock );
199
198
mono_time -> last_clock_update = true;
200
199
#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 );
203
201
#ifdef OS_WIN32
204
202
pthread_mutex_unlock (& mono_time -> last_clock_lock );
205
203
#endif
@@ -228,7 +226,7 @@ uint64_t mono_time_get_ms(const Mono_Time *mono_time)
228
226
229
227
uint64_t mono_time_get (const Mono_Time * mono_time )
230
228
{
231
- return mono_time_get_ms (mono_time ) / 1000ULL ;
229
+ return mono_time_get_ms (mono_time ) / UINT64_C ( 1000 ) ;
232
230
}
233
231
234
232
bool 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,
248
246
}
249
247
}
250
248
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
- */
256
249
uint64_t current_time_monotonic (Mono_Time * mono_time )
257
250
{
258
251
/* For WIN32 we don't want to change overflow state of mono_time here */
0 commit comments