-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cython monotonic_time_coarse implementation (#38)
- Loading branch information
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import cython | ||
|
||
from posix.time cimport clock_gettime, timespec | ||
|
||
|
||
def _monotonic_time_coarse(): | ||
cdef timespec ts | ||
cdef double current | ||
clock_gettime(6, &ts) | ||
current = ts.tv_sec + (ts.tv_nsec / 1000000000.) | ||
return current |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Bluetooth time utils.""" | ||
from __future__ import annotations | ||
|
||
import platform | ||
import time | ||
from contextlib import suppress | ||
from functools import partial | ||
|
||
CLOCK_MONOTONIC_COARSE = 6 | ||
|
||
|
||
def __gen_monotonic_time_coarse() -> partial[float]: | ||
"""Return a function that provides monotonic time in seconds. | ||
This is the coarse version of time_monotonic, which is faster but less accurate. | ||
Since many arm64 and 32-bit platforms don't support VDSO with time.monotonic | ||
because of errata, we can't rely on the kernel to provide a fast | ||
monotonic time. | ||
https://lore.kernel.org/lkml/[email protected]/ | ||
""" | ||
# We use a partial here since its implementation is in native code | ||
# which allows us to avoid the overhead of the global lookup | ||
# of CLOCK_MONOTONIC_COARSE. | ||
return partial(time.clock_gettime, CLOCK_MONOTONIC_COARSE) | ||
|
||
|
||
monotonic_time_coarse = time.monotonic | ||
_USE_COARSE_MONOTONIC_TIME = False | ||
|
||
with suppress(Exception): | ||
if ( | ||
platform.system() == "Linux" | ||
and abs(time.monotonic() - __gen_monotonic_time_coarse()()) < 1 | ||
): | ||
monotonic_time_coarse = __gen_monotonic_time_coarse() | ||
_USE_COARSE_MONOTONIC_TIME = True | ||
|
||
if _USE_COARSE_MONOTONIC_TIME: | ||
with suppress(ImportError): | ||
from ._time_impl import ( # type: ignore[no-redef] # noqa: F811 F401 | ||
_monotonic_time_coarse as monotonic_time_coarse, | ||
) | ||
|
||
|
||
__all__ = [ | ||
"monotonic_time_coarse", | ||
] |