1# Passing a Task with the Specified Priority to an ArkTS Thread from an Asynchronous Thread Using Node-API 2You can use **napi_call_threadsafe_function_with_priority** to pass a task to an ArkTS thread from an asynchronous thread in a thread-safe manner. Then, the task will be processed based on its priority and enqueuing mode. 3 4## Function Description 5 6```cpp 7napi_status napi_call_threadsafe_function_with_priority(napi_threadsafe_function func, 8 void *data, 9 napi_task_priority priority, 10 bool isTail); 11``` 12 13| Name | Description | 14| :------------- | :----------------------------- | 15| func | Thread-safe function to call. | 16| data | Data to be passed from the asynchronous thread to the main thread. | 17| priority | Priority of the task. For details, see [napi_task_priority](napi-data-types-interfaces.md#thread-safe-task-priority).| 18| isTail | Whether to add the task to the end (tail) of the task queue. The value **true** means to add the task to the end of the task queue; the value **false** means to add the task to the head of the queue.| 19 20## When to Use 21Pass a task to an ArkTS thread from an asynchronous thread in a thread-safe manner. Then, the task will be processed based on its priority and enqueuing mode. 22 23## Calling an ArkTS API Asynchronously 24 25### Example 26 27- Register the module. 28 29 ```c++ 30 // hello.cpp 31 #include "napi/native_api.h" 32 #include <string.h> 33 #include <stdlib.h> 34 35 struct CallbackData { 36 napi_threadsafe_function tsfn; 37 napi_async_work work; 38 }; 39 // Callback implementation in the ArkTS thread. 40 static void CallJs(napi_env env, napi_value jsCb, void *context, void *data) { 41 if (env == nullptr) { 42 return; 43 } 44 napi_value resultNumber = nullptr; 45 napi_value undefined = nullptr; 46 napi_get_undefined(env, &undefined); 47 napi_value number1 = nullptr; 48 napi_create_int32(env, 12, &number1); 49 napi_value number2 = nullptr; 50 napi_create_int32(env, 15, &number2); 51 napi_value argv[2] = {number1, number2}; 52 napi_call_function(env, undefined, jsCb, 2, argv, &resultNumber); 53 int32_t res = 0; 54 napi_get_value_int32(env, resultNumber, &res); 55 } 56 57 // Call this API in an asynchronous thread to pass a task with the specified priority and enqueuing mode to an ArkTS thread. 58 static void ExecuteWork(napi_env env, void *data) { 59 CallbackData *callbackData = reinterpret_cast<CallbackData *>(data); 60 // The task priority is napi_priority_idle, and the task is added to the end of the task queue. 61 napi_call_threadsafe_function_with_priority(callbackData->tsfn, nullptr, napi_priority_idle, true); 62 napi_call_threadsafe_function_with_priority(callbackData->tsfn, nullptr, napi_priority_low, true); 63 napi_call_threadsafe_function_with_priority(callbackData->tsfn, nullptr, napi_priority_high, true); 64 napi_call_threadsafe_function_with_priority(callbackData->tsfn, nullptr, napi_priority_immediate, true); 65 // The task priority is napi_priority_high, and the task is added to the head of the task queue. 66 napi_call_threadsafe_function_with_priority(callbackData->tsfn, nullptr, napi_priority_high, false); 67 } 68 69 static void WorkComplete(napi_env env, napi_status status, void *data) { 70 CallbackData *callbackData = reinterpret_cast<CallbackData *>(data); 71 napi_release_threadsafe_function(callbackData->tsfn, napi_tsfn_release); 72 napi_delete_async_work(env, callbackData->work); 73 callbackData->work = nullptr; 74 callbackData->tsfn = nullptr; 75 } 76 77 static napi_value CallThreadSafeWithPriority(napi_env env, napi_callback_info info) { 78 size_t argc = 1; 79 napi_value jsCb = nullptr; 80 CallbackData *callbackData = nullptr; 81 napi_get_cb_info(env, info, &argc, &jsCb, nullptr, reinterpret_cast<void **>(&callbackData)); 82 napi_value resourceName = nullptr; 83 napi_create_string_utf8(env, "Thread-safe Function Demo", NAPI_AUTO_LENGTH, &resourceName); 84 napi_create_threadsafe_function(env, jsCb, nullptr, resourceName, 0, 1, callbackData, nullptr, callbackData, CallJs, 85 &callbackData->tsfn); 86 napi_create_async_work(env, nullptr, resourceName, ExecuteWork, WorkComplete, callbackData, &callbackData->work); 87 napi_queue_async_work(env, callbackData->work); 88 return nullptr; 89 } 90 91 // Register the module. 92 EXTERN_C_START 93 static napi_value Init(napi_env env, napi_value exports) 94 { 95 CallbackData *callbackData = new CallbackData(); 96 napi_property_descriptor desc[] = { 97 { "callThreadSafeWithPriority", nullptr, CallThreadSafeWithPriority, nullptr, nullptr, nullptr, napi_default, callbackData } 98 }; 99 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 100 return exports; 101 } 102 EXTERN_C_END 103 104 static napi_module nativeModule = { 105 .nm_version = 1, 106 .nm_flags = 0, 107 .nm_filename = nullptr, 108 .nm_register_func = Init, 109 .nm_modname = "entry", 110 .nm_priv = nullptr, 111 .reserved = { 0 }, 112 }; 113 114 extern "C" __attribute__((constructor)) void RegisterEntryModule() 115 { 116 napi_module_register(&nativeModule); 117 } 118 ``` 119 120- Declare the API. 121 122 ```ts 123 // index.d.ts 124 export const callThreadSafeWithPriority: (cb: (a: number, b: number) => number) => void; 125 ``` 126 127- Configure compile settings. 128 129 Configure the **CMakeLists.txt** file as follows: 130 131 ``` 132 // CMakeLists.txt 133 # the minimum version of CMake. 134 cmake_minimum_required(VERSION 3.4.1) 135 project(myapplication) 136 137 set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) 138 139 if(DEFINED PACKAGE_FIND_FILE) 140 include(${PACKAGE_FIND_FILE}) 141 endif() 142 143 include_directories(${NATIVERENDER_ROOT_PATH} 144 ${NATIVERENDER_ROOT_PATH}/include) 145 add_library(entry SHARED hello.cpp) 146 target_link_libraries(entry PUBLIC libace_napi.z.so) 147 ``` 148 149- ArkTS sample code 150 151 ```ts 152 // index.ets 153 import testNapi from 'libentry.so' 154 155 let callback = (a: number, b: number) : number => { 156 console.info('result is ' + (a + b)) 157 return a + b; 158 } 159 testNapi.callThreadSafeWithPriority(callback); 160 ``` 161