Commit d8bd05d0 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Move gettime to kernel.c.

parent 454b486f
#include <sys/time.h>
#include <time.h>
#ifdef __APPLE__ #ifdef __APPLE__
#include "kernel_socket.c" #include "kernel_socket.c"
#else #else
#include "kernel_netlink.c" #include "kernel_netlink.c"
#endif #endif
/* Like gettimeofday, but should return monotonic time. If POSIX clocks
are not available, falls back to gettimeofday. */
int
gettime(struct timeval *tv)
{
#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(CLOCK_MONOTONIC)
static int have_posix_clocks = -1;
if(have_posix_clocks < 0) {
struct timespec ts;
int rc;
rc = clock_gettime(CLOCK_MONOTONIC, &ts);
if(rc < 0) {
have_posix_clocks = 0;
} else {
have_posix_clocks = 1;
}
}
if(have_posix_clocks) {
struct timespec ts;
int rc;
rc = clock_gettime(CLOCK_MONOTONIC, &ts);
if(rc < 0)
return rc;
tv->tv_sec = ts.tv_sec;
tv->tv_usec = ts.tv_nsec / 1000;
return rc;
}
#endif
return gettimeofday(tv, NULL);
}
...@@ -58,3 +58,5 @@ int kernel_route(int operation, const unsigned char *dest, unsigned short plen, ...@@ -58,3 +58,5 @@ int kernel_route(int operation, const unsigned char *dest, unsigned short plen,
int kernel_routes(struct kernel_route *routes, int maxroutes); int kernel_routes(struct kernel_route *routes, int maxroutes);
int kernel_callback(int (*fn)(int, void*), void *closure); int kernel_callback(int (*fn)(int, void*), void *closure);
int kernel_addresses(struct kernel_route *routes, int maxroutes); int kernel_addresses(struct kernel_route *routes, int maxroutes);
int gettime(struct timeval *tv);
...@@ -69,40 +69,6 @@ roughly(int value) ...@@ -69,40 +69,6 @@ roughly(int value)
return value * 3 / 4 + random() % (value / 2); return value * 3 / 4 + random() % (value / 2);
} }
/* Like gettimeofday, but should return monotonic time. If POSIX clocks
are not available, falls back to gettimeofday. */
int
gettime(struct timeval *tv)
{
#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(CLOCK_MONOTONIC)
static int have_posix_clocks = -1;
if(have_posix_clocks < 0) {
struct timespec ts;
int rc;
rc = clock_gettime(CLOCK_MONOTONIC, &ts);
if(rc < 0) {
have_posix_clocks = 0;
} else {
have_posix_clocks = 1;
}
}
if(have_posix_clocks) {
struct timespec ts;
int rc;
rc = clock_gettime(CLOCK_MONOTONIC, &ts);
if(rc < 0)
return rc;
tv->tv_sec = ts.tv_sec;
tv->tv_usec = ts.tv_nsec / 1000;
return rc;
}
#endif
return gettimeofday(tv, NULL);
}
void void
timeval_minus(struct timeval *d, timeval_minus(struct timeval *d,
const struct timeval *s1, const struct timeval *s2) const struct timeval *s1, const struct timeval *s2)
......
...@@ -27,7 +27,6 @@ int seqno_minus(unsigned short s1, unsigned short s2) ...@@ -27,7 +27,6 @@ int seqno_minus(unsigned short s1, unsigned short s2)
unsigned short seqno_plus(unsigned short s, int plus) unsigned short seqno_plus(unsigned short s, int plus)
ATTRIBUTE ((const)); ATTRIBUTE ((const));
int roughly(int value); int roughly(int value);
int gettime(struct timeval *tv);
void timeval_minus(struct timeval *d, void timeval_minus(struct timeval *d,
const struct timeval *s1, const struct timeval *s2); const struct timeval *s1, const struct timeval *s2);
int timeval_minus_msec(const struct timeval *s1, const struct timeval *s2) int timeval_minus_msec(const struct timeval *s1, const struct timeval *s2)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment