Replace gettimeofday with clock_gettime
authorSung-jae Park <nicesj.park@samsung.com>
Thu, 26 Sep 2013 01:15:01 +0000 (10:15 +0900)
committerSung-jae Park <nicesj.park@samsung.com>
Thu, 26 Sep 2013 01:47:00 +0000 (10:47 +0900)
Change-Id: Ice0e00e2ffbba6f5a31012b56cf6df0f4884db95

CMakeLists.txt
include/util.h
packaging/libcom-core.spec
src/packet.c
src/util.c

index d17b78e..02448be 100644 (file)
@@ -29,6 +29,7 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
 ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"")
 ADD_DEFINITIONS("-DLOG_TAG=\"COM_CORE\"")
 ADD_DEFINITIONS("-DNDEBUG")
+ADD_DEFINITIONS("-D_USE_ECORE_TIME_GET")
 
 ADD_LIBRARY(${PROJECT_NAME} SHARED
        src/dlist.c
index e3e45c1..4f33355 100644 (file)
@@ -16,6 +16,7 @@
 */
 
 extern const char *util_basename(const char *name);
+extern double util_timestamp(void);
 
 #define CRITICAL_SECTION_BEGIN(handle) \
 do { \
index 6c25bf3..2b71a7d 100644 (file)
@@ -1,6 +1,6 @@
 Name: libcom-core
 Summary: Library for the light-weight IPC 
-Version: 0.5.2
+Version: 0.5.3
 Release: 1
 Group: HomeTF/Framework
 License: Apache License
index 9da8d3b..6057b56 100644 (file)
@@ -364,7 +364,6 @@ EAPI struct packet *packet_create(const char *cmd, const char *fmt, ...)
        struct packet *packet;
        int payload_size;
        va_list va;
-       struct timeval tv;
 
        if (strlen(cmd) >= PACKET_MAX_CMD) {
                ErrPrint("Command is too long\n");
@@ -388,11 +387,10 @@ EAPI struct packet *packet_create(const char *cmd, const char *fmt, ...)
        }
 
        packet->state = VALID;
-       gettimeofday(&tv, NULL);
        packet->data->head.source = 0lu;
        packet->data->head.destination = 0lu;
        packet->data->head.mask = 0xFFFFFFFF;
-       packet->data->head.seq = tv.tv_sec + tv.tv_usec / 1000000.0f;
+       packet->data->head.seq = util_timestamp();
        packet->data->head.type = PACKET_REQ;
        packet->data->head.version = PACKET_VERSION;
        strncpy(packet->data->head.command, cmd, sizeof(packet->data->head.command));
@@ -411,7 +409,6 @@ EAPI struct packet *packet_create_noack(const char *cmd, const char *fmt, ...)
        int payload_size;
        struct packet *result;
        va_list va;
-       struct timeval tv;
 
        if (strlen(cmd) >= PACKET_MAX_CMD) {
                ErrPrint("Command is too long\n");
@@ -435,11 +432,10 @@ EAPI struct packet *packet_create_noack(const char *cmd, const char *fmt, ...)
        }
 
        result->state = VALID;
-       gettimeofday(&tv, NULL);
        result->data->head.source = 0lu;
        result->data->head.destination = 0lu;
        result->data->head.mask = 0xFFFFFFFF;
-       result->data->head.seq = tv.tv_sec + tv.tv_usec / 1000000.0f;
+       result->data->head.seq = util_timestamp();
        result->data->head.type = PACKET_REQ_NOACK;
        result->data->head.version = PACKET_VERSION;
        strncpy(result->data->head.command, cmd, sizeof(result->data->head.command));
index a99bd02..e16306d 100644 (file)
 
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include <sys/time.h>
 #include <errno.h>
+#include <time.h>
+#include <dlog.h>
 
+#include "debug.h"
 #include "util.h"
 
 int errno;
+#if defined(_USE_ECORE_TIME_GET)
+static struct {
+       clockid_t type;
+} s_info = {
+       .type = CLOCK_MONOTONIC,
+};
+#endif
 
 const char *util_basename(const char *name)
 {
@@ -36,4 +47,36 @@ const char *util_basename(const char *name)
        return length <= 0 ? name : name + length + (name[length] == '/');
 }
 
+double util_timestamp(void)
+{
+#if defined(_USE_ECORE_TIME_GET)
+       struct timespec ts;
+
+       do {
+               if (clock_gettime(s_info.type, &ts) == 0) {
+                       return ts.tv_sec + ts.tv_nsec / 1000000000.0f;
+               }
+
+               ErrPrint("%d: %s\n", s_info.type, strerror(errno));
+               if (s_info.type == CLOCK_MONOTONIC) {
+                       s_info.type = CLOCK_REALTIME;
+               } else if (s_info.type == CLOCK_REALTIME) {
+                       break;
+               }
+       } while (1);
+
+       return 0.0f;
+#else
+       struct timeval tv;
+
+       if (gettimeofday(&tv, NULL) < 0) {
+               ErrPrint("gettimeofday: %s\n", strerror(errno));
+               tv.tv_sec = rand();
+               tv.tv_usec = rand();
+       }
+
+       return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
+#endif
+}
+
 /* End of a file */