Use clock_gettime() instead of gettimeofday() to have a monotonic clock and use CLOCK_CONF_SECOND explicitly

This commit is contained in:
Laurent Deru 2017-12-07 14:09:09 +01:00
parent 00d703a9a2
commit ccb705ed31
3 changed files with 39 additions and 10 deletions

View File

@ -79,11 +79,11 @@ rtimer_arch_schedule(rtimer_clock_t t)
c = t - (unsigned short)clock_time(); c = t - (unsigned short)clock_time();
val.it_value.tv_sec = c / 1000; val.it_value.tv_sec = c / CLOCK_SECOND;
val.it_value.tv_usec = (c % 1000) * 1000; val.it_value.tv_usec = (c % CLOCK_SECOND) * CLOCK_SECOND;
PRINTF("rtimer_arch_schedule time %u %u in %d.%d seconds\n", t, c, c / 1000, PRINTF("rtimer_arch_schedule time %u %u in %d.%d seconds\n", t, c, val.it_value.tv_sec,
(c % 1000) * 1000); val.it_value.tv_usec);
val.it_interval.tv_sec = val.it_interval.tv_usec = 0; val.it_interval.tv_sec = val.it_interval.tv_usec = 0;
setitimer(ITIMER_REAL, &val, NULL); setitimer(ITIMER_REAL, &val, NULL);

View File

@ -19,6 +19,10 @@ else
CONTIKI_TARGET_SOURCEFILES += tun6-net.c CONTIKI_TARGET_SOURCEFILES += tun6-net.c
endif endif
ifeq ($(HOST_OS),Linux)
TARGET_LIBFILES += -lrt
endif
CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES)
.SUFFIXES: .SUFFIXES:

View File

@ -42,24 +42,49 @@
#include <sys/time.h> #include <sys/time.h>
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
clock_time_t typedef struct clock_timespec_s {
clock_time(void) time_t tv_sec;
long tv_nsec;
} clock_timespec_t;
/*---------------------------------------------------------------------------*/
static void
get_time(clock_timespec_t *spec)
{ {
#if defined(__linux__) || (defined(__MACH__) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200)
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
spec->tv_sec = ts.tv_sec;
spec->tv_nsec = ts.tv_nsec;
#else
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000; spec->tv_sec = tv.tv_sec;
spec->tv_nsec = tv.tv_usec * 1000;
#endif
}
/*---------------------------------------------------------------------------*/
clock_time_t
clock_time(void)
{
clock_timespec_t ts;
get_time(&ts);
return ts.tv_sec * CLOCK_SECOND + ts.tv_nsec / (1000000000 / CLOCK_SECOND);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
unsigned long unsigned long
clock_seconds(void) clock_seconds(void)
{ {
struct timeval tv; clock_timespec_t ts;
gettimeofday(&tv, NULL); get_time(&ts);
return tv.tv_sec; return ts.tv_sec;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void