diff options
author | Rémi Lefèvre <remi.lefevre@parrot.com> | 2016-12-13 18:43:02 +0100 |
---|---|---|
committer | Rémi Lefèvre <remi.lefevre@parrot.com> | 2017-01-23 17:39:46 +0100 |
commit | aef5572053fa8c55a0ded6ed08e4250bfc8b7986 (patch) | |
tree | c88307473dcf818e59dfc0791808ba33b212b3a3 /src/clocks.c | |
parent | e2ecbe4e473988da5387fcd018def79bc11fed77 (diff) |
complete and factorize clocks API
Signed-off-by: Rémi Lefèvre <remi.lefevre@parrot.com>
Diffstat (limited to 'src/clocks.c')
-rw-r--r-- | src/clocks.c | 56 |
1 files changed, 32 insertions, 24 deletions
diff --git a/src/clocks.c b/src/clocks.c index f8dc9b6283..88d3783e8e 100644 --- a/src/clocks.c +++ b/src/clocks.c @@ -11,55 +11,63 @@ inline int clock_gettime(clockid_t clk_id, struct timespec *ts) { } #endif -inline time_t now_realtime_sec(void) { +static inline time_t now_sec(clockid_t clk_id) { struct timespec ts; - if(unlikely(clock_gettime(CLOCK_REALTIME, &ts) == -1)) + if(unlikely(clock_gettime(clk_id, &ts) == -1)) return 0; return ts.tv_sec; } -inline int now_realtime_timeval(struct timeval *tv) { +static inline usec_t now_usec(clockid_t clk_id) { + struct timespec ts; + if(unlikely(clock_gettime(clk_id, &ts) == -1)) + return 0; + return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC; +} + +static inline int now_timeval(clockid_t clk_id, struct timeval *tv) { struct timespec ts; - if(unlikely(clock_gettime(CLOCK_REALTIME, &ts) == -1)) + if(unlikely(clock_gettime(clk_id, &ts) == -1)) return -1; tv->tv_sec = ts.tv_sec; tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC; return 0; } +inline time_t now_realtime_sec(void) { + return now_sec(CLOCK_REALTIME); +} + inline usec_t now_realtime_usec(void) { - struct timespec ts; - if(unlikely(clock_gettime(CLOCK_REALTIME, &ts) == -1)) - return 0; - return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC; + return now_usec(CLOCK_REALTIME); +} + +inline int now_realtime_timeval(struct timeval *tv) { + return now_timeval(CLOCK_REALTIME, tv); } inline time_t now_monotonic_sec(void) { - struct timespec ts; - if(unlikely(clock_gettime(CLOCK_MONOTONIC, &ts) == -1)) - return 0; - return ts.tv_sec; + return now_sec(CLOCK_MONOTONIC); } inline usec_t now_monotonic_usec(void) { - struct timespec ts; - if(unlikely(clock_gettime(CLOCK_MONOTONIC, &ts) == -1)) - return 0; - return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC; + return now_usec(CLOCK_MONOTONIC); +} + +inline int now_monotonic_timeval(struct timeval *tv) { + return now_timeval(CLOCK_MONOTONIC, tv); } inline time_t now_boottime_sec(void) { - struct timespec ts; - if(unlikely(clock_gettime(CLOCK_BOOTTIME, &ts) == -1)) - return 0; - return ts.tv_sec; + return now_sec(CLOCK_BOOTTIME); } inline usec_t now_boottime_usec(void) { - struct timespec ts; - if(unlikely(clock_gettime(CLOCK_BOOTTIME, &ts) == -1)) - return 0; - return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC; + return now_usec(CLOCK_BOOTTIME); +} + +inline int now_boottime_timeval(struct timeval *tv) { + return now_timeval(CLOCK_BOOTTIME, tv); } inline usec_t timeval_usec(struct timeval *tv) { |