[stgt] [PATCH 3/3] use unsigned int instead of timeval in tgt_work struct

FUJITA Tomonori fujita.tomonori at lab.ntt.co.jp
Thu Jan 20 07:26:04 CET 2011


For the future improvement with timerfd.

This also increases the interval from 250 to 500 msecs. work_add
provides just second precision. I guess that 500 msecs interval is
good enough.

Signed-off-by: FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp>
---
 usr/work.c |   45 ++++++++++++++++++++++++++++-----------------
 usr/work.h |    2 +-
 2 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/usr/work.c b/usr/work.c
index f825581..c3e1f34 100644
--- a/usr/work.c
+++ b/usr/work.c
@@ -31,10 +31,8 @@
 #include "work.h"
 #include "tgtd.h"
 
-#define time_before(w1, w2)     timercmp(w1, w2, <)
-
 #define WORK_TIMER_INT_SEC      0
-#define WORK_TIMER_INT_MSEC     250
+#define WORK_TIMER_INT_MSEC     500
 #define WORK_TIMER_INT_USEC     (WORK_TIMER_INT_MSEC * 1000)
 
 static struct itimerval work_timer = {
@@ -42,16 +40,21 @@ static struct itimerval work_timer = {
 	{WORK_TIMER_INT_SEC, WORK_TIMER_INT_USEC}
 };
 
-static int timer_started;
+static int elapsed_msecs;
 static int timer_pending;
-static int timer_fd[2] = {0, 0};
+static int timer_fd[2];
 
 static LIST_HEAD(active_work_list);
 static LIST_HEAD(inactive_work_list);
 
 static void execute_work(void);
 
-static inline void work_timer_schedule_evt(void)
+static inline unsigned int timeval_to_msecs(struct timeval t)
+{
+	return t.tv_sec * 1000 + t.tv_usec / 1000;
+}
+
+static void work_timer_schedule_evt(void)
 {
 	unsigned int n = 0;
 	int err;
@@ -90,12 +93,18 @@ static void work_timer_evt_handler(int fd, int events, void *data)
 int work_timer_start(void)
 {
 	struct sigaction s;
+	struct timeval t;
 	int err;
 
-	if (timer_started)
+	if (elapsed_msecs)
 		return 0;
 
-	timer_started = 1;
+	err = gettimeofday(&t, NULL);
+	if (err) {
+		eprintf("gettimeofday failed, %m\n");
+		exit(1);
+	}
+	elapsed_msecs = timeval_to_msecs(t);
 
 	sigemptyset(&s.sa_mask);
 	sigaddset(&s.sa_mask, SIGALRM);
@@ -139,10 +148,10 @@ int work_timer_stop(void)
 {
 	int err;
 
-	if (!timer_started)
+	if (!elapsed_msecs)
 		return 0;
 
-	timer_started = 0;
+	elapsed_msecs = 0;
 
 	tgt_event_del(timer_fd[0]);
 
@@ -163,18 +172,19 @@ int work_timer_stop(void)
 void add_work(struct tgt_work *work, unsigned int second)
 {
 	struct tgt_work *ent;
+	struct timeval t;
 	int err;
 
 	if (second) {
-		err = gettimeofday(&work->when, NULL);
+		err = gettimeofday(&t, NULL);
 		if (err) {
 			eprintf("gettimeofday failed, %m\n");
-			return;
+			exit(1);
 		}
-		work->when.tv_sec += second;
+		work->when = timeval_to_msecs(t) + second * 1000;
 
 		list_for_each_entry(ent, &inactive_work_list, entry) {
-			if (time_before(&work->when, &ent->when))
+			if (before(work->when, ent->when))
 				break;
 		}
 
@@ -199,11 +209,13 @@ static void execute_work()
 	err = gettimeofday(&cur_time, NULL);
 	if (err) {
 		eprintf("gettimeofday failed, %m\n");
-		return;
+		exit(1);
 	}
 
+	elapsed_msecs = timeval_to_msecs(cur_time);
+
 	list_for_each_entry_safe(work, n, &inactive_work_list, entry) {
-		if (time_before(&cur_time, &work->when))
+		if (before(elapsed_msecs, work->when))
 			break;
 
 		list_del(&work->entry);
@@ -217,4 +229,3 @@ static void execute_work()
 		work->func(work->data);
 	}
 }
-
diff --git a/usr/work.h b/usr/work.h
index c43243b..98a28d5 100644
--- a/usr/work.h
+++ b/usr/work.h
@@ -7,7 +7,7 @@ struct tgt_work {
 	struct list_head entry;
 	void (*func)(void *);
 	void *data;
-	struct timeval when;
+	unsigned int when;
 };
 
 extern int work_timer_start(void);
-- 
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



More information about the stgt mailing list