uloop: use clock_gettime with the monotonic clock instead of using gettimeofday()
authorFelix Fietkau <nbd@openwrt.org>
Sun, 6 Jan 2013 02:04:04 +0000 (03:04 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 6 Jan 2013 02:04:04 +0000 (03:04 +0100)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
CMakeLists.txt
uloop.c

index ab82270..b756247 100644 (file)
@@ -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 (file)
--- 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;