2009-02-24 06:22:58 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "timer.h"
|
|
|
|
|
2009-06-29 19:30:39 +00:00
|
|
|
|
|
|
|
|
2009-07-02 01:07:32 +00:00
|
|
|
double
|
2009-10-19 22:35:18 +00:00
|
|
|
timeval_to_double(struct timeval * tv)
|
2009-06-29 19:30:39 +00:00
|
|
|
{
|
2009-10-19 22:35:18 +00:00
|
|
|
double d;
|
|
|
|
|
|
|
|
d = tv->tv_sec + tv->tv_usec / 1000000;
|
|
|
|
|
2009-06-29 19:30:39 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2009-07-02 01:07:32 +00:00
|
|
|
double
|
2009-10-19 22:35:18 +00:00
|
|
|
timeval_diff(struct timeval * tv0, struct timeval * tv1)
|
2009-06-29 19:30:39 +00:00
|
|
|
{
|
2009-10-19 22:35:18 +00:00
|
|
|
//return timeval_to_double(tv1) - timeval_to_double(tv0);
|
|
|
|
return (tv1->tv_sec - tv0->tv_sec) + abs(tv1->tv_usec - tv0->tv_usec) / 1000000.0;
|
2009-06-29 19:30:39 +00:00
|
|
|
}
|
|
|
|
|
2009-02-24 06:22:58 +00:00
|
|
|
int
|
2009-10-19 22:35:18 +00:00
|
|
|
timer_expired(struct timer * tp)
|
|
|
|
{
|
|
|
|
/* for timer with zero time */
|
2009-07-28 20:21:30 +00:00
|
|
|
if (tp->end.tv_sec == tp->begin.tv_sec && tp->end.tv_usec == tp->begin.tv_usec)
|
2009-10-19 22:35:18 +00:00
|
|
|
return 0;
|
|
|
|
|
2009-06-23 00:07:36 +00:00
|
|
|
struct timeval now;
|
2009-10-19 22:35:18 +00:00
|
|
|
int64_t end = 0, current = 0, diff = 0;
|
|
|
|
if (gettimeofday(&now, NULL) < 0)
|
|
|
|
{
|
|
|
|
perror("gettimeofday");
|
|
|
|
return -1;
|
2009-06-23 00:07:36 +00:00
|
|
|
}
|
2009-10-19 22:35:18 +00:00
|
|
|
end += tp->end.tv_sec * 1000000;
|
|
|
|
end += tp->end.tv_usec;
|
|
|
|
|
|
|
|
current += now.tv_sec * 1000000;
|
|
|
|
current += now.tv_usec;
|
|
|
|
|
2009-07-09 23:44:05 +00:00
|
|
|
diff = end - current;
|
2009-10-19 22:35:18 +00:00
|
|
|
return diff <= 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* currently using microsecond limit. Else we need to introduce timespec
|
|
|
|
* instread of timeval
|
|
|
|
*/
|
|
|
|
}
|
2009-06-23 00:07:36 +00:00
|
|
|
|
2009-07-02 01:07:32 +00:00
|
|
|
void
|
2009-10-19 22:35:18 +00:00
|
|
|
update_timer(struct timer * tp, time_t sec, suseconds_t usec)
|
2009-07-02 01:07:32 +00:00
|
|
|
{
|
2009-10-19 22:35:18 +00:00
|
|
|
if (gettimeofday(&tp->begin, NULL) < 0)
|
|
|
|
{
|
|
|
|
perror("gettimeofday");
|
2009-07-02 01:07:32 +00:00
|
|
|
}
|
|
|
|
memcpy(&tp->end, &tp->begin, sizeof(struct timer));
|
|
|
|
tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec;
|
|
|
|
tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec;
|
2009-10-19 22:35:18 +00:00
|
|
|
|
2009-07-02 01:07:32 +00:00
|
|
|
tp->expired = timer_expired;
|
|
|
|
}
|
2009-06-23 00:07:36 +00:00
|
|
|
|
2009-02-24 06:22:58 +00:00
|
|
|
struct timer *
|
|
|
|
new_timer(time_t sec, suseconds_t usec)
|
|
|
|
{
|
|
|
|
struct timer *tp;
|
|
|
|
tp = (struct timer *) malloc(sizeof(struct timer));
|
|
|
|
|
2009-10-19 22:35:18 +00:00
|
|
|
if (gettimeofday(&tp->begin, NULL) < 0)
|
|
|
|
{
|
|
|
|
perror("gettimeofday");
|
|
|
|
return NULL;
|
2009-02-24 06:22:58 +00:00
|
|
|
}
|
|
|
|
memcpy(&tp->end, &tp->begin, sizeof(struct timer));
|
|
|
|
tp->end.tv_sec = tp->begin.tv_sec + (time_t) sec;
|
|
|
|
tp->end.tv_usec = tp->begin.tv_usec + (time_t) usec;
|
2009-10-19 22:35:18 +00:00
|
|
|
|
2009-06-29 19:30:39 +00:00
|
|
|
tp->expired = timer_expired;
|
2009-10-19 22:35:18 +00:00
|
|
|
|
2009-02-24 06:22:58 +00:00
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-10-19 22:35:18 +00:00
|
|
|
free_timer(struct timer * tp)
|
2009-02-24 06:22:58 +00:00
|
|
|
{
|
|
|
|
free(tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
delay(int64_t ns)
|
|
|
|
{
|
|
|
|
struct timespec req, rem;
|
|
|
|
|
|
|
|
req.tv_sec = 0;
|
|
|
|
|
2009-10-19 22:35:18 +00:00
|
|
|
while (ns >= 1000000000L)
|
2009-02-24 06:22:58 +00:00
|
|
|
{
|
2009-10-19 22:35:18 +00:00
|
|
|
ns -= 1000000000L;
|
|
|
|
req.tv_sec += 1;
|
2009-02-24 06:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req.tv_nsec = ns;
|
|
|
|
|
2009-10-19 22:35:18 +00:00
|
|
|
while (nanosleep(&req, &rem) == -1)
|
|
|
|
if (EINTR == errno)
|
|
|
|
memcpy(&req, &rem, sizeof rem);
|
|
|
|
else
|
|
|
|
return -1;
|
2009-02-24 06:22:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-22 20:12:57 +00:00
|
|
|
# ifdef DELAY_SELECT_METHOD
|
|
|
|
int
|
|
|
|
delay(int us)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = us;
|
|
|
|
(void) select(1, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &tv);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-10-19 22:35:18 +00:00
|
|
|
int64_t
|
|
|
|
timer_remaining(struct timer * tp)
|
2009-07-02 01:07:32 +00:00
|
|
|
{
|
|
|
|
struct timeval now;
|
2009-10-19 22:35:18 +00:00
|
|
|
long int end_time = 0, current_time = 0, diff = 0;
|
|
|
|
if (gettimeofday(&now, NULL) < 0)
|
|
|
|
{
|
|
|
|
perror("gettimeofday");
|
|
|
|
return -1;
|
2009-07-02 01:07:32 +00:00
|
|
|
}
|
2009-10-19 22:35:18 +00:00
|
|
|
end_time += tp->end.tv_sec * 1000000;
|
|
|
|
end_time += tp->end.tv_usec;
|
|
|
|
|
|
|
|
current_time += now.tv_sec * 1000000;
|
|
|
|
current_time += now.tv_usec;
|
|
|
|
|
2009-07-06 18:37:21 +00:00
|
|
|
diff = end_time - current_time;
|
2009-10-19 22:35:18 +00:00
|
|
|
if (diff > 0)
|
|
|
|
return diff;
|
2009-07-02 01:07:32 +00:00
|
|
|
else
|
2009-10-19 22:35:18 +00:00
|
|
|
return 0;
|
2009-07-02 01:07:32 +00:00
|
|
|
}
|