From 1ac7751e5b9df5f09500aca9e5b34bb9cfa912b4 Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Fri, 9 Dec 2011 07:12:44 +0100 Subject: Initial support of event streaming in cli --- tools.c | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) (limited to 'tools.c') diff --git a/tools.c b/tools.c index c2d8bd7..4874f7f 100644 --- a/tools.c +++ b/tools.c @@ -1,9 +1,12 @@ +#define _POSIX_C_SOURCE 200112L + #include #include #include #include #include #include +#include #include #include @@ -251,20 +254,24 @@ int pcilib_get_page_mask() { return pagemask; } -int calc_deadline(struct timeval *tv, pcilib_timeout_t timeout) { - gettimeofday(tv, NULL); +int pcilib_add_timeout(struct timeval *tv, pcilib_timeout_t timeout) { tv->tv_usec += timeout%1000000; if (tv->tv_usec > 999999) { tv->tv_usec -= 1000000; - tv->tv_sec = 1 + timeout/1000000; + tv->tv_sec += 1 + timeout/1000000; } else { - tv->tv_sec = timeout/1000000; + tv->tv_sec += timeout/1000000; } - +} + +int pcilib_calc_deadline(struct timeval *tv, pcilib_timeout_t timeout) { + gettimeofday(tv, NULL); + pcilib_add_timeout(tv, timeout); + return 0; } -int check_deadline(struct timeval *tve, pcilib_timeout_t timeout) { +int pcilib_check_deadline(struct timeval *tve, pcilib_timeout_t timeout) { int64_t res; struct timeval tvs; @@ -272,12 +279,15 @@ int check_deadline(struct timeval *tve, pcilib_timeout_t timeout) { gettimeofday(&tvs, NULL); res = ((tve->tv_sec - tvs.tv_sec)*1000000 + (tve->tv_usec - tvs.tv_usec)); - if (res < timeout) return 1; + // Hm... Some problems comparing signed and unsigned. So, sign check first + if ((res < 0)||(res < timeout)) { + return 1; + } return 0; } -pcilib_timeout_t calc_time_to_deadline(struct timeval *tve) { +pcilib_timeout_t pcilib_calc_time_to_deadline(struct timeval *tve) { int64_t res; struct timeval tvs; @@ -287,3 +297,23 @@ pcilib_timeout_t calc_time_to_deadline(struct timeval *tve) { if (res < 0) return 0; return res; } + +int pcilib_sleep_until_deadline(struct timeval *tv) { + struct timespec wait; + pcilib_timeout_t duration; + + duration = pcilib_calc_time_to_deadline(tv); + wait.tv_sec = duration / 1000000; + wait.tv_nsec = 1000 * (duration % 1000000); + nanosleep(&wait, NULL); + + return 0; +} + +int pcilib_timecmp(struct timeval *tv1, struct timeval *tv2) { + if (tv1->tv_sec > tv2->tv_sec) return 1; + else if (tv1->tv_sec > tv2->tv_sec) return -1; + else if (tv1->tv_usec > tv2->tv_usec) return 1; + else if (tv1->tv_usec < tv2->tv_usec) return -1; + return 0; +} -- cgit v1.2.3