From dde64e47cadbd245a99f145869eb400b68cc82e9 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sun, 6 Jan 2013 03:04:04 +0100 Subject: [PATCH] uloop: use clock_gettime with the monotonic clock instead of using gettimeofday() Signed-off-by: Felix Fietkau --- CMakeLists.txt | 12 ++++++++++++ uloop.c | 13 +++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ab82270..b756247 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,6 @@ cmake_minimum_required(VERSION 2.6) +INCLUDE(CheckLibraryExists) +INCLUDE(CheckFunctionExists) PROJECT(ubox C) ADD_DEFINITIONS(-Os -Wall -Werror --std=gnu99 -g3 -Wmissing-declarations) @@ -14,6 +16,16 @@ SET(SOURCES avl.c avl-cmp.c blob.c blobmsg.c uloop.c usock.c ustream.c ustream-f ADD_LIBRARY(ubox SHARED ${SOURCES}) +SET(LIBS) +CHECK_FUNCTION_EXISTS(clock_gettime HAVE_GETTIME) +IF(NOT HAVE_GETTIME) + CHECK_LIBRARY_EXISTS(rt clock_gettime "" NEED_GETTIME) + IF(NEED_GETTIME) + TARGET_LINK_LIBRARIES(ubox rt) + ENDIF() +ENDIF() + + SET(CMAKE_INSTALL_PREFIX /usr) FILE(GLOB headers *.h) diff --git a/uloop.c b/uloop.c index ecf26ec..2de4feb 100644 --- a/uloop.c +++ b/uloop.c @@ -351,6 +351,15 @@ int uloop_timeout_add(struct uloop_timeout *timeout) return 0; } +static void uloop_gettime(struct timeval *tv) +{ + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / 1000; +} + int uloop_timeout_set(struct uloop_timeout *timeout, int msecs) { struct timeval *time = &timeout->time; @@ -358,7 +367,7 @@ int uloop_timeout_set(struct uloop_timeout *timeout, int msecs) if (timeout->pending) uloop_timeout_cancel(timeout); - gettimeofday(&timeout->time, NULL); + uloop_gettime(&timeout->time); time->tv_sec += msecs / 1000; time->tv_usec += (msecs % 1000) * 1000; @@ -521,7 +530,7 @@ void uloop_run(void) uloop_setup_signals(); while(!uloop_cancelled) { - gettimeofday(&tv, NULL); + uloop_gettime(&tv); uloop_process_timeouts(&tv); if (uloop_cancelled) break; -- 2.25.1