Fix pkgmgr_client_free() in thread race condition 27/222427/2
authorsooyeon.kim <sooyeon.kim@samsung.com>
Mon, 13 Jan 2020 12:12:27 +0000 (21:12 +0900)
committerwn.jang <wn.jang@samsung.com>
Wed, 15 Jan 2020 02:13:12 +0000 (11:13 +0900)
Change-Id: I1cb6b08e965555618332dafd555be2bf937a6b56
Signed-off-by: sooyeon.kim <sooyeon.kim@samsung.com>
client/tts.c

index 50b2e59..cf37dc6 100644 (file)
@@ -21,6 +21,7 @@
 #include <system_info.h>
 #include <vconf.h>
 #include <package-manager.h>
+#include <pthread.h>
 
 #include "tts.h"
 #include "tts_client.h"
@@ -52,6 +53,8 @@ static int g_speed = -1;
 static pkgmgr_client* g_pkgmgr = NULL;
 static char* g_engine_name = NULL;
 static int g_engine_update_status = 0;
+static pthread_mutex_t g_pkgmgr_mutex = PTHREAD_MUTEX_INITIALIZER;
+
 
 /* Function definition */
 static Eina_Bool __tts_notify_state_changed(void *data);
@@ -280,6 +283,8 @@ static void __create_pkgmgr_thread(void* data, Ecore_Thread* thread)
 {
        SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] create pkgmgr thread");
 
+       pthread_mutex_lock(&g_pkgmgr_mutex);
+
        while (!g_pkgmgr) {
                g_pkgmgr = pkgmgr_client_new(PC_LISTENING);
                if (NULL == g_pkgmgr) {
@@ -297,6 +302,8 @@ static void __create_pkgmgr_thread(void* data, Ecore_Thread* thread)
                usleep(10000);
        }
 
+       pthread_mutex_unlock(&g_pkgmgr_mutex);
+
        return ;
 }
 
@@ -405,11 +412,13 @@ int tts_destroy(tts_h tts)
                return TTS_ERROR_OPERATION_FAILED;
        }
 
+       pthread_mutex_lock(&g_pkgmgr_mutex);
        if (g_pkgmgr) {
                pkgmgr_client_remove_listen_status(g_pkgmgr);
                pkgmgr_client_free(g_pkgmgr);
                g_pkgmgr = NULL;
        }
+       pthread_mutex_unlock(&g_pkgmgr_mutex);
 
        tts_config_mgr_finalize(client->uid);