1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <sys/time.h> 5 #include <sys/errno.h> 6 #include <sys/types.h> 7 #include <stdint.h> 8 #include <time.h> 9 10 #include "timer.h" 11 12 13 14 double 15 timeval_to_double(struct timeval * tv) 16 { 17 double d; 18 19 d = tv->tv_sec + tv->tv_usec / 1000000; 20 21 return d; 22 } 23 24 double 25 timeval_diff(struct timeval * tv0, struct timeval * tv1) 26 { 27 //return timeval_to_double(tv1) - timeval_to_double(tv0); 28 return (tv1->tv_sec - tv0->tv_sec) + abs(tv1->tv_usec - tv0->tv_usec) / 1000000.0; 29 } 30 31 int 32 timer_expired(struct timer * tp) 33 { 34 /* for timer with zero time */ 35 if (tp->end.tv_sec == tp->begin.tv_sec && tp->end.tv_usec == tp->begin.tv_usec) 36 { 37 //printf(" timer_expired: begining and end times are equal \n"); 38 return 0; 39 } 40 41 struct timeval now; 42 int64_t end = 0, current = 0, diff = 0; 43 44 //printf("checking if timer has expired \n"); 45 if (gettimeofday(&now, NULL) < 0) 46 { 47 perror("gettimeofday"); 48 return -1; 49 } 50 end += tp->end.tv_sec * 1000000; 51 end += tp->end.tv_usec; 52 53 current += now.tv_sec * 1000000; 54 current += now.tv_usec; 55 56 diff = end - current; 57 return diff <= 0; 58 59 } 60 61 void 62 update_timer(struct timer * tp, time_t sec, suseconds_t usec) 63 { 64 if (gettimeofday(&tp->begin, NULL) < 0) 65 { 66 perror("gettimeofday"); 67 } 68 tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec; 69 tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec; 70 71 tp->expired = timer_expired; 72 } 73 74 struct timer * 75 new_timer(time_t sec, suseconds_t usec) 76 { 77 struct timer *tp = NULL; 78 tp = (struct timer *) calloc(1, sizeof(struct timer)); 79 if (tp == NULL) 80 { 81 perror("malloc"); 82 return NULL; 83 } 84 85 if (gettimeofday(&tp->begin, NULL) < 0) 86 { 87 perror("gettimeofday"); 88 return NULL; 89 } 90 91 tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec; 92 tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec; 93 94 tp->expired = timer_expired; 95 96 return tp; 97 } 98 99 void 100 free_timer(struct timer * tp) 101 { 102 free(tp); 103 } 104 105 int 106 delay(int64_t ns) 107 { 108 struct timespec req, rem; 109 110 req.tv_sec = 0; 111 112 while (ns >= 1000000000L) 113 { 114 ns -= 1000000000L; 115 req.tv_sec += 1; 116 } 117 118 req.tv_nsec = ns; 119 120 while (nanosleep(&req, &rem) == -1) 121 if (EINTR == errno) 122 memcpy(&req, &rem, sizeof rem); 123 else 124 return -1; 125 return 0; 126 } 127 128 # ifdef DELAY_SELECT_METHOD 129 int 130 delay(int us) 131 { 132 struct timeval tv; 133 134 tv.tv_sec = 0; 135 tv.tv_usec = us; 136 (void) select(1, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &tv); 137 return (1); 138 } 139 #endif 140 141 int64_t 142 timer_remaining(struct timer * tp) 143 { 144 struct timeval now; 145 long int end_time = 0, current_time = 0, diff = 0; 146 if (gettimeofday(&now, NULL) < 0) 147 { 148 perror("gettimeofday"); 149 return -1; 150 } 151 end_time += tp->end.tv_sec * 1000000; 152 end_time += tp->end.tv_usec; 153 154 current_time += now.tv_sec * 1000000; 155 current_time += now.tv_usec; 156 157 diff = end_time - current_time; 158 if (diff > 0) 159 return diff; 160 else 161 return 0; 162 } 163