diff --git a/core/include_internal/ten_runtime/addon/addon_host.h b/core/include_internal/ten_runtime/addon/addon_host.h index 52e96f8d42..a2b09de829 100644 --- a/core/include_internal/ten_runtime/addon/addon_host.h +++ b/core/include_internal/ten_runtime/addon/addon_host.h @@ -98,7 +98,8 @@ TEN_RUNTIME_PRIVATE_API ten_addon_host_t *ten_addon_host_find( TEN_RUNTIME_PRIVATE_API ten_addon_host_t * ten_addon_host_find_or_create_one_if_not_found(TEN_ADDON_TYPE addon_type, - const char *addon_name); + const char *addon_name, + bool *newly_created); TEN_RUNTIME_PRIVATE_API void ten_addon_host_load_metadata( ten_addon_host_t *self, ten_env_t *ten_env, diff --git a/core/include_internal/ten_runtime/addon/common/store.h b/core/include_internal/ten_runtime/addon/common/store.h index 268803aca3..e2ebf3f050 100644 --- a/core/include_internal/ten_runtime/addon/common/store.h +++ b/core/include_internal/ten_runtime/addon/common/store.h @@ -35,4 +35,5 @@ TEN_RUNTIME_PRIVATE_API ten_addon_host_t *ten_addon_store_find( TEN_RUNTIME_PRIVATE_API ten_addon_host_t * ten_addon_store_find_or_create_one_if_not_found(ten_addon_store_t *store, TEN_ADDON_TYPE addon_type, - const char *addon_name); + const char *addon_name, + bool *newly_created); diff --git a/core/src/ten_runtime/addon/addon.c b/core/src/ten_runtime/addon/addon.c index 07f5107585..fad76d3886 100644 --- a/core/src/ten_runtime/addon/addon.c +++ b/core/src/ten_runtime/addon/addon.c @@ -227,10 +227,15 @@ ten_addon_host_t *ten_addon_register(TEN_ADDON_TYPE addon_type, // extension addons, the action of checking for unregistered addons and adding // a new addon needs to be atomic. This ensures that the same addon is not // loaded multiple times. - ten_addon_host_t *addon_host = - ten_addon_host_find_or_create_one_if_not_found(addon_type, addon_name); + bool newly_created = false; + ten_addon_host_t *addon_host = ten_addon_host_find_or_create_one_if_not_found( + addon_type, addon_name, &newly_created); TEN_ASSERT(addon_host, "Should not happen."); + if (!newly_created) { + goto done; + } + if (register_ctx) { // If `register_ctx` exists, its content will be used to assist in the addon // registration process. @@ -261,6 +266,7 @@ ten_addon_host_t *ten_addon_register(TEN_ADDON_TYPE addon_type, ten_addon_register_internal(addon_store, addon_host, addon_name, base_dir, addon); +done: return addon_host; } diff --git a/core/src/ten_runtime/addon/addon_host.c b/core/src/ten_runtime/addon/addon_host.c index b6e71892e2..e64fd5ef76 100644 --- a/core/src/ten_runtime/addon/addon_host.c +++ b/core/src/ten_runtime/addon/addon_host.c @@ -294,25 +294,29 @@ ten_addon_host_t *ten_addon_host_find(TEN_ADDON_TYPE addon_type, } ten_addon_host_t *ten_addon_host_find_or_create_one_if_not_found( - TEN_ADDON_TYPE addon_type, const char *addon_name) { + TEN_ADDON_TYPE addon_type, const char *addon_name, bool *newly_created) { TEN_ASSERT(addon_name, "Should not happen."); switch (addon_type) { case TEN_ADDON_TYPE_EXTENSION: return ten_addon_store_find_or_create_one_if_not_found( - ten_extension_get_global_store(), addon_type, addon_name); + ten_extension_get_global_store(), addon_type, addon_name, + newly_created); case TEN_ADDON_TYPE_EXTENSION_GROUP: return ten_addon_store_find_or_create_one_if_not_found( - ten_extension_group_get_global_store(), addon_type, addon_name); + ten_extension_group_get_global_store(), addon_type, addon_name, + newly_created); case TEN_ADDON_TYPE_PROTOCOL: return ten_addon_store_find_or_create_one_if_not_found( - ten_protocol_get_global_store(), addon_type, addon_name); + ten_protocol_get_global_store(), addon_type, addon_name, + newly_created); case TEN_ADDON_TYPE_ADDON_LOADER: return ten_addon_store_find_or_create_one_if_not_found( - ten_addon_loader_get_global_store(), addon_type, addon_name); + ten_addon_loader_get_global_store(), addon_type, addon_name, + newly_created); default: TEN_ASSERT(0, "Should not happen."); diff --git a/core/src/ten_runtime/addon/common/store.c b/core/src/ten_runtime/addon/common/store.c index b656103778..a18a9ee1f7 100644 --- a/core/src/ten_runtime/addon/common/store.c +++ b/core/src/ten_runtime/addon/common/store.c @@ -117,12 +117,14 @@ ten_addon_host_t *ten_addon_store_find(ten_addon_store_t *store, } ten_addon_host_t *ten_addon_store_find_or_create_one_if_not_found( - ten_addon_store_t *store, TEN_ADDON_TYPE addon_type, - const char *addon_name) { + ten_addon_store_t *store, TEN_ADDON_TYPE addon_type, const char *addon_name, + bool *newly_created) { TEN_ASSERT(store, "Invalid argument."); TEN_ASSERT(addon_name, "Invalid argument."); + TEN_ASSERT(newly_created, "Invalid argument."); ten_addon_host_t *result = NULL; + *newly_created = false; ten_mutex_lock(store->lock); @@ -133,6 +135,8 @@ ten_addon_host_t *ten_addon_store_find_or_create_one_if_not_found( TEN_ASSERT(result, "Should not happen."); ten_addon_store_add(store, result); + + *newly_created = true; } ten_mutex_unlock(store->lock); diff --git a/core/src/ten_runtime/app/ten_env/on_xxx.c b/core/src/ten_runtime/app/ten_env/on_xxx.c index a6c74a9cf2..3542739ed5 100644 --- a/core/src/ten_runtime/app/ten_env/on_xxx.c +++ b/core/src/ten_runtime/app/ten_env/on_xxx.c @@ -29,6 +29,7 @@ #include "include_internal/ten_runtime/ten_env/log.h" #include "include_internal/ten_runtime/ten_env/metadata_cb.h" #include "include_internal/ten_runtime/ten_env/ten_env.h" +#include "include_internal/ten_runtime/test/test_extension.h" #include "ten_runtime/app/app.h" #include "ten_runtime/ten_env/internal/on_xxx_done.h" #include "ten_runtime/ten_env/ten_env.h" @@ -193,13 +194,19 @@ void ten_app_on_configure_done(ten_env_t *ten_env) { ten_addon_load_all_from_ten_package_base_dirs(&self->ten_package_base_dirs, &err); + // @{ // Register all addons. + ten_builtin_extension_group_addon_register(); + ten_builtin_test_extension_addon_register(); + ten_addon_manager_t *manager = ten_addon_manager_get_instance(); ten_addon_register_ctx_t *register_ctx = ten_addon_register_ctx_create(); register_ctx->app = self; ten_addon_manager_register_all_addons(manager, (void *)register_ctx); ten_addon_register_ctx_destroy(register_ctx); + // @} + // Create addon loader singleton instances. bool need_to_wait_all_addon_loaders_created = ten_addon_loader_addons_create_singleton_instance(ten_env); if (!need_to_wait_all_addon_loaders_created) { diff --git a/core/src/ten_runtime/global/on_load.c b/core/src/ten_runtime/global/on_load.c index c4e9e8dec3..590cf10f68 100644 --- a/core/src/ten_runtime/global/on_load.c +++ b/core/src/ten_runtime/global/on_load.c @@ -8,7 +8,6 @@ #include "include_internal/ten_runtime/extension_group/builtin/builtin_extension_group.h" #include "include_internal/ten_runtime/global/global.h" #include "include_internal/ten_runtime/global/signal.h" -#include "include_internal/ten_runtime/test/test_extension.h" #include "include_internal/ten_utils/backtrace/backtrace.h" #include "include_internal/ten_utils/log/log.h" #include "ten_utils/macro/ctor.h" @@ -35,18 +34,9 @@ TEN_CONSTRUCTOR(ten_runtime_on_load) { ten_global_setup_signal_stuff(); ten_log_global_init(); ten_log_global_set_output_level(DEFAULT_LOG_OUTPUT_LEVEL); - - // Since the built-in extension group is general-purpose and can be used by - // multiple apps within a single process, they are registered in the global - // addon store. - ten_builtin_extension_group_addon_register(); - ten_builtin_test_extension_addon_register(); } TEN_DESTRUCTOR(ten_runtime_on_unload) { - ten_builtin_test_extension_addon_unregister(); - ten_builtin_extension_group_addon_unregister(); - ten_global_deinit(); ten_log_global_deinit(); ten_backtrace_destroy_global();