Skip to content

Commit

Permalink
chore: remove condition wait in log api in Python (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxilin authored Jan 13, 2025
1 parent 80bf3b7 commit 7ef33e0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 44 deletions.
61 changes: 35 additions & 26 deletions core/src/ten_runtime/binding/python/native/ten_env/ten_env_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
#include "include_internal/ten_runtime/binding/python/ten_env/ten_env.h"
#include "include_internal/ten_runtime/ten_env/log.h"
#include "ten_runtime/ten_env/internal/log.h"
#include "ten_utils/lib/error.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/macro/memory.h"

typedef struct ten_env_notify_log_ctx_t {
int32_t level;
const char *func_name;
const char *file_name;
ten_string_t func_name;
ten_string_t file_name;
size_t line_no;
const char *msg;
ten_event_t *completed;
ten_string_t msg;
} ten_env_notify_log_ctx_t;

static ten_env_notify_log_ctx_t *ten_env_notify_log_ctx_create(
Expand All @@ -29,19 +29,35 @@ static ten_env_notify_log_ctx_t *ten_env_notify_log_ctx_create(
TEN_ASSERT(ctx, "Failed to allocate memory.");

ctx->level = level;
ctx->func_name = func_name;
ctx->file_name = file_name;

if (func_name) {
ten_string_init_from_c_str(&ctx->func_name, func_name, strlen(func_name));
} else {
ten_string_init(&ctx->func_name);
}

if (file_name) {
ten_string_init_from_c_str(&ctx->file_name, file_name, strlen(file_name));
} else {
ten_string_init(&ctx->file_name);
}
ctx->line_no = line_no;
ctx->msg = msg;
ctx->completed = ten_event_create(0, 1);

if (msg) {
ten_string_init_from_c_str(&ctx->msg, msg, strlen(msg));
} else {
ten_string_init(&ctx->msg);
}

return ctx;
}

static void ten_env_notify_log_ctx_destroy(ten_env_notify_log_ctx_t *ctx) {
TEN_ASSERT(ctx, "Invalid argument.");

ten_event_destroy(ctx->completed);
ten_string_deinit(&ctx->func_name);
ten_string_deinit(&ctx->file_name);
ten_string_deinit(&ctx->msg);

TEN_FREE(ctx);
}
Expand All @@ -54,10 +70,11 @@ static void ten_env_proxy_notify_log(ten_env_t *ten_env, void *user_data) {
ten_env_notify_log_ctx_t *ctx = user_data;
TEN_ASSERT(ctx, "Should not happen.");

ten_env_log(ten_env, ctx->level, ctx->func_name, ctx->file_name, ctx->line_no,
ctx->msg);
ten_env_log(ten_env, ctx->level, ten_string_get_raw_str(&ctx->func_name),
ten_string_get_raw_str(&ctx->file_name), ctx->line_no,
ten_string_get_raw_str(&ctx->msg));

ten_event_set(ctx->completed);
ten_env_notify_log_ctx_destroy(ctx);
}

PyObject *ten_py_ten_env_log(PyObject *self, PyObject *args) {
Expand All @@ -84,33 +101,25 @@ PyObject *ten_py_ten_env_log(PyObject *self, PyObject *args) {
ten_error_t err;
ten_error_init(&err);

ten_env_notify_log_ctx_t *ctx =
ten_env_notify_log_ctx_create(level, func_name, file_name, line_no, msg);

if (py_ten_env->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_without_check_thread(py_ten_env->c_ten_env, ctx->level,
ctx->func_name, ctx->file_name,
ctx->line_no, ctx->msg);
ten_env_log_without_check_thread(py_ten_env->c_ten_env, level, func_name,
file_name, line_no, msg);
} else {
ten_env_notify_log_ctx_t *ctx = ten_env_notify_log_ctx_create(
level, func_name, file_name, line_no, msg);

if (!ten_env_proxy_notify(py_ten_env->c_ten_env_proxy,
ten_env_proxy_notify_log, ctx, false, &err)) {
goto done;
ten_env_notify_log_ctx_destroy(ctx);
}

PyThreadState *saved_py_thread_state = PyEval_SaveThread();
ten_event_wait(ctx->completed, -1);
PyEval_RestoreThread(saved_py_thread_state);
}

done:
ten_error_deinit(&err);
ten_env_notify_log_ctx_destroy(ctx);

Py_RETURN_NONE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@

typedef struct ten_env_tester_notify_log_ctx_t {
int32_t level;
const char *func_name;
const char *file_name;
ten_string_t func_name;
ten_string_t file_name;
size_t line_no;
const char *msg;
ten_event_t *completed;
ten_string_t msg;
} ten_env_tester_notify_log_ctx_t;

static ten_env_tester_notify_log_ctx_t *ten_env_tester_notify_log_ctx_create(
Expand All @@ -30,11 +29,25 @@ static ten_env_tester_notify_log_ctx_t *ten_env_tester_notify_log_ctx_create(
TEN_ASSERT(ctx, "Failed to allocate memory.");

ctx->level = level;
ctx->func_name = func_name;
ctx->file_name = file_name;

if (func_name) {
ten_string_init_from_c_str(&ctx->func_name, func_name, strlen(func_name));
} else {
ten_string_init(&ctx->func_name);
}

if (file_name) {
ten_string_init_from_c_str(&ctx->file_name, file_name, strlen(file_name));
} else {
ten_string_init(&ctx->file_name);
}

ctx->line_no = line_no;
ctx->msg = msg;
ctx->completed = ten_event_create(0, 1);
if (msg) {
ten_string_init_from_c_str(&ctx->msg, msg, strlen(msg));
} else {
ten_string_init(&ctx->msg);
}

return ctx;
}
Expand All @@ -43,7 +56,9 @@ static void ten_env_tester_notify_log_ctx_destroy(
ten_env_tester_notify_log_ctx_t *ctx) {
TEN_ASSERT(ctx, "Invalid argument.");

ten_event_destroy(ctx->completed);
ten_string_deinit(&ctx->func_name);
ten_string_deinit(&ctx->file_name);
ten_string_deinit(&ctx->msg);

TEN_FREE(ctx);
}
Expand All @@ -53,10 +68,12 @@ static void ten_py_ten_env_tester_log_proxy_notify(
ten_env_tester_notify_log_ctx_t *ctx = user_data;
TEN_ASSERT(ctx, "Should not happen.");

ten_env_tester_log(ten_env_tester, ctx->level, ctx->func_name, ctx->file_name,
ctx->line_no, ctx->msg, NULL);
ten_env_tester_log(ten_env_tester, ctx->level,
ten_string_get_raw_str(&ctx->func_name),
ten_string_get_raw_str(&ctx->file_name), ctx->line_no,
ten_string_get_raw_str(&ctx->msg), NULL);

ten_event_set(ctx->completed);
ten_env_tester_notify_log_ctx_destroy(ctx);
}

PyObject *ten_py_ten_env_tester_log(PyObject *self, TEN_UNUSED PyObject *args) {
Expand Down Expand Up @@ -90,15 +107,10 @@ PyObject *ten_py_ten_env_tester_log(PyObject *self, TEN_UNUSED PyObject *args) {
if (!ten_env_tester_proxy_notify(py_ten_env_tester->c_ten_env_tester_proxy,
ten_py_ten_env_tester_log_proxy_notify, ctx,
&err)) {
TEN_ASSERT(false, "Should not happen.");
ten_env_tester_notify_log_ctx_destroy(ctx);
}

PyThreadState *saved_py_thread_state = PyEval_SaveThread();
ten_event_wait(ctx->completed, -1);
PyEval_RestoreThread(saved_py_thread_state);

ten_error_deinit(&err);
ten_env_tester_notify_log_ctx_destroy(ctx);

Py_RETURN_NONE;
}

0 comments on commit 7ef33e0

Please sign in to comment.