Skip to content

Commit

Permalink
feat: add ten:builtin_extension_group (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Sep 25, 2024
1 parent 4f3b32e commit cc3716d
Show file tree
Hide file tree
Showing 28 changed files with 260 additions and 152 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"request": "launch",
"program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_runtime_smoke_test",
"args": [
"--gtest_filter=CmdConversionTest.CmdConversionPathArray1"
"--gtest_filter=ExtensionTest.BasicHelloWorld1"
],
"cwd": "${workspaceFolder}/out/linux/x64/tests/standalone/",
"env": {
Expand Down
4 changes: 1 addition & 3 deletions core/include/ten_runtime/addon/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
ten_addon_register_##TYPE(#NAME, (ADDON)); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_##TYPE##_addon____) { \
ten_addon_unregister_##TYPE(#NAME, (ADDON)); \
ten_addon_unregister_##TYPE(#NAME); \
}

typedef struct ten_addon_t ten_addon_t;
Expand Down Expand Up @@ -51,5 +51,3 @@ TEN_RUNTIME_API void ten_addon_init(
ten_addon_on_destroy_instance_async_func_t on_destroy_instance_async);

TEN_RUNTIME_API void ten_addon_destroy(ten_addon_t *self);

TEN_RUNTIME_API ten_env_t *ten_addon_get_ten(ten_addon_t *self);
7 changes: 3 additions & 4 deletions core/include/ten_runtime/addon/extension/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ typedef struct ten_addon_host_t ten_addon_host_t;
TEN_RUNTIME_API ten_addon_host_t *ten_addon_register_extension(
const char *name, ten_addon_t *addon);

TEN_RUNTIME_API void ten_addon_unregister_extension(const char *name,
ten_addon_t *addon);
TEN_RUNTIME_API ten_addon_t *ten_addon_unregister_extension(const char *name);

TEN_RUNTIME_API ten_extension_t *ten_addon_create_extension(
ten_env_t *ten_env, const char *addon_name, const char *instance_name,
Expand All @@ -41,8 +40,8 @@ TEN_RUNTIME_API bool ten_addon_create_extension_async_for_mock(
ten_error_t *err);

TEN_RUNTIME_API bool ten_addon_destroy_extension(ten_env_t *ten_env,
ten_extension_t *extension,
ten_error_t *err);
ten_extension_t *extension,
ten_error_t *err);

TEN_RUNTIME_API bool ten_addon_destroy_extension_async(
ten_env_t *ten_env, ten_extension_t *extension,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ typedef struct ten_addon_host_t ten_addon_host_t;
TEN_RUNTIME_API ten_addon_host_t *ten_addon_register_extension_group(
const char *name, ten_addon_t *addon);

TEN_RUNTIME_API void ten_addon_unregister_extension_group(const char *name,
ten_addon_t *addon);
TEN_RUNTIME_API ten_addon_t *ten_addon_unregister_extension_group(
const char *name);
22 changes: 11 additions & 11 deletions core/include/ten_runtime/binding/cpp/internal/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ class addon_t {
addon_t()
: c_addon(ten_addon_create(proxy_on_init, proxy_on_deinit,
proxy_on_create_instance,
proxy_on_destroy_instance)),
cpp_ten_env(new ten_env_t(ten_addon_get_ten(c_addon))) {
proxy_on_destroy_instance)) {
ten_binding_handle_set_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(c_addon), this);

TEN_ASSERT(cpp_ten_env, "Should not happen.");
}

virtual ~addon_t() {
Expand Down Expand Up @@ -112,11 +109,16 @@ class addon_t {
auto *cpp_addon =
static_cast<addon_t *>(ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(addon)));
auto *cpp_ten_env =
static_cast<ten_env_t *>(ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(ten_env)));
TEN_ASSERT(!ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(ten_env)),
"Should not happen.");

auto *cpp_ten_env = new ten_env_t(ten_env);
TEN_ASSERT(cpp_addon && cpp_ten_env, "Should not happen.");

// Remember it so that we can destroy it when C++ addon is destroyed.
cpp_addon->cpp_ten_env = cpp_ten_env;

cpp_addon->invoke_cpp_addon_on_init(*cpp_ten_env);
}

Expand Down Expand Up @@ -239,8 +241,7 @@ class extension_addon_t : public addon_t {
#NAME, g_##NAME##_default_extension_group_addon->get_c_addon()); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_##TYPE##_addon____) { \
ten_addon_unregister_extension_group( \
#NAME, g_##NAME##_default_extension_group_addon->get_c_addon()); \
ten_addon_unregister_extension_group(#NAME); \
delete g_##NAME##_default_extension_group_addon; \
}

Expand All @@ -266,7 +267,6 @@ class extension_addon_t : public addon_t {
#NAME, g_##NAME##_default_extension_addon->get_c_addon()); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_##TYPE##_addon____) { \
ten_addon_unregister_extension( \
#NAME, g_##NAME##_default_extension_addon->get_c_addon()); \
ten_addon_unregister_extension(#NAME); \
delete g_##NAME##_default_extension_addon; \
}
10 changes: 4 additions & 6 deletions core/include_internal/ten_runtime/addon/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ typedef struct ten_addon_t {
ten_addon_on_destroy_instance_async_func_t on_destroy_instance_async;

void *user_data;
ten_env_t *ten_env;
} ten_addon_t;

typedef struct ten_addon_host_t {
Expand Down Expand Up @@ -112,14 +111,13 @@ TEN_RUNTIME_PRIVATE_API ten_addon_host_t *ten_addon_host_create(

TEN_RUNTIME_API void ten_addon_host_destroy(ten_addon_host_t *self);

TEN_RUNTIME_PRIVATE_API void ten_addon_register(ten_addon_store_t *store,
ten_addon_host_t *item,
TEN_RUNTIME_PRIVATE_API void ten_addon_register(ten_addon_store_t *addon_store,
ten_addon_host_t *addon_host,
const char *name,
ten_addon_t *addon);

TEN_RUNTIME_PRIVATE_API void ten_addon_unregister(ten_addon_store_t *store,
const char *addon_name,
ten_addon_t *addon);
TEN_RUNTIME_PRIVATE_API ten_addon_t *ten_addon_unregister(
ten_addon_store_t *store, const char *addon_name);

TEN_RUNTIME_PRIVATE_API const char *ten_addon_host_get_name(
ten_addon_host_t *self);
Expand Down
7 changes: 4 additions & 3 deletions core/include_internal/ten_runtime/addon/common/store.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "ten_utils/lib/mutex.h"

typedef struct ten_addon_host_t ten_addon_host_t;
typedef struct ten_addon_t ten_addon_t;

typedef struct ten_addon_store_t {
ten_atomic_t valid;
Expand All @@ -23,10 +24,10 @@ typedef struct ten_addon_store_t {
TEN_RUNTIME_PRIVATE_API void ten_addon_store_init(ten_addon_store_t *store);

TEN_RUNTIME_PRIVATE_API void ten_addon_store_add(ten_addon_store_t *store,
ten_addon_host_t *addon_host);
ten_addon_host_t *addon_host);

TEN_RUNTIME_PRIVATE_API void ten_addon_store_del(ten_addon_store_t *store,
const char *name);
TEN_RUNTIME_PRIVATE_API ten_addon_t *ten_addon_store_del(
ten_addon_store_t *store, const char *name);

TEN_RUNTIME_PRIVATE_API ten_addon_host_t *ten_addon_store_find(
ten_addon_store_t *store, const char *name);
5 changes: 2 additions & 3 deletions core/include_internal/ten_runtime/addon/protocol/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ TEN_RUNTIME_PRIVATE_API ten_addon_host_t *ten_addon_protocol_find(
const char *protocol);

TEN_RUNTIME_API void ten_addon_register_protocol(const char *name,
ten_addon_t *addon);

TEN_RUNTIME_API void ten_addon_unregister_protocol(const char *name,
ten_addon_t *addon);

TEN_RUNTIME_API ten_addon_t *ten_addon_unregister_protocol(const char *name);
1 change: 1 addition & 0 deletions core/include_internal/ten_runtime/common/constant_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,4 @@
#define TEN_STR_UNDERLINE_TEN "_ten"

#define TEN_STR_DEFAULT_EXTENSION_GROUP "default_extension_group"
#define TEN_STR_BUILTIN_EXTENSION_GROUP "ten:builtin_extension_group"
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
typedef struct ten_addon_t ten_addon_t;
typedef struct ten_env_t ten_env_t;

TEN_RUNTIME_PRIVATE_API void ten_test_extension_group_addon_on_init(
TEN_RUNTIME_PRIVATE_API void ten_builtin_extension_group_addon_on_init(
TEN_UNUSED ten_addon_t *addon, ten_env_t *ten_env);

TEN_RUNTIME_PRIVATE_API void ten_test_extension_group_addon_create_instance(
TEN_RUNTIME_PRIVATE_API void ten_builtin_extension_group_addon_create_instance(
ten_addon_t *addon, ten_env_t *ten_env, const char *name, void *context);

TEN_RUNTIME_PRIVATE_API void ten_test_extension_group_addon_destroy_instance(
TEN_RUNTIME_PRIVATE_API void ten_builtin_extension_group_addon_destroy_instance(
TEN_UNUSED ten_addon_t *addon, ten_env_t *ten_env, void *_extension_group,
void *context);

TEN_RUNTIME_PRIVATE_API void ten_builtin_extension_group_addon_register(void);

TEN_RUNTIME_PRIVATE_API void ten_builtin_extension_group_addon_unregister(void);
2 changes: 1 addition & 1 deletion core/include_internal/ten_runtime/ten_env/ten_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "ten_utils/io/runloop.h"
#include "ten_utils/macro/check.h"

#define TEN_SIGNATURE 0x1336D348DA779EA6U
#define TEN_ENV_SIGNATURE 0x1336D348DA779EA6U

typedef struct ten_engine_t ten_engine_t;

Expand Down
42 changes: 17 additions & 25 deletions core/src/ten_runtime/addon/addon.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
#include "include_internal/ten_runtime/metadata/metadata_info.h"
#include "include_internal/ten_runtime/protocol/protocol.h"
#include "include_internal/ten_runtime/ten_env/ten_env.h"
#include "ten_utils/macro/check.h"
#include "ten_runtime/app/app.h"
#include "ten_runtime/binding/common.h"
#include "ten_runtime/ten_env/ten_env.h"
#include "ten_utils/lib/alloc.h"
#include "ten_utils/lib/path.h"
#include "ten_utils/lib/signature.h"
#include "ten_utils/lib/string.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/macro/mark.h"

bool ten_addon_host_check_integrity(ten_addon_host_t *self) {
Expand Down Expand Up @@ -63,8 +63,6 @@ void ten_addon_init(
self->on_destroy_instance_async = on_destroy_instance_async;

self->user_data = NULL;

self->ten_env = ten_env_create();
}

ten_addon_t *ten_addon_create(
Expand Down Expand Up @@ -172,28 +170,28 @@ static void ten_addon_load_metadata(ten_addon_host_t *self, ten_env_t *ten_env,
* 'addon'. However, the developer could override the 'on_init' function to
* perform user-defined operations the addon needs.
*/
void ten_addon_register(ten_addon_store_t *store, ten_addon_host_t *item,
const char *name, ten_addon_t *addon) {
TEN_ASSERT(item && ten_addon_host_check_integrity(item),
void ten_addon_register(ten_addon_store_t *addon_store,
ten_addon_host_t *addon_host, const char *name,
ten_addon_t *addon) {
TEN_ASSERT(addon_host && ten_addon_host_check_integrity(addon_host),
"Should not happen.");
TEN_ASSERT(!addon_host->ten_env, "Invalid argument.");
TEN_ASSERT(name, "Should not happen.");

TEN_LOGD("Register addon base: %s", name);

if (!addon->ten_env) {
addon->ten_env = ten_env_create();
}

item->addon = addon;
item->store = store;
item->ten_env = addon->ten_env;
ten_env_set_attach_to(item->ten_env, TEN_ENV_ATTACH_TO_ADDON, item);
addon_host->addon = addon;
addon_host->store = addon_store;
addon_host->ten_env = ten_env_create();
ten_env_set_attach_to(addon_host->ten_env, TEN_ENV_ATTACH_TO_ADDON,
addon_host);

if (name) {
ten_string_set_formatted(&item->name, "%s", name);
ten_string_set_formatted(&addon_host->name, "%s", name);
}

ten_addon_load_metadata(item, item->ten_env, item->addon->on_init);
ten_addon_load_metadata(addon_host, addon_host->ten_env,
addon_host->addon->on_init);
}

ten_addon_on_create_instance_info_t *ten_addon_on_create_instance_info_create(
Expand Down Expand Up @@ -275,13 +273,13 @@ ten_addon_host_t *ten_addon_host_create(TEN_ADDON_TYPE type) {
* 'on_deinit_done'. However, developers could override this through providing a
* user-defined 'on_deinit' function.
*/
void ten_addon_unregister(ten_addon_store_t *store, const char *addon_name,
TEN_UNUSED ten_addon_t *addon) {
ten_addon_t *ten_addon_unregister(ten_addon_store_t *store,
const char *addon_name) {
TEN_ASSERT(store && addon_name, "Should not happen.");

TEN_LOGV("Unregistered addon '%s'", addon_name);

ten_addon_store_del(store, addon_name);
return ten_addon_store_del(store, addon_name);
}

ten_addon_host_t *ten_addon_host_find(const char *addon_name,
Expand Down Expand Up @@ -598,9 +596,3 @@ ten_string_t *ten_addon_host_get_base_dir(ten_addon_host_t *self) {

return base_dir;
}

ten_env_t *ten_addon_get_ten(ten_addon_t *self) {
TEN_ASSERT(self && ten_addon_check_integrity(self), "Invalid argument.");

return self->ten_env;
}
15 changes: 10 additions & 5 deletions core/src/ten_runtime/addon/common/store.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
#include <string.h>

#include "include_internal/ten_runtime/addon/addon.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/container/list.h"
#include "ten_utils/lib/atomic.h"
#include "ten_utils/lib/mutex.h"
#include "ten_utils/lib/ref.h"
#include "ten_utils/lib/string.h"
#include "ten_utils/macro/check.h"

void ten_addon_store_init(ten_addon_store_t *store) {
TEN_ASSERT(store, "Can not init empty addon store.");
Expand Down Expand Up @@ -48,23 +48,28 @@ void ten_addon_store_add(ten_addon_store_t *store, ten_addon_host_t *addon) {
ten_mutex_unlock(store->lock);
}

void ten_addon_store_del(ten_addon_store_t *store, const char *name) {
ten_addon_t *ten_addon_store_del(ten_addon_store_t *store, const char *name) {
TEN_ASSERT(store, "Invalid argument.");
TEN_ASSERT(name, "Invalid argument.");

ten_addon_t *addon = NULL;

ten_mutex_lock(store->lock);

ten_list_foreach (&store->store, iter) {
ten_addon_host_t *addon = ten_ptr_listnode_get(iter.node);
TEN_ASSERT(addon, "Should not happen.");
ten_addon_host_t *addon_host = ten_ptr_listnode_get(iter.node);
TEN_ASSERT(addon_host, "Should not happen.");

if (ten_string_is_equal_c_str(&addon->name, name)) {
if (ten_string_is_equal_c_str(&addon_host->name, name)) {
addon = addon_host->addon;
ten_list_remove_node(&store->store, iter.node);
break;
}
}

ten_mutex_unlock(store->lock);

return addon;
}

ten_addon_host_t *ten_addon_store_find(ten_addon_store_t *store,
Expand Down
5 changes: 2 additions & 3 deletions core/src/ten_runtime/addon/extension/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,10 @@ ten_addon_host_t *ten_addon_register_extension(const char *name,
return addon_host;
}

void ten_addon_unregister_extension(const char *name,
TEN_UNUSED ten_addon_t *addon) {
ten_addon_t *ten_addon_unregister_extension(const char *name) {
TEN_ASSERT(name, "Should not happen.");

TEN_LOGV("Unregistered addon of extension '%s'.", name);

ten_addon_unregister(ten_extension_get_store(), name, addon);
return ten_addon_unregister(ten_extension_get_store(), name);
}
5 changes: 2 additions & 3 deletions core/src/ten_runtime/addon/extension_group/extension_group.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ ten_addon_host_t *ten_addon_register_extension_group(const char *name,
return addon_host;
}

void ten_addon_unregister_extension_group(const char *name,
ten_addon_t *addon) {
ten_addon_t *ten_addon_unregister_extension_group(const char *name) {
TEN_ASSERT(name, "Should not happen.");

ten_addon_unregister(ten_extension_group_get_store(), name, addon);
return ten_addon_unregister(ten_extension_group_get_store(), name);
}

bool ten_addon_extension_group_create(
Expand Down
8 changes: 2 additions & 6 deletions core/src/ten_runtime/addon/protocol/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "ten_utils/lib/string.h"
#include "ten_utils/lib/uri.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/macro/mark.h"
#include "ten_utils/value/value.h"
#include "ten_utils/value/value_get.h"
#include "ten_utils/value/value_object.h"
Expand All @@ -28,13 +27,10 @@ static ten_addon_store_t g_protocol_store = {

ten_addon_store_t *ten_protocol_get_store(void) { return &g_protocol_store; }

void ten_addon_unregister_protocol(const char *name,
TEN_UNUSED ten_addon_t *addon) {
ten_addon_t *ten_addon_unregister_protocol(const char *name) {
TEN_ASSERT(name, "Should not happen.");

TEN_LOGV("Unregistered protocol '%s'", name);

ten_addon_store_del(ten_protocol_get_store(), name);
return ten_addon_unregister(ten_protocol_get_store(), name);
}

void ten_addon_register_protocol(const char *name, ten_addon_t *addon) {
Expand Down
1 change: 1 addition & 0 deletions core/src/ten_runtime/app/ten_env/on_xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "include_internal/ten_runtime/app/migration.h"
#include "include_internal/ten_runtime/app/predefined_graph.h"
#include "include_internal/ten_runtime/common/constant_str.h"
#include "include_internal/ten_runtime/extension_group/builtin/builtin_extension_group.h"
#include "include_internal/ten_runtime/metadata/metadata.h"
#include "include_internal/ten_runtime/metadata/metadata_info.h"
#include "include_internal/ten_runtime/schema_store/store.h"
Expand Down
Loading

0 comments on commit cc3716d

Please sign in to comment.