Add pthread_mutex for safety 97/107097/1
authorsooyeon.kim <sooyeon.kim@samsung.com>
Mon, 26 Dec 2016 11:41:45 +0000 (20:41 +0900)
committersooyeon.kim <sooyeon.kim@samsung.com>
Mon, 26 Dec 2016 11:41:45 +0000 (20:41 +0900)
Change-Id: I2a25f3346954190a6784eabe01e082454dc73b43
Signed-off-by: sooyeon.kim <sooyeon.kim@samsung.com>
server/ttsd_data.cpp

index 932ea35..385b84d 100644 (file)
 
 using namespace std;
 
-typedef struct 
+typedef struct
 {
        char*   lang;
        int     vctype;
 }used_voice_s;
 
-typedef struct 
+typedef struct
 {
        int             pid;
        int             uid;
@@ -41,6 +41,7 @@ typedef struct
 
 static vector<app_data_s> 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;
 
 /*
@@ -68,7 +69,7 @@ int __data_show_list()
 int __data_show_sound_list(int index)
 {
        SLOG(LOG_DEBUG, tts_tag(), "----- Sound list -----");
-       
+
        unsigned int i = 0;
        std::list<sound_data_s*>::iterator iter;
        for (iter = g_app_list[index].m_wav_data.begin(); iter != g_app_list[index].m_wav_data.end(); iter++) {
@@ -145,7 +146,7 @@ int ttsd_data_new_client(int pid, int uid)
 
 #ifdef DATA_DEBUG
        __data_show_list();
-#endif 
+#endif
        return TTSD_ERROR_NONE;
 }
 
@@ -154,7 +155,7 @@ int ttsd_data_delete_client(int uid)
        int index = 0;
 
        index = ttsd_data_is_client(uid);
-       
+
        if (index < 0) {
                SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
                return -1;
@@ -169,7 +170,7 @@ int ttsd_data_delete_client(int uid)
 
 #ifdef DATA_DEBUG
        __data_show_list();
-#endif 
+#endif
        return TTSD_ERROR_NONE;
 }
 
@@ -179,7 +180,7 @@ int ttsd_data_is_client(int uid)
 
        for (int i = 0; i < vsize; i++) {
                if(g_app_list[i].uid == uid) {
-                       return i;               
+                       return i;
                }
        }
 
@@ -196,7 +197,7 @@ int ttsd_data_get_pid(int uid)
        int index;
 
        index = ttsd_data_is_client(uid);
-       
+
        if (index < 0)  {
                SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
                return TTSD_ERROR_INVALID_PARAMETER;
@@ -209,13 +210,19 @@ int ttsd_data_get_speak_data_size(int uid)
 {
        int index = 0;
        index = ttsd_data_is_client(uid);
-       
+
        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_speak_data_mutex);
        int size = g_app_list[index].m_speak_data.size();
+
+       /* mutex is unlocked */
+       pthread_mutex_unlock(&g_speak_data_mutex);
+
        return size;
 }
 
@@ -278,7 +285,7 @@ int ttsd_data_reset_used_voice(int uid, ttsd_used_voice_cb callback)
                if (NULL != iter->lang) {
                        free(iter->lang);
                }
-       } 
+       }
 
        g_app_list[index].m_used_voice.clear();
 
@@ -298,7 +305,10 @@ int ttsd_data_add_speak_data(int uid, speak_data_s* data)
                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_speak_data_mutex);
+
        g_app_list[index].m_speak_data.insert(g_app_list[index].m_speak_data.end(), data);
 
        if (1 == data->utt_id)
@@ -306,7 +316,9 @@ int ttsd_data_add_speak_data(int uid, speak_data_s* data)
 
 #ifdef DATA_DEBUG
        __data_show_text_list(index);
-#endif 
+#endif
+       pthread_mutex_unlock(&g_speak_data_mutex);
+
        return TTSD_ERROR_NONE;
 }
 
@@ -320,10 +332,14 @@ int ttsd_data_get_speak_data(int uid, speak_data_s** data)
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
+       /* mutex is locked */
+       pthread_mutex_lock(&g_speak_data_mutex);
+
        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 
+               SLOG(LOG_WARN, tts_tag(), "[DATA WARNING] There is no speak data");
+#endif
+               pthread_mutex_unlock(&g_speak_data_mutex);
                return -1;
        }
 
@@ -333,7 +349,9 @@ int ttsd_data_get_speak_data(int uid, speak_data_s** data)
 
 #ifdef DATA_DEBUG
        __data_show_text_list(index);
-#endif 
+#endif
+       pthread_mutex_unlock(&g_speak_data_mutex);
+
        return TTSD_ERROR_NONE;
 }
 
@@ -358,7 +376,7 @@ int ttsd_data_add_sound_data(int uid, sound_data_s* data)
 
 #ifdef DATA_DEBUG
        __data_show_sound_list(index);
-#endif 
+#endif
 
        /* mutex is unlocked */
        pthread_mutex_unlock(&g_sound_data_mutex);
@@ -394,7 +412,7 @@ int ttsd_data_get_sound_data(int uid, sound_data_s** data)
 
 #ifdef DATA_DEBUG
        __data_show_sound_list(index);
-#endif 
+#endif
 
        /* mutex is unlocked */
        pthread_mutex_unlock(&g_sound_data_mutex);
@@ -433,6 +451,9 @@ int ttsd_data_clear_data(int uid)
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
+       /* mutex is locked */
+       pthread_mutex_lock(&g_sound_data_mutex);
+
        int removed_last_uttid = -1;
        speak_data_s* temp_speak = NULL;
        sound_data_s* temp_sound = NULL;
@@ -488,6 +509,9 @@ int ttsd_data_clear_data(int uid)
        g_app_list[index].m_speak_data.clear();
        g_app_list[index].m_wav_data.clear();
 
+       /* mutex is unlocked */
+       pthread_mutex_unlock(&g_sound_data_mutex);
+
        return TTSD_ERROR_NONE;
 }
 
@@ -555,8 +579,8 @@ int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
 
 #ifdef DATA_DEBUG
        __data_show_list();
-#endif 
-       
+#endif
+
        /* Copy app info */
        vector<app_data_s> temp_app_list;
        int vsize = g_app_list.size();
@@ -582,7 +606,7 @@ int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
        for (i = 0;i < vsize;i++) {
                temp_app_list.erase(temp_app_list.begin());
        }
-       
+
        return 0;
 }
 
@@ -608,7 +632,7 @@ int ttsd_data_is_current_playing()
 
        for (int i = 0; i < vsize; i++) {
                if(g_app_list[i].state == APP_STATE_PLAYING) {
-                       return g_app_list[i].uid;               
+                       return g_app_list[i].uid;
                }
        }
 
@@ -622,7 +646,7 @@ int ttsd_data_get_same_pid_client_count(int pid)
 
        for (int i = 0;i < vsize;i++) {
                if(g_app_list[i].pid == pid) {
-                       number++;               
+                       number++;
                }
        }
 
@@ -670,7 +694,7 @@ int ttsd_data_save_error_log(int uid, FILE* fp)
        }
 
        fprintf(fp, "----------------------");
-       
+
        /* get speck data */
        fprintf(fp, "----- Text list -----");
 
@@ -682,6 +706,6 @@ int ttsd_data_save_error_log(int uid, FILE* fp)
                i++;
        }
        fprintf(fp, "---------------------");
-       
+
        return 0;
 }