From d62ca0713d22a450e8c44032d0468138dfba548f Mon Sep 17 00:00:00 2001 From: sungrae jo Date: Mon, 16 Mar 2020 16:14:47 +0900 Subject: [PATCH] Added mutex code for command list Added mutex code Fixed resource release order for list destory. Change-Id: I050ad117a32c4f051f2f669b64c0cb5890b88ee3 Signed-off-by: sungrae jo --- common/vc_command.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/common/vc_command.c b/common/vc_command.c index e271c0b..6d0b546 100644 --- a/common/vc_command.c +++ b/common/vc_command.c @@ -34,8 +34,11 @@ #include "voice_control_key_defines.h" static pthread_mutex_t g_cmd_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_cmd_list_mutex = PTHREAD_MUTEX_INITIALIZER; #define CMD_MUTEX_LOCK() pthread_mutex_lock(&g_cmd_mutex) #define CMD_MUTEX_UNLOCK() pthread_mutex_unlock(&g_cmd_mutex) +#define CMD_LIST_MUTEX_LOCK() pthread_mutex_lock(&g_cmd_list_mutex) +#define CMD_LIST_MUTEX_UNLOCK() pthread_mutex_unlock(&g_cmd_list_mutex) static int g_feature_enabled = -1; static bool g_privilege_allowed = false; @@ -181,8 +184,11 @@ int vc_cmd_list_create(vc_cmd_list_h* vc_cmd_list) return VC_ERROR_PERMISSION_DENIED; } + CMD_LIST_MUTEX_LOCK(); + if (NULL == vc_cmd_list) { SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL"); + CMD_LIST_MUTEX_UNLOCK(); return VC_ERROR_INVALID_PARAMETER; } @@ -190,6 +196,7 @@ int vc_cmd_list_create(vc_cmd_list_h* vc_cmd_list) if (NULL == list) { SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Not enough memory"); + CMD_LIST_MUTEX_UNLOCK(); return VC_ERROR_OUT_OF_MEMORY; } @@ -202,6 +209,7 @@ int vc_cmd_list_create(vc_cmd_list_h* vc_cmd_list) SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list(%p)", *vc_cmd_list); + CMD_LIST_MUTEX_UNLOCK(); return VC_ERROR_NONE; } @@ -214,12 +222,14 @@ int vc_cmd_list_destroy(vc_cmd_list_h vc_cmd_list, bool release_command) return VC_ERROR_PERMISSION_DENIED; } + CMD_LIST_MUTEX_LOCK(); + if (NULL == vc_cmd_list) { SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL"); + CMD_LIST_MUTEX_UNLOCK(); return VC_ERROR_INVALID_PARAMETER; } - vc_cmd_list_remove_all(vc_cmd_list, release_command); vc_cmd_list_s* list = NULL; list = (vc_cmd_list_s*)vc_cmd_list; @@ -230,15 +240,19 @@ int vc_cmd_list_destroy(vc_cmd_list_h vc_cmd_list, bool release_command) iter = g_list_find(g_cmdlist_list, list); if (NULL == iter) { SLOG(LOG_ERROR, TAG_VCCMD, "Fail to destroy client : handle is not valid"); + CMD_LIST_MUTEX_UNLOCK(); return VC_ERROR_INVALID_PARAMETER; } g_cmdlist_list = g_list_remove_link(g_cmdlist_list, iter); - if (iter->data) - free(iter->data); - iter->data = NULL; - iter = NULL; + + vc_cmd_list_remove_all((vc_cmd_list_h)list, release_command); + free(list); + list = NULL; + SLOG(LOG_DEBUG, TAG_VCCMD, "Client destroy"); + + CMD_LIST_MUTEX_UNLOCK(); return VC_ERROR_NONE; } @@ -372,11 +386,14 @@ int vc_cmd_list_remove_all(vc_cmd_list_h vc_cmd_list, bool release_command) SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list (%p), release command (%s)" , list, release_command ? "true" : "false"); - if (true == release_command) - g_slist_foreach(list->list, __vc_cmd_list_remove_all_foreach, NULL); - g_slist_free(list->list); - list->list = NULL; - list->index = -1; + if (true == release_command) { + if (list->list) { + g_slist_foreach(list->list, __vc_cmd_list_remove_all_foreach, NULL); + g_slist_free(list->list); + list->list = NULL; + } + list->index = -1; + } SLOG(LOG_DEBUG, TAG_VCCMD, "@@@"); -- 2.7.4