Add mutex to avoid race condition of tidl handle 36/260836/2
authorSuyeon Hwang <stom.hwang@samsung.com>
Mon, 5 Jul 2021 11:07:10 +0000 (20:07 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Wed, 7 Jul 2021 01:36:19 +0000 (10:36 +0900)
Change-Id: Iaa19e1b18eb0601655ac492bfa9c54e326ce2e62
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
server/ttsd_tidl.c

index 2c67fa2..662d678 100644 (file)
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
+
 #include <stdlib.h>
 #include <Ecore.h>
+#include <pthread.h>
+
 #include "ttsd_main.h"
 #include "ttsd_ipc.h"
 #include "ttsd_data.h"
@@ -28,6 +31,9 @@ static GList* g_tidl_proxy_infos = NULL;
 
 rpc_port_stub_tts_callback_s g_callback;
 
+static pthread_mutex_t g_tidl_proxy_infos_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+
 static tts_tidl_proxy_info_s* __get_tidl_proxy_info_s(int uid)
 {
        GList* iter = NULL;
@@ -54,23 +60,28 @@ static tts_tidl_proxy_info_s* __get_tidl_proxy_info_s(int uid)
 void __send_msg(int pid, int uid, bundle* msg)
 {
        SLOG(LOG_INFO, tts_tag(), "[TIDL] start : send msg : pid(%d), uid(%d)", pid, uid);
+       pthread_mutex_lock(&g_tidl_proxy_infos_mutex);
        tts_tidl_proxy_info_s* info = __get_tidl_proxy_info_s(uid);
        if (NULL == info) {
                SLOG(LOG_ERROR, tts_tag(), "[TIDL] Uid is not valid. pid(%d), uid(%d)", pid, uid);
+               pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
                return;
        }
 
        rpc_port_tts_notify_cb_h handle = info->notify_cb_h;
        if (NULL == handle) {
                SLOG(LOG_INFO, tts_tag(), "notify callback handle null");
+               pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
                return;
        }
 
        if (0 != rpc_port_tts_notify_cb_invoke(handle, pid, uid, msg)) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to send msg");
+               pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
                return;
        }
        SLOG(LOG_INFO, tts_tag(), "send msg : pid(%d), uid(%d)", pid, uid);
+       pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
 }
 
 static void __create_client_cb(rpc_port_stub_tts_context_h context, void *user_data)
@@ -130,7 +141,9 @@ static void __register_cb(rpc_port_stub_tts_context_h context, int pid, int uid,
        }
 
        info->uid = uid;
+       pthread_mutex_lock(&g_tidl_proxy_infos_mutex);
        g_tidl_proxy_infos = g_list_append(g_tidl_proxy_infos, info);
+       pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
 
        SLOG(LOG_INFO, tts_tag(), "create player instance");
        ttsdc_tidl_send_hello(pid, uid, ret, credential_needed);
@@ -157,7 +170,9 @@ static int __register_cb_sync(rpc_port_stub_tts_context_h context, int pid, int
        }
 
        info->uid = uid;
+       pthread_mutex_lock(&g_tidl_proxy_infos_mutex);
        g_tidl_proxy_infos = g_list_append(g_tidl_proxy_infos, info);
+       pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
 
        SLOG(LOG_ERROR, tts_tag(), "<<<<<<<<<<<");
 
@@ -218,9 +233,11 @@ static int __finalize_cb(rpc_port_stub_tts_context_h context, int uid, void *use
                return -1;
        }
 
+       pthread_mutex_lock(&g_tidl_proxy_infos_mutex);
        tts_tidl_proxy_info_s* info = __get_tidl_proxy_info_s(uid);
        if (NULL == info) {
                SLOG(LOG_ERROR, tts_tag(), "[TIDL ERROR] Fail to set notify callback");
+               pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
@@ -232,6 +249,7 @@ static int __finalize_cb(rpc_port_stub_tts_context_h context, int uid, void *use
 
        SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
 
+       pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
        return TTSE_ERROR_NONE;
 }