From 291c329f8365eb919749f640808ed13dbe2975e5 Mon Sep 17 00:00:00 2001 From: Artur Mullakhmetov Date: Wed, 25 Jan 2017 16:16:00 +0300 Subject: [PATCH] Refactor cross-platform libc usage old-commit-hash: 9cb3c7b6e6b5de70a8c8d66d4dd9f057395edf11 --- common/realtime.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/common/realtime.py b/common/realtime.py index e86c07ad73..48c96c93a8 100644 --- a/common/realtime.py +++ b/common/realtime.py @@ -6,6 +6,9 @@ import subprocess import multiprocessing import os +from ctypes.util import find_library + + CLOCK_MONOTONIC_RAW = 4 # see CLOCK_BOOTTIME = 7 @@ -15,28 +18,30 @@ class timespec(ctypes.Structure): ('tv_nsec', ctypes.c_long), ] +libc_name = find_library('c') +if libc_name is None: + platform_name = platform.system() + if platform_name.startswith('linux'): + libc_name = 'libc.so.6' + if platform_name.startswith(('freebsd', 'netbsd')): + libc_name = 'libc.so' + elif platform_name.lower() == 'darwin': + libc_name = 'libc.dylib' try: - libc = ctypes.CDLL('libc.so', use_errno=True) + libc = ctypes.CDLL(libc_name, use_errno=True) except OSError: - try: - libc = ctypes.CDLL('libc.so.6', use_errno=True) - except OSError: - libc = None + libc = None if libc is not None: libc.clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)] def clock_gettime(clk_id): - if platform.system().lower() == "darwin": - # TODO: fix this - return time.time() - else: - t = timespec() - if libc.clock_gettime(clk_id, ctypes.pointer(t)) != 0: - errno_ = ctypes.get_errno() - raise OSError(errno_, os.strerror(errno_)) - return t.tv_sec + t.tv_nsec * 1e-9 + t = timespec() + if libc.clock_gettime(clk_id, ctypes.pointer(t)) != 0: + errno_ = ctypes.get_errno() + raise OSError(errno_, os.strerror(errno_)) + return t.tv_sec + t.tv_nsec * 1e-9 def monotonic_time(): return clock_gettime(CLOCK_MONOTONIC_RAW)