Add mutex lock to avoid thread unsafety issue 55/319355/1
authorsooyeon <sooyeon.kim@samsung.com>
Mon, 21 Oct 2024 14:30:09 +0000 (23:30 +0900)
committerTizen AI <ai.tzn.sec@samsung.com>
Tue, 22 Oct 2024 01:53:36 +0000 (10:53 +0900)
Change-Id: Idc7991e2986c03d400e284d54bef9a1b5dcfef71
Signed-off-by: sooyeon <sooyeon.kim@samsung.com>
client/vc_mgr_ducking.cpp

index ee49f018ad7d6ea28592e57497e615fa74cbfa14..1b8bd874a7ee6657b471065e093b0bd8e82f29e3 100644 (file)
@@ -15,6 +15,7 @@
 */
 
 #include <sound_manager.h>
+#include <pthread.h>
 #include <unordered_map>
 
 #include "vc_main.h"
@@ -32,6 +33,7 @@ static sound_stream_ducking_h g_alarm_stream_h = nullptr;
 static sound_stream_ducking_h g_voice_information_stream_h = nullptr;
 static std::unordered_map<sound_stream_type_e, bool> g_is_requested_activation;
 static std::unordered_map<sound_stream_type_e, bool> g_is_deactivated;
+static pthread_mutex_t g_ducking_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 static bool g_is_created = false;
 
@@ -135,12 +137,15 @@ int vc_mgr_ducking_destroy(void)
 
 static int activate_ducking_sound_stream(sound_stream_type_e type, sound_stream_ducking_h header, double ratio)
 {
+       pthread_mutex_lock(&g_ducking_mutex);
+
        bool is_ducked = false;
        int ret = SOUND_MANAGER_ERROR_NONE;
        ret = sound_manager_is_ducked(header, &is_ducked);
        if (is_ducked) {
                SLOG(LOG_DEBUG, TAG_VCM, "The %s is already ducked", get_ducking_stream(type));
                g_is_deactivated[type] = false;
+               pthread_mutex_unlock(&g_ducking_mutex);
                return ret;
        }
 
@@ -155,6 +160,7 @@ static int activate_ducking_sound_stream(sound_stream_type_e type, sound_stream_
                g_is_deactivated[type] = false;
        }
 
+       pthread_mutex_unlock(&g_ducking_mutex);
        return ret;
 }
 
@@ -181,8 +187,12 @@ int vc_mgr_ducking_activate(double ratio)
 
 static bool deactivate_ducking_sound_stream(sound_stream_type_e type, sound_stream_ducking_h header)
 {
-       if (true == g_is_deactivated[type])
+       pthread_mutex_lock(&g_ducking_mutex);
+
+       if (true == g_is_deactivated[type]) {
+               pthread_mutex_unlock(&g_ducking_mutex);
                return true;
+       }
 
        bool is_ducked = false;
        int ret = SOUND_MANAGER_ERROR_NONE;
@@ -190,17 +200,20 @@ static bool deactivate_ducking_sound_stream(sound_stream_type_e type, sound_stre
        if (false == is_ducked && false == g_is_requested_activation[type]) {
                SLOG(LOG_DEBUG, TAG_VCM, "The %s is already recovered from ducking", get_ducking_stream(type));
                g_is_deactivated[type] = true;
+               pthread_mutex_unlock(&g_ducking_mutex);
                return true;
        }
 
        if (false == is_ducked && true == g_is_requested_activation[type]) {
                SLOG(LOG_DEBUG, TAG_VCM, "The %s is not activated to be ducked. Please wait for duration time and try again.", get_ducking_stream(type));
+               pthread_mutex_unlock(&g_ducking_mutex);
                return false;
        }
 
        ret = sound_manager_deactivate_ducking(header);
        if (SOUND_MANAGER_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to deactivate ducking for %s. Please try again.", get_ducking_stream(type));
+               pthread_mutex_unlock(&g_ducking_mutex);
                return false;
        }
 
@@ -208,6 +221,7 @@ static bool deactivate_ducking_sound_stream(sound_stream_type_e type, sound_stre
        g_is_requested_activation[type] = false;
        g_is_deactivated[type] = true;
 
+       pthread_mutex_unlock(&g_ducking_mutex);
        return true;
 }