From 5cb2dfd7a9a61cb45d565152f2cad4ea59d26dcf Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Tue, 12 Nov 2024 19:08:56 +0800 Subject: [PATCH 1/6] add napi thread snippets Signed-off-by: chen-zhongwei050 --- src/vscode_plugin/package.json | 4 ++ .../snippets/napi_thread_snippets.json | 59 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/vscode_plugin/snippets/napi_thread_snippets.json diff --git a/src/vscode_plugin/package.json b/src/vscode_plugin/package.json index 3d361525..58823333 100644 --- a/src/vscode_plugin/package.json +++ b/src/vscode_plugin/package.json @@ -112,6 +112,10 @@ "language": "cpp", "path": "./snippets/napi_class_snippets.json" }, + { + "language": "cpp", + "path": "./snippets/napi_thread_snippets.json" + }, { "language": "cpp", "path": "./snippets/napi_async_snippets.json" diff --git a/src/vscode_plugin/snippets/napi_thread_snippets.json b/src/vscode_plugin/snippets/napi_thread_snippets.json new file mode 100644 index 00000000..e6666e78 --- /dev/null +++ b/src/vscode_plugin/snippets/napi_thread_snippets.json @@ -0,0 +1,59 @@ +{ + "Napi thread function": { + "prefix": "napithreadfunc", + "body": [ + "// Thread-safe JavaScript function object.", + "napi_threadsafe_function g_threadsafeFunction;", + "// JavaScript callback function.", + "static void CallbackFunction(napi_env env, napi_value jsCallback, void *context, void *data)", + "{", + " // Execute the callback function in a JavaScript environment.", + " size_t argc = 1;", + " napi_value argv[1];", + " napi_create_string_utf8(env, \"threadsafe func hellowolrd\", NAPI_AUTO_LENGTH, &argv[0]);", + " // Execute the callback function in the JavaScript environment.", + " napi_call_function(env, nullptr, jsCallback, argc, argv, nullptr);", + "}", + "// Thread function, asynchronously call the JavaScript function here.", + "static void ThreadFunction(void *data)", + "{", + " // Asynchronously call a JavaScript function in another thread.", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"napi_call_threadsafe_function called\");", + " napi_call_threadsafe_function(g_threadsafeFunction, nullptr, napi_tsfn_nonblocking);", + "}", + "napi_value setThreadsafefunc(napi_env env, napi_callback_info info)", + "{", + " // Todo: you can use 'napiobjectin' command that get object value from js. 感觉这个可以不用加?", + " napi_value name;", + " napi_create_string_utf8(env, \"testThreadsafefunc\", NAPI_AUTO_LENGTH, &name);", + " // Create a thread-safe function that can be called from worker threads.", + " status = napi_create_threadsafe_function(env, argv[0], nullptr, name, 0, 1,", + " nullptr, nullptr, nullptr, CallbackFunction, &g_threadsafeFunction);", + " if (status != napi_ok) {", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"Failed to create threadsafe function\");", + " return nullptr;", + " }", + " // Set the release mode to NAPI_TSFN_RELEASE.", + " napi_threadsafe_function_release_mode release_mode = napi_tsfn_release;", + " // Decrement the reference count of a threadsafe function, potentially destroying it.", + " napi_release_threadsafe_function(g_threadsafeFunction, release_mode);", + " // Start a new thread.", + " std::thread newThread(ThreadFunction, nullptr);", + " newThread.join();", + " // Todo: Main thread ", + " return nullptr;", + "}", + "EXTERN_C_START", + "static napi_value Init(napi_env env, napi_value exports) {", + " // Define properties and methods for a N-API object.", + " napi_property_descriptor desc[] = {", + " {\"testNapiThreadsafefunc\", nullptr, setThreadsafefunc, nullptr, nullptr, nullptr, napi_default, nullptr}", + " };", + " // Add an array of properties to a ArkTs object.", + " napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);", + " return exports;", + "}", + "EXTERN_C_END" + ] + } +} \ No newline at end of file -- Gitee From 8db12901b41f43fc0d964ce845881aba06964658 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Wed, 13 Nov 2024 17:58:44 +0800 Subject: [PATCH 2/6] modify napi threadsafe snippets Signed-off-by: chen-zhongwei050 --- .../snippets/napi_thread_snippets.json | 72 +++++-------------- 1 file changed, 18 insertions(+), 54 deletions(-) diff --git a/src/vscode_plugin/snippets/napi_thread_snippets.json b/src/vscode_plugin/snippets/napi_thread_snippets.json index e6666e78..8dcd84d6 100644 --- a/src/vscode_plugin/snippets/napi_thread_snippets.json +++ b/src/vscode_plugin/snippets/napi_thread_snippets.json @@ -1,59 +1,23 @@ { - "Napi thread function": { - "prefix": "napithreadfunc", + "Napi create threadsafe function": { + "prefix": "napicreatethreadsafefunc", "body": [ - "// Thread-safe JavaScript function object.", - "napi_threadsafe_function g_threadsafeFunction;", - "// JavaScript callback function.", - "static void CallbackFunction(napi_env env, napi_value jsCallback, void *context, void *data)", - "{", - " // Execute the callback function in a JavaScript environment.", - " size_t argc = 1;", - " napi_value argv[1];", - " napi_create_string_utf8(env, \"threadsafe func hellowolrd\", NAPI_AUTO_LENGTH, &argv[0]);", - " // Execute the callback function in the JavaScript environment.", - " napi_call_function(env, nullptr, jsCallback, argc, argv, nullptr);", - "}", - "// Thread function, asynchronously call the JavaScript function here.", - "static void ThreadFunction(void *data)", - "{", - " // Asynchronously call a JavaScript function in another thread.", - " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"napi_call_threadsafe_function called\");", - " napi_call_threadsafe_function(g_threadsafeFunction, nullptr, napi_tsfn_nonblocking);", - "}", - "napi_value setThreadsafefunc(napi_env env, napi_callback_info info)", - "{", - " // Todo: you can use 'napiobjectin' command that get object value from js. 感觉这个可以不用加?", - " napi_value name;", - " napi_create_string_utf8(env, \"testThreadsafefunc\", NAPI_AUTO_LENGTH, &name);", - " // Create a thread-safe function that can be called from worker threads.", - " status = napi_create_threadsafe_function(env, argv[0], nullptr, name, 0, 1,", - " nullptr, nullptr, nullptr, CallbackFunction, &g_threadsafeFunction);", - " if (status != napi_ok) {", - " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"Failed to create threadsafe function\");", - " return nullptr;", - " }", - " // Set the release mode to NAPI_TSFN_RELEASE.", - " napi_threadsafe_function_release_mode release_mode = napi_tsfn_release;", - " // Decrement the reference count of a threadsafe function, potentially destroying it.", - " napi_release_threadsafe_function(g_threadsafeFunction, release_mode);", - " // Start a new thread.", - " std::thread newThread(ThreadFunction, nullptr);", - " newThread.join();", - " // Todo: Main thread ", - " return nullptr;", - "}", - "EXTERN_C_START", - "static napi_value Init(napi_env env, napi_value exports) {", - " // Define properties and methods for a N-API object.", - " napi_property_descriptor desc[] = {", - " {\"testNapiThreadsafefunc\", nullptr, setThreadsafefunc, nullptr, nullptr, nullptr, napi_default, nullptr}", - " };", - " // Add an array of properties to a ArkTs object.", - " napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);", - " return exports;", - "}", - "EXTERN_C_END" + "// Create a thread-safe function that can be called from worker threads.", + "napi_create_threadsafe_function(env, jsFunction, asyncResource, name, maxQueueSize, initThreadCount, threadFinalizeData, threadFinalizeCb, context, CallJs, &g_threadsafeFunction);" + ] + }, + "Napi call threadsafe function": { + "prefix": "napicallthreadsafefunc", + "body": [ + "// Asynchronously call a JavaScript function in another thread.", + "napi_call_threadsafe_function(g_threadsafeFunction, data, is_blocking);" + ] + }, + "Napi release threadsafe function": { + "prefix": "napireleasethreadsafefunc", + "body": [ + "// Decrement the reference count of a threadsafe function, potentially destroying it.", + "napi_release_threadsafe_function(g_threadsafeFunction, release_mode);" ] } } \ No newline at end of file -- Gitee From bfe3d34dea65e2e6bc79b953c01680e3d484c028 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Thu, 14 Nov 2024 09:47:13 +0800 Subject: [PATCH 3/6] modify threadsafe snippets Signed-off-by: chen-zhongwei050 --- src/vscode_plugin/snippets/napi_thread_snippets.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/vscode_plugin/snippets/napi_thread_snippets.json b/src/vscode_plugin/snippets/napi_thread_snippets.json index 8dcd84d6..38289514 100644 --- a/src/vscode_plugin/snippets/napi_thread_snippets.json +++ b/src/vscode_plugin/snippets/napi_thread_snippets.json @@ -3,21 +3,21 @@ "prefix": "napicreatethreadsafefunc", "body": [ "// Create a thread-safe function that can be called from worker threads.", - "napi_create_threadsafe_function(env, jsFunction, asyncResource, name, maxQueueSize, initThreadCount, threadFinalizeData, threadFinalizeCb, context, CallJs, &g_threadsafeFunction);" + "napi_create_threadsafe_function(env, jsFunction, resource, name, size, count, threadFinalizeData, threadFinalizeCb, context, CallJs, &gThreadsafeFunction);" ] }, "Napi call threadsafe function": { "prefix": "napicallthreadsafefunc", "body": [ "// Asynchronously call a JavaScript function in another thread.", - "napi_call_threadsafe_function(g_threadsafeFunction, data, is_blocking);" + "napi_call_threadsafe_function(gThreadsafeFunction, data, isBlocking);" ] }, "Napi release threadsafe function": { "prefix": "napireleasethreadsafefunc", "body": [ "// Decrement the reference count of a threadsafe function, potentially destroying it.", - "napi_release_threadsafe_function(g_threadsafeFunction, release_mode);" + "napi_release_threadsafe_function(gThreadsafeFunction, releaseMode);" ] } } \ No newline at end of file -- Gitee From db8f4f3932c4768d3c9cf9f8088f3ade59402ae7 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Thu, 14 Nov 2024 11:49:37 +0800 Subject: [PATCH 4/6] modify thread snippets Signed-off-by: chen-zhongwei050 --- .../snippets/napi_thread_snippets.json | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/vscode_plugin/snippets/napi_thread_snippets.json b/src/vscode_plugin/snippets/napi_thread_snippets.json index 38289514..f1d73ac2 100644 --- a/src/vscode_plugin/snippets/napi_thread_snippets.json +++ b/src/vscode_plugin/snippets/napi_thread_snippets.json @@ -19,5 +19,34 @@ "// Decrement the reference count of a threadsafe function, potentially destroying it.", "napi_release_threadsafe_function(gThreadsafeFunction, releaseMode);" ] + }, + "Napi aysnc thread": { + "prefix": "napiasyncthread", + "body": [ + "// Thread-safe JavaScript function object.", + "napi_threadsafe_function gThreadsafeFunction;", + "// JavaScript callback function.", + "static void CallJs(napi_env env, napi_value jsCallback, void *context, void *data)", + "{", + " // Todo: you can use \"napicallfunc\" command that execute the callback function in the JavaScript environment.", + "}", + "// Thread function, asynchronously call the JavaScript function here.", + "static void ThreadFunction(void *data)", + "{", + " std::this_thread::sleep_for(std::chrono::seconds(2));", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC THREAD\", \"Another thread sleep 2s.\");", + " // Todo: you can use \"napicallthreadsafefunc\" command that async call a JavaScript function in another thread.", + "}", + "napi_value setThreadsafefunc(napi_env env, napi_callback_info info)", + "{", + " // Todo: you can use \"napicreatethreadsafefunc\" command that create a thread-safe function that can be called from worker threads.", + " // Todo: you can use \"napireleasethreadsafefunc\" command that set the release mode.", + " // Start a new thread.", + " std::thread newThread(ThreadFunction, nullptr);", + " newThread.join();", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC THREAD\", \"Main thread.\");", + " return nullptr;", + "}" + ] } } \ No newline at end of file -- Gitee From 623a34e789cf3d6d7393cb260d8be098b8a535c5 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Fri, 22 Nov 2024 09:02:00 +0800 Subject: [PATCH 5/6] modify threadsafe snippets Signed-off-by: chen-zhongwei050 --- .../snippets/napi_thread_snippets.json | 45 +++++++------------ 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/src/vscode_plugin/snippets/napi_thread_snippets.json b/src/vscode_plugin/snippets/napi_thread_snippets.json index f1d73ac2..549acf88 100644 --- a/src/vscode_plugin/snippets/napi_thread_snippets.json +++ b/src/vscode_plugin/snippets/napi_thread_snippets.json @@ -1,50 +1,39 @@ { - "Napi create threadsafe function": { - "prefix": "napicreatethreadsafefunc", - "body": [ - "// Create a thread-safe function that can be called from worker threads.", - "napi_create_threadsafe_function(env, jsFunction, resource, name, size, count, threadFinalizeData, threadFinalizeCb, context, CallJs, &gThreadsafeFunction);" - ] - }, - "Napi call threadsafe function": { - "prefix": "napicallthreadsafefunc", - "body": [ - "// Asynchronously call a JavaScript function in another thread.", - "napi_call_threadsafe_function(gThreadsafeFunction, data, isBlocking);" - ] - }, - "Napi release threadsafe function": { - "prefix": "napireleasethreadsafefunc", - "body": [ - "// Decrement the reference count of a threadsafe function, potentially destroying it.", - "napi_release_threadsafe_function(gThreadsafeFunction, releaseMode);" - ] - }, - "Napi aysnc thread": { - "prefix": "napiasyncthread", + "Napi async thread": { + "prefix": "napiasyncthreadsafefunc", "body": [ "// Thread-safe JavaScript function object.", "napi_threadsafe_function gThreadsafeFunction;", "// JavaScript callback function.", "static void CallJs(napi_env env, napi_value jsCallback, void *context, void *data)", "{", - " // Todo: you can use \"napicallfunc\" command that execute the callback function in the JavaScript environment.", + " size_t argc = 1;", + " napi_value argv[1];", + " napi_create_string_utf8(env, \"threadsafe func hellowolrd\", NAPI_AUTO_LENGTH, &argv[0]);", + " napi_call_function(env, nullptr, jsCallback, argc, argv, nullptr);", "}", "// Thread function, asynchronously call the JavaScript function here.", "static void ThreadFunction(void *data)", "{", " std::this_thread::sleep_for(std::chrono::seconds(2));", " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC THREAD\", \"Another thread sleep 2s.\");", - " // Todo: you can use \"napicallthreadsafefunc\" command that async call a JavaScript function in another thread.", + " napi_call_threadsafe_function(gThreadsafeFunction, nullptr, napi_tsfn_nonblocking);", "}", - "napi_value setThreadsafefunc(napi_env env, napi_callback_info info)", + "napi_value StartThread(napi_env env, napi_callback_info info)", "{", - " // Todo: you can use \"napicreatethreadsafefunc\" command that create a thread-safe function that can be called from worker threads.", - " // Todo: you can use \"napireleasethreadsafefunc\" command that set the release mode.", + " size_t argc = 1;", + " napi_value args[1];", + " napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", + " napi_value name;", + " napi_create_string_utf8(env, \"testThreadsafefunc\", NAPI_AUTO_LENGTH, &name);", + " // Create a thread-safe function that can be called from worker threads.", + " napi_create_threadsafe_function(env, args[0], nullptr, name, 0, 1, nullptr, nullptr, nullptr, CallbackFunction, &gThreadsafeFunction);", " // Start a new thread.", " std::thread newThread(ThreadFunction, nullptr);", " newThread.join();", " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC THREAD\", \"Main thread.\");", + " // Decrement the reference count of a threadsafe function, potentially destroying it.", + " napi_release_threadsafe_function(g_threadsafeFunction, napi_tsfn_release);", " return nullptr;", "}" ] -- Gitee From 85a62d437c1b5bae397c5e04f1a37173e6e4d830 Mon Sep 17 00:00:00 2001 From: chen-zhongwei050 Date: Fri, 22 Nov 2024 17:22:36 +0800 Subject: [PATCH 6/6] modify threadsafe snippets Signed-off-by: chen-zhongwei050 --- .../snippets/napi_thread_snippets.json | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/vscode_plugin/snippets/napi_thread_snippets.json b/src/vscode_plugin/snippets/napi_thread_snippets.json index 549acf88..4483a504 100644 --- a/src/vscode_plugin/snippets/napi_thread_snippets.json +++ b/src/vscode_plugin/snippets/napi_thread_snippets.json @@ -2,38 +2,50 @@ "Napi async thread": { "prefix": "napiasyncthreadsafefunc", "body": [ + "struct MyContext {", + " std::string importantString;", + "};", "// Thread-safe JavaScript function object.", - "napi_threadsafe_function gThreadsafeFunction;", - "// JavaScript callback function.", - "static void CallJs(napi_env env, napi_value jsCallback, void *context, void *data)", + "napi_threadsafe_function g_threadsafeFunction;", + "static void CallbackFunction(napi_env env, napi_value jsCallback, void *context, void *data)", "{", " size_t argc = 1;", " napi_value argv[1];", - " napi_create_string_utf8(env, \"threadsafe func hellowolrd\", NAPI_AUTO_LENGTH, &argv[0]);", + " MyContext* myContext = static_cast(context);", + " napi_create_string_utf8(env, myContext->importantString.c_str(), NAPI_AUTO_LENGTH, &argv[0]);", + " // Execute the callback function in a JavaScript environment. After the data is callbacked to JS, it will not wait for JS to finish executing.", " napi_call_function(env, nullptr, jsCallback, argc, argv, nullptr);", "}", - "// Thread function, asynchronously call the JavaScript function here.", + "static void FinalizeFunction(napi_env env, void* data, void* hint)", + "{", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"FinalizeFunction called.\");", + " MyContext* myContext = static_cast(data);", + " delete myContext;", + "}", "static void ThreadFunction(void *data)", "{", - " std::this_thread::sleep_for(std::chrono::seconds(2));", - " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC THREAD\", \"Another thread sleep 2s.\");", - " napi_call_threadsafe_function(gThreadsafeFunction, nullptr, napi_tsfn_nonblocking);", + " // Asynchronously call a JavaScript function in another thread.", + " std::this_thread::sleep_for(std::chrono::seconds(1));", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"Another thread sleep 1s.\");", + " // Invoke a JavaScript function asynchronously from a native thread. After this, it will execute CallbackFunction to callback data to js.", + " napi_call_threadsafe_function(g_threadsafeFunction, nullptr, napi_tsfn_nonblocking);", + " // Decrement the reference count of a threadsafe function, potentially destroying it. After this, it will execute FinalizeFunction to release data.", + " napi_release_threadsafe_function(g_threadsafeFunction, napi_tsfn_release);", "}", - "napi_value StartThread(napi_env env, napi_callback_info info)", + "napi_value StartThreadsafefunc(napi_env env, napi_callback_info info)", "{", " size_t argc = 1;", " napi_value args[1];", " napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", + " MyContext* context = new MyContext{\"Hello from C++\"};", " napi_value name;", " napi_create_string_utf8(env, \"testThreadsafefunc\", NAPI_AUTO_LENGTH, &name);", " // Create a thread-safe function that can be called from worker threads.", - " napi_create_threadsafe_function(env, args[0], nullptr, name, 0, 1, nullptr, nullptr, nullptr, CallbackFunction, &gThreadsafeFunction);", + " napi_create_threadsafe_function(env, args[0], nullptr, name, 0, 1, context, FinalizeFunction, context, CallbackFunction, &g_threadsafeFunction);", " // Start a new thread.", " std::thread newThread(ThreadFunction, nullptr);", " newThread.join();", - " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC THREAD\", \"Main thread.\");", - " // Decrement the reference count of a threadsafe function, potentially destroying it.", - " napi_release_threadsafe_function(g_threadsafeFunction, napi_tsfn_release);", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"Main thread.\");", " return nullptr;", "}" ] -- Gitee