Change gettimeofday to clock_gettime 80/216580/2
authorInkyun Kil <inkyun.kil@samsung.com>
Tue, 29 Oct 2019 06:28:17 +0000 (15:28 +0900)
committerInkyun Kil <inkyun.kil@samsung.com>
Wed, 13 Nov 2019 23:19:52 +0000 (08:19 +0900)
Change-Id: Iafa02a659985ca406de4f96f26aa3a981182ed3b
Signed-off-by: Inkyun Kil <inkyun.kil@samsung.com>
include/alarm-internal.h
lib/alarm-lib.c
server/alarm-manager.c

index d8959ff..00b1c47 100644 (file)
@@ -41,6 +41,9 @@ extern "C" {
 #define MIN_INEXACT_INTERVAL 600
 #define REGULAR_UID_MIN 5000
 
+#define BILLION 1000000000 /* for calculating nano seconds */
+#define MILLION 1000000 /* for calculating micro seconds */
+
 typedef struct {
        GDBusConnection *connection;
        GDBusConnection *session_conn;
index ac475bd..3b7190d 100644 (file)
@@ -20,7 +20,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <sys/time.h>
+#include <time.h>
 #include <string.h>
 #include <glib.h>
 #include <fcntl.h>
@@ -98,18 +98,18 @@ static const GDBusInterfaceVTable interface_vtable = {
 void _initialize_alarm_info(alarm_info_t *alarm_info, int alarm_type,
                time_t trigger_at_time, time_t interval, bool precision)
 {
-       struct timeval current_time;
+       struct timespec current_time;
        struct tm duetime_tm;
 
-       gettimeofday(&current_time, NULL);
+       clock_gettime(CLOCK_REALTIME, &current_time);
 
        memset(alarm_info, 0, sizeof(alarm_info_t));
 
        alarm_info->mode.repeat = ALARM_REPEAT_MODE_ONCE;
        alarm_info->alarm_type = alarm_type;
 
-       if (current_time.tv_usec > 500 * 1000) {
-               /* When the millisecond part of the current_time is bigger than 500ms,
+       if (current_time.tv_nsec > (BILLION / 2)) {
+               /* When the nanosecond part of the current_time is bigger than 500ms,
                 * the duetime increases by extra 1sec. */
                current_time.tv_sec += (trigger_at_time + 1);
        } else {
@@ -140,7 +140,7 @@ void _initialize_alarm_info(alarm_info_t *alarm_info, int alarm_type,
                alarm_info->mode.u_interval.interval = interval;
        }
 
-       alarm_info->msec = precision ? (int)current_time.tv_usec / 1000 : 0;
+       alarm_info->msec = precision ? (int)current_time.tv_nsec / MILLION : 0;
 
        LOGD("trigger_at_time(%ld), start(%d-%d-%d, %02d:%02d:%02d),\
                        repeat(%d), interval(%ld), type(%d)",
@@ -221,7 +221,7 @@ static void __handle_expiry_method_call(GDBusConnection *conn,
                const gchar *name, const gchar *path, const gchar *interface,
                const gchar *method, GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
 {
-       struct timeval current_time;
+       struct timespec current_time;
 
        if (method && strcmp(method, "alarm_expired") == 0) {
                gchar *package_name = NULL;
@@ -233,8 +233,8 @@ static void __handle_expiry_method_call(GDBusConnection *conn,
                LOGD("[alarm-lib] : Alarm expired for [%s] : Alarm id [%d]", package_name, alarm_id);
 
                if (msec > 0) {
-                       gettimeofday(&current_time, NULL);
-                       msec =  msec - (int)current_time.tv_usec / 1000;
+                       clock_gettime(CLOCK_REALTIME, &current_time);
+                       msec =  msec - (int)current_time.tv_nsec / MILLION;
                }
 
                if (alarm_context.alarm_handler != NULL) {
@@ -486,13 +486,13 @@ static int __compare_api_version(int *result, uid_t uid)
 
 static void __adjust_current_milliseconds(alarm_info_t *alarm_info)
 {
-       struct timeval current_time;
+       struct timespec current_time;
        struct tm start_tm;
        time_t start;
 
-       gettimeofday(&current_time, NULL);
-       if (current_time.tv_usec > 500 * 1000) {
-               /* When the millisecond part of the current_time is bigger than 500ms,
+       clock_gettime(CLOCK_REALTIME, &current_time);
+       if (current_time.tv_nsec > (BILLION / 2)) {
+               /* When the nanosecond part of the current_time is bigger than 500ms,
                 * the duetime increases by extra 1sec. */
 
                start_tm.tm_year = alarm_info->start.year - 1900;
index 7b7c16e..da17b39 100755 (executable)
@@ -52,8 +52,6 @@
 #include <glib/gmacros.h>
 #endif
 
-#define BILLION 1000000000 /* for calculating nano seconds */
-
 /* link path for timezone info */
 #define TIMEZONE_INFO_LINK_PATH        tzplatform_mkpath(TZ_SYS_ETC, "localtime")
 
@@ -2046,12 +2044,12 @@ int alarm_manager_alarm_set_rtc_time(GVariant *parameters)
        }
 }
 
-static int accrue_msec = 0; /* To check a millisecond part of current time at changing the system time(sec) */
+static int accrue_nsec = 0; /* To check a millisecond part of current time at changing the system time(sec) */
 
 int alarm_manager_alarm_set_time(GVariant* parameters, pid_t pid)
 {
        double diff_time = 0.0;
-       struct timeval cur_time = {0,};
+       struct timespec cur_time;
        gint64 time_sec;
        time_t new_time;
        char sender_id[MAX_APP_ID_LEN];
@@ -2064,12 +2062,12 @@ int alarm_manager_alarm_set_time(GVariant* parameters, pid_t pid)
        _alarm_disable_timer(); /* Disable the timer to reschedule the alarm before the time is changed. */
 
        tzset();
-       gettimeofday(&cur_time, NULL);
+       clock_gettime(CLOCK_REALTIME, &cur_time);
 
-       accrue_msec += (cur_time.tv_usec / 1000); /* Accrue the millisecond to compensate the time */
-       if (accrue_msec > 500) {
+       accrue_nsec += (cur_time.tv_nsec / MILLION); /* Accrue the nanosecond to compensate the time */
+       if (accrue_nsec > (BILLION / 2)) { /* Over 500ms */
                diff_time = difftime(new_time, cur_time.tv_sec) - 1;
-               accrue_msec -= 1000;
+               accrue_nsec -= BILLION;
        } else {
                diff_time = difftime(new_time, cur_time.tv_sec);
        }
@@ -2101,7 +2099,7 @@ int alarm_manager_alarm_set_time_with_propagation_delay(GVariant* parameters, pi
        struct timespec delay = {0,};
        struct timespec sleep_time = {0,};
        time_t real_newtime = 0;
-       accrue_msec = 0; /* reset accrued msec */
+       accrue_nsec = 0; /* reset accrued msec */
        time_t new_sec, req_sec;
        long new_nsec, req_nsec;
        gint64 tmp_new_sec, tmp_req_sec, tmp_new_nsec, tmp_req_nsec;