diff options
author | sethdelliott <sethdelliott@gmail.com> | 2010-06-16 21:48:20 +0000 |
---|---|---|
committer | sethdelliott <sethdelliott@gmail.com> | 2010-06-16 21:48:20 +0000 |
commit | ff385f98e97fa0d73745a58ec9c1ff0276d47a5e (patch) | |
tree | 3500cc834816f3a62c2753ac77976834ff5e2543 /src/timer.c | |
parent | 3e402adeb715f4d906c758e07912e3ddcbfe8012 (diff) | |
download | iperf3-ff385f98e97fa0d73745a58ec9c1ff0276d47a5e.tar.gz |
Fixed format and improved readibility of timer.c functions. Also formalized timer.h header.
Diffstat (limited to 'src/timer.c')
-rw-r--r-- | src/timer.c | 75 |
1 files changed, 33 insertions, 42 deletions
diff --git a/src/timer.c b/src/timer.c index 4d2487e..6c3b758 100644 --- a/src/timer.c +++ b/src/timer.c @@ -9,12 +9,10 @@ #include "timer.h" - - double timeval_to_double(struct timeval * tv) { - double d; + double d; d = tv->tv_sec + tv->tv_usec / 1000000; @@ -24,46 +22,41 @@ timeval_to_double(struct timeval * tv) double timeval_diff(struct timeval * tv0, struct timeval * tv1) { - return ((tv1->tv_sec - tv0->tv_sec) + (abs(tv1->tv_usec - tv0->tv_usec) / 1000000.0)); + return ((abs(tv1->tv_sec - tv0->tv_sec)) + (abs(tv1->tv_usec - tv0->tv_usec) / 1000000.0)); } int timer_expired(struct timer * tp) { /* for timer with zero time */ - if (tp->end.tv_sec == tp->begin.tv_sec && tp->end.tv_usec == tp->begin.tv_usec) - { - //printf(" timer_expired: begining and end times are equal \n"); - return 0; + if (tp->end.tv_sec == tp->begin.tv_sec && tp->end.tv_usec == tp->begin.tv_usec) { + return 0; } struct timeval now; - int64_t end = 0, current = 0, diff = 0; + int64_t end = 0, current = 0; - //printf("checking if timer has expired \n"); - if (gettimeofday(&now, NULL) < 0) - { - perror("gettimeofday"); - return -1; + if (gettimeofday(&now, NULL) < 0) { + perror("gettimeofday"); + return -1; } + end += tp->end.tv_sec * 1000000; end += tp->end.tv_usec; current += now.tv_sec * 1000000; current += now.tv_usec; - diff = end - current; - return diff <= 0; - + return current > end; } void update_timer(struct timer * tp, time_t sec, suseconds_t usec) { - if (gettimeofday(&tp->begin, NULL) < 0) - { - perror("gettimeofday"); + if (gettimeofday(&tp->begin, NULL) < 0) { + perror("gettimeofday"); } + tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec; tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec; @@ -75,16 +68,14 @@ new_timer(time_t sec, suseconds_t usec) { struct timer *tp = NULL; tp = (struct timer *) calloc(1, sizeof(struct timer)); - if (tp == NULL) - { - perror("malloc"); - return NULL; + if (tp == NULL) { + perror("malloc"); + return NULL; } - if (gettimeofday(&tp->begin, NULL) < 0) - { - perror("gettimeofday"); - return NULL; + if (gettimeofday(&tp->begin, NULL) < 0) { + perror("gettimeofday"); + return NULL; } tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec; @@ -108,19 +99,18 @@ delay(int64_t ns) req.tv_sec = 0; - while (ns >= 1000000000L) - { - ns -= 1000000000L; - req.tv_sec += 1; + while (ns >= 1000000000L) { + ns -= 1000000000L; + req.tv_sec += 1; } req.tv_nsec = ns; while (nanosleep(&req, &rem) == -1) - if (EINTR == errno) - memcpy(&req, &rem, sizeof rem); - else - return -1; + if (EINTR == errno) + memcpy(&req, &rem, sizeof rem); + else + return -1; return 0; } @@ -142,11 +132,12 @@ timer_remaining(struct timer * tp) { struct timeval now; long int end_time = 0, current_time = 0, diff = 0; - if (gettimeofday(&now, NULL) < 0) - { - perror("gettimeofday"); - return -1; + + if (gettimeofday(&now, NULL) < 0) { + perror("gettimeofday"); + return -1; } + end_time += tp->end.tv_sec * 1000000; end_time += tp->end.tv_usec; @@ -155,7 +146,7 @@ timer_remaining(struct timer * tp) diff = end_time - current_time; if (diff > 0) - return diff; + return diff; else - return 0; + return 0; } |