From 0a2004f4bf88571558ac42c9b3ec9e1dfff4f11b Mon Sep 17 00:00:00 2001 From: "sooyeon.kim" Date: Fri, 26 Jan 2018 17:10:18 +0900 Subject: [PATCH] Fix unsafe mutex Change-Id: I46c409c1198d3e13065e2787f569973d9069c79e Signed-off-by: sooyeon.kim (cherry picked from commit 01930ea699257266b25650ee971703bacaea0013) --- server/ttsd_data.cpp | 150 ++++++++++++++++++++++++++++++--------------------- server/ttsd_data.h | 4 +- server/ttsd_player.c | 17 +++--- server/ttsd_server.c | 6 +-- 4 files changed, 101 insertions(+), 76 deletions(-) diff --git a/server/ttsd_data.cpp b/server/ttsd_data.cpp index 1056101..9181131 100644 --- a/server/ttsd_data.cpp +++ b/server/ttsd_data.cpp @@ -44,6 +44,7 @@ static vector g_app_list; static pthread_mutex_t g_speak_data_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_sound_data_mutex = PTHREAD_MUTEX_INITIALIZER; + /* If engine is running */ static ttsd_synthesis_control_e g_synth_control; @@ -363,6 +364,27 @@ int ttsd_data_add_speak_data(int uid, speak_data_s* data) return TTSD_ERROR_NONE; } +int __get_speak_data(int index, speak_data_s** data) +{ + if (0 == g_app_list[index].m_speak_data.size()) { +#ifdef DATA_DEBUG + SLOG(LOG_WARN, tts_tag(), "[DATA WARNING] There is no speak data"); +#endif + return -1; + } + + if (!g_app_list[index].m_speak_data.empty()) { + std::list::iterator iter = g_app_list[index].m_speak_data.begin(); + *data = *iter; + g_app_list[index].m_speak_data.pop_front(); + } + +#ifdef DATA_DEBUG + __data_show_text_list(index); +#endif + return 0; +} + int ttsd_data_get_speak_data(int uid, speak_data_s** data) { int index = 0; @@ -376,23 +398,12 @@ int ttsd_data_get_speak_data(int uid, speak_data_s** data) /* mutex is locked */ pthread_mutex_lock(&g_speak_data_mutex); - if (0 == g_app_list[index].m_speak_data.size()) { -#ifdef DATA_DEBUG + if (0 != __get_speak_data(index, data)) { SLOG(LOG_WARN, tts_tag(), "[DATA WARNING] There is no speak data"); -#endif pthread_mutex_unlock(&g_speak_data_mutex); return -1; } - if (!g_app_list[index].m_speak_data.empty()) { - std::list::iterator iter = g_app_list[index].m_speak_data.begin(); - *data = *iter; - g_app_list[index].m_speak_data.pop_front(); - } - -#ifdef DATA_DEBUG - __data_show_text_list(index); -#endif pthread_mutex_unlock(&g_speak_data_mutex); return TTSD_ERROR_NONE; @@ -437,21 +448,8 @@ int ttsd_data_add_sound_data(int uid, sound_data_s* data) return TTSD_ERROR_NONE; } -int ttsd_data_get_sound_data(int uid, sound_data_s** data) +int __get_sound_data(int index, sound_data_s** data) { - int index = 0; - index = ttsd_data_is_client(uid); - - SLOG(LOG_DEBUG, tts_tag(), "[DATA] sound_data_s: %p", *data); - - if (index < 0) { - SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid); - return TTSD_ERROR_INVALID_PARAMETER; - } - - /* mutex is locked */ - pthread_mutex_lock(&g_sound_data_mutex); - if (0 == g_app_list[index].m_wav_data.size()) { #ifdef DATA_DEBUG SLOG(LOG_DEBUG, tts_tag(), "[DATA] There is no wav data"); @@ -470,7 +468,30 @@ int ttsd_data_get_sound_data(int uid, sound_data_s** data) #ifdef DATA_DEBUG __data_show_sound_list(index); #endif + return 0; +} + +int ttsd_data_get_sound_data(int uid, sound_data_s** data) +{ + int index = 0; + index = ttsd_data_is_client(uid); + + SLOG(LOG_DEBUG, tts_tag(), "[DATA] sound_data_s: %p", *data); + + if (index < 0) { + SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid); + return TTSD_ERROR_INVALID_PARAMETER; + } + /* mutex is locked */ + pthread_mutex_lock(&g_sound_data_mutex); + + if (0 != __get_sound_data(index, data)) { + SLOG(LOG_DEBUG, tts_tag(), "[DATA] There is no wav data"); + /* mutex is unlocked */ + pthread_mutex_unlock(&g_sound_data_mutex); + return -1; + } /* mutex is unlocked */ pthread_mutex_unlock(&g_sound_data_mutex); @@ -498,25 +519,31 @@ int ttsd_data_get_sound_data_size(int uid) return data_size; } -int ttsd_data_clear_speak_data(speak_data_s** speak_data) +int ttsd_data_clear_speak_data(int uid, speak_data_s** speak_data) { pthread_mutex_lock(&g_speak_data_mutex); - if (NULL != *speak_data) { - SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] utt(%d), text(%s), lang(%s), vctype(%d) speed(%d)", - (*speak_data)->utt_id, (*speak_data)->text, (*speak_data)->lang, (*speak_data)->vctype, (*speak_data)->speed); - - if (NULL != (*speak_data)->text) { - free((*speak_data)->text); - (*speak_data)->text = NULL; - } - if (NULL != (*speak_data)->lang) { - free((*speak_data)->lang); - (*speak_data)->lang = NULL; + int index = 0; + index = ttsd_data_is_client(uid); + if (index >= 0) { + if (!g_app_list[index].m_speak_data.empty()) { + if (NULL != *speak_data) { + SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] utt(%d), text(%s), lang(%s), vctype(%d) speed(%d)", + (*speak_data)->utt_id, (*speak_data)->text, (*speak_data)->lang, (*speak_data)->vctype, (*speak_data)->speed); + + if (NULL != (*speak_data)->text) { + free((*speak_data)->text); + (*speak_data)->text = NULL; + } + if (NULL != (*speak_data)->lang) { + free((*speak_data)->lang); + (*speak_data)->lang = NULL; + } + + free(*speak_data); + *speak_data = NULL; + } } - - free(*speak_data); - *speak_data = NULL; } pthread_mutex_unlock(&g_speak_data_mutex); @@ -524,23 +551,28 @@ int ttsd_data_clear_speak_data(speak_data_s** speak_data) return TTSD_ERROR_NONE; } -int ttsd_data_clear_sound_data(sound_data_s** sound_data) +int ttsd_data_clear_sound_data(int uid, sound_data_s** sound_data) { pthread_mutex_lock(&g_sound_data_mutex); - if (NULL != *sound_data) { - SLOG(LOG_ERROR, tts_tag(), "[DEBUG][%p] event(%d) data(%p) size(%d) rate(%d) utt(%d)", - (*sound_data), (*sound_data)->event, (*sound_data)->data, (*sound_data)->data_size, (*sound_data)->rate, (*sound_data)->utt_id); - - if (NULL != (*sound_data)->data) { - free((*sound_data)->data); - (*sound_data)->data = NULL; + int index = 0; + index = ttsd_data_is_client(uid); + if (index >= 0) { + if (!g_app_list[index].m_wav_data.empty()) { + if (NULL != *sound_data) { + SLOG(LOG_ERROR, tts_tag(), "[DEBUG][%p] event(%d) data(%p) size(%d) rate(%d) utt(%d)", + (*sound_data), (*sound_data)->event, (*sound_data)->data, (*sound_data)->data_size, (*sound_data)->rate, (*sound_data)->utt_id); + + if (NULL != (*sound_data)->data) { + free((*sound_data)->data); + (*sound_data)->data = NULL; + } + + free(*sound_data); + *sound_data = NULL; + } } - - free(*sound_data); - *sound_data = NULL; } - pthread_mutex_unlock(&g_sound_data_mutex); return TTSD_ERROR_NONE; @@ -561,12 +593,12 @@ int ttsd_data_clear_data(int uid) sound_data_s* temp_sound = NULL; /* free allocated data */ + pthread_mutex_lock(&g_speak_data_mutex); while(1) { - if (0 != ttsd_data_get_speak_data(uid, &temp_speak)) { + if (0 != __get_speak_data(index, &temp_speak)) { break; } - pthread_mutex_lock(&g_speak_data_mutex); if (NULL != temp_speak) { SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] utt(%d), text(%s), lang(%s), vctype(%d) speed(%d)", temp_speak->utt_id, temp_speak->text, temp_speak->lang, temp_speak->vctype, temp_speak->speed); @@ -584,23 +616,21 @@ int ttsd_data_clear_data(int uid) free(temp_speak); temp_speak = NULL; } - pthread_mutex_unlock(&g_speak_data_mutex); } if (-1 != removed_last_uttid) { g_app_list[index].utt_id_stopped = removed_last_uttid; } - pthread_mutex_lock(&g_speak_data_mutex); g_app_list[index].m_speak_data.clear(); pthread_mutex_unlock(&g_speak_data_mutex); + pthread_mutex_lock(&g_sound_data_mutex); while(1) { - if (0 != ttsd_data_get_sound_data(uid, &temp_sound)) { + if (0 != __get_sound_data(index, &temp_sound)) { break; } - pthread_mutex_lock(&g_sound_data_mutex); if (NULL != temp_sound) { SLOG(LOG_ERROR, tts_tag(), "[DEBUG][%p] uid(%d), event(%d) data(%p) size(%d) rate(%d) utt(%d)", temp_sound, uid, temp_sound->event, temp_sound->data, temp_sound->data_size, temp_sound->rate, temp_sound->utt_id); @@ -613,10 +643,8 @@ int ttsd_data_clear_data(int uid) free(temp_sound); temp_sound = NULL; } - pthread_mutex_unlock(&g_sound_data_mutex); } - pthread_mutex_lock(&g_sound_data_mutex); g_app_list[index].m_wav_data.clear(); pthread_mutex_unlock(&g_sound_data_mutex); diff --git a/server/ttsd_data.h b/server/ttsd_data.h index 3acbf22..de87895 100644 --- a/server/ttsd_data.h +++ b/server/ttsd_data.h @@ -77,7 +77,7 @@ int ttsd_data_get_speak_data(int uid, speak_data_s** data); int ttsd_data_get_speak_data_size(int uid); -int ttsd_data_clear_speak_data(speak_data_s** speak_data); +int ttsd_data_clear_speak_data(int uid, speak_data_s** speak_data); /* sound data */ int ttsd_data_add_sound_data(int uid, sound_data_s* data); @@ -86,7 +86,7 @@ int ttsd_data_get_sound_data(int uid, sound_data_s** data); int ttsd_data_get_sound_data_size(int uid); -int ttsd_data_clear_sound_data(sound_data_s** sound_data); +int ttsd_data_clear_sound_data(int uid, sound_data_s** sound_data); int ttsd_data_clear_data(int uid); diff --git a/server/ttsd_player.c b/server/ttsd_player.c index 0c7679d..41fe11c 100644 --- a/server/ttsd_player.c +++ b/server/ttsd_player.c @@ -499,7 +499,6 @@ static void __play_thread(void *data, Ecore_Thread *thread) /* unset volume policy, volume will be 100% */ __unset_policy_for_playing(); - ttsd_data_clear_sound_data(&sound_data); return; } @@ -524,7 +523,6 @@ static void __play_thread(void *data, Ecore_Thread *thread) SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid"); /* unset volume policy, volume will be 100% */ __unset_policy_for_playing(); - ttsd_data_clear_sound_data(&sound_data); return; } if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) { @@ -533,7 +531,7 @@ static void __play_thread(void *data, Ecore_Thread *thread) } } SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id); - ttsd_data_clear_sound_data(&sound_data); + ttsd_data_clear_sound_data(player->uid, &sound_data); continue; } } @@ -549,7 +547,7 @@ static void __play_thread(void *data, Ecore_Thread *thread) /* unset volume policy, volume will be 100% */ __unset_policy_for_playing(); - ttsd_data_clear_sound_data(&sound_data); + ttsd_data_clear_sound_data(player->uid, &sound_data); return; } @@ -576,7 +574,7 @@ static void __play_thread(void *data, Ecore_Thread *thread) /* unset volume policy, volume will be 100% */ __unset_policy_for_playing(); - ttsd_data_clear_sound_data(&sound_data); + ttsd_data_clear_sound_data(player->uid, &sound_data); return; } @@ -602,7 +600,7 @@ static void __play_thread(void *data, Ecore_Thread *thread) /* unset volume policy, volume will be 100% */ __unset_policy_for_playing(); - ttsd_data_clear_sound_data(&sound_data); + ttsd_data_clear_sound_data(player->uid, &sound_data); return; } @@ -643,7 +641,7 @@ static void __play_thread(void *data, Ecore_Thread *thread) SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio"); } - ttsd_data_clear_sound_data(&sound_data); + ttsd_data_clear_sound_data(player->uid, &sound_data); /* unset volume policy, volume will be 100% */ __unset_policy_for_playing(); return; @@ -659,7 +657,6 @@ static void __play_thread(void *data, Ecore_Thread *thread) /* unset volume policy, volume will be 100% */ __unset_policy_for_playing(); - ttsd_data_clear_sound_data(&sound_data); return; } @@ -669,14 +666,14 @@ static void __play_thread(void *data, Ecore_Thread *thread) /* unset volume policy, volume will be 100% */ __unset_policy_for_playing(); - ttsd_data_clear_sound_data(&sound_data); + ttsd_data_clear_sound_data(player->uid, &sound_data); return; } SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id); } - ttsd_data_clear_sound_data(&sound_data); + ttsd_data_clear_sound_data(player->uid, &sound_data); if (NULL == g_playing_info) { SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL"); diff --git a/server/ttsd_server.c b/server/ttsd_server.c index 72cee8f..e9289cc 100644 --- a/server/ttsd_server.c +++ b/server/ttsd_server.c @@ -92,7 +92,7 @@ static int __synthesis(int uid, const char* credential) ttsdc_send_set_state_message(pid, uid, APP_STATE_READY); - ttsd_data_clear_speak_data(&speak_data); + ttsd_data_clear_speak_data(uid, &speak_data); return 0; } @@ -123,9 +123,9 @@ static int __synthesis(int uid, const char* credential) g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential); } - ttsd_data_clear_speak_data(&speak_data); + ttsd_data_clear_speak_data(uid, &speak_data); } else { - ttsd_data_clear_speak_data(&speak_data); + ttsd_data_clear_speak_data(uid, &speak_data); } SLOG(LOG_DEBUG, tts_tag(), "@@@ SYNTHESIS END"); -- 2.7.4