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
|
|
|
|
|
|
|
|
|
|
|
double timeval_to_double(struct timeval *tv)
|
|
|
|
{
|
|
|
|
double d;
|
|
|
|
|
|
|
|
d = tv->tv_sec + tv->tv_usec /1000000;
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double timeval_diff(struct timeval *tv0, struct timeval *tv1)
|
|
|
|
{
|
|
|
|
return timeval_to_double(tv1) - timeval_to_double(tv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-02-24 06:22:58 +00:00
|
|
|
int
|
|
|
|
timer_expired(struct timer *tp)
|
|
|
|
{
|
|
|
|
struct timeval now;
|
2009-06-29 19:30:39 +00:00
|
|
|
double d= 0;
|
2009-02-24 06:22:58 +00:00
|
|
|
if(gettimeofday(&now, NULL) < 0) {
|
|
|
|
perror("gettimeofday");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-06-29 19:30:39 +00:00
|
|
|
|
|
|
|
d = timeval_to_double(&tp->end) - timeval_to_double(&now);
|
|
|
|
|
|
|
|
return d <= 0;
|
2009-02-24 06:22:58 +00:00
|
|
|
}
|
2009-06-29 19:30:39 +00:00
|
|
|
*/
|
2009-02-24 06:22:58 +00:00
|
|
|
|
2009-06-23 00:07:36 +00:00
|
|
|
int
|
2009-06-29 19:30:39 +00:00
|
|
|
timer_expired(struct timer *tp)
|
2009-06-23 00:07:36 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
struct timeval now;
|
2009-06-29 19:30:39 +00:00
|
|
|
int64_t end = 0, current= 0, diff= 0;
|
2009-06-23 00:07:36 +00:00
|
|
|
if(gettimeofday(&now, NULL) < 0) {
|
|
|
|
perror("gettimeofday");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-06-29 19:30:39 +00:00
|
|
|
end+= tp->end.tv_sec * 1000000 ;
|
|
|
|
end+= tp->end.tv_usec;
|
2009-06-23 00:07:36 +00:00
|
|
|
|
|
|
|
current+= now.tv_sec * 1000000 ;
|
|
|
|
current+= now.tv_usec;
|
|
|
|
|
2009-06-29 19:30:39 +00:00
|
|
|
diff = end - current;
|
2009-06-23 00:07:36 +00:00
|
|
|
|
2009-06-29 19:30:39 +00:00
|
|
|
return diff <= 0;
|
2009-06-23 00:07:36 +00:00
|
|
|
|
2009-06-29 19:30:39 +00:00
|
|
|
}
|
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));
|
|
|
|
|
|
|
|
if(gettimeofday(&tp->begin, NULL) < 0) {
|
|
|
|
perror("gettimeofday");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-29 19:30:39 +00:00
|
|
|
|
|
|
|
tp->expired = timer_expired;
|
2009-06-23 00:07:36 +00:00
|
|
|
|
2009-02-24 06:22:58 +00:00
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
free_timer(struct timer *tp)
|
|
|
|
{
|
|
|
|
free(tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
delay(int64_t ns)
|
|
|
|
{
|
|
|
|
struct timespec req, rem;
|
|
|
|
|
|
|
|
req.tv_sec = 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-26 00:55:56 +00:00
|
|
|
|