mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix implementation of monotonic() on windows
This commit is contained in:
parent
2638d047df
commit
eb6b2dcf2c
@ -21,15 +21,22 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
import ctypes
|
import ctypes
|
||||||
import sys
|
import sys
|
||||||
|
NSEC_PER_SEC = 1e9
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if sys.platform == 'win32':
|
if sys.platform == 'win32':
|
||||||
# Windows only
|
# Windows only
|
||||||
GetTickCount64 = ctypes.windll.kernel32.GetTickCount64
|
perf_frequency = ctypes.c_uint64()
|
||||||
GetTickCount64.restype = ctypes.c_ulonglong
|
if ctypes.windll.kernel32.QueryPerformanceFrequency(ctypes.byref(perf_frequency)) == 0:
|
||||||
|
from time import time as monotonic # noqa
|
||||||
|
else:
|
||||||
|
perf_frequency = perf_frequency.value
|
||||||
|
|
||||||
def monotonic(): # NOQA
|
def monotonic():
|
||||||
return GetTickCount64() / 1000
|
perf_counter = ctypes.c_uint64()
|
||||||
|
if ctypes.windll.kernel32.QueryPerformanceCounter(ctypes.byref(perf_counter)) == 0:
|
||||||
|
raise RuntimeError('monotonic() failed: %s' % ctypes.FormatError())
|
||||||
|
return perf_counter.value / perf_frequency
|
||||||
|
|
||||||
elif sys.platform == 'darwin':
|
elif sys.platform == 'darwin':
|
||||||
# Mac OS X
|
# Mac OS X
|
||||||
@ -62,7 +69,7 @@ except ImportError:
|
|||||||
return (timebase.numer, timebase.denom)
|
return (timebase.numer, timebase.denom)
|
||||||
|
|
||||||
timebase = mach_timebase_info()
|
timebase = mach_timebase_info()
|
||||||
factor = timebase[0] / timebase[1] * 1e-9
|
factor = timebase[0] / timebase[1] * NSEC_PER_SEC
|
||||||
|
|
||||||
def monotonic(): # NOQA
|
def monotonic(): # NOQA
|
||||||
return mach_absolute_time() * factor
|
return mach_absolute_time() * factor
|
||||||
@ -93,12 +100,11 @@ except ImportError:
|
|||||||
else:
|
else:
|
||||||
raise OSError
|
raise OSError
|
||||||
|
|
||||||
def monotonic(): # NOQA
|
def monotonic():
|
||||||
if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(tspec)) != 0:
|
if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(tspec)) != 0:
|
||||||
errno_ = ctypes.get_errno()
|
errno_ = ctypes.get_errno()
|
||||||
raise OSError(errno_, os.strerror(errno_))
|
raise OSError(errno_, os.strerror(errno_))
|
||||||
return tspec.tv_sec + tspec.tv_nsec / 1e9
|
return tspec.tv_sec + tspec.tv_nsec / NSEC_PER_SEC
|
||||||
|
|
||||||
except:
|
except:
|
||||||
from time import time as monotonic # NOQA
|
from time import time as monotonic # noqa
|
||||||
monotonic
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user