|
|
@@ -1,6 +1,6 @@
|
|
|
/*
|
|
|
* Copyright (c) 2003-2004 MontaVista Software, Inc.
|
|
|
- * Copyright (c) 2006 Red Hat, Inc.
|
|
|
+ * Copyright (c) 2006-2007 Red Hat, Inc.
|
|
|
*
|
|
|
* All rights reserved.
|
|
|
*
|
|
|
@@ -41,6 +41,7 @@
|
|
|
#include <stdlib.h>
|
|
|
#include <errno.h>
|
|
|
#include <string.h>
|
|
|
+#include <sys/param.h>
|
|
|
|
|
|
#include "../include/list.h"
|
|
|
|
|
|
@@ -53,7 +54,7 @@ struct timerlist {
|
|
|
|
|
|
struct timerlist_timer {
|
|
|
struct list_head list;
|
|
|
- struct timeval tv;
|
|
|
+ unsigned long long nano_from_epoch;
|
|
|
void (*timer_fn)(void *data);
|
|
|
void *data;
|
|
|
timer_handle handle_addr;
|
|
|
@@ -64,10 +65,15 @@ static inline void timerlist_init (struct timerlist *timerlist)
|
|
|
list_init (&timerlist->timer_head);
|
|
|
}
|
|
|
|
|
|
-static inline void timeval_adjust_to_msec (struct timeval *tv) {
|
|
|
- tv->tv_usec = (tv->tv_usec / 1000) * 1000;
|
|
|
-}
|
|
|
+static inline unsigned long long timerlist_nano_from_epoch (void)
|
|
|
+{
|
|
|
+ unsigned long long nano_from_epoch;
|
|
|
+ struct timeval time_from_epoch;
|
|
|
+ gettimeofday (&time_from_epoch, 0);
|
|
|
|
|
|
+ nano_from_epoch = ((time_from_epoch.tv_sec * 1000000000ULL) + (time_from_epoch.tv_usec * 1000ULL));
|
|
|
+ return (nano_from_epoch);
|
|
|
+}
|
|
|
|
|
|
static inline void timerlist_add (struct timerlist *timerlist, struct timerlist_timer *timer)
|
|
|
{
|
|
|
@@ -75,8 +81,6 @@ static inline void timerlist_add (struct timerlist *timerlist, struct timerlist_
|
|
|
struct timerlist_timer *timer_from_list;
|
|
|
int found;
|
|
|
|
|
|
- timeval_adjust_to_msec (&timer->tv);
|
|
|
-//printf ("Adding timer %d %d\n", timer->tv.tv_sec, timer->tv.tv_usec);
|
|
|
for (found = 0, timer_list = timerlist->timer_head.next;
|
|
|
timer_list != &timerlist->timer_head;
|
|
|
timer_list = timer_list->next) {
|
|
|
@@ -84,9 +88,7 @@ static inline void timerlist_add (struct timerlist *timerlist, struct timerlist_
|
|
|
timer_from_list = list_entry (timer_list,
|
|
|
struct timerlist_timer, list);
|
|
|
|
|
|
- if ((timer_from_list->tv.tv_sec > timer->tv.tv_sec) ||
|
|
|
- ((timer_from_list->tv.tv_sec == timer->tv.tv_sec) &&
|
|
|
- (timer_from_list->tv.tv_usec > timer->tv.tv_usec))) {
|
|
|
+ if (timer_from_list->nano_from_epoch > timer->nano_from_epoch) {
|
|
|
list_add (&timer->list, timer_list->prev);
|
|
|
found = 1;
|
|
|
break; /* for timer iteration */
|
|
|
@@ -97,16 +99,13 @@ static inline void timerlist_add (struct timerlist *timerlist, struct timerlist_
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-static inline int timerlist_add_future (struct timerlist *timerlist,
|
|
|
+static inline int timerlist_add_absolute (struct timerlist *timerlist,
|
|
|
void (*timer_fn) (void *data),
|
|
|
void *data,
|
|
|
- unsigned int msec_in_future,
|
|
|
+ unsigned long long nano_from_epoch,
|
|
|
timer_handle *handle)
|
|
|
{
|
|
|
struct timerlist_timer *timer;
|
|
|
- struct timeval current_time;
|
|
|
- unsigned int seconds;
|
|
|
- unsigned int mseconds;
|
|
|
|
|
|
timer = (struct timerlist_timer *)malloc (sizeof (struct timerlist_timer));
|
|
|
if (timer == 0) {
|
|
|
@@ -114,17 +113,31 @@ static inline int timerlist_add_future (struct timerlist *timerlist,
|
|
|
return (-1);
|
|
|
}
|
|
|
|
|
|
- seconds = msec_in_future / 1000;
|
|
|
- mseconds = msec_in_future % 1000;
|
|
|
-
|
|
|
- gettimeofday (¤t_time, 0);
|
|
|
- timeval_adjust_to_msec (¤t_time);
|
|
|
- timer->tv.tv_sec = current_time.tv_sec + seconds;
|
|
|
- timer->tv.tv_usec = current_time.tv_usec + mseconds * 1000;
|
|
|
- if (timer->tv.tv_usec >= 1000000) {
|
|
|
- timer->tv.tv_sec++;
|
|
|
- timer->tv.tv_usec -= 1000000;
|
|
|
+ timer->nano_from_epoch = nano_from_epoch;
|
|
|
+ timer->data = data;
|
|
|
+ timer->timer_fn = timer_fn;
|
|
|
+ timer->handle_addr = handle;
|
|
|
+ timerlist_add (timerlist, timer);
|
|
|
+
|
|
|
+ *handle = timer;
|
|
|
+ return (0);
|
|
|
+}
|
|
|
+
|
|
|
+static inline int timerlist_add_duration (struct timerlist *timerlist,
|
|
|
+ void (*timer_fn) (void *data),
|
|
|
+ void *data,
|
|
|
+ unsigned long long nano_duration,
|
|
|
+ timer_handle *handle)
|
|
|
+{
|
|
|
+ struct timerlist_timer *timer;
|
|
|
+
|
|
|
+ timer = (struct timerlist_timer *)malloc (sizeof (struct timerlist_timer));
|
|
|
+ if (timer == 0) {
|
|
|
+ errno = ENOMEM;
|
|
|
+ return (-1);
|
|
|
}
|
|
|
+
|
|
|
+ timer->nano_from_epoch = timerlist_nano_from_epoch() + nano_duration;
|
|
|
timer->data = data;
|
|
|
timer->timer_fn = timer_fn;
|
|
|
timer->handle_addr = handle;
|
|
|
@@ -141,7 +154,7 @@ static inline void timerlist_del (struct timerlist *timerlist, timer_handle time
|
|
|
memset (timer->handle_addr, 0, sizeof (struct timerlist_timer *));
|
|
|
/*
|
|
|
* If the next timer after the currently expiring timer because
|
|
|
- * timerlist_del is called from a timer handler, get to the enxt
|
|
|
+ * timerlist_del is called from a timer handler, get to the next
|
|
|
* timer
|
|
|
*/
|
|
|
if (timerlist->timer_iter == &timer->list) {
|
|
|
@@ -152,16 +165,6 @@ static inline void timerlist_del (struct timerlist *timerlist, timer_handle time
|
|
|
free (timer);
|
|
|
}
|
|
|
|
|
|
-static inline void timerlist_del_data (struct timerlist *timerlist, timer_handle timer_handle)
|
|
|
-{
|
|
|
- struct timerlist_timer *timer = (struct timerlist_timer *)timer_handle;
|
|
|
-
|
|
|
- if (timer->data) {
|
|
|
- free (timer->data);
|
|
|
- }
|
|
|
- timerlist_del(timerlist,timer_handle);
|
|
|
-}
|
|
|
-
|
|
|
static inline void timerlist_pre_dispatch (struct timerlist *timerlist, timer_handle timer_handle)
|
|
|
{
|
|
|
struct timerlist_timer *timer = (struct timerlist_timer *)timer_handle;
|
|
|
@@ -178,57 +181,14 @@ static inline void timerlist_post_dispatch (struct timerlist *timerlist, timer_h
|
|
|
free (timer);
|
|
|
}
|
|
|
|
|
|
-#ifdef CODE_COVERAGE_COMPILE_OUT
|
|
|
-static inline int timer_expire_get_tv (struct timerlist *timerlist, struct timeval *tv)
|
|
|
-{
|
|
|
- struct timeval current_time;
|
|
|
- struct timerlist_timer *timer_from_list;
|
|
|
-
|
|
|
- /*
|
|
|
- * empty list, no expire
|
|
|
- */
|
|
|
- if (timerlist->timer_head.next == &timerlist->timer_head) {
|
|
|
- return (-1);
|
|
|
- }
|
|
|
-
|
|
|
- timer_from_list = list_entry (timerlist->timer_head.next,
|
|
|
- struct timerlist_timer, list);
|
|
|
-
|
|
|
- gettimeofday (¤t_time, 0);
|
|
|
- timeval_adjust_to_msec (¤t_time);
|
|
|
-
|
|
|
- /*
|
|
|
- * timer at head of list is expired, zero msecs required
|
|
|
- */
|
|
|
- if ((timer_from_list->tv.tv_sec < current_time.tv_sec) ||
|
|
|
- ((timer_from_list->tv.tv_sec == current_time.tv_sec) &&
|
|
|
- (timer_from_list->tv.tv_usec <= current_time.tv_usec))) {
|
|
|
-
|
|
|
- tv->tv_sec = 0;
|
|
|
- tv->tv_usec = 0;
|
|
|
- }
|
|
|
-
|
|
|
- tv->tv_sec = timer_from_list->tv.tv_sec - current_time.tv_sec;
|
|
|
- tv->tv_usec = timer_from_list->tv.tv_usec - current_time.tv_usec;
|
|
|
- if (tv->tv_usec < 0) {
|
|
|
- tv->tv_sec -= 1;
|
|
|
- tv->tv_usec += 1000000;
|
|
|
- }
|
|
|
- if (tv->tv_sec < 0) {
|
|
|
- tv->tv_sec = 0;
|
|
|
- tv->tv_usec = 0;
|
|
|
- }
|
|
|
-
|
|
|
- timeval_adjust_to_msec (tv);
|
|
|
- return (0);
|
|
|
-}
|
|
|
-#endif /* CODE_COVERAGE_COMPILE_OUT */
|
|
|
-
|
|
|
-static inline unsigned int timerlist_timeout_msec (struct timerlist *timerlist)
|
|
|
+/*
|
|
|
+ * returns the number of msec until the next timer will expire for use with poll
|
|
|
+ */
|
|
|
+static inline unsigned long long timerlist_msec_duration_to_expire (struct timerlist *timerlist)
|
|
|
{
|
|
|
- struct timeval current_time;
|
|
|
struct timerlist_timer *timer_from_list;
|
|
|
- int time_in_msec;
|
|
|
+ volatile unsigned long long nano_from_epoch;
|
|
|
+ volatile unsigned long long msec_duration_to_expire;
|
|
|
|
|
|
/*
|
|
|
* empty list, no expire
|
|
|
@@ -240,32 +200,30 @@ static inline unsigned int timerlist_timeout_msec (struct timerlist *timerlist)
|
|
|
timer_from_list = list_entry (timerlist->timer_head.next,
|
|
|
struct timerlist_timer, list);
|
|
|
|
|
|
- gettimeofday (¤t_time, 0);
|
|
|
- timeval_adjust_to_msec (¤t_time);
|
|
|
+ nano_from_epoch = timerlist_nano_from_epoch();
|
|
|
|
|
|
/*
|
|
|
* timer at head of list is expired, zero msecs required
|
|
|
*/
|
|
|
- if ((timer_from_list->tv.tv_sec < current_time.tv_sec) ||
|
|
|
- ((timer_from_list->tv.tv_sec == current_time.tv_sec) &&
|
|
|
- (timer_from_list->tv.tv_usec <= current_time.tv_usec))) {
|
|
|
+ if (timer_from_list->nano_from_epoch < nano_from_epoch) {
|
|
|
return (0);
|
|
|
}
|
|
|
- time_in_msec = ((timer_from_list->tv.tv_sec - current_time.tv_sec) * 1000) + ((timer_from_list->tv.tv_usec - current_time.tv_usec) / 1000);
|
|
|
|
|
|
- if (time_in_msec < 0) {
|
|
|
- return (0);
|
|
|
- }
|
|
|
- return time_in_msec;
|
|
|
+
|
|
|
+ msec_duration_to_expire = ((timer_from_list->nano_from_epoch - nano_from_epoch) / 1000000ULL) +
|
|
|
+ (1000 / HZ);
|
|
|
+ return (msec_duration_to_expire);
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
+ * Expires any timers that should be expired
|
|
|
+ */
|
|
|
static inline void timerlist_expire (struct timerlist *timerlist)
|
|
|
{
|
|
|
struct timerlist_timer *timer_from_list;
|
|
|
- struct timeval current_time;
|
|
|
+ unsigned long long nano_from_epoch;
|
|
|
|
|
|
- gettimeofday (¤t_time, 0);
|
|
|
- timeval_adjust_to_msec (¤t_time);
|
|
|
+ nano_from_epoch = timerlist_nano_from_epoch();
|
|
|
|
|
|
for (timerlist->timer_iter = timerlist->timer_head.next;
|
|
|
timerlist->timer_iter != &timerlist->timer_head;) {
|
|
|
@@ -273,10 +231,7 @@ static inline void timerlist_expire (struct timerlist *timerlist)
|
|
|
timer_from_list = list_entry (timerlist->timer_iter,
|
|
|
struct timerlist_timer, list);
|
|
|
|
|
|
- if ((timer_from_list->tv.tv_sec < current_time.tv_sec) ||
|
|
|
- ((timer_from_list->tv.tv_sec == current_time.tv_sec) &&
|
|
|
- (timer_from_list->tv.tv_usec <= current_time.tv_usec))) {
|
|
|
-//printf ("Executing timer %d %d\n", timer_from_list->tv.tv_sec, timer_from_list->tv.tv_usec);
|
|
|
+ if (timer_from_list->nano_from_epoch < nano_from_epoch) {
|
|
|
timerlist->timer_iter = timerlist->timer_iter->next;
|
|
|
|
|
|
timerlist_pre_dispatch (timerlist, timer_from_list);
|