Add mutex to remove race condition among functions of ttsd_player 93/254093/1
authorSuyeon Hwang <stom.hwang@samsung.com>
Tue, 23 Feb 2021 07:59:37 +0000 (16:59 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Tue, 23 Feb 2021 09:22:32 +0000 (09:22 +0000)
Change-Id: I34493c7367bda630ae5e0cb78348a3db9caaefb1
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
(cherry picked from commit c6181d507d85f69474837dba5fc8733775f66e18)

server/ttsd_player.c

index 8af5aa6..aaab405 100644 (file)
@@ -99,6 +99,7 @@ static int g_focus_watch_id = -1;
 static double g_bg_volume_ratio;
 
 static pthread_mutex_t g_play_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t g_player_control_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /*
 * Internal Interfaces
@@ -1241,18 +1242,23 @@ int ttsd_player_destroy_instance(int uid)
 
 int ttsd_player_play(int uid)
 {
+       pthread_mutex_lock(&g_player_control_mutex);
        if (false == g_player_init) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
+               pthread_mutex_unlock(&g_player_control_mutex);
                return -1;
        }
 
        if (NULL != g_playing_info) {
                if (uid == g_playing_info->uid) {
                        SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
+                       pthread_mutex_unlock(&g_player_control_mutex);
                        return 0;
                } else {
                        SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
+                       pthread_mutex_unlock(&g_player_control_mutex);
                        ttsd_player_stop(g_playing_info->uid);
+                       pthread_mutex_lock(&g_player_control_mutex);
                }
        }
 
@@ -1261,6 +1267,7 @@ int ttsd_player_play(int uid)
        /* Check sound queue size */
        if (0 == ttsd_data_get_sound_data_size(uid)) {
                SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
+               pthread_mutex_unlock(&g_player_control_mutex);
                return -1;
        }
 
@@ -1269,6 +1276,7 @@ int ttsd_player_play(int uid)
        current = __player_get_item(uid);
        if (NULL == current) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
+               pthread_mutex_unlock(&g_player_control_mutex);
                return -1;
        }
 
@@ -1283,14 +1291,17 @@ int ttsd_player_play(int uid)
                ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
        }
 
+       pthread_mutex_unlock(&g_player_control_mutex);
        return 0;
 }
 
 int ttsd_player_stop(int uid)
 {
+       pthread_mutex_lock(&g_player_control_mutex);
        int ret = ttsd_player_clear(uid);
        if (0 != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to stop player, ret(%d)", ret);
+               pthread_mutex_unlock(&g_player_control_mutex);
                return ret;
        }
 
@@ -1309,30 +1320,12 @@ int ttsd_player_stop(int uid)
                SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
                SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
                SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
-
-               /* The thread should be released */
-               // int thread_count = ecore_thread_active_get();
-               // int count = 0;
-               // while (0 < thread_count) {
-               //      usleep(20000);
-
-               //      count++;
-               //      if (50 == count) {
-               //              SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
-               //              break;
-               //      }
-
-               //      thread_count = ecore_thread_active_get();
-               // }
-
-               SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
-               SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
-               SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
        }
        pthread_mutex_unlock(&g_play_thread_mutex);
 
        SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Stop player : uid(%d)", uid);
 
+       pthread_mutex_unlock(&g_player_control_mutex);
        return 0;
 }
 
@@ -1368,10 +1361,12 @@ int ttsd_player_clear(int uid)
 
 int ttsd_player_pause(int uid)
 {
+       pthread_mutex_lock(&g_player_control_mutex);
        SLOG(LOG_DEBUG, tts_tag(), "[Player] pause player : uid(%d)", uid);
 
        if (false == g_player_init) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
+               pthread_mutex_unlock(&g_player_control_mutex);
                return -1;
        }
 
@@ -1380,6 +1375,7 @@ int ttsd_player_pause(int uid)
        current = __player_get_item(uid);
        if (NULL == current) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] ttsd_player_pause() : uid(%d) is not valid", uid);
+               pthread_mutex_unlock(&g_player_control_mutex);
                return -1;
        }
 
@@ -1403,42 +1399,24 @@ int ttsd_player_pause(int uid)
                SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
                SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
                SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
-
-               /* The thread should be released */
-               // int thread_count = ecore_thread_active_get();
-               // int count = 0;
-               // while (0 < thread_count) {
-               //      usleep(10000);
-
-               //      count++;
-               //      SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] current(%p), state(%d)", current, current->state);
-
-               //      if (30 == count) {
-               //              SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue. current(%p) current state(%d)", current, current->state);
-               //              break;
-               //      }
-
-               //      thread_count = ecore_thread_active_get();
-               // }
-
-               SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
-               SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
-               SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
        }
        pthread_mutex_unlock(&g_play_thread_mutex);
 
 
        SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%d)", uid);
 
+       pthread_mutex_unlock(&g_player_control_mutex);
        return 0;
 }
 
 int ttsd_player_resume(int uid)
 {
+       pthread_mutex_lock(&g_player_control_mutex);
        SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume player : uid(%d)", uid);
 
        if (false == g_player_init) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
+               pthread_mutex_unlock(&g_player_control_mutex);
                return -1;
        }
 
@@ -1447,6 +1425,7 @@ int ttsd_player_resume(int uid)
        current = __player_get_item(uid);
        if (NULL == current) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
+               pthread_mutex_unlock(&g_player_control_mutex);
                return -1;
        }
 
@@ -1460,13 +1439,16 @@ int ttsd_player_resume(int uid)
        SLOG(LOG_INFO, tts_tag(), "[Player] Resume to run thread");
        ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
 
+       pthread_mutex_unlock(&g_player_control_mutex);
        return 0;
 }
 
 int ttsd_player_all_stop()
 {
+       pthread_mutex_lock(&g_player_control_mutex);
        if (false == g_player_init) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
+               pthread_mutex_unlock(&g_player_control_mutex);
                return -1;
        }
 
@@ -1511,13 +1493,16 @@ int ttsd_player_all_stop()
 
        SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] player all stop!!");
 
+       pthread_mutex_unlock(&g_player_control_mutex);
        return 0;
 }
 
 int ttsd_player_play_pcm(int uid)
 {
+       pthread_mutex_lock(&g_player_control_mutex);
        if (false == g_player_init) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
+               pthread_mutex_unlock(&g_player_control_mutex);
                return -1;
        }
 
@@ -1527,7 +1512,9 @@ int ttsd_player_play_pcm(int uid)
                        return 0;
                } else {
                        SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
+                       pthread_mutex_unlock(&g_player_control_mutex);
                        ttsd_player_stop(g_playing_info->uid);
+                       pthread_mutex_lock(&g_player_control_mutex);
                }
        }
 
@@ -1543,6 +1530,7 @@ int ttsd_player_play_pcm(int uid)
        current = __player_get_item(uid);
        if (NULL == current) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
+               pthread_mutex_unlock(&g_player_control_mutex);
                return -1;
        }
 
@@ -1557,6 +1545,7 @@ int ttsd_player_play_pcm(int uid)
                ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
        }
 
+       pthread_mutex_unlock(&g_player_control_mutex);
        return 0;
 }