Skip to content

Commit c5da11c

Browse files
Use pygettimeofday to replace pymonotonic implementation
1 parent 2b64608 commit c5da11c

1 file changed

Lines changed: 1 addition & 107 deletions

File tree

python_config/pytime.c

Lines changed: 1 addition & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -561,113 +561,7 @@ _PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
561561
static int
562562
pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
563563
{
564-
#if defined(MS_WINDOWS)
565-
ULONGLONG result;
566-
567-
assert(info == NULL || raise);
568-
569-
result = GetTickCount64();
570-
571-
*tp = result * MS_TO_NS;
572-
if (*tp / MS_TO_NS != result) {
573-
if (raise) {
574-
_PyTime_overflow();
575-
return -1;
576-
}
577-
/* Hello, time traveler! */
578-
assert(0);
579-
}
580-
581-
if (info) {
582-
DWORD timeAdjustment, timeIncrement;
583-
BOOL isTimeAdjustmentDisabled, ok;
584-
info->implementation = "GetTickCount64()";
585-
info->monotonic = 1;
586-
ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
587-
&isTimeAdjustmentDisabled);
588-
if (!ok) {
589-
PyErr_SetFromWindowsErr(0);
590-
return -1;
591-
}
592-
info->resolution = timeIncrement * 1e-7;
593-
info->adjustable = 0;
594-
}
595-
596-
#elif defined(__APPLE__)
597-
static mach_timebase_info_data_t timebase;
598-
uint64_t time;
599-
600-
if (timebase.denom == 0) {
601-
/* According to the Technical Q&A QA1398, mach_timebase_info() cannot
602-
fail: https://developer.apple.com/library/mac/#qa/qa1398/ */
603-
(void)mach_timebase_info(&timebase);
604-
}
605-
606-
time = mach_absolute_time();
607-
608-
/* apply timebase factor */
609-
time *= timebase.numer;
610-
time /= timebase.denom;
611-
612-
*tp = time;
613-
614-
if (info) {
615-
info->implementation = "mach_absolute_time()";
616-
info->resolution = (double)timebase.numer / timebase.denom * 1e-9;
617-
info->monotonic = 1;
618-
info->adjustable = 0;
619-
}
620-
621-
#else
622-
struct timespec ts;
623-
#ifdef CLOCK_HIGHRES
624-
const clockid_t clk_id = CLOCK_HIGHRES;
625-
const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
626-
#else
627-
const clockid_t clk_id = (clockid_t)4;
628-
const char *implementation = "clock_gettime((clockid_t)4)";
629-
#endif
630-
631-
assert(info == NULL || raise);
632-
633-
if (clock_gettime(clk_id, &ts) != 0) {
634-
if (raise) {
635-
PyErr_SetFromErrno(PyExc_OSError);
636-
return -1;
637-
}
638-
return -1;
639-
}
640-
641-
if (info) {
642-
struct timespec res;
643-
info->monotonic = 1;
644-
info->implementation = implementation;
645-
info->adjustable = 0;
646-
if (clock_getres(clk_id, &res) != 0) {
647-
PyErr_SetFromErrno(PyExc_OSError);
648-
return -1;
649-
}
650-
info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
651-
}
652-
if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
653-
return -1;
654-
#endif
655-
return 0;
656-
}
657-
658-
_PyTime_t
659-
_PyTime_GetMonotonicClock(void)
660-
{
661-
_PyTime_t t;
662-
if (pymonotonic(&t, NULL, 0) < 0) {
663-
/* should not happen, _PyTime_Init() checked that monotonic clock at
664-
startup */
665-
assert(0);
666-
667-
/* use a fixed value instead of a random value from the stack */
668-
t = 0;
669-
}
670-
return t;
564+
return pygettimeofday(tp, info, raise); /* should be good enough? */
671565
}
672566

673567
int

0 commit comments

Comments
 (0)