added deinit to unref thread in contacts sync 76/83676/7
authorJeesun Kim <iamjs.kim@samsung.com>
Fri, 12 Aug 2016 09:32:15 +0000 (18:32 +0900)
committerJeesun Kim <iamjs.kim@samsung.com>
Fri, 19 Aug 2016 08:23:40 +0000 (17:23 +0900)
Change-Id: I09710b493e5df3e4f8a355787a3ecbefb765ae18

server/cal_server_contacts.c

index 6be2d09..02d0d5f 100644 (file)
 #include "cal_db.h"
 #include "cal_db_util.h"
 #include "cal_time.h"
-#include "cal_access_control.h"
-#include "cal_server_contacts.h"
 #include "cal_server_service.h"
 #include "cal_server_ondemand.h"
+#include "cal_access_control.h"
+#include "cal_server_contacts.h"
 
 #define CAL_SERVER_CONTACTS_SYNC_THREAD_NAME "cal_server_contacts_sync"
 #define BULK_MAX_COUNT 100
@@ -40,6 +40,7 @@
 GThread *_cal_server_contacts_sync_thread = NULL;
 GCond _cal_server_contacts_sync_cond;
 GMutex _cal_server_contacts_sync_mutex;
+static bool server_killed = false;
 
 static int _cal_server_contacts_set_new_event(int id, char *label, int calendar_type,
                int date, char *type_str, int account_id, calendar_record_h *out_event)
@@ -645,8 +646,9 @@ static gpointer _cal_server_contacts_sync_main(gpointer user_data)
 
        while (1) {
                /*
-                * while syncing with contacts, calendar-service could be stopped by on-demand.
-                * so, on-demand timeout is stopped.
+                * While syncing with contacts,
+                * because calendar-service could be stopped by on-demand,
+                * holding on-demand is needed.
                 */
                cal_server_ondemand_hold();
 
@@ -654,6 +656,7 @@ static gpointer _cal_server_contacts_sync_main(gpointer user_data)
                if (CALENDAR_ERROR_NONE != ret) {
                        /* LCOV_EXCL_START */
                        ERR("cal_connect() Fail(%d)", ret);
+                       cal_server_ondemand_release();
                        cal_server_ondemand_start();
                        break;
                        /* LCOV_EXCL_STOP */
@@ -668,7 +671,6 @@ static gpointer _cal_server_contacts_sync_main(gpointer user_data)
                        }
                }
                cal_access_control_unset_client_info();
-
                cal_disconnect();
 
                g_mutex_lock(&_cal_server_contacts_sync_mutex);
@@ -677,7 +679,10 @@ static gpointer _cal_server_contacts_sync_main(gpointer user_data)
                cal_server_ondemand_start();
                g_cond_wait(&_cal_server_contacts_sync_cond, &_cal_server_contacts_sync_mutex);
                g_mutex_unlock(&_cal_server_contacts_sync_mutex);
+               if (server_killed)
+                       break;
        }
+       g_thread_exit(NULL);
 
        return NULL;
 }
@@ -686,13 +691,15 @@ static void cal_server_contacts_sync_start(void)
 {
        CAL_FN_CALL();
 
+       if (server_killed)
+               return;
+
        if (NULL == _cal_server_contacts_sync_thread) {
                g_mutex_init(&_cal_server_contacts_sync_mutex);
                g_cond_init(&_cal_server_contacts_sync_cond);
                _cal_server_contacts_sync_thread = g_thread_new(CAL_SERVER_CONTACTS_SYNC_THREAD_NAME,
                                _cal_server_contacts_sync_main, NULL);
        }
-
        /* don't use mutex. */
        g_cond_signal(&_cal_server_contacts_sync_cond);
 }
@@ -727,8 +734,19 @@ int cal_server_contacts_init(void)
        return CALENDAR_ERROR_NONE;
 }
 
+void cal_server_contacts_end(void)
+{
+       server_killed = true;
+       /* don't use mutex. */
+       g_cond_signal(&_cal_server_contacts_sync_cond);
+       g_thread_join(_cal_server_contacts_sync_thread);
+       g_thread_unref(_cal_server_contacts_sync_thread);
+}
+
 void cal_server_contacts_deinit(void)
 {
+       cal_server_contacts_end();
+
        contacts_db_remove_changed_cb(_contacts_event._uri, _changed_cb, NULL);
        contacts_db_remove_changed_cb(_contacts_name._uri, _changed_cb, NULL);