Add mutex to wait play thread safely 94/248094/1 submit/tizen/20201120.141139 submit/tizen/20201123.062812
authorSuyeon Hwang <stom.hwang@samsung.com>
Fri, 20 Nov 2020 07:28:59 +0000 (16:28 +0900)
committerSooyeon Kim <sooyeon.kim@samsung.com>
Fri, 20 Nov 2020 08:59:49 +0000 (08:59 +0000)
Change-Id: I4120e70c86042c9fcb87c31c969dba9df6640296
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
server/ttsd_player.c

index a3b3231..8af5aa6 100644 (file)
@@ -15,6 +15,7 @@
 #include <Ecore.h>
 #include <sound_manager.h>
 #include <sound_manager_internal.h>
+#include <pthread.h>
 
 #include "ttsd_main.h"
 #include "ttsd_player.h"
@@ -97,6 +98,8 @@ static int g_focus_watch_id = -1;
 
 static double g_bg_volume_ratio;
 
+static pthread_mutex_t g_play_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
+
 /*
 * Internal Interfaces
 */
@@ -564,6 +567,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
        /* set volume policy as 40% */
        __set_policy_for_playing(40);
        while (1) { // 1st while(1)
+               pthread_mutex_lock(&g_play_thread_mutex);
                /* check g_playing_info one more time */
                if (false == __is_player_valid(player)) {
                        SLOG(LOG_INFO, tts_tag(), "[Player INFO] Player is not valid");
@@ -573,6 +577,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
                        }
                        __unset_policy_for_playing();
+                       pthread_mutex_unlock(&g_play_thread_mutex);
                        return;
                }
 
@@ -625,6 +630,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
 
                                /* unset volume policy, volume will be 100% */
                                __unset_policy_for_playing();
+                               pthread_mutex_unlock(&g_play_thread_mutex);
                                return;
                        }
                        SLOG(LOG_INFO, tts_tag(), "[Player] Sound info : id(%d) data(%p) size(%d) audiotype(%d) rate(%d) event(%d)",
@@ -654,6 +660,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                                }
                                                /* unset volume policy, volume will be 100% */
                                                __unset_policy_for_playing();
+                                               pthread_mutex_unlock(&g_play_thread_mutex);
                                                return;
                                        } else if (0 < ttsd_data_get_sound_data_size(player->uid)) {
                                                /* new audio data come */
@@ -690,6 +697,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
 
                                /* resume play thread */
                                player->state = APP_STATE_PLAYING;
+                               pthread_mutex_unlock(&g_play_thread_mutex);
                                continue;
                        } // NULL == sound_data
 
@@ -706,6 +714,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);
+                                       pthread_mutex_unlock(&g_play_thread_mutex);
                                        return;
                                }
 
@@ -731,6 +740,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);
+                                               pthread_mutex_unlock(&g_play_thread_mutex);
                                                return;
                                        }
 
@@ -745,6 +755,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                } // TTSE_RESULT_EVENT_FINISH == sound_data->event
                                SLOG(LOG_INFO, tts_tag(), "[Player] Event(%d) utterance : uid(%d), uttid(%d)", sound_data->event, player->uid, sound_data->utt_id);
                                ttsd_data_clear_sound_data(&sound_data);
+                               pthread_mutex_unlock(&g_play_thread_mutex);
                                continue;
                        } // (NULL == sound_data->data || 0 >= sound_data->data_size)
                } // NO player->is_paused_data
@@ -763,6 +774,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
 
                                ttsd_data_clear_sound_data(&sound_data);
 
+                               pthread_mutex_unlock(&g_play_thread_mutex);
                                return;
                        }
 
@@ -797,6 +809,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
 
                                        ttsd_data_clear_sound_data(&sound_data);
 
+                                       pthread_mutex_unlock(&g_play_thread_mutex);
                                        return;
                                }
                                SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Prepare audio");
@@ -830,6 +843,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
 
                                ttsd_data_clear_sound_data(&sound_data);
 
+                               pthread_mutex_unlock(&g_play_thread_mutex);
                                return;
                        } // (NULL == g_playing_info && APP_STATE_PAUSED != player->state)
 
@@ -856,6 +870,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                }
                                /* unset volume policy, volume will be 100% */
                                __unset_policy_for_playing();
+                               pthread_mutex_unlock(&g_play_thread_mutex);
                                return;
                        } // (APP_STATE_PAUSED == player->state)
                } // while (APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state)
@@ -876,6 +891,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);
+                       pthread_mutex_unlock(&g_play_thread_mutex);
                        return;
                } // (NULL == g_playing_info && APP_STATE_READY == player->state)
 
@@ -889,6 +905,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);
+                               pthread_mutex_unlock(&g_play_thread_mutex);
                                return;
                        }
 
@@ -898,6 +915,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);
+                               pthread_mutex_unlock(&g_play_thread_mutex);
                                return;
                        }
 
@@ -916,8 +934,11 @@ 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);
+                       pthread_mutex_unlock(&g_play_thread_mutex);
                        return;
                }
+
+               pthread_mutex_unlock(&g_play_thread_mutex);
        } // end of 1st while(1)
 }
 
@@ -1283,30 +1304,32 @@ int ttsd_player_stop(int uid)
                SLOG(LOG_DEBUG, tts_tag(), "[Player] No current playing");
        }
 
+       pthread_mutex_lock(&g_play_thread_mutex);
        if (NULL == g_playing_info) {
                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;
-                       }
+               // int thread_count = ecore_thread_active_get();
+               // int count = 0;
+               // while (0 < thread_count) {
+               //      usleep(20000);
 
-                       thread_count = ecore_thread_active_get();
-               }
+               //      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);
 
@@ -1375,32 +1398,34 @@ int ttsd_player_pause(int uid)
 
        current->state = APP_STATE_PAUSED;
 
+       pthread_mutex_lock(&g_play_thread_mutex);
        if (NULL == g_playing_info) {
                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);
+               // 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);
+               //      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;
-                       }
+               //      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();
-               }
+               //      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);