Use a helper function to add milliseconds to timeval structs
authorTobias Brunner <tobias@strongswan.org>
Tue, 16 Oct 2012 10:38:54 +0000 (12:38 +0200)
committerTobias Brunner <tobias@strongswan.org>
Thu, 18 Oct 2012 10:25:59 +0000 (12:25 +0200)
src/frontends/android/jni/libandroidbridge/kernel/android_net.c
src/libhydra/plugins/kernel_netlink/kernel_netlink_net.c
src/libhydra/plugins/kernel_pfroute/kernel_pfroute_net.c
src/libstrongswan/threading/mutex.c
src/libstrongswan/threading/rwlock.c
src/libstrongswan/utils.h

index 710bee5..430c95b 100644 (file)
@@ -75,12 +75,7 @@ static void connectivity_cb(private_kernel_android_net_t *this,
                this->mutex->unlock(this->mutex);
                return;
        }
-       now.tv_usec += ROAM_DELAY * 1000;
-       while (now.tv_usec > 1000000)
-       {
-               now.tv_sec++;
-               now.tv_usec -= 1000000;
-       }
+       timeval_add_ms(&now, ROAM_DELAY);
        this->next_roam = now;
        this->mutex->unlock(this->mutex);
 
index 3f63a84..e5f070e 100644 (file)
@@ -543,12 +543,7 @@ static void queue_route_reinstall(private_kernel_netlink_net_t *this,
        time_monotonic(&now);
        if (timercmp(&now, &this->last_route_reinstall, >))
        {
-               now.tv_usec += ROUTE_DELAY * 1000;
-               while (now.tv_usec > 1000000)
-               {
-                       now.tv_sec++;
-                       now.tv_usec -= 1000000;
-               }
+               timeval_add_ms(&now, ROUTE_DELAY);
                this->last_route_reinstall = now;
 
                job = (job_t*)callback_job_create((callback_job_cb_t)reinstall_routes,
@@ -704,12 +699,7 @@ static void fire_roam_event(private_kernel_netlink_net_t *this, bool address)
                this->roam_lock->unlock(this->roam_lock);
                return;
        }
-       now.tv_usec += ROAM_DELAY * 1000;
-       while (now.tv_usec > 1000000)
-       {
-               now.tv_sec++;
-               now.tv_usec -= 1000000;
-       }
+       timeval_add_ms(&now, ROAM_DELAY);
        this->next_roam = now;
        this->roam_lock->unlock(this->roam_lock);
 
index 16a46bb..47a8829 100644 (file)
@@ -285,12 +285,7 @@ static void fire_roam_event(private_kernel_pfroute_net_t *this, bool address)
        time_monotonic(&now);
        if (timercmp(&now, &this->last_roam, >))
        {
-               now.tv_usec += ROAM_DELAY * 1000;
-               while (now.tv_usec > 1000000)
-               {
-                       now.tv_sec++;
-                       now.tv_usec -= 1000000;
-               }
+               timeval_add_ms(&now, ROAM_DELAY);
                this->last_roam = now;
 
                job = (job_t*)callback_job_create((callback_job_cb_t)roam_event,
index 2ef918a..df0cd27 100644 (file)
@@ -282,13 +282,7 @@ METHOD(condvar_t, timed_wait, bool,
        ms = timeout % 1000;
 
        tv.tv_sec += s;
-       tv.tv_usec += ms * 1000;
-
-       if (tv.tv_usec > 1000000 /* 1s */)
-       {
-               tv.tv_usec -= 1000000;
-               tv.tv_sec++;
-       }
+       timeval_add_ms(&tv, ms);
        return timed_wait_abs(this, mutex, tv);
 }
 
index 7097a8e..9ce9c6a 100644 (file)
@@ -433,13 +433,8 @@ METHOD(rwlock_condvar_t, timed_wait, bool,
        ms = timeout % 1000;
 
        tv.tv_sec += s;
-       tv.tv_usec += ms * 1000;
+       timeval_add_ms(&tv, ms);
 
-       if (tv.tv_usec > 1000000 /* 1s */)
-       {
-               tv.tv_usec -= 1000000;
-               tv.tv_sec++;
-       }
        return timed_wait_abs(this, lock, tv);
 }
 
index f47c65a..4e2065f 100644 (file)
@@ -455,6 +455,22 @@ void closefrom(int lowfd);
 time_t time_monotonic(timeval_t *tv);
 
 /**
+ * Add the given number of milliseconds to the given timeval struct
+ *
+ * @param tv           timeval struct to modify
+ * @param ms           number of milliseconds
+ */
+static inline void timeval_add_ms(timeval_t *tv, u_int ms)
+{
+       tv->tv_usec += ms * 1000;
+       while (tv->tv_usec > 1000000 /* 1s */)
+       {
+               tv->tv_usec -= 1000000;
+               tv->tv_sec++;
+       }
+}
+
+/**
  * returns null
  */
 void *return_null();