Simplified OICGetCurrentTime() implementation.
authorOssama Othman <ossama.othman@intel.com>
Tue, 17 Nov 2015 23:36:47 +0000 (15:36 -0800)
committerJon A. Cruz <jonc@osg.samsung.com>
Wed, 18 Nov 2015 22:03:57 +0000 (22:03 +0000)
Restructured the OICGetCurrentTime() implementation to remove two
unnecessary variables.  This allows us to drop a run-time conditional
in the _POSIX_MONOTONIC_CLOCK == 0 case, as well as fix an "unused
variable" compile-time warning.  Include directives were also cleaned
up to the minimum needed.

Change-Id: I8f478c08310a74ad642aad5b45f9fc3310b102de
Signed-off-by: Ossama Othman <ossama.othman@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4249
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
resource/c_common/oic_time/src/oic_time.c

index ba7e464..2415d42 100644 (file)
 #endif
 
 #include "oic_time.h"
-#include <stdio.h>
+
+#include <stddef.h>        // For NULL
 
 #ifndef WITH_ARDUINO
-#include <unistd.h>
-#include <time.h>
-#include <sys/time.h>
+# if _POSIX_TIMERS > 0
+#  include <time.h>        // For clock_gettime()
+# else
+#  include <sys/time.h>    // For gettimeofday()
+# endif  // _POSIX_TIMERS > 0
 #endif
 
 #define TAG "OIC_TIME"
 uint64_t OICGetCurrentTime(OICTimePrecision precision)
 {
     uint64_t currentTime = 0;
-    int err = 0;
 
 #ifdef WITH_ARDUINO
-    currentTime = (TIME_IN_MS == precision) ? (uint64_t)millis() : (uint64_t)micros();
+    currentTime = (TIME_IN_MS == precision) ? millis() : micros();
 #else
-    #if _POSIX_TIMERS > 0
-        int32_t clockId = CLOCK_REALTIME;
-        static int32_t cachedRet = 0;
+# if _POSIX_TIMERS > 0
+#   if defined(CLOCK_MONOTONIC_COARSE)
+    static const clockid_t clockId = CLOCK_MONOTONIC_COARSE;
+#   elif _POSIX_MONOTONIC_CLOCK >= 0
+    // Option _POSIX_MONOTONIC_CLOCK == 0 indicates that the option is
+    // available at compile time but may not be supported at run
+    // time.  Check if option _POSIX_MONOTONIC_CLOCK is supported at
+    // run time.
+#     if _POSIX_MONOTONIC_CLOCK == 0
+    static const clockid_t clockId =
+        sysconf(_SC_MONOTONIC_CLOCK) > 0 ? CLOCK_MONOTONIC : CLOCK_REALTIME;
+#     else
+    static const clockid_t clockId = CLOCK_MONOTONIC;
+#     endif  // _POSIX_MONOTONIC_CLOCK == 0
+#   else
+    static const clockid_t clockId = CLOCK_REALTIME;
+#   endif  // CLOCK_MONOTONIC_COARSE
 
-        #if defined(CLOCK_MONOTONIC_COARSE)
-            clockId = CLOCK_MONOTONIC_COARSE;
-        #elif _POSIX_MONOTONIC_CLOCK >= 0
-            //Option _POSIX_MONOTONIC_CLOCK == 0 indicates that the option is
-            //available at compile time but may not be supported at run time.
-            //Checking if option _POSIX_MONOTONIC_CLOCK  is supported at run time.
-            #if _POSIX_MONOTONIC_CLOCK == 0
-                cachedRet = (0 == cachedRet) ? sysconf(_SC_MONOTONIC_CLOCK) : cachedRet;
-                if(cachedRet > 0)
-                {
-                    clockId = CLOCK_MONOTONIC;
-                }
-            #else
-                clockId = CLOCK_MONOTONIC;
-            #endif
-        #else
-            clockId = CLOCK_REALTIME;
-        #endif
+    struct timespec current = { .tv_sec = 0, .tv_nsec = 0 };
+    if (clock_gettime(clockId, &current) == 0)
+    {
+        currentTime =
+            (TIME_IN_MS == precision)
+            ? (((uint64_t) current.tv_sec * MS_PER_SEC) + (current.tv_nsec / NS_PER_MS))
+            : (((uint64_t) current.tv_sec * US_PER_SEC) + (current.tv_nsec / NS_PER_US));
+    }
+# else
+    struct timeval current = { .tv_sec = 0, .tv_usec = 0 };
+    if (gettimeofday(&current, NULL) == 0)
+    {
+        currentTime =
+            (TIME_IN_MS == precision)
+            ? (((uint64_t) current.tv_sec * MS_PER_SEC) + (current.tv_usec / US_PER_MS))
+            : (((uint64_t) current.tv_sec * US_PER_SEC) + (current.tv_usec));
+    }
+# endif  // _POSIX_TIMERS > 0
+#endif  // WITH_ARDUINO
 
-        struct timespec current = {.tv_sec=0, .tv_nsec=0};
-        if((err = clock_gettime(clockId, &current)) != -1)
-        {
-            currentTime = (TIME_IN_MS == precision) ?
-                          (((uint64_t)current.tv_sec * MS_PER_SEC) + (current.tv_nsec / NS_PER_MS)):
-                          (((uint64_t)current.tv_sec * US_PER_SEC) + (current.tv_nsec / NS_PER_US));
-        }
-    #else
-        struct timeval current = {.tv_sec=0, .tv_usec=0};
-        if((err = gettimeofday(&current, NULL)) != -1)
-        {
-            currentTime = (TIME_IN_MS == precision) ?
-                          (((uint64_t)current.tv_sec * MS_PER_SEC) + (current.tv_usec / US_PER_MS)):
-                          (((uint64_t)current.tv_sec * US_PER_SEC) + (current.tv_usec));
-        }
-    #endif
-#endif
-    return (!err) ? currentTime : 0;
+    return currentTime;
 }
-