Update to lock/unlock entirely 55/207655/1 accepted/tizen/unified/20190613.061530 submit/tizen/20190612.011342
authorYoungHun Kim <yh8004.kim@samsung.com>
Tue, 11 Jun 2019 00:24:53 +0000 (09:24 +0900)
committerYoungHun Kim <yh8004.kim@samsung.com>
Tue, 11 Jun 2019 00:24:55 +0000 (09:24 +0900)
Change-Id: I74e12c65238ef676b2cd62ff5a9b05b86cb22b1c

core/src/muse_core.c

index e11d090..f5dab2b 100644 (file)
@@ -80,8 +80,6 @@ static json_object *_muse_msg_json_tokener_parse_len(const char *str, int *len,
 
        muse_return_val_if_fail(str, NULL);
 
-       g_mutex_lock(&msg_lock);
-
        tok = json_tokener_new();
 
        if (!tok) {
@@ -111,8 +109,6 @@ out:
        if (tok)
                json_tokener_free(tok);
 
-       g_mutex_unlock(&msg_lock);
-
        return obj;
 }
 
@@ -175,18 +171,13 @@ static json_object *_muse_msg_json_find_obj(json_object *jobj, const char *find_
 
        muse_return_val_if_fail(find_key, NULL);
 
-       g_mutex_lock(&msg_lock);
-
        key_len = strlen(find_key);
 
        json_object_object_foreach(jobj, key, val) {
-               if (strlen(key) == key_len && !memcmp(key, find_key, key_len)) {
-                       g_mutex_unlock(&msg_lock);
+               if (strlen(key) == key_len && !memcmp(key, find_key, key_len))
                        return val;
-               }
        }
 
-       g_mutex_unlock(&msg_lock);
        return NULL;
 }
 
@@ -508,27 +499,27 @@ bool muse_core_msg_deserialize(const char *key, char *buf, int *parse_len, muse_
 {
        int j_type, idx, len;
        int *int_data;
-       json_object *val, *jobj;
+       json_object *val = NULL, *jobj = NULL;
 
        muse_return_val_if_fail(key, false);
        muse_return_val_if_fail(buf, false);
        muse_return_val_if_fail(m_type >= MUSE_TYPE_INT && m_type < MUSE_TYPE_MAX, false);
        muse_return_val_if_fail(data, false);
 
+       g_mutex_lock(&msg_lock);
+
        jobj = _muse_msg_json_tokener_parse_len(buf, parse_len, err);
-       muse_return_val_if_fail(jobj, false);
+       if (!jobj) {
+               LOGE("jobj is NULL");
+               goto out;
+       }
 
        val = _muse_msg_json_find_obj(jobj, key);
        if (!val) {
                LOGE("\"%s\" key is not founded", key);
-               g_mutex_lock(&msg_lock);
-               json_object_put(jobj);
-               g_mutex_unlock(&msg_lock);
-               return false;
+               goto out;
        }
 
-       g_mutex_lock(&msg_lock);
-
        j_type = json_object_get_type(val);
        switch (j_type) {
        case json_type_null:
@@ -547,10 +538,8 @@ bool muse_core_msg_deserialize(const char *key, char *buf, int *parse_len, muse_
                        *(int64_t *)data = json_object_get_int64(val);
                } else if (m_type == MUSE_TYPE_POINTER) {
                        if (!_muse_msg_is_mem_ptr_valid(val)) {
-                               json_object_put(jobj);
-                               g_mutex_unlock(&msg_lock);
                                LOGE("memory pointer is not valid");
-                               return false;
+                               goto out;
                        }
                        if (sizeof(intptr_t) == 8)
                                *(intptr_t *)data = json_object_get_int64(val);
@@ -581,6 +570,14 @@ bool muse_core_msg_deserialize(const char *key, char *buf, int *parse_len, muse_
        g_mutex_unlock(&msg_lock);
 
        return true;
+
+out:
+       if (jobj)
+               json_object_put(jobj);
+
+       g_mutex_unlock(&msg_lock);
+
+       return false;
 }
 
 void muse_core_msg_free(char *msg)
@@ -590,8 +587,17 @@ void muse_core_msg_free(char *msg)
 
 void *muse_core_msg_object_new(char *str, int *parse_len, muse_core_msg_parse_err_e *err)
 {
+       void *jobj = NULL;
+
        muse_return_val_if_fail(str, NULL);
-       return (void *)_muse_msg_json_tokener_parse_len(str, parse_len, err);
+
+       g_mutex_lock(&msg_lock);
+
+       jobj = (void *)_muse_msg_json_tokener_parse_len(str, parse_len, err);
+
+       g_mutex_unlock(&msg_lock);
+
+       return jobj;
 }
 
 bool muse_core_msg_object_get_value(const char *key, void *jobj, muse_core_msg_type_e m_type, void *data)
@@ -605,14 +611,14 @@ bool muse_core_msg_object_get_value(const char *key, void *jobj, muse_core_msg_t
        muse_return_val_if_fail(data, false);
        muse_return_val_if_fail(m_type >= MUSE_TYPE_INT && m_type < MUSE_TYPE_MAX, false);
 
+       g_mutex_lock(&msg_lock);
+
        val = _muse_msg_json_find_obj((json_object *)jobj, key);
        if (!val) {
                LOGE("\"%s\" key is not found", key);
-               return false;
+               goto out;
        }
 
-       g_mutex_lock(&msg_lock);
-
        j_type = json_object_get_type(val);
        switch (j_type) {
        case json_type_null:
@@ -631,9 +637,8 @@ bool muse_core_msg_object_get_value(const char *key, void *jobj, muse_core_msg_t
                        *(int64_t *)data = json_object_get_int64(val);
                } else if (m_type == MUSE_TYPE_POINTER) {
                        if (!_muse_msg_is_mem_ptr_valid(val)) {
-                               g_mutex_unlock(&msg_lock);
                                LOGE("memory pointer is not valid");
-                               return false;
+                               goto out;
                        }
                        if (sizeof(intptr_t) == 8)
                                *(intptr_t *)data = json_object_get_int64(val);
@@ -662,6 +667,11 @@ bool muse_core_msg_object_get_value(const char *key, void *jobj, muse_core_msg_t
        g_mutex_unlock(&msg_lock);
 
        return true;
+
+out:
+       g_mutex_unlock(&msg_lock);
+
+       return false;
 }
 
 void muse_core_msg_object_free(void *jobj)