Refactor cross-platform libc usage

old-commit-hash: 9cb3c7b6e6
commatwo_master
Artur Mullakhmetov 8 years ago
parent f4bd99ce10
commit 291c329f83
  1. 21
      common/realtime.py

@ -6,6 +6,9 @@ import subprocess
import multiprocessing import multiprocessing
import os import os
from ctypes.util import find_library
CLOCK_MONOTONIC_RAW = 4 # see <linux/time.h> CLOCK_MONOTONIC_RAW = 4 # see <linux/time.h>
CLOCK_BOOTTIME = 7 CLOCK_BOOTTIME = 7
@ -15,12 +18,18 @@ class timespec(ctypes.Structure):
('tv_nsec', ctypes.c_long), ('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: 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: except OSError:
libc = None libc = None
@ -28,10 +37,6 @@ if libc is not None:
libc.clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)] libc.clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
def clock_gettime(clk_id): def clock_gettime(clk_id):
if platform.system().lower() == "darwin":
# TODO: fix this
return time.time()
else:
t = timespec() t = timespec()
if libc.clock_gettime(clk_id, ctypes.pointer(t)) != 0: if libc.clock_gettime(clk_id, ctypes.pointer(t)) != 0:
errno_ = ctypes.get_errno() errno_ = ctypes.get_errno()

Loading…
Cancel
Save