From b39a6d07e46bb96692879cefcbd89e67ac0728ee Mon Sep 17 00:00:00 2001 From: liaoxingxing Date: Sun, 18 May 2025 10:06:25 +0800 Subject: [PATCH] cherry pick 5.1.0 Signed-off-by: liaoxingxing --- include/uv.h | 2 +- src/threadpool.c | 18 ++++++++---------- src/timer.c | 2 +- src/unix/core.c | 24 +++++++++--------------- src/unix/loop.c | 4 ++++ src/uv-common.h | 3 +++ 6 files changed, 26 insertions(+), 27 deletions(-) diff --git a/include/uv.h b/include/uv.h index 5546298..1ca1466 100644 --- a/include/uv.h +++ b/include/uv.h @@ -1955,7 +1955,7 @@ UV_EXTERN void* uv_loop_get_data(const uv_loop_t*); UV_EXTERN void uv_loop_set_data(uv_loop_t*, void* data); UV_EXTERN int uv_register_task_to_event(struct uv_loop_s* loop, uv_post_task func, void* handler); UV_EXTERN int uv_unregister_task_to_event(struct uv_loop_s* loop); -UV_EXTERN int uv_check_data_valid(struct uv_loop_data* data); +UV_EXTERN int uv_check_data_valid(uv_loop_t* loop); /* String utilities needed internally for dealing with Windows. */ size_t uv_utf16_length_as_wtf8(const uint16_t* utf16, diff --git a/src/threadpool.c b/src/threadpool.c index 4d1675e..4b021f5 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -649,11 +649,10 @@ static int uv__work_cancel(uv_loop_t* loop, uv_req_t* req, struct uv__work* w) { uv__loop_internal_fields_t* lfields = uv__get_internal_fields(w->loop); int qos = (ffrt_qos_t)(intptr_t)req->reserved[0]; - if (uv_check_data_valid((struct uv_loop_data*)(w->loop->data)) == 0) { + if (uv_check_data_valid(loop) == 0) { int status = (w->work == uv__cancelled) ? UV_ECANCELED : 0; - struct uv_loop_data* addr = (struct uv_loop_data*)((uint64_t)w->loop->data - - (UV_EVENT_MAGIC_OFFSET << UV_EVENT_MAGIC_OFFSETBITS)); - addr->post_task_func(addr->event_handler, uv__task_done_wrapper, (void*)w, status, qos); + struct uv_loop_data* data = (struct uv_loop_data*)loop->data; + data->post_task_func(data->event_handler, uv__task_done_wrapper, (void*)w, status, qos); } else { uv__queue_insert_tail(&(lfields->wq_sub[qos]), &w->wq); uv_async_send(&loop->wq_async); @@ -676,7 +675,7 @@ void uv__work_done(uv_async_t* handle) { loop = container_of(handle, uv_loop_t, wq_async); #ifdef USE_OHOS_DFX - if (uv_check_data_valid((struct uv_loop_data*)(loop->data)) != 0) + if (uv_check_data_valid(loop) != 0) uv__print_active_reqs(loop, "complete"); #endif rdlock_closed_uv_loop_rwlock(); @@ -802,11 +801,10 @@ void uv__ffrt_work(ffrt_executor_task_t* data, ffrt_qos_t qos) uv_mutex_lock(&loop->wq_mutex); w->work = NULL; /* Signal uv_cancel() that the work req is done executing. */ - if (uv_check_data_valid((struct uv_loop_data*)(loop->data)) == 0) { + if (uv_check_data_valid(loop) == 0) { int status = (w->work == uv__cancelled) ? UV_ECANCELED : 0; - struct uv_loop_data* addr = (struct uv_loop_data*)((uint64_t)loop->data - - (UV_EVENT_MAGIC_OFFSET << UV_EVENT_MAGIC_OFFSETBITS)); - addr->post_task_func(addr->event_handler, uv__task_done_wrapper, (void*)w, status, qos); + struct uv_loop_data* data = (struct uv_loop_data*)loop->data; + data->post_task_func(data->event_handler, uv__task_done_wrapper, (void*)w, status, qos); } else { uv__loop_internal_fields_t* lfields = uv__get_internal_fields(loop); uv__queue_insert_tail(&(lfields->wq_sub[qos]), &w->wq); @@ -946,7 +944,7 @@ int uv_queue_work_with_qos(uv_loop_t* loop, STATIC_ASSERT(uv_qos_user_initiated == ffrt_qos_user_initiated); STATIC_ASSERT(uv_qos_user_interactive == ffrt_qos_user_interactive); if (qos == uv_qos_reserved) { - UV_LOGW("Invaild qos %{public}d", (int)qos); + UV_LOGW("Invalid qos %{public}d", (int)qos); return UV_EINVAL; } if (qos < ffrt_qos_background || qos > ffrt_qos_user_interactive) { diff --git a/src/timer.c b/src/timer.c index 6bae907..6bb1a45 100644 --- a/src/timer.c +++ b/src/timer.c @@ -104,7 +104,7 @@ int uv_timer_start(uv_timer_t* handle, timer_less_than); uv__handle_start(handle); #ifdef __linux__ - if (uv_check_data_valid((struct uv_loop_data*)handle->loop->data) == 0) { + if (uv_check_data_valid(handle->loop) == 0) { uv_async_send(&handle->loop->wq_async); } #endif diff --git a/src/unix/core.c b/src/unix/core.c index 771dc18..58d90e9 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -1942,17 +1942,13 @@ int uv_register_task_to_event(struct uv_loop_s* loop, uv_post_task func, void* h struct uv_loop_data* data = (struct uv_loop_data*)malloc(sizeof(struct uv_loop_data)); if (data == NULL) return -1; - if ((uint64_t)data >> UV_EVENT_MAGIC_OFFSETBITS != 0x0) { - UV_LOGE("malloc address error"); - free(data); - return -1; - } (void)memset(data, 0, sizeof(struct uv_loop_data)); data->post_task_func = func; data->event_handler = handler; - data = (struct uv_loop_data*)((uint64_t)data | (UV_EVENT_MAGIC_OFFSET << UV_EVENT_MAGIC_OFFSETBITS)); loop->data = (void *)data; + uv__loop_internal_fields_t* lfields_flag = uv__get_internal_fields(loop); + lfields_flag->register_flag = 1; return 0; #else return -1; @@ -1963,13 +1959,12 @@ int uv_register_task_to_event(struct uv_loop_s* loop, uv_post_task func, void* h int uv_unregister_task_to_event(struct uv_loop_s* loop) { #if defined(__aarch64__) - if (loop == NULL || loop->data == NULL || - ((uint64_t)loop->data >> UV_EVENT_MAGIC_OFFSETBITS) != (uint64_t)(UV_EVENT_MAGIC_OFFSET)) + uv__loop_internal_fields_t* lfields_flag = uv__get_internal_fields(loop); + if (loop == NULL || loop->data == NULL || lfields_flag->register_flag == 0) return -1; - loop->data = (struct uv_loop_data*)((uint64_t)loop->data - - (UV_EVENT_MAGIC_OFFSET << UV_EVENT_MAGIC_OFFSETBITS)); free(loop->data); loop->data = NULL; + lfields_flag->register_flag = 0; return 0; #else return -1; @@ -1977,14 +1972,13 @@ int uv_unregister_task_to_event(struct uv_loop_s* loop) } -int uv_check_data_valid(struct uv_loop_data* data) { +int uv_check_data_valid(uv_loop_t* loop) { #if defined(__aarch64__) - if (data == NULL || ((uint64_t)data >> UV_EVENT_MAGIC_OFFSETBITS) != (uint64_t)(UV_EVENT_MAGIC_OFFSET)) { + uv__loop_internal_fields_t* lfields_flag = uv__get_internal_fields(loop); + if (loop == NULL || loop->data == NULL || lfields_flag->register_flag == 0) { return -1; } - struct uv_loop_data* addr = (struct uv_loop_data*)((uint64_t)data - - (UV_EVENT_MAGIC_OFFSET << UV_EVENT_MAGIC_OFFSETBITS)); - if (addr->post_task_func == NULL) { + if (((struct uv_loop_data*)loop->data)->post_task_func == NULL) { UV_LOGE("post_task_func NULL"); return -1; } diff --git a/src/unix/loop.c b/src/unix/loop.c index 32206a2..92ff87d 100644 --- a/src/unix/loop.c +++ b/src/unix/loop.c @@ -70,6 +70,10 @@ int uv_loop_init(uv_loop_t* loop) { loop->active_handles = 0; #if defined(USE_OHOS_DFX) && defined(__aarch64__) uv__init_thread_id(loop); +#endif +#ifdef __aarch64__ + uv__loop_internal_fields_t* lfields_flag = uv__get_internal_fields(loop); + lfields_flag->register_flag = 0; #endif loop->active_reqs.count = 0; loop->nfds = 0; diff --git a/src/uv-common.h b/src/uv-common.h index 25b9011..1a198db 100644 --- a/src/uv-common.h +++ b/src/uv-common.h @@ -460,6 +460,9 @@ struct uv__loop_internal_fields_s { #if defined(USE_OHOS_DFX) && defined(__aarch64__) unsigned int thread_id; #endif +#ifdef __aarch64__ + unsigned int register_flag; +#endif }; uint64_t uv__get_addr_tag(void* addr); -- Gitee