diff mbox series

[ovs-dev,1/7] timeval: Introduce macros to convert timespec and timeval.

Message ID 1510158959-16851-2-git-send-email-bhanuprakash.bodireddy@intel.com
State Changes Requested
Headers show
Series Introduce high resolution sleep support. | expand

Commit Message

Bodireddy, Bhanuprakash Nov. 8, 2017, 4:35 p.m. UTC
This commit replaces the numbers with MSEC_PER_SEC, NSEC_PER_SEC and USEC_PER_MSEC
macros when dealing with timespec and timeval.

Signed-off-by: Bhanuprakash Bodireddy <bhanuprakash.bodireddy@intel.com>
---
 lib/timeval.c | 29 ++++++++++++++++-------------
 lib/timeval.h |  7 +++++++
 2 files changed, 23 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/lib/timeval.c b/lib/timeval.c
index b60bf30..8875fed 100644
--- a/lib/timeval.c
+++ b/lib/timeval.c
@@ -266,7 +266,7 @@  time_alarm(unsigned int secs)
     time_init();
 
     now = time_msec();
-    msecs = secs * 1000LL;
+    msecs = secs * MSEC_PER_SEC;
     deadline = now < LLONG_MAX - msecs ? now + msecs : LLONG_MAX;
 }
 
@@ -372,25 +372,28 @@  time_poll(struct pollfd *pollfds, int n_pollfds, HANDLE *handles OVS_UNUSED,
 long long int
 timespec_to_msec(const struct timespec *ts)
 {
-    return (long long int) ts->tv_sec * 1000 + ts->tv_nsec / (1000 * 1000);
+    return (long long int) ts->tv_sec * MSEC_PER_SEC +
+            ts->tv_nsec / NSEC_PER_MSEC;
 }
 
 long long int
 timeval_to_msec(const struct timeval *tv)
 {
-    return (long long int) tv->tv_sec * 1000 + tv->tv_usec / 1000;
+    return (long long int) tv->tv_sec * MSEC_PER_SEC +
+            tv->tv_usec / USEC_PER_MSEC;
 }
 
 long long int
 timespec_to_usec(const struct timespec *ts)
 {
-    return (long long int) ts->tv_sec * 1000 * 1000 + ts->tv_nsec / 1000;
+    return (long long int) ts->tv_sec * USEC_PER_SEC +
+            ts->tv_nsec / NSEC_PER_USEC;
 }
 
 long long int
 timeval_to_usec(const struct timeval *tv)
 {
-    return (long long int) tv->tv_sec * 1000 * 1000 + tv->tv_usec;
+    return (long long int) tv->tv_sec * USEC_PER_SEC + tv->tv_usec;
 }
 
 /* Returns the monotonic time at which the "time" module was initialized, in
@@ -510,8 +513,8 @@  xclock_gettime(clock_t id, struct timespec *ts)
 static void
 msec_to_timespec(long long int ms, struct timespec *ts)
 {
-    ts->tv_sec = ms / 1000;
-    ts->tv_nsec = (ms % 1000) * 1000 * 1000;
+    ts->tv_sec = ms / MSEC_PER_SEC;
+    ts->tv_nsec = (ms % 1000) * NSEC_PER_MSEC;
 }
 
 static void
@@ -596,8 +599,8 @@  timespec_add(struct timespec *sum,
 
     tmp.tv_sec = a->tv_sec + b->tv_sec;
     tmp.tv_nsec = a->tv_nsec + b->tv_nsec;
-    if (tmp.tv_nsec >= 1000 * 1000 * 1000) {
-        tmp.tv_nsec -= 1000 * 1000 * 1000;
+    if (tmp.tv_nsec >= NSEC_PER_SEC) {
+        tmp.tv_nsec -= NSEC_PER_SEC;
         tmp.tv_sec++;
     }
 
@@ -621,7 +624,7 @@  log_poll_interval(long long int last_wakeup)
 {
     long long int interval = time_msec() - last_wakeup;
 
-    if (interval >= 1000 && !is_warped(&monotonic_clock)) {
+    if (interval >= MSEC_PER_SEC && !is_warped(&monotonic_clock)) {
         const struct rusage *last_rusage = get_recent_rusage();
         struct rusage rusage;
 
@@ -713,7 +716,7 @@  refresh_rusage(void)
 
     if (!getrusage_thread(recent_rusage)) {
         long long int now = time_msec();
-        if (now >= t->newer.when + 3 * 1000) {
+        if (now >= t->newer.when + 3 * MSEC_PER_SEC) {
             t->older = t->newer;
             t->newer.when = now;
             t->newer.cpu = (timeval_to_msec(&recent_rusage->ru_utime) +
@@ -837,7 +840,7 @@  strftime_msec(char *s, size_t max, const char *format,
 struct tm_msec *
 localtime_msec(long long int now, struct tm_msec *result)
 {
-  time_t now_sec = now / 1000;
+  time_t now_sec = now / MSEC_PER_SEC;
   localtime_r(&now_sec, &result->tm);
   result->msec = now % 1000;
   return result;
@@ -846,7 +849,7 @@  localtime_msec(long long int now, struct tm_msec *result)
 struct tm_msec *
 gmtime_msec(long long int now, struct tm_msec *result)
 {
-  time_t now_sec = now / 1000;
+  time_t now_sec = now / MSEC_PER_SEC;
   gmtime_r(&now_sec, &result->tm);
   result->msec = now % 1000;
   return result;
diff --git a/lib/timeval.h b/lib/timeval.h
index c3dbb51..5e2a731 100644
--- a/lib/timeval.h
+++ b/lib/timeval.h
@@ -40,6 +40,13 @@  BUILD_ASSERT_DECL(TYPE_IS_SIGNED(time_t));
 #define TIME_MAX TYPE_MAXIMUM(time_t)
 #define TIME_MIN TYPE_MINIMUM(time_t)
 
+#define MSEC_PER_SEC    1000L
+#define USEC_PER_SEC    1000000L
+#define NSEC_PER_SEC    1000000000L
+#define USEC_PER_MSEC   1000L
+#define NSEC_PER_MSEC   1000000L
+#define NSEC_PER_USEC   1000L
+
 #ifdef _WIN32
 #define localtime_r(timep, result) localtime_s(result, timep)
 #define gmtime_r(timep, result) gmtime_s(result, timep)