Add mutex to ensure thread safety 18/250718/2 accepted/tizen/unified/20210105.125826 submit/tizen/20210104.060658
authorJinWang An <jinwang.an@samsung.com>
Mon, 4 Jan 2021 05:27:57 +0000 (14:27 +0900)
committerJinWang An <jinwang.an@samsung.com>
Mon, 4 Jan 2021 05:55:00 +0000 (14:55 +0900)
 - sst_vconf_add_multi_cb()
 - sst_vconf_del_multi_cb()
 - sst_vconf_subscribe()
 - sst_vconf_unsubscribe()

Change-Id: Id23e8dc349e6bde2ef4b3b9b24f61926eb6799d9
Signed-off-by: JinWang An <jinwang.an@samsung.com>
src/sst_vconf.c

index 1d148ed..c3ce026 100644 (file)
@@ -29,6 +29,9 @@ static GHashTable *sst_vconf_map_old = NULL;
 static GHashTable *sst_vconf_map = NULL;
 static GHashTable *sst_vconf_map_new = NULL;
 
+static pthread_mutex_t sst_vconf_map_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t sst_vconf_map_new_lock = PTHREAD_MUTEX_INITIALIZER;
+
 struct sst_vconf_info_s {
        system_settings_key_e key;
        system_settings_changed_cb cb;
@@ -292,22 +295,27 @@ static gint _compare_cb(gconstpointer a, gconstpointer b)
 int sst_vconf_add_multi_cb(sst_interface *iface, system_settings_changed_cb cb, void *user_data)
 {
        GList *list = NULL;
+       int ret;
 
        RETV_IF(NULL == iface, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
        RETV_IF(NULL == cb, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
 
+       pthread_mutex_lock(&sst_vconf_map_lock);
        if (sst_vconf_map) {
                list = g_hash_table_lookup(sst_vconf_map, iface->vconf_key);
                GList *found = g_list_find_custom(list, cb, _compare_cb);
                if (found) {
                        ERR("callback Already Exist");
+                       pthread_mutex_unlock(&sst_vconf_map_lock);
                        return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
                }
        } else {
                sst_vconf_map = g_hash_table_new(g_str_hash, g_str_equal);
        }
 
-       return _subscribe(iface, cb, user_data, &sst_vconf_map, list, _callback_fn, NULL);
+       ret = _subscribe(iface, cb, user_data, &sst_vconf_map, list, _callback_fn, NULL);
+       pthread_mutex_unlock(&sst_vconf_map_lock);
+       return ret;
 }
 
 int sst_vconf_del_multi_cb(sst_interface *iface, system_settings_changed_cb cb)
@@ -316,14 +324,19 @@ int sst_vconf_del_multi_cb(sst_interface *iface, system_settings_changed_cb cb)
        RETV_IF(NULL == cb, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
        RETV_IF(NULL == sst_vconf_map, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
 
+       pthread_mutex_lock(&sst_vconf_map_lock);
        GList *list = g_hash_table_lookup(sst_vconf_map, iface->vconf_key);
        GList *found = g_list_find_custom(list, cb, _compare_cb);
+       int ret;
        if (NULL == found) {
                ERR("No callback");
+               pthread_mutex_unlock(&sst_vconf_map_lock);
                return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
        }
 
-       return _unsubscribe(iface->vconf_key, found, &sst_vconf_map, _callback_fn);
+       ret = _unsubscribe(iface->vconf_key, found, &sst_vconf_map, _callback_fn);
+       pthread_mutex_unlock(&sst_vconf_map_lock);
+       return ret;
 }
 
 static void _callback_fn_new(keynode_t *node, void *user_data)
@@ -342,17 +355,21 @@ static void _callback_fn_new(keynode_t *node, void *user_data)
 int sst_vconf_subscribe(sst_interface *iface, system_settings_changed_cb cb, void *user_data, system_settings_cb_id *id)
 {
        GList *list = NULL;
+       int ret;
 
        RETV_IF(NULL == iface, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
        RETV_IF(NULL == cb, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
 
+       pthread_mutex_lock(&sst_vconf_map_new_lock);
        if (sst_vconf_map_new) {
                list = g_hash_table_lookup(sst_vconf_map_new, iface->vconf_key);
        } else {
                sst_vconf_map_new = g_hash_table_new(g_str_hash, g_str_equal);
        }
 
-       return _subscribe(iface, cb, user_data, &sst_vconf_map_new, list, _callback_fn_new, id);
+       ret = _subscribe(iface, cb, user_data, &sst_vconf_map_new, list, _callback_fn_new, id);
+       pthread_mutex_unlock(&sst_vconf_map_new_lock);
+       return ret;
 }
 
 int sst_vconf_unsubscribe(sst_interface *iface, system_settings_cb_id id)
@@ -361,12 +378,17 @@ int sst_vconf_unsubscribe(sst_interface *iface, system_settings_cb_id id)
        RETV_IF(NULL == id, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
        RETV_IF(NULL == sst_vconf_map_new, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
 
+       pthread_mutex_lock(&sst_vconf_map_new_lock);
        GList *list = g_hash_table_lookup(sst_vconf_map_new, iface->vconf_key);
        GList *found = g_list_find(list, id);
+       int ret;
        if (NULL == found) {
                ERR("No callback");
+               pthread_mutex_unlock(&sst_vconf_map_new_lock);
                return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
        }
 
-       return _unsubscribe(iface->vconf_key, found, &sst_vconf_map_new, _callback_fn_new);
+       ret = _unsubscribe(iface->vconf_key, found, &sst_vconf_map_new, _callback_fn_new);
+       pthread_mutex_unlock(&sst_vconf_map_new_lock);
+       return ret;
 }