Lock all accessing state code and Use cpp style lock and iteration 51/256651/1
authorSuyeon Hwang <stom.hwang@samsung.com>
Thu, 8 Apr 2021 11:53:02 +0000 (20:53 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Thu, 8 Apr 2021 11:53:02 +0000 (20:53 +0900)
Change-Id: I37d67af97884e332ca9392b7d880d91371db5b31
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
server/ttsd_data.cpp

index da73d6d..8e675f7 100644 (file)
@@ -13,6 +13,7 @@
 
 #include <list>
 #include <pthread.h>
+#include <mutex>
 #include <vector>
 
 #include "ttsd_main.h"
@@ -47,32 +48,31 @@ 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;
 
-static pthread_mutex_t g_app_state_mutex = PTHREAD_MUTEX_INITIALIZER;
+static mutex g_app_state_mutex;
 
 /* If engine is running */
 static ttsd_synthesis_control_e        g_synth_control;
 
-/*
-* functions for debug
-*/
-int __data_show_list()
+#ifdef DATA_DEBUG
+static void __data_show_list()
 {
        int vsize = g_app_list.size();
 
        SLOG(LOG_DEBUG, tts_tag(), "----- client list -----");
 
-       for (int i=0; i < vsize; i++) {
-               SECURE_SLOG(LOG_DEBUG, tts_tag(), "[%dth] pid(%d), uid(%d), state(%d)", i, g_app_list[i].pid, g_app_list[i].uid, g_app_list[i].state);
-       }
-
        if (0 == vsize) {
                SLOG(LOG_DEBUG, tts_tag(), "No Client");
+               return;
        }
 
-       SLOG(LOG_DEBUG, tts_tag(), "-----------------------");
+       lock_guard<mutex> lock(g_app_state_mutex);
+       for (int i = 0; i < vsize; i++) {
+               SECURE_SLOG(LOG_DEBUG, tts_tag(), "[%dth] pid(%d), uid(%d), state(%d)", i, g_app_list[i].pid, g_app_list[i].uid, g_app_list[i].state);
+       }
 
-       return TTSD_ERROR_NONE;
+       SLOG(LOG_DEBUG, tts_tag(), "-----------------------");
 }
+#endif
 
 int __data_show_sound_list(int index)
 {
@@ -756,61 +756,62 @@ int ttsd_data_get_client_state(int uid, app_tts_state_e* state)
        int index = 0;
 
        index = ttsd_data_is_client(uid);
-       if (index < 0)  {
+       if (index < 0) {
                SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
-       //TODO : lock more specific memory
-       pthread_mutex_lock(&g_app_state_mutex);
+       lock_guard<mutex> lock(g_app_state_mutex);
        *state = g_app_list[index].state;
-       pthread_mutex_unlock(&g_app_state_mutex);
 
        return TTSD_ERROR_NONE;
 }
 
+static int __get_playing_app_uid()
+{
+       for (auto app : g_app_list) {
+               if (APP_STATE_PLAYING == app.state) {
+                       return app.uid;
+               }
+       }
+
+       return -1;
+}
+
 int ttsd_data_set_client_state(int uid, app_tts_state_e state)
 {
        int index = 0;
 
        index = ttsd_data_is_client(uid);
-       if (index < 0)  {
+       if (index < 0) {
                SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
-       //TODO : lock more specific memory
-       pthread_mutex_lock(&g_app_state_mutex);
+       lock_guard<mutex> lock(g_app_state_mutex);
+       if (g_app_list[index].state == state) {
+               SLOG(LOG_ERROR, tts_tag(), "[DATA] Already current state. (%d)", state);
+               return TTSD_ERROR_NONE;
+       }
+
        /* The client of playing state of all clients is only one. need to check state. */
-       if (APP_STATE_PLAYING == state) {
-               int vsize = g_app_list.size();
-               for (int i = 0; i < vsize; i++) {
-                       if(g_app_list[i].state == APP_STATE_PLAYING) {
-                               SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] A playing client has already existed.");
-                               pthread_mutex_unlock(&g_app_state_mutex);
-                               return -1;
-                       }
-               }
+       int playing_uid = __get_playing_app_uid();
+       if (APP_STATE_PLAYING == state && 0 < playing_uid) {
+               SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] A playing client has already existed. playing app uid(%d)", playing_uid);
+               return TTSD_ERROR_OPERATION_FAILED;
        }
 
        g_app_list[index].state = state;
-       pthread_mutex_unlock(&g_app_state_mutex);
 
        return TTSD_ERROR_NONE;
 }
 
 int ttsd_data_get_current_playing()
 {
-       int vsize = g_app_list.size();
+       lock_guard<mutex> lock(g_app_state_mutex);
+       int uid = __get_playing_app_uid();
 
-       for (int i = 0; i < vsize; i++) {
-               if (APP_STATE_PLAYING == g_app_list[i].state) {
-                       SLOG(LOG_DEBUG, tts_tag(), "[DATA] uid(%d) is playing", g_app_list[i].uid);
-                       return g_app_list[i].uid;
-               }
-       }
-
-       return -1;
+       return uid;
 }
 
 int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
@@ -828,8 +829,8 @@ int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
        vector<app_data_s> temp_app_list;
        int vsize = g_app_list.size();
 
-       int i = 0;
-       for (i = 0;i < vsize;i++) {
+       g_app_state_mutex.lock();
+       for (int i = 0; i < vsize; i++) {
                app_data_s app;
                app.pid = g_app_list[i].pid;
                app.uid = g_app_list[i].uid;
@@ -838,18 +839,15 @@ int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
 
                temp_app_list.insert(temp_app_list.end(), app);
        }
+       g_app_state_mutex.unlock();
 
-       for (i = 0;i < vsize;i++) {
+       for (int i = 0; i < vsize; i++) {
                SECURE_SLOG(LOG_DEBUG, tts_tag(), "[%dth] pid(%d), uid(%d), state(%d)", i, temp_app_list[i].pid, temp_app_list[i].uid, temp_app_list[i].state);
                if (false == callback(temp_app_list[i].pid, temp_app_list[i].uid, temp_app_list[i].state, user_data)) {
                        break;
                }
        }
 
-       for (i = 0;i < vsize;i++) {
-               temp_app_list.erase(temp_app_list.begin());
-       }
-
        return 0;
 }
 
@@ -869,19 +867,6 @@ bool ttsd_data_is_uttid_valid(int uid, int uttid)
        return true;
 }
 
-int ttsd_data_is_current_playing()
-{
-       int vsize = g_app_list.size();
-
-       for (int i = 0; i < vsize; i++) {
-               if(g_app_list[i].state == APP_STATE_PLAYING) {
-                       return g_app_list[i].uid;
-               }
-       }
-
-       return -1;
-}
-
 int ttsd_data_get_same_pid_client_count(int pid)
 {
        int vsize = g_app_list.size();