uloop: use clock_gettime with the monotonic clock instead of using gettimeofday()

Signed-off-by: Felix Fietkau <nbd@openwrt.org>
This commit is contained in:
Felix Fietkau 2013-01-06 03:04:04 +01:00
parent aa4f3bde06
commit dde64e47ca
2 changed files with 23 additions and 2 deletions

View file

@ -1,4 +1,6 @@
cmake_minimum_required(VERSION 2.6) cmake_minimum_required(VERSION 2.6)
INCLUDE(CheckLibraryExists)
INCLUDE(CheckFunctionExists)
PROJECT(ubox C) PROJECT(ubox C)
ADD_DEFINITIONS(-Os -Wall -Werror --std=gnu99 -g3 -Wmissing-declarations) 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}) 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) SET(CMAKE_INSTALL_PREFIX /usr)
FILE(GLOB headers *.h) FILE(GLOB headers *.h)

13
uloop.c
View file

@ -351,6 +351,15 @@ int uloop_timeout_add(struct uloop_timeout *timeout)
return 0; 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) int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
{ {
struct timeval *time = &timeout->time; struct timeval *time = &timeout->time;
@ -358,7 +367,7 @@ int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
if (timeout->pending) if (timeout->pending)
uloop_timeout_cancel(timeout); uloop_timeout_cancel(timeout);
gettimeofday(&timeout->time, NULL); uloop_gettime(&timeout->time);
time->tv_sec += msecs / 1000; time->tv_sec += msecs / 1000;
time->tv_usec += (msecs % 1000) * 1000; time->tv_usec += (msecs % 1000) * 1000;
@ -521,7 +530,7 @@ void uloop_run(void)
uloop_setup_signals(); uloop_setup_signals();
while(!uloop_cancelled) while(!uloop_cancelled)
{ {
gettimeofday(&tv, NULL); uloop_gettime(&tv);
uloop_process_timeouts(&tv); uloop_process_timeouts(&tv);
if (uloop_cancelled) if (uloop_cancelled)
break; break;