From 8e0ec5daca6a6f41e397abe27d0ff0efcf138a81 Mon Sep 17 00:00:00 2001 From: sooyeon Date: Mon, 21 Oct 2024 23:30:09 +0900 Subject: [PATCH] Add mutex lock to avoid thread unsafety issue Change-Id: Idc7991e2986c03d400e284d54bef9a1b5dcfef71 Signed-off-by: sooyeon --- client/vc_mgr_ducking.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/client/vc_mgr_ducking.cpp b/client/vc_mgr_ducking.cpp index ee49f01..1b8bd87 100644 --- a/client/vc_mgr_ducking.cpp +++ b/client/vc_mgr_ducking.cpp @@ -15,6 +15,7 @@ */ #include +#include #include #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 g_is_requested_activation; static std::unordered_map 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; } -- 2.34.1