Skip to content

Commit

Permalink
refactor: refine addon mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn committed Jan 2, 2025
1 parent 398ab54 commit db21721
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 21 deletions.
3 changes: 2 additions & 1 deletion core/include_internal/ten_runtime/addon/addon_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion core/include_internal/ten_runtime/addon/common/store.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
10 changes: 8 additions & 2 deletions core/src/ten_runtime/addon/addon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down
14 changes: 9 additions & 5 deletions core/src/ten_runtime/addon/addon_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
8 changes: 6 additions & 2 deletions core/src/ten_runtime/addon/common/store.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions core/src/ten_runtime/app/ten_env/on_xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 0 additions & 10 deletions core/src/ten_runtime/global/on_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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();
Expand Down

0 comments on commit db21721

Please sign in to comment.