Add mutex for set repeat text 75/284375/1
authorSuyeon Hwang <stom.hwang@samsung.com>
Wed, 16 Nov 2022 11:11:19 +0000 (20:11 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Wed, 16 Nov 2022 11:11:19 +0000 (20:11 +0900)
- Issue:
Sometimes, invalid access occurs if app tries to add text in multi
thread enviroment.

- Solution:
App can add text frequently. And also, app can add text in multi thread
enviroment. However, the text_repeat property of client is not safe in
multi thread enviroment. Thus, this patch adds mutex lock for protect
text_repeat from race condition. Through this patch, app can be add text
safely in multi thread.

Change-Id: Iffcce54fcbc34436bf9f4d7ec7aadf571f05889b
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
client/tts_client.c
client/tts_client.h
client/tts_core.c

index e32045c..9515946 100644 (file)
@@ -26,6 +26,7 @@ static GList *g_client_list = NULL;
 /* pthread mutex */
 static pthread_mutex_t g_allocated_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t g_client_list_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t g_repeat_text_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 
 static unsigned int __client_generate_uid(unsigned int pid)
@@ -209,7 +210,12 @@ int tts_client_destroy(tts_h tts)
 
                                free(data->err_msg);
                                free(data->credential);
+
+                               pthread_mutex_lock(&g_repeat_text_mutex);
                                free(data->text_repeat);
+                               data->text_repeat = NULL;
+                               pthread_mutex_unlock(&g_repeat_text_mutex);
+
                                memset(data, 0, sizeof(tts_client_s));
 
                                free(data);
@@ -473,6 +479,7 @@ void tts_client_set_repeat_text(tts_client_s* client, const char* text)
                return;
        }
 
+       pthread_mutex_lock(&g_repeat_text_mutex);
        if (NULL != client->text_repeat) {
                free(client->text_repeat);
        }
@@ -482,15 +489,23 @@ void tts_client_set_repeat_text(tts_client_s* client, const char* text)
        } else {
                client->text_repeat = NULL;
        }
+       pthread_mutex_unlock(&g_repeat_text_mutex);
 }
 
-const char* tts_client_get_repeat_text(tts_client_s* client)
+char* tts_client_get_repeat_text(tts_client_s* client)
 {
        if (NULL == client || false == tts_client_is_valid_client(client)) {
                return NULL;
        }
 
-       return client->text_repeat;
+       char *repeat_text = NULL;
+       pthread_mutex_lock(&g_repeat_text_mutex);
+       if (NULL != client->text_repeat) {
+               repeat_text = strdup(client->text_repeat);
+       }
+       pthread_mutex_unlock(&g_repeat_text_mutex);
+
+       return repeat_text;
 }
 
 int tts_client_new_utterance_id(tts_client_s* client)
index dcbf6b5..da298c5 100644 (file)
@@ -123,7 +123,7 @@ void tts_client_set_mode(tts_client_s* client, tts_mode_e mode);
 tts_mode_e tts_client_get_mode(tts_client_s* client);
 
 void tts_client_set_repeat_text(tts_client_s* client, const char* text);
-const char* tts_client_get_repeat_text(tts_client_s* client);
+char* tts_client_get_repeat_text(tts_client_s* client);
 
 int tts_client_new_utterance_id(tts_client_s* client);
 
index cf30fff..34d7679 100644 (file)
@@ -1262,7 +1262,7 @@ int tts_core_repeat(tts_client_s* client, char** text_repeat, int* utt_id)
        RETVM_IF(NULL == text_repeat || NULL == utt_id, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Parameter is invalid.");
        RETVM_IF(false == tts_client_is_valid_client(client), TTS_ERROR_INVALID_PARAMETER, "[ERROR] Client is invalid.");
 
-       const char* repeat_text = tts_client_get_repeat_text(client);
+       char* repeat_text = tts_client_get_repeat_text(client);
        if (NULL == repeat_text) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] There is no previous added texts. Please add texts");
                return TTS_ERROR_OPERATION_FAILED;
@@ -1272,6 +1272,7 @@ int tts_core_repeat(tts_client_s* client, char** text_repeat, int* utt_id)
        int ret = __request_add_text(client, repeat_text, g_language, g_voice_type, g_speed, &new_utt_id);
        if (TTS_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to add texts for repetition.");
+               free(repeat_text);
                return ret;
        }
 
@@ -1279,13 +1280,14 @@ int tts_core_repeat(tts_client_s* client, char** text_repeat, int* utt_id)
        ret = __request_play(client);
        if (TTS_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to play texts for repetition.");
+               free(repeat_text);
                return ret;
        }
 
        SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] text to repeat(%s), utt_id(%d)", repeat_text, new_utt_id);
 
        *utt_id = new_utt_id;
-       *text_repeat = strdup(repeat_text);
+       *text_repeat = repeat_text;
        return TTS_ERROR_NONE;
 }