Skip to content

Commit ca22676

Browse files
sunxilinhalajohn
andauthored
feat: remove addon_store singleton and add thread check for it (#1009)
Co-authored-by: Hu Yueh-Wei <[email protected]>
1 parent b385f70 commit ca22676

File tree

32 files changed

+483
-476
lines changed

32 files changed

+483
-476
lines changed

core/include/ten_utils/sanitizer/thread_check.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515

1616
#define TEN_SANITIZER_THREAD_CHECK_SIGNATURE 0x6204388773560E59U
1717

18+
#define TEN_SANITIZER_THREAD_CHECK_INIT_VAL \
19+
(ten_sanitizer_thread_check_t) { \
20+
TEN_SANITIZER_THREAD_CHECK_SIGNATURE, NULL, false \
21+
}
22+
1823
typedef struct ten_sanitizer_thread_check_t {
1924
ten_signature_t signature;
2025

core/include_internal/ten_runtime/addon/addon_autoload.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ typedef void (*ten_addon_register_func_t)(void *register_ctx);
1919

2020
TEN_RUNTIME_PRIVATE_API bool
2121
ten_addon_load_all_protocols_and_addon_loaders_from_app_base_dir(
22-
const char *app_base_dir, ten_error_t *err);
22+
ten_app_t *app, ten_error_t *err);
2323

2424
TEN_RUNTIME_PRIVATE_API bool
2525
ten_addon_try_load_specific_addon_using_native_addon_loader(
@@ -28,7 +28,7 @@ ten_addon_try_load_specific_addon_using_native_addon_loader(
2828

2929
TEN_RUNTIME_PRIVATE_API void
3030
ten_addon_try_load_specific_addon_using_all_addon_loaders(
31-
TEN_ADDON_TYPE addon_type, const char *addon_name);
31+
ten_env_t *ten_env, TEN_ADDON_TYPE addon_type, const char *addon_name);
3232

3333
// Traverse the <app_base_dir>/ten_packages/extension/ directory and attempt
3434
// to load all extension addons using all available addon_loaders. After this
@@ -37,4 +37,4 @@ ten_addon_try_load_specific_addon_using_all_addon_loaders(
3737
// (phase 2). This means all register functions are added to the addon_manager's
3838
// registry but not executed.
3939
TEN_RUNTIME_PRIVATE_API bool ten_addon_load_all_extensions_from_app_base_dir(
40-
const char *app_base_dir, ten_error_t *err);
40+
ten_env_t *ten_env, const char *app_base_dir, ten_error_t *err);

core/include_internal/ten_runtime/addon/addon_loader/addon_loader.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,12 @@ typedef struct ten_app_on_addon_loader_deinit_done_ctx_t {
4444
void *cb_data;
4545
} ten_app_on_addon_loader_deinit_done_ctx_t;
4646

47-
TEN_RUNTIME_PRIVATE_API ten_addon_store_t *ten_addon_loader_get_global_store(
48-
void);
49-
5047
TEN_RUNTIME_API ten_addon_host_t *ten_addon_register_addon_loader(
5148
const char *name, const char *base_dir, ten_addon_t *addon,
5249
void *register_ctx);
5350

54-
TEN_RUNTIME_API ten_addon_t *ten_addon_unregister_addon_loader(
55-
const char *name);
56-
57-
TEN_RUNTIME_PRIVATE_API void ten_addon_unregister_all_addon_loader(void);
51+
TEN_RUNTIME_PRIVATE_API void ten_addon_unregister_all_addon_loader(
52+
ten_env_t *ten_env);
5853

5954
TEN_RUNTIME_PRIVATE_API bool ten_addon_create_addon_loader(
6055
ten_env_t *ten_env, const char *addon_name, const char *instance_name,
@@ -69,6 +64,3 @@ TEN_RUNTIME_PRIVATE_API void ten_addon_loader_destroy_all_singleton_instances(
6964
ten_env_t *ten_env,
7065
ten_addon_loader_on_all_singleton_instances_destroyed_cb_t cb,
7166
void *cb_data);
72-
73-
TEN_RUNTIME_PRIVATE_API void
74-
ten_addon_loader_destroy_all_singleton_instances_immediately(void);

core/include_internal/ten_runtime/addon/common/common.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,7 @@
1111
#include "ten_runtime/addon/addon.h"
1212

1313
typedef struct ten_addon_host_t ten_addon_host_t;
14+
typedef struct ten_app_t ten_app_t;
1415

1516
TEN_RUNTIME_PRIVATE_API ten_addon_host_t *ten_addon_store_find_by_type(
16-
TEN_ADDON_TYPE addon_type, const char *addon_name);
17-
18-
TEN_RUNTIME_PRIVATE_API int ten_addon_store_lock_by_type(
19-
TEN_ADDON_TYPE addon_type);
20-
21-
TEN_RUNTIME_PRIVATE_API int ten_addon_store_unlock_by_type(
22-
TEN_ADDON_TYPE addon_type);
23-
24-
TEN_RUNTIME_PRIVATE_API int ten_addon_store_lock_all_type(void);
25-
26-
TEN_RUNTIME_PRIVATE_API int ten_addon_store_unlock_all_type(void);
17+
ten_app_t *app, TEN_ADDON_TYPE addon_type, const char *addon_name);

core/include_internal/ten_runtime/addon/common/store.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,36 @@
1010

1111
#include "ten_runtime/addon/addon.h"
1212
#include "ten_utils/container/list.h"
13-
#include "ten_utils/lib/mutex.h"
13+
#include "ten_utils/lib/signature.h"
14+
#include "ten_utils/sanitizer/thread_check.h"
15+
16+
#define TEN_ADDON_STORE_SIGNATURE 0x8A7F2C91E5D63B04U
1417

1518
typedef struct ten_addon_host_t ten_addon_host_t;
1619
typedef struct ten_addon_t ten_addon_t;
1720

1821
typedef struct ten_addon_store_t {
19-
ten_mutex_t *lock;
22+
ten_signature_t signature;
23+
ten_sanitizer_thread_check_t thread_check;
24+
2025
ten_list_t store; // ten_addon_host_t
2126
} ten_addon_store_t;
2227

23-
#define TEN_ADDON_STORE_INIT_VAL \
24-
(ten_addon_store_t) { NULL, TEN_LIST_INIT_VAL }
28+
#define TEN_ADDON_STORE_INIT_VAL \
29+
(ten_addon_store_t) { \
30+
TEN_ADDON_STORE_SIGNATURE, TEN_SANITIZER_THREAD_CHECK_INIT_VAL, \
31+
TEN_LIST_INIT_VAL \
32+
}
2533

2634
#define TEN_ADDON_STORE_INIT(var) \
2735
do { \
2836
(var) = TEN_ADDON_STORE_INIT_VAL; \
2937
ten_addon_store_init(&(var)); \
3038
} while (0)
3139

40+
TEN_RUNTIME_PRIVATE_API bool ten_addon_store_check_integrity(
41+
ten_addon_store_t *store, bool check_thread);
42+
3243
TEN_RUNTIME_PRIVATE_API void ten_addon_store_init(ten_addon_store_t *store);
3344

3445
TEN_RUNTIME_PRIVATE_API void ten_addon_store_deinit(ten_addon_store_t *store);
@@ -41,9 +52,5 @@ TEN_RUNTIME_PRIVATE_API ten_addon_t *ten_addon_store_del(
4152

4253
TEN_RUNTIME_PRIVATE_API void ten_addon_store_del_all(ten_addon_store_t *store);
4354

44-
TEN_RUNTIME_PRIVATE_API int ten_addon_store_lock(ten_addon_store_t *store);
45-
46-
TEN_RUNTIME_PRIVATE_API int ten_addon_store_unlock(ten_addon_store_t *store);
47-
4855
TEN_RUNTIME_PRIVATE_API ten_addon_host_t *ten_addon_store_find(
4956
ten_addon_store_t *store, const char *name);

core/include_internal/ten_runtime/addon/extension/extension.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ typedef struct ten_addon_create_extension_done_ctx_t {
2525
ten_extension_group_create_extensions_done_ctx_t *create_extensions_done_ctx;
2626
} ten_addon_create_extension_done_ctx_t;
2727

28-
TEN_RUNTIME_PRIVATE_API ten_addon_store_t *ten_extension_get_global_store(void);
29-
30-
TEN_RUNTIME_PRIVATE_API void ten_addon_unregister_all_extension(void);
28+
TEN_RUNTIME_PRIVATE_API void ten_addon_unregister_all_extension(
29+
ten_env_t *ten_env);
3130

3231
TEN_RUNTIME_PRIVATE_API ten_addon_create_extension_done_ctx_t *
3332
ten_addon_create_extension_done_ctx_create(
@@ -47,7 +46,5 @@ TEN_RUNTIME_PRIVATE_API bool ten_addon_destroy_extension(
4746
ten_env_addon_destroy_instance_done_cb_t cb, void *user_data,
4847
ten_error_t *err);
4948

50-
TEN_RUNTIME_API ten_addon_t *ten_addon_unregister_extension(const char *name);
51-
5249
TEN_RUNTIME_PRIVATE_API void ten_addon_on_create_extension_instance_ctx_destroy(
5350
ten_addon_on_create_extension_instance_ctx_t *self);

core/include_internal/ten_runtime/addon/extension_group/extension_group.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ typedef struct ten_addon_store_t ten_addon_store_t;
1818
typedef struct ten_addon_t ten_addon_t;
1919
typedef struct ten_addon_host_t ten_addon_host_t;
2020

21-
TEN_RUNTIME_PRIVATE_API ten_addon_store_t *ten_extension_group_get_global_store(
22-
void);
23-
2421
TEN_RUNTIME_PRIVATE_API bool ten_addon_create_extension_group(
2522
ten_env_t *ten_env, const char *addon_name, const char *instance_name,
2623
ten_env_addon_create_instance_done_cb_t cb, void *user_data);
@@ -33,7 +30,5 @@ TEN_RUNTIME_PRIVATE_API ten_addon_host_t *ten_addon_register_extension_group(
3330
const char *name, const char *base_dir, ten_addon_t *addon,
3431
void *register_ctx);
3532

36-
TEN_RUNTIME_PRIVATE_API ten_addon_t *ten_addon_unregister_extension_group(
37-
const char *name);
38-
39-
TEN_RUNTIME_PRIVATE_API void ten_addon_unregister_all_extension_group(void);
33+
TEN_RUNTIME_PRIVATE_API void ten_addon_unregister_all_extension_group(
34+
ten_env_t *ten_env);

core/include_internal/ten_runtime/addon/protocol/protocol.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,12 @@ TEN_RUNTIME_PRIVATE_API bool ten_addon_create_protocol(
3939
TEN_PROTOCOL_ROLE role, ten_env_addon_on_create_protocol_async_cb_t cb,
4040
void *user_data, ten_error_t *err);
4141

42-
TEN_RUNTIME_PRIVATE_API ten_addon_store_t *ten_protocol_get_global_store(void);
43-
4442
TEN_RUNTIME_PRIVATE_API ten_addon_host_t *ten_addon_protocol_find(
45-
const char *protocol);
43+
ten_env_t *ten_env, const char *protocol);
4644

4745
TEN_RUNTIME_API ten_addon_host_t *ten_addon_register_protocol(
4846
const char *name, const char *base_dir, ten_addon_t *addon,
4947
void *register_ctx);
5048

51-
TEN_RUNTIME_API ten_addon_t *ten_addon_unregister_protocol(const char *name);
52-
53-
TEN_RUNTIME_PRIVATE_API void ten_addon_unregister_all_protocol(void);
49+
TEN_RUNTIME_PRIVATE_API void ten_addon_unregister_all_protocol(
50+
ten_env_t *ten_env);

core/include_internal/ten_runtime/addon_loader/addon_loader.h

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,34 @@
1010

1111
#include "include_internal/ten_runtime/binding/common.h"
1212
#include "ten_runtime/addon/addon.h"
13-
#include "ten_utils/lib/mutex.h"
1413
#include "ten_utils/lib/signature.h"
14+
#include "ten_utils/sanitizer/thread_check.h"
1515

1616
#define TEN_ADDON_LOADER_SIGNATURE 0xAE4FCDE7983727E4U
17+
#define TEN_ADDON_LOADER_SINGLETON_STORE_SIGNATURE 0x8B3F2A9C7D1E6054U
1718

1819
typedef struct ten_addon_loader_t ten_addon_loader_t;
1920
typedef struct ten_addon_host_t ten_addon_host_t;
2021

2122
typedef struct ten_addon_loader_singleton_store_t {
22-
ten_atomic_t valid;
23-
ten_mutex_t *lock;
23+
ten_signature_t signature;
24+
ten_sanitizer_thread_check_t thread_check;
25+
2426
ten_list_t store; // ten_addon_loader_t
2527
} ten_addon_loader_singleton_store_t;
2628

29+
#define TEN_ADDON_LOADER_SINGLETON_STORE_INIT_VAL \
30+
(ten_addon_loader_singleton_store_t) { \
31+
TEN_ADDON_LOADER_SINGLETON_STORE_SIGNATURE, \
32+
TEN_SANITIZER_THREAD_CHECK_INIT_VAL, TEN_LIST_INIT_VAL \
33+
}
34+
35+
#define TEN_ADDON_LOADER_SINGLETON_STORE_INIT(var) \
36+
do { \
37+
(var) = TEN_ADDON_LOADER_SINGLETON_STORE_INIT_VAL; \
38+
ten_addon_loader_singleton_store_init(&(var)); \
39+
} while (0)
40+
2741
typedef void (*ten_addon_loader_on_init_func_t)(
2842
ten_addon_loader_t *addon_loader, ten_env_t *ten_env);
2943

@@ -67,11 +81,14 @@ TEN_RUNTIME_API bool ten_addon_loader_check_integrity(ten_addon_loader_t *self,
6781
TEN_RUNTIME_API ten_env_t *ten_addon_loader_get_ten_env(
6882
ten_addon_loader_t *self);
6983

70-
TEN_RUNTIME_PRIVATE_API int ten_addon_loader_singleton_store_lock(void);
84+
TEN_RUNTIME_PRIVATE_API bool ten_addon_loader_singleton_store_check_integrity(
85+
ten_addon_loader_singleton_store_t *store, bool check_thread);
7186

72-
TEN_RUNTIME_PRIVATE_API int ten_addon_loader_singleton_store_unlock(void);
87+
TEN_RUNTIME_PRIVATE_API void ten_addon_loader_singleton_store_init(
88+
ten_addon_loader_singleton_store_t *store);
7389

74-
TEN_RUNTIME_PRIVATE_API ten_list_t *ten_addon_loader_singleton_get_all(void);
90+
TEN_RUNTIME_PRIVATE_API void ten_addon_loader_singleton_store_deinit(
91+
ten_addon_loader_singleton_store_t *store);
7592

7693
TEN_RUNTIME_API ten_addon_loader_t *ten_addon_loader_create(
7794
ten_addon_loader_on_init_func_t on_init,

core/include_internal/ten_runtime/app/app.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <stdbool.h>
1212

1313
#include "include_internal/ten_runtime/addon/common/store.h"
14+
#include "include_internal/ten_runtime/addon_loader/addon_loader.h"
1415
#include "include_internal/ten_runtime/binding/common.h"
1516
#include "include_internal/ten_runtime/metadata/metadata.h"
1617
#include "include_internal/ten_runtime/schema_store/store.h"
@@ -134,6 +135,8 @@ typedef struct ten_app_t {
134135
ten_addon_store_t protocol_store;
135136
ten_addon_store_t addon_loader_store;
136137

138+
ten_addon_loader_singleton_store_t addon_loader_singleton_store;
139+
137140
// Default value is false, meaning addons are loaded on-demand - if not used,
138141
// they won't be loaded.
139142
// If set to true, all addons will be preloaded during app startup by

0 commit comments

Comments
 (0)