diff options
author | Suren A. Chilingaryan <csa@dside.dyndns.org> | 2011-12-09 07:12:44 +0100 |
---|---|---|
committer | Suren A. Chilingaryan <csa@dside.dyndns.org> | 2011-12-09 07:12:44 +0100 |
commit | 1ac7751e5b9df5f09500aca9e5b34bb9cfa912b4 (patch) | |
tree | 42e3705aa8b20fc7f03025cb83ae2f928350174d /tools.c | |
parent | c3812d3569aa98e23953eea323d980912580e659 (diff) | |
download | pcitool-1ac7751e5b9df5f09500aca9e5b34bb9cfa912b4.tar.gz pcitool-1ac7751e5b9df5f09500aca9e5b34bb9cfa912b4.tar.bz2 pcitool-1ac7751e5b9df5f09500aca9e5b34bb9cfa912b4.tar.xz pcitool-1ac7751e5b9df5f09500aca9e5b34bb9cfa912b4.zip |
Initial support of event streaming in cli
Diffstat (limited to 'tools.c')
-rw-r--r-- | tools.c | 46 |
1 files changed, 38 insertions, 8 deletions
@@ -1,9 +1,12 @@ +#define _POSIX_C_SOURCE 200112L + #include <stdio.h> #include <string.h> #include <unistd.h> #include <stdint.h> #include <assert.h> #include <ctype.h> +#include <time.h> #include <arpa/inet.h> #include <sys/time.h> @@ -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; +} |