common: Fix __open/close_backend() to control usage_count exclusively 11/319811/6 accepted/tizen/unified/20250310.131219 accepted/tizen/unified/x/20250311.125538
authorYoungjae Cho <y0.cho@samsung.com>
Tue, 18 Feb 2025 06:59:56 +0000 (15:59 +0900)
committeryoungjae cho <y0.cho@samsung.com>
Fri, 7 Mar 2025 08:14:51 +0000 (08:14 +0000)
Let the usage_count be encapsulated within the __open_backend() and
__close_backend(). On calling those functions, the usage_count will
be calculated, and operations will be or won't be taken based on the
usage_count. Nothing will be necessary to manage the usage_count
outside of __open_backend() or __close_backend().

Change-Id: If4c3253be8e830f727b8b7741a3b946a1bc6e3fa
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
src/hal-api-common.c

index 8773a81018223310903566bed951be13ee24ebee..dc4a1022bb47c215a6f0fddb14c9ce1e52760c9f 100644 (file)
@@ -162,8 +162,8 @@ static int __open_backend(struct __hal_module_info *info)
        const char *backend_library_name = NULL;
        int ret;
 
-       if (info->backend && info->handle)
-               return 0;
+       if (info->usage_count > 0)
+               goto out;
 
        backend_library_name = get_backend_library_name(info);
        if (!backend_library_name) {
@@ -201,6 +201,9 @@ static int __open_backend(struct __hal_module_info *info)
                goto err_dlclose;
        }
 
+out:
+       info->usage_count++;
+
        return 0;
 
 err_dlclose:
@@ -214,12 +217,15 @@ err:
 
 static void __close_backend(struct __hal_module_info *info)
 {
-       if (!info->backend || !info->handle)
+       info->usage_count--;
+
+       if (info->usage_count > 0)
                return;
 
        if (info->handle)
                dlclose(info->handle);
 
+       info->usage_count = 0;
        info->backend = NULL;
        info->handle = NULL;
 }
@@ -373,8 +379,6 @@ static int __get_backend(enum hal_module module,
                goto err_dlclose;
        }
 
-       info->usage_count++;
-
        _I("%s: Get HAL backend: name(%s)/vendor(%s)/library(%s)/count(%d) by %s\n",
                info->module_name, info->backend->name, info->backend->vendor,
                get_backend_library_name(info), info->usage_count,
@@ -439,20 +443,18 @@ static int __put_backend(enum hal_module module,
                goto out;
        }
 
-       info->usage_count--;
-
        _I("%s: Exit and Prepare to put HAL backend: name(%s)/vendor(%s)/library(%s)/count(%d) by %s\n",
                info->module_name, info->backend->name, info->backend->vendor,
                get_backend_library_name(info), info->usage_count,
                program_invocation_name);
 
+       __close_backend(info);
+
        if (info->usage_count > 0) {
                ret = 0;
                goto out;
        }
 
-       __close_backend(info);
-
        _I("%s: Put HAL backend: library(%s)/count(%d) by %s\n",
                info->module_name, get_backend_library_name(info), info->usage_count,
                program_invocation_name);
@@ -504,7 +506,7 @@ static int __get_backend_data(enum hal_module module,
                if (!info->backend->name || (len + 1 > name_size)) {
                        _E("%s: Invalid size of name[] array\n", info->module_name);
                        ret = -EINVAL;
-                       goto err_conf_exit;
+                       goto err_close_backend;
                }
 
                strncpy(name, info->backend->name, name_size);
@@ -516,7 +518,7 @@ static int __get_backend_data(enum hal_module module,
                if (!info->backend->vendor || (len + 1 > vendor_size)) {
                        _E("%s: Invalid size of vendor[] array\n", info->module_name);
                        ret = -EINVAL;
-                       goto err_conf_exit;
+                       goto err_close_backend;
                }
 
                strncpy(vendor, info->backend->vendor, vendor_size);
@@ -526,10 +528,12 @@ static int __get_backend_data(enum hal_module module,
        } else {
                _E("%s: Failed to get backend data\n", info->module_name);
                ret = -EINVAL;
-               goto err_conf_exit;
+               goto err_close_backend;
        }
        ret = 0;
 
+err_close_backend:
+       __close_backend(info);
 err_conf_exit:
        _hal_api_conf_exit();
 err_unlock: