Skip to content

Commit

Permalink
fix: refine codes
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn committed Feb 7, 2025
1 parent b4c9019 commit ef018cc
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
#define TEN_GO_TEN_ENV_IS_ALIVE_REGION_BEGIN(ten_env_bridge, err_stmt) \
do { \
ten_rwlock_lock((ten_env_bridge)->lock, 1); \
if (((ten_env_bridge)->c_ten_env == NULL) || \
(((ten_env_bridge)->c_ten_env->attach_to != \
TEN_ENV_ATTACH_TO_ADDON) && \
((ten_env_bridge)->c_ten_env_proxy == NULL))) { \
if (((ten_env_bridge)->c_ten_env == NULL) && \
((ten_env_bridge)->c_ten_env_proxy == NULL)) { \
ten_rwlock_unlock((ten_env_bridge)->lock, 1); \
{ \
err_stmt \
Expand Down
8 changes: 0 additions & 8 deletions core/src/ten_runtime/binding/go/native/ten_env/ten_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ static void ten_go_ten_env_destroy_c_part(void *ten_env_bridge_) {
TEN_ASSERT(ten_env_bridge && ten_go_ten_env_check_integrity(ten_env_bridge),
"Should not happen.");

// The C `ten_env` has already been destroyed, so the pointer needs to be
// updated to prevent the Go world from using the C `ten_env` again.
// Additionally, the writer lock must be held to avoid race conditions between
// the Go routine thread and the TEN internal thread.
ten_rwlock_lock(ten_env_bridge->lock, 0);
ten_env_bridge->c_ten_env = NULL;
ten_rwlock_unlock(ten_env_bridge->lock, 0);

ten_go_bridge_destroy_c_part(&ten_env_bridge->bridge);

// Remove the Go ten_env object from the global map.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ ten_go_error_t ten_go_ten_env_init_property_from_json_bytes(
done:
ten_go_error_from_error(&cgo_error, &ctx->err);
ten_env_notify_init_property_ctx_destroy(ctx);

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);

ten_is_close:
Expand Down
31 changes: 17 additions & 14 deletions core/src/ten_runtime/binding/go/native/ten_env/ten_env_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,31 +115,34 @@ ten_go_error_t ten_go_ten_env_log(uintptr_t bridge_addr, int level,
ten_error_t err;
ten_error_init(&err);

if (self->c_ten_env->attach_to == TEN_ENV_ATTACH_TO_ADDON) {
// TODO(Wei): This function is currently specifically designed for the addon
// because the addon currently does not have a main thread, so it's unable
// to check thread safety. Once the main thread for the addon is determined
// in the future, these hacks made specifically for the addon can be
// completely removed, and comprehensive thread safety checking can be
// implemented.
ten_env_log_with_size_formatted_without_check_thread(
self->c_ten_env, ctx->level, ctx->func_name, ctx->func_name_len,
ctx->file_name, ctx->file_name_len, ctx->line_no, "%.*s", ctx->msg_len,
ctx->msg);
} else {
if (self->c_ten_env_proxy) {
if (!ten_env_proxy_notify(self->c_ten_env_proxy, ten_env_proxy_notify_log,
ctx, false, &err)) {
ten_go_error_from_error(&cgo_error, &err);
} else {
ten_event_wait(ctx->completed, -1);
}
}
} else {
// TODO(Wei): This function is currently specifically designed for the addon
// because the addon currently does not have a main thread, so it's unable
// to use the ten_env_proxy mechanism to maintain thread safety. Once the
// main thread for the addon is determined in the future, these hacks made
// specifically for the addon can be completely removed, and comprehensive
// thread safety mechanism can be implemented.
TEN_ASSERT(self->c_ten_env->attach_to == TEN_ENV_ATTACH_TO_ADDON,
"Should not happen.");

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);
ten_env_log_with_size_formatted_without_check_thread(
self->c_ten_env, ctx->level, ctx->func_name, ctx->func_name_len,
ctx->file_name, ctx->file_name_len, ctx->line_no, "%.*s", ctx->msg_len,
ctx->msg);
}

ten_error_deinit(&err);
ten_env_notify_log_ctx_destroy(ctx);

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);

ten_is_close:
return cgo_error;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "ten_runtime/binding/go/interface/ten/ten_env.h"
#include "ten_runtime/ten_env_proxy/ten_env_proxy.h"
#include "ten_utils/lib/error.h"
#include "ten_utils/lib/rwlock.h"
#include "ten_utils/macro/check.h"

static void ten_go_ten_env_detach_proxy(ten_go_ten_env_t *ten_env_bridge,
Expand All @@ -27,6 +26,7 @@ static void ten_go_ten_env_detach_proxy(ten_go_ten_env_t *ten_env_bridge,
TEN_ASSERT(ten_env_proxy_get_thread_cnt(c_ten_env_proxy, err) == 1,
"Should not happen.");

ten_env_bridge->c_ten_env = NULL;
ten_env_bridge->c_ten_env_proxy = NULL;

bool rc = ten_env_proxy_release(c_ten_env_proxy, err);
Expand Down Expand Up @@ -77,22 +77,30 @@ void ten_go_ten_env_on_deinit_done(uintptr_t bridge_addr) {
ten_error_init(&err);

bool rc = true;
if (self->c_ten_env->attach_to == TEN_ENV_ATTACH_TO_ADDON) {
rc = ten_env_on_deinit_done(self->c_ten_env, &err);
} else {

if (self->c_ten_env_proxy) {
rc = ten_env_proxy_notify(self->c_ten_env_proxy,
ten_env_proxy_notify_on_deinit_done, self, false,
&err);
}
} else {
// TODO(Wei): This function is currently specifically designed for the addon
// because the addon currently does not have a main thread, so it's unable
// to use the ten_env_proxy mechanism to maintain thread safety. Once the
// main thread for the addon is determined in the future, these hacks made
// specifically for the addon can be completely removed, and comprehensive
// thread safety mechanism can be implemented.
TEN_ASSERT(self->c_ten_env->attach_to == TEN_ENV_ATTACH_TO_ADDON,
"Should not happen.");

rc = ten_env_on_deinit_done(self->c_ten_env, &err);
}
TEN_ASSERT(rc, "Should not happen.");

ten_go_ten_env_detach_proxy(self, &err);
ten_error_deinit(&err);

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);

ten_error_deinit(&err);

ten_is_close:
return;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,21 @@ void ten_go_ten_env_on_init_done(uintptr_t bridge_addr) {

bool rc = true;

if (self->c_ten_env->attach_to == TEN_ENV_ATTACH_TO_ADDON) {
rc = ten_env_on_init_done(self->c_ten_env, &err);
} else {
if (self->c_ten_env_proxy) {
rc = ten_env_proxy_notify(self->c_ten_env_proxy,
ten_env_proxy_notify_on_init_done, NULL, false,
&err);
} else {
// TODO(Wei): This function is currently specifically designed for the addon
// because the addon currently does not have a main thread, so it's unable
// to use the ten_env_proxy mechanism to maintain thread safety. Once the
// main thread for the addon is determined in the future, these hacks made
// specifically for the addon can be completely removed, and comprehensive
// thread safety mechanism can be implemented.
TEN_ASSERT(self->c_ten_env->attach_to == TEN_ENV_ATTACH_TO_ADDON,
"Should not happen.");

rc = ten_env_on_init_done(self->c_ten_env, &err);
}
TEN_ASSERT(rc, "Should not happen.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ void ten_go_ten_env_on_start_done(uintptr_t bridge_addr) {
TEN_ASSERT(rc, "Should not happen.");

ten_error_deinit(&err);

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);

ten_is_close:
return;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ void ten_go_ten_env_on_stop_done(uintptr_t bridge_addr) {
TEN_ASSERT(rc, "Should not happen.");

ten_error_deinit(&err);

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);

ten_is_close:
return;
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ ten_go_error_t ten_go_ten_env_return_result(uintptr_t bridge_addr,
}

ten_error_deinit(&err);

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);

ten_is_close:
Expand Down Expand Up @@ -221,6 +222,7 @@ ten_go_error_t ten_go_ten_env_return_result_directly(
}

ten_error_deinit(&err);

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);

ten_is_close:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ ten_go_error_t ten_go_ten_env_send_audio_frame(
}

ten_error_deinit(&err);

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);

ten_is_close:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ ten_go_error_t ten_go_ten_env_send_cmd(uintptr_t bridge_addr,
ten_go_error_from_error(&cgo_error, &err);
}

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);
ten_error_deinit(&err);

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);

ten_is_close:
return cgo_error;
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ ten_go_error_t ten_go_ten_env_send_data(uintptr_t bridge_addr,
ten_go_error_from_error(&cgo_error, &err);
}

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);
ten_error_deinit(&err);

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);

ten_is_close:
return cgo_error;
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ ten_go_error_t ten_go_ten_env_send_video_frame(
ten_go_error_from_error(&cgo_error, &err);
}

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);
ten_error_deinit(&err);

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);

ten_is_close:
return cgo_error;
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ static ten_go_error_t ten_go_ten_env_set_property(ten_go_ten_env_t *self,
}

ten_error_deinit(&err);

TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self);

ten_is_close:
Expand Down

0 comments on commit ef018cc

Please sign in to comment.