diff --git a/entry/src/main/cpp/napi_init.cpp b/entry/src/main/cpp/napi_init.cpp index 549714526cff90e2ffe292a8bc8319645a35a646..a78ee72401482d4b54b5fcde5354e581aa5f59ee 100644 --- a/entry/src/main/cpp/napi_init.cpp +++ b/entry/src/main/cpp/napi_init.cpp @@ -28,6 +28,7 @@ static int g_value = 0; struct CallbackContext { napi_env env = nullptr; napi_ref callbackRef = nullptr; + uv_async_t *async = nullptr; }; // [End callback_context] @@ -97,25 +98,16 @@ static napi_value ThreadSafeCase(napi_env env, napi_callback_info info) { } // [End thread_safe_case] -// [Start uv_loop_async] -//Global variable, used to store loop information and async handle. -uv_loop_t *loop = nullptr; -uv_async_t *async = nullptr; -// [End uv_loop_async] - // [Start async_handler] void async_handler(uv_async_t *handle) { - CallbackContext *context = reinterpret_cast(handle->data); + CallbackContext *context = static_cast(handle->data); napi_handle_scope scope = nullptr; napi_open_handle_scope(context->env, &scope); if (scope == nullptr) { napi_delete_reference(context->env, context->callbackRef); delete context; context = nullptr; - if (handle != nullptr) { - delete handle; - handle = nullptr; - } + uv_close(reinterpret_cast(handle), [](uv_handle_t *handle) { delete reinterpret_cast(handle); }); return; } napi_value callback = nullptr; @@ -130,7 +122,7 @@ void async_handler(uv_async_t *handle) { napi_delete_reference(context->env, context->callbackRef); delete context; context = nullptr; - uv_close((uv_handle_t *)async, [](uv_handle_t *handle) { delete (uv_async_t *)handle; }); + uv_close(reinterpret_cast(handle), [](uv_handle_t *handle) { delete reinterpret_cast(handle); }); } // [End async_handler] @@ -140,9 +132,7 @@ void CallbackUvWorkTest(CallbackContext *context) { return; } OH_LOG_INFO(LOG_APP, "LibUV ChildThread, value:[%{public}d]", g_value); - - async->data = reinterpret_cast(context); - uv_async_send(async); + uv_async_send(context->async); } // [End callback_uv_work_test] @@ -157,15 +147,7 @@ static napi_value LibUvCase(napi_env env, napi_callback_info info) { if (valueType != napi_valuetype::napi_function) { return nullptr; } - // [End lib_uv_case] - // [Start uv_default_loop] - loop = uv_default_loop(); - async = new uv_async_t; - uv_async_init(loop, async, async_handler); - // [End uv_default_loop] - - // [Start lib_uv_case] auto asyncContext = new CallbackContext(); if (asyncContext == nullptr) { return nullptr; @@ -173,10 +155,21 @@ static napi_value LibUvCase(napi_env env, napi_callback_info info) { asyncContext->env = env; napi_create_reference(env, callback_function, 1, &asyncContext->callbackRef); // [End lib_uv_case] + // [Start uv_default_loop] + uv_loop_t *loop = nullptr; + if (napi_get_uv_event_loop(env, &loop) != napi_ok) { + delete asyncContext; + return nullptr; + } + + uv_async_t *async = new uv_async_t(); + uv_async_init(loop, async, async_handler); + async->data = asyncContext; + asyncContext->async = async; + std::thread caseThread(CallbackUvWorkTest, asyncContext); caseThread.detach(); - uv_run(loop, UV_RUN_DEFAULT); return nullptr; // [End uv_default_loop] }