Skip to content

Commit

Permalink
chore: refine c++ addon relevant codes (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Nov 29, 2024
1 parent 564e16d commit 4dff756
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 225 deletions.
33 changes: 0 additions & 33 deletions core/include/ten_runtime/binding/cpp/detail/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "ten_runtime/binding/cpp/detail/common.h"
#include "ten_runtime/binding/cpp/detail/ten_env.h"
#include "ten_runtime/ten_env/ten_env.h"
#include "ten_utils/lib/path.h" // IWYU pragma: export

namespace ten {

Expand Down Expand Up @@ -206,35 +205,3 @@ class extension_addon_t : public addon_t {
};

} // namespace ten

#define TEN_CPP_REGISTER_ADDON_AS_EXTENSION(NAME, CLASS) \
class NAME##_default_extension_addon_t : public ten::extension_addon_t { \
public: \
void on_create_instance(ten::ten_env_t &ten_env, const char *name, \
void *context) override { \
auto *instance = new CLASS(name); \
ten_env.on_create_instance_done(instance, context); \
} \
void on_destroy_instance(ten::ten_env_t &ten_env, void *instance, \
void *context) override { \
delete static_cast<CLASS *>(instance); \
ten_env.on_destroy_instance_done(context); \
} \
}; \
static ten::addon_t *g_##NAME##_default_extension_addon = nullptr; \
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_extension_addon____) { \
g_##NAME##_default_extension_addon = \
new NAME##_default_extension_addon_t(); \
ten_string_t *base_dir = \
ten_path_get_module_path(/* NOLINTNEXTLINE */ \
(void *) \
____ctor_ten_declare_##NAME##_extension_addon____); \
ten_addon_register_extension( \
#NAME, ten_string_get_raw_str(base_dir), \
g_##NAME##_default_extension_addon->get_c_addon()); \
ten_string_destroy(base_dir); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_extension_addon____) { \
ten_addon_unregister_extension(#NAME); \
delete g_##NAME##_default_extension_addon; \
}
74 changes: 74 additions & 0 deletions core/include/ten_runtime/binding/cpp/detail/addon_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// Copyright © 2024 Agora
// This file is part of TEN Framework, an open source project.
// Licensed under the Apache License, Version 2.0, with certain conditions.
// Refer to the "LICENSE" file in the root directory for more information.
//
#pragma once

#include <cassert>
#include <cstddef>

#define TEN_CPP_REGISTER_ADDON_AS_EXTENSION(NAME, CLASS) \
class NAME##_default_extension_addon_t : public ten::extension_addon_t { \
public: \
void on_create_instance(ten::ten_env_t &ten_env, const char *name, \
void *context) override { \
auto *instance = new CLASS(name); \
ten_env.on_create_instance_done(instance, context); \
} \
void on_destroy_instance(ten::ten_env_t &ten_env, void *instance, \
void *context) override { \
delete static_cast<CLASS *>(instance); \
ten_env.on_destroy_instance_done(context); \
} \
}; \
static ten::addon_t *g_##NAME##_default_extension_addon = nullptr; \
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_extension_addon____) { \
g_##NAME##_default_extension_addon = \
new NAME##_default_extension_addon_t(); \
ten_string_t *base_dir = \
ten_path_get_module_path(/* NOLINTNEXTLINE */ \
(void *) \
____ctor_ten_declare_##NAME##_extension_addon____); \
ten_addon_register_extension( \
#NAME, ten_string_get_raw_str(base_dir), \
g_##NAME##_default_extension_addon->get_c_addon()); \
ten_string_destroy(base_dir); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_extension_addon____) { \
ten_addon_unregister_extension(#NAME); \
delete g_##NAME##_default_extension_addon; \
}

#define TEN_CPP_REGISTER_ADDON_AS_EXTENSION_V2(NAME, CLASS) \
class NAME##_default_extension_addon_t : public ten::extension_addon_t { \
public: \
void on_create_instance(ten::ten_env_t &ten_env, const char *name, \
void *context) override { \
auto *instance = new CLASS(name); \
ten_env.on_create_instance_done(instance, context); \
} \
void on_destroy_instance(ten::ten_env_t &ten_env, void *instance, \
void *context) override { \
delete static_cast<CLASS *>(instance); \
ten_env.on_destroy_instance_done(context); \
} \
}; \
static ten::addon_t *g_##NAME##_default_extension_addon = nullptr; \
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_extension_addon____) { \
g_##NAME##_default_extension_addon = \
new NAME##_default_extension_addon_t(); \
ten_string_t *base_dir = \
ten_path_get_module_path(/* NOLINTNEXTLINE */ \
(void *) \
____ctor_ten_declare_##NAME##_extension_addon____); \
ten_addon_register_extension( \
#NAME, ten_string_get_raw_str(base_dir), \
g_##NAME##_default_extension_addon->get_c_addon()); \
ten_string_destroy(base_dir); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_extension_addon____) { \
ten_addon_unregister_extension(#NAME); \
delete g_##NAME##_default_extension_addon; \
}
5 changes: 3 additions & 2 deletions core/include/ten_runtime/binding/cpp/ten.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
// This header file should be the only header file where outside world should
// include in the C++ programming language.

#include "ten_runtime/addon/extension/extension.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/addon.h" // IWYU pragma: export
#include "ten_runtime/addon/extension/extension.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/addon.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/addon_manager.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/app.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/common.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/extension.h" // IWYU pragma: export
Expand Down
5 changes: 3 additions & 2 deletions core/include_internal/ten_runtime/binding/cpp/ten.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
#include "include_internal/ten_runtime/binding/cpp/detail/msg/cmd/timer.h" // IWYU pragma: export
#include "include_internal/ten_runtime/binding/cpp/detail/ten_env_impl.h" // IWYU pragma: export
#include "include_internal/ten_runtime/binding/cpp/detail/ten_env_internal_accessor.h" // IWYU pragma: export
#include "ten_runtime/addon/extension/extension.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/addon.h" // IWYU pragma: export
#include "ten_runtime/addon/extension/extension.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/addon.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/addon_manager.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/app.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/common.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/extension.h" // IWYU pragma: export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@
#include "include_internal/ten_runtime/binding/python/common/python_stuff.h"
#include "ten_utils/lib/string.h"

typedef struct ten_py_decorator_register_addon_t {
typedef struct ten_py_addon_manager_register_addon_decorator_t {
PyObject_HEAD
ten_string_t addon_name;
ten_string_t base_dir;
} ten_py_decorator_register_addon_t;
} ten_py_addon_manager_register_addon_decorator_t;

typedef struct ten_py_decorator_register_addon_v2_t {
typedef struct ten_py_addon_manager_register_addon_decorator_v2_t {
PyObject_HEAD
} ten_py_decorator_register_addon_v2_t;
} ten_py_addon_manager_register_addon_decorator_v2_t;

TEN_RUNTIME_PRIVATE_API bool
ten_py_decorator_register_addon_as_extension_init_for_module(PyObject *module);
ten_py_addon_manager_register_addon_as_extension_decorator_init_for_module(
PyObject *module);

TEN_RUNTIME_PRIVATE_API bool
ten_py_decorator_register_addon_as_extension_init_for_module_v2(
ten_py_addon_manager_register_addon_as_extension_decorator_init_for_module_v2(
PyObject *module);
68 changes: 44 additions & 24 deletions core/src/ten_runtime/binding/go/interface/ten/addon_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ import (
"unsafe"
)

// Define a registry map to store addon registration functions.
// The key is the addonName (string), and the value is a function that takes
// a registerCtx (interface{}) and returns an error.
var (
addonRegistry = make(map[string]func(interface{}) error)
addonRegistryMutex = sync.RWMutex{}
)
type AddonManager struct {
// Define a registry map to store addon registration functions.
// The key is the addonName (string), and the value is a function that takes
// a registerCtx (interface{}) and returns an error.
addonRegistry map[string]func(interface{}) error

addonRegistryMutex sync.RWMutex
}

func newAddonManager() *AddonManager {
return &AddonManager{
addonRegistry: make(map[string]func(interface{}) error),
}
}

// RegisterAddonAsExtension registers the addon as an extension.
func RegisterAddonAsExtension(addonName string, instance Addon) error {
Expand Down Expand Up @@ -77,7 +84,10 @@ func RegisterAddonAsExtension(addonName string, instance Addon) error {
}

// RegisterAddonAsExtensionV2 registers the addon as an extension.
func RegisterAddonAsExtensionV2(addonName string, instance Addon) error {
func (am *AddonManager) RegisterAddonAsExtensionV2(
addonName string,
instance Addon,
) error {
if len(addonName) == 0 {
return newTenError(
ErrnoInvalidArgument,
Expand All @@ -100,14 +110,14 @@ func RegisterAddonAsExtensionV2(addonName string, instance Addon) error {
)
}

addonWrapper := &addon{
Addon: instance,
}

addonID := newImmutableHandle(addonWrapper)

// Define the registration function that will be stored in the registry.
registerFunc := func(registerCtx interface{}) error {
addonWrapper := &addon{
Addon: instance,
}

addonID := newImmutableHandle(addonWrapper)

var bridge C.uintptr_t
status := C.ten_go_addon_register_extension_v2(
unsafe.Pointer(unsafe.StringData(addonName)),
Expand All @@ -131,40 +141,40 @@ func RegisterAddonAsExtensionV2(addonName string, instance Addon) error {
}

// Store the registration function in the registry map.
addonRegistryMutex.Lock()
defer addonRegistryMutex.Unlock()
am.addonRegistryMutex.Lock()
defer am.addonRegistryMutex.Unlock()

if _, exists := addonRegistry[addonName]; exists {
if _, exists := am.addonRegistry[addonName]; exists {
return newTenError(
ErrnoInvalidArgument,
fmt.Sprintf("addon '%s' is already registered", addonName),
)
}

addonRegistry[addonName] = registerFunc
am.addonRegistry[addonName] = registerFunc

return nil
}

// LoadAllAddons executes all registered addon registration functions.
func LoadAllAddons(registerCtx interface{}) error {
addonRegistryMutex.Lock()
defer addonRegistryMutex.Unlock()
func (am *AddonManager) LoadAllAddons(registerCtx interface{}) error {
am.addonRegistryMutex.Lock()
defer am.addonRegistryMutex.Unlock()

for name, registerFunc := range addonRegistry {
for name, registerFunc := range am.addonRegistry {
if err := registerFunc(registerCtx); err != nil {
return fmt.Errorf("failed to register addon %s: %w", name, err)
}
}

// Clear the addonRegistry to free up memory.
addonRegistry = make(map[string]func(interface{}) error)
am.addonRegistry = make(map[string]func(interface{}) error)

return nil
}

// unloadAllAddons unloads all addons.
func unloadAllAddons() error {
func (am *AddonManager) unloadAllAddons() error {
clearImmutableHandles(func(value any) {
if addon, ok := value.(*addon); ok {
C.ten_go_addon_unregister(addon.cPtr)
Expand All @@ -173,3 +183,13 @@ func unloadAllAddons() error {

return nil
}

var defaultAddonManager = newAddonManager()

func RegisterAddonAsExtensionV2(addonName string, instance Addon) error {
return defaultAddonManager.RegisterAddonAsExtensionV2(addonName, instance)
}

func LoadAllAddons(registerCtx interface{}) error {
return defaultAddonManager.LoadAllAddons(registerCtx)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .async_extension import AsyncExtension
from .async_ten_env import AsyncTenEnv
from .addon import Addon
from .decorator import (
from .addon_manager import (
register_addon_as_extension,
register_addon_as_extension_v2,
)
Expand Down
Loading

0 comments on commit 4dff756

Please sign in to comment.