]> git.webhop.me Git - lcd4linux.git/commitdiff
timer.c: exchanged "proprietary code" with timeradd() and timersub() functions
authormzuther <mzuther@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Sat, 6 Feb 2010 11:29:12 +0000 (11:29 +0000)
committermzuther <mzuther@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Sat, 6 Feb 2010 11:29:12 +0000 (11:29 +0000)
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@1102 3ae390bd-cb1e-0410-b409-cd5a39f66f1f

timer.c

diff --git a/timer.c b/timer.c
index 174e1e39bd2e7ba7d11865f077e1be5f3c1dfd3d..3ae864bc44a2258847b708e494945a9a134349ed 100644 (file)
--- a/timer.c
+++ b/timer.c
@@ -77,13 +77,11 @@ int nTimers = 0;
 
 static void timer_inc(struct timeval *tv, const int msec)
 {
-    tv->tv_sec += msec / 1000;
-    tv->tv_usec += (msec - 1000 * (msec / 1000)) * 1000;
+    struct timeval diff;
+    diff.tv_sec = msec / 1000;
+    diff.tv_usec = (msec % 1000) * 1000;
 
-    if (tv->tv_usec >= 1000000) {
-       tv->tv_usec -= 1000000;
-       tv->tv_sec++;
-    }
+    timeradd(tv, &diff, tv);
 }
 
 int timer_remove(void (*callback) (void *data), void *data)
@@ -206,14 +204,12 @@ int timer_process(struct timespec *delay)
     }
 
     /* delay until next timer event */
-    delay->tv_sec = Timers[min].when.tv_sec - now.tv_sec;
-    delay->tv_nsec = Timers[min].when.tv_usec - now.tv_usec;
-    if (delay->tv_nsec < 0) {
-       delay->tv_sec--;
-       delay->tv_nsec += 1000000;
-    }
-    /* nanoseconds!! */
-    delay->tv_nsec *= 1000;
+    struct timeval diff;
+    timersub(&Timers[min].when, &now, &diff);
+
+    delay->tv_sec = diff.tv_sec;
+    /* microseconds to nanoseconds!! */
+    delay->tv_nsec = diff.tv_usec * 1000;
 
     /* check if date changed */
     if ((delay->tv_sec) > CLOCK_SKEW_DETECT_TIME_IN_S) {