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 memcpy(&tp->end, &tp->begin, sizeof(struct timer)); 69 tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec; 70 tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec; 71 72 tp->expired = timer_expired; 73 } 74 75 struct timer * 76 new_timer(time_t sec, suseconds_t usec) 77 { 78 struct timer *tp; 79 tp = (struct timer *) malloc(sizeof(struct timer)); 80 if (tp == NULL) 81 { 82 perror("malloc"); 83 return NULL; 84 } 85 86 if (gettimeofday(&tp->begin, NULL) < 0) 87 { 88 perror("gettimeofday"); 89 return NULL; 90 } 91 memcpy(&tp->end, &tp->begin, sizeof(struct timer)); 92 tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec; 93 tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec; 94 95 tp->expired = timer_expired; 96 97 return tp; 98 } 99 100 void 101 free_timer(struct timer * tp) 102 { 103 free(tp); 104 } 105 106 int 107 delay(int64_t ns) 108 { 109 struct timespec req, rem; 110 111 req.tv_sec = 0; 112 113 while (ns >= 1000000000L) 114 { 115 ns -= 1000000000L; 116 req.tv_sec += 1; 117 } 118 119 req.tv_nsec = ns; 120 121 while (nanosleep(&req, &rem) == -1) 122 if (EINTR == errno) 123 memcpy(&req, &rem, sizeof rem); 124 else 125 return -1; 126 return 0; 127 } 128 129 # ifdef DELAY_SELECT_METHOD 130 int 131 delay(int us) 132 { 133 struct timeval tv; 134 135 tv.tv_sec = 0; 136 tv.tv_usec = us; 137 (void) select(1, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &tv); 138 return (1); 139 } 140 #endif 141 142 int64_t 143 timer_remaining(struct timer * tp) 144 { 145 struct timeval now; 146 long int end_time = 0, current_time = 0, diff = 0; 147 if (gettimeofday(&now, NULL) < 0) 148 { 149 perror("gettimeofday"); 150 return -1; 151 } 152 end_time += tp->end.tv_sec * 1000000; 153 end_time += tp->end.tv_usec; 154 155 current_time += now.tv_sec * 1000000; 156 current_time += now.tv_usec; 157 158 diff = end_time - current_time; 159 if (diff > 0) 160 return diff; 161 else 162 return 0; 163 } 164