halapi: Split the internal functions finely to get flexibility 15/257515/3
authorChanwoo Choi <cw00.choi@samsung.com>
Thu, 22 Apr 2021 10:12:04 +0000 (19:12 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Wed, 28 Apr 2021 06:19:19 +0000 (15:19 +0900)
New functions will use only __open_backend() without __close_backend()
in order to get the data of hal_backend structure. But this code have
been tightly coupled implemented in __get_backend and __put_backend.
In order to make the code more flexibility, split __get_backend/__put_backend
finley to two steps such as open/close and init/exit.

[Simple description for functions]
- __open_backend() : Open backend library and get backend symbol
- __close_backend() : Close backend library
- __init_backend() : After getting backend, initialize HAL backend driver
- __exit_backend() : After getting backend, exit HAL backend driver
- __get_backend()
__open_backend()
__init_backend()
Increase usage_count
- __put_backend()
__exit_backen()
__close_backend()
Decrease usage_count

[Change variable name in struct __hal_module_info]
- library_backend -> backend
- library_handle -> handle

Change-Id: I358e3f4db1991d26c8cf0498b3f37ec543da3e38
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
src/common.h
src/hal-api-common.c

index c641610..69d7c13 100644 (file)
@@ -93,8 +93,8 @@ struct __hal_module_info {
 
        char *library_name;
        char *library_name_64bit;
-       void *library_handle;
-       hal_backend *library_backend;
+       void *handle;
+       hal_backend *backend;
        char *symbol_name;
 
        unsigned int num_abi_versions;
index 530d18c..18eab76 100644 (file)
@@ -132,23 +132,157 @@ out:
        return ret;
 }
 
-static int __get_backend(enum hal_module module, void **data, const char *library_name)
+static int __open_backend(struct __hal_module_info *info)
 {
-       struct __hal_module_info *info = NULL;
-       void *handle = NULL;
-       hal_backend *backend;
-       const char *symbol_name = NULL;
        const char *backend_library_name = NULL;
-       int ret = 0;
+       int ret;
+
+       if (info->backend && info->handle)
+               return 0;
+
+       backend_library_name = get_backend_library_name(info);
+       if (!backend_library_name) {
+               _E("%s: Failed to get backend library name\n",
+                               info->module_name);
+               ret = TIZEN_ERROR_INVALID_PARAMETER;
+               goto err;
+       }
+
+       if (!info->symbol_name) {
+               _E("%s: Failed to get backend symbol name\n",
+                               info->module_name);
+               ret = TIZEN_ERROR_INVALID_PARAMETER;
+               goto err;
+       }
+
+       ret = access(backend_library_name, F_OK);
+       if (ret < 0) {
+               _E("%s: Failed to find backend library (%s)\n",
+                               info->module_name, backend_library_name);
+               ret = TIZEN_ERROR_INVALID_PARAMETER;
+               goto err;
+       }
+
+       info->handle = dlopen(backend_library_name, RTLD_LAZY);
+       if (!info->handle) {
+               _E("%s: Failed to load backend library (%s)\n",
+                               info->module_name, dlerror());
+               ret = TIZEN_ERROR_INVALID_PARAMETER;
+               goto err;
+       }
+
+       info->backend = dlsym(info->handle, info->symbol_name);
+       if (!info->backend) {
+               _E("%s: Failed to find backend data (%s)\n",
+                               info->module_name, dlerror());
+               ret = TIZEN_ERROR_INVALID_PARAMETER;
+               goto err_dlclose;
+       }
+
+       _I("%s: Open HAL backend: name(%s)/vendor(%s)/library(%s)/count(%d) by %s\n",
+               info->module_name, info->backend->name, info->backend->vendor,
+               backend_library_name, info->usage_count,
+               program_invocation_name);
+
+       return 0;
+
+err_dlclose:
+       dlclose(info->handle);
+err:
+       info->backend = NULL;
+       info->handle = NULL;
+
+       return ret;
+}
+
+static void __close_backend(struct __hal_module_info *info)
+{
+       if (!info->backend || !info->handle)
+               return;
+
+       _I("%s: Close 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);
+
+       if (info->handle)
+               dlclose(info->handle);
+
+       info->backend = NULL;
+       info->handle = NULL;
+}
+
+static int __init_backend(struct __hal_module_info *info, void **data,
+                               const char *library_name)
+{
+       int ret;
+
+       if (!info->handle || !info->backend) {
+               _I("%s: Has not yet dlopend backend\n", info->module_name);
+               return TIZEN_ERROR_NONE;
+       }
+
+       if (!info->backend->init) {
+               _E("%s: hal_backend->init() is NULL\n", info->module_name);
+               return TIZEN_ERROR_INVALID_PARAMETER;
+       }
+
+       /* Check HAL ABI Version */
+       ret = hal_common_check_backend_abi_version(info->module,
+                                               info->backend->abi_version);
+       if (ret < 0) {
+               _E("%s: Failed to check ABI version\n", info->module_name);
+               return TIZEN_ERROR_INVALID_PARAMETER;
+       }
+
+       /* Initialize backend */
+       ret = info->backend->init(data);
+       if (ret < 0) {
+               _E("%s: Failed to initialize backend: name(%s)/vendor(%s)\n",
+                       info->module_name, info->backend->name,
+                       info->backend->vendor);
+               return TIZEN_ERROR_INVALID_PARAMETER;
+       }
+
+       return TIZEN_ERROR_NONE;
+}
+
+static int __exit_backend(struct __hal_module_info *info, void *data,
+                               const char *library_name)
+{
+       int ret;
+
+       if (!info->handle || !info->backend) {
+               _I("%s: Has not yet dlopend backend\n", info->module_name);
+               return TIZEN_ERROR_NONE;
+       }
+
+       /* Exit backend */
+       if (info->backend->exit) {
+               ret = info->backend->exit(data);
+               if (ret < 0) {
+                       _E("%s: Failed to exit backend: name(%s)/vendor(%s)\n",
+                               info->module_name, info->backend->name,
+                               info->backend->vendor);
+                       return TIZEN_ERROR_INVALID_PARAMETER;
+               }
+       }
+
+       return 0;
+}
+
+static int __get_backend(enum hal_module module, void **data,
+                               const char *library_name)
+{
+       struct __hal_module_info *info = NULL;
+       int ret;
 
-       /* Check parameter whether is valid or not */
        if (module <= HAL_MODULE_UNKNOWN || module >= HAL_MODULE_END) {
                _E("Invalid parameter of HAL module (%d)\n", module);
                return TIZEN_ERROR_INVALID_PARAMETER;
        }
 
        G_LOCK(hal_common_lock);
-
        if (_hal_api_conf_init()) {
                ret = TIZEN_ERROR_UNKNOWN;
                goto err;
@@ -165,96 +299,27 @@ static int __get_backend(enum hal_module module, void **data, const char *librar
                goto err;
        }
 
-       backend_library_name = get_backend_library_name(info);
-       if (!backend_library_name) {
-               _E("%s: Failed to get backend library name\n",
+       ret = __open_backend(info);
+       if (ret < 0) {
+               _E("%s: Failed to get the backend library by dlopen\n",
                                info->module_name);
                ret = TIZEN_ERROR_INVALID_PARAMETER;
                goto err;
        }
 
-       if (info->usage_count == 0) {
-               /*
-                * Load HAL backend library at first loading time
-                * when usage_count is 0.
-                */
-               ret = access(backend_library_name, F_OK);
-               if (ret < 0) {
-                       _E("%s: Failed to find backend library (%s)\n",
-                                       info->module_name, backend_library_name);
-                       ret = TIZEN_ERROR_INVALID_PARAMETER;
-                       goto err;
-               }
-
-               handle = dlopen(backend_library_name, RTLD_LAZY);
-               if (!handle) {
-                       _E("%s: Failed to load backend library (%s)\n",
-                                       info->module_name, dlerror());
-                       ret = TIZEN_ERROR_INVALID_PARAMETER;
-                       goto err;
-               }
-
-               symbol_name = info->symbol_name;
-               if (!symbol_name) {
-                       _E("%s: Failed to get backend symbol name\n",
-                                       info->module_name);
-                       ret = TIZEN_ERROR_INVALID_PARAMETER;
-                       goto err_dlclose;
-               }
-
-               backend = dlsym(handle, symbol_name);
-               if (!backend) {
-                       _E("%s: Failed to find backend data (%s)\n",
-                                       info->module_name, dlerror());
-                       ret = TIZEN_ERROR_INVALID_PARAMETER;
-                       goto err_dlclose;
-               }
-
-               _I("%s: Open HAL backend: name(%s)/vendor(%s)/library(%s)/count(%d) by %s\n",
-                       info->module_name, backend->name, backend->vendor,
-                       get_backend_library_name(info), info->usage_count,
-                       program_invocation_name);
-
-               info->library_backend = backend;
-               info->library_handle = handle;
-       } else {
-               /*
-                * Re-use the already loaded HAL backend instance
-                * when usage_count is larger than 0.
-                */
-               backend = info->library_backend;
-       }
-
-       /* Check HAL ABI Version */
-       ret = hal_common_check_backend_abi_version(module, backend->abi_version);
+       ret = __init_backend(info, data, NULL);
        if (ret < 0) {
-               _E("%s: Failed to check ABI version\n",
+               _E("%s: Failed to initialize the backend library\n",
                                info->module_name);
                ret = TIZEN_ERROR_INVALID_PARAMETER;
                goto err_dlclose;
        }
 
-       /* Get the backend module data */
-       if (!backend->init) {
-               _E("%s: hal_backend->init() is NULL\n",
-                               info->module_name);
-               ret = TIZEN_ERROR_INVALID_PARAMETER;
-               goto err_dlclose;
-       }
-
-       ret = backend->init(data);
-       if (ret < 0) {
-               _E("%s: Failed to initialize backend: name(%s)/vendor(%s)\n",
-                               info->module_name, backend->name, backend->vendor);
-               ret = TIZEN_ERROR_INVALID_PARAMETER;
-               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, backend->name, backend->vendor,
-               backend_library_name, info->usage_count,
+               info->module_name, info->backend->name, info->backend->vendor,
+               get_backend_library_name(info), info->usage_count,
                program_invocation_name);
 
        G_UNLOCK(hal_common_lock);
@@ -262,17 +327,16 @@ static int __get_backend(enum hal_module module, void **data, const char *librar
 
 err_dlclose:
        _hal_api_conf_exit();
-       dlclose(handle);
+       __close_backend(info);
 err:
        G_UNLOCK(hal_common_lock);
        return ret;
 }
 
-static int __put_backend(enum hal_module module, void *data, const char *library_name)
+static int __put_backend(enum hal_module module, void *data,
+                               const char *library_name)
 {
        struct __hal_module_info *info = NULL;
-       hal_backend *backend = NULL;
-       void *handle = NULL;
        int ret;
 
        /* Check parameter whether is valid or not */
@@ -290,29 +354,30 @@ static int __put_backend(enum hal_module module, void *data, const char *library
                goto out;
        }
 
-       backend = info->library_backend;
-       handle = info->library_handle;
+       if (!info->handle || !info->backend) {
+               _I("%s: Has not yet dlopend backend\n", info->module_name);
+               ret = TIZEN_ERROR_NONE;
+               goto out;
+       }
 
-       if (!backend || info->usage_count == 0) {
+       if (!info->usage_count) {
                _I("%s: Already fully put for HAL module\n", info->module_name);
-               ret = TIZEN_ERROR_NONE;
+               ret =  TIZEN_ERROR_NONE;
                goto out;
        }
 
-       if (backend->exit) {
-               ret = backend->exit(data);
-               if (ret < 0) {
-                       _E("%s: Failed to exit backend: name(%s)/vendor(%s)\n",
-                               info->module_name, backend->name, backend->vendor);
-                       ret = TIZEN_ERROR_INVALID_PARAMETER;
-                       goto out;
-               }
+       ret = __exit_backend(info, data, NULL);
+       if (ret < 0) {
+               _E("%s: Failed to exit the backend library\n",
+                               info->module_name);
+               ret = TIZEN_ERROR_INVALID_PARAMETER;
+               goto out;
        }
 
        info->usage_count--;
 
        _I("%s: Put HAL backend: name(%s)/vendor(%s)/library(%s)/count(%d) by %s\n",
-               info->module_name, backend->name, backend->vendor,
+               info->module_name, info->backend->name, info->backend->vendor,
                get_backend_library_name(info), info->usage_count,
                program_invocation_name);
 
@@ -321,17 +386,7 @@ static int __put_backend(enum hal_module module, void *data, const char *library
                goto out;
        }
 
-       _I("%s: Close HAL backend: name(%s)/vendor(%s)/library(%s)/count(%d) by %s\n",
-               info->module_name, backend->name, backend->vendor,
-               get_backend_library_name(info), info->usage_count,
-               program_invocation_name);
-
-       if (handle)
-               dlclose(handle);
-
-       info->library_backend = NULL;
-       info->library_handle = NULL;
-
+       __close_backend(info);
        _hal_api_conf_exit();
 
        ret = TIZEN_ERROR_NONE;