diff --git a/code/BasicFeature/Native/NdkNativeFence/entry/src/main/cpp/CMakeLists.txt b/code/BasicFeature/Native/NdkNativeFence/entry/src/main/cpp/CMakeLists.txt index 2017e5d3f883d3a74a292f5d983a1c3a6b1ab4aa..1f348729f2db9287b3bb526214e22c615ff819c1 100755 --- a/code/BasicFeature/Native/NdkNativeFence/entry/src/main/cpp/CMakeLists.txt +++ b/code/BasicFeature/Native/NdkNativeFence/entry/src/main/cpp/CMakeLists.txt @@ -2,14 +2,14 @@ cmake_minimum_required(VERSION 3.5.0) project(456) -set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +set(NATIVEFENCE_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) if(DEFINED PACKAGE_FIND_FILE) include(${PACKAGE_FIND_FILE}) endif() -include_directories(${NATIVERENDER_ROOT_PATH} - ${NATIVERENDER_ROOT_PATH}/include) +include_directories(${NATIVEFENCE_ROOT_PATH} + ${NATIVEFENCE_ROOT_PATH}/include) add_library(entry SHARED napi_init.cpp) diff --git a/code/BasicFeature/Native/NdkNativeFence/entry/src/main/cpp/napi_init.cpp b/code/BasicFeature/Native/NdkNativeFence/entry/src/main/cpp/napi_init.cpp index ce78552d583d43962c6c1541ce876b78c0d40cab..c39e293abd1245c0c93c97b84a18c7611dbacbb9 100755 --- a/code/BasicFeature/Native/NdkNativeFence/entry/src/main/cpp/napi_init.cpp +++ b/code/BasicFeature/Native/NdkNativeFence/entry/src/main/cpp/napi_init.cpp @@ -60,60 +60,63 @@ static napi_value Add(napi_env env, napi_callback_info info) static napi_value SyncFenceWait(napi_env env, napi_callback_info info) { bool result = OH_NativeFence_Wait(INVALID_FD, TIMEOUT_MS); - DRAWING_LOGI("get result %{public}d", result); bool isValid = OH_NativeFence_IsValid(INVALID_FD); - DRAWING_LOGI("input fenceFd is: %{public}d", isValid); std::atomic signaled(false); sigset_t mask; sigemptyset(&mask); sigaddset(&mask, SIGINT); // Monitor SIGINT signal (Ctrl C) - sigaddset(&mask, SIGTERM); // Monitor SIGTERM signal (kill command) + sigaddset(&mask, SIGURG); // Generated when urgent data or out of band data arrives at the socket sigprocmask(SIG_BLOCK, &mask, NULL); int sfd = signalfd(-1, &mask, 0); - DRAWING_LOGI("input fenceFd is: %{public}d", sfd); if (sfd == -1) { - perror("signalfd failed"); + perror("SyncFenceWait signalfd failed"); exit(1); } isValid = OH_NativeFence_IsValid(sfd); - DRAWING_LOGI("input fenceFd is: %{public}d", isValid); std::thread waitThread([&]() { bool result2 = false; auto startTime = std::chrono::steady_clock::now(); result2 = OH_NativeFence_Wait(sfd, TIMEOUT_MS); auto endTime = std::chrono::steady_clock::now(); auto duration = std::chrono::duration_cast(endTime - startTime).count(); - DRAWING_LOGI("get result2 %{public}d, cost_time: %{public}d", result2, duration); + if (result2) { + DRAWING_LOGI("SyncFenceWait has an event occurring result2 %{public}d, cost_time: %{public}d", + result2, duration); + } else { + DRAWING_LOGI("SyncFenceWait timeout with no event occurrence result2 %{public}d, cost_time: %{public}d", + result2, duration); + } signaled.store(true); }); std::this_thread::sleep_for(std::chrono::seconds(3)); // 3 means main thread sleep 3 seconds. pid_t target_pid = getpid(); - int ret = kill(target_pid, SIGINT); + int ret = kill(target_pid, SIGURG); if (ret < 0) { - DRAWING_LOGI("kill failed: %{public}d", strerror(errno)); + DRAWING_LOGI("SyncFenceWait kill failed: %{public}d", strerror(errno)); } // Waiting for waitThread to complete waitThread.join(); - // checks the signaled variable to ensure that OH_NativeFence_Wait has returned - DRAWING_LOGI("get signaled.load() %{public}d", signaled.load()); OH_NativeFence_Close(sfd); + napi_value funcResult = nullptr; + napi_create_int32(env, result ? 1 : 0, &funcResult); + + return funcResult; } static napi_value SyncFenceWaitForever(napi_env env, napi_callback_info info) { bool result = OH_NativeFence_WaitForever(INVALID_FD); - DRAWING_LOGI("get result %{public}d", result); std::atomic signaled(false); sigset_t mask; sigemptyset(&mask); sigaddset(&mask, SIGINT); // Monitor SIGINT signal (Ctrl C) - sigaddset(&mask, SIGTERM); // Monitor SIGTERM signal (kill command) + sigaddset(&mask, SIGURG); // Generated when urgent data or out of band data arrives at the socket sigprocmask(SIG_BLOCK, &mask, NULL); int sfd = signalfd(-1, &mask, 0); if (sfd == -1) { - perror("signalfd failed"); + perror("SyncFenceWaitForever signalfd failed"); exit(1); } std::thread waitThread([&]() { @@ -122,20 +125,28 @@ static napi_value SyncFenceWaitForever(napi_env env, napi_callback_info info) result2 = OH_NativeFence_WaitForever(sfd); auto endTime = std::chrono::steady_clock::now(); auto duration = std::chrono::duration_cast(endTime - startTime).count(); - DRAWING_LOGI("get result2 %{public}d, cost_time: %{public}d", result2, duration); + if (result2) { + DRAWING_LOGI("SyncFenceWaitForever has an event occurring result2 %{public}d, cost_time: %{public}d", + result2, duration); + } else { + DRAWING_LOGI("SyncFenceWaitForever timeout with no event occurrence" + "result2 %{public}d, cost_time: %{public}d", result2, duration); + } signaled.store(true); }); - std::this_thread::sleep_for(std::chrono::seconds(3)); // 3 means main thread sleep 3 seconds. + std::this_thread::sleep_for(std::chrono::seconds(2)); // 2 means main thread sleep 2 seconds. pid_t target_pid = getpid(); - int ret = kill(target_pid, SIGINT); + int ret = kill(target_pid, SIGURG); if (ret < 0) { - DRAWING_LOGI("kill failed: %{public}d", strerror(errno)); + DRAWING_LOGI("SyncFenceWaitForever kill failed: %{public}d", strerror(errno)); } // Waiting for waitThread to complete waitThread.join(); - // checks the signaled variable to ensure that OH_NativeFence_Wait has returned - DRAWING_LOGI("get signaled.load() %{public}d", signaled.load()); OH_NativeFence_Close(sfd); + napi_value funcResult = nullptr; + napi_create_int32(env, result ? 1 : 0, &funcResult); + + return funcResult; } // EXTERN_C_START diff --git a/code/BasicFeature/Native/NdkNativeFence/entry/src/main/cpp/types/libentry/Index.d.ts b/code/BasicFeature/Native/NdkNativeFence/entry/src/main/cpp/types/libentry/Index.d.ts index 3a942ca1ec1f093b56cf0bcca43a3995c2489ff7..78dad3f6cf6ddc2b6c8c18cfdf67921e441a4669 100755 --- a/code/BasicFeature/Native/NdkNativeFence/entry/src/main/cpp/types/libentry/Index.d.ts +++ b/code/BasicFeature/Native/NdkNativeFence/entry/src/main/cpp/types/libentry/Index.d.ts @@ -14,5 +14,5 @@ */ export const add: (a: number, b: number) => number; -export const syncFence_wait: () => void; -export const syncFence_waitForever: () => void; \ No newline at end of file +export const syncFence_wait: () => number; +export const syncFence_waitForever: () => number; \ No newline at end of file diff --git a/code/BasicFeature/Native/NdkNativeFence/entry/src/ohosTest/ets/test/Ability.test.ets b/code/BasicFeature/Native/NdkNativeFence/entry/src/ohosTest/ets/test/Ability.test.ets index b564fe7a5f2dba3e502e0310cd60eb386d84bb94..a77b6aceb1231ae2e53147772be072a4ef7eff8a 100755 --- a/code/BasicFeature/Native/NdkNativeFence/entry/src/ohosTest/ets/test/Ability.test.ets +++ b/code/BasicFeature/Native/NdkNativeFence/entry/src/ohosTest/ets/test/Ability.test.ets @@ -60,7 +60,7 @@ export default function abilityTest() { await driver.assertComponentExist(ON.text('SyncFenceWait')); let syncFenceBtn1 = await driver.findComponent(ON.text('SyncFenceWait')); // 点击'syncFenceWait'按钮 - // await syncFenceBtn1.click(); 注释掉,因为点击按钮后,会直接退出应用(接口调用后,会直接退出应用,所以这里不进行点击操作) + await syncFenceBtn1.click(); await driver.delayMs(1000); }) @@ -83,7 +83,7 @@ export default function abilityTest() { await driver.assertComponentExist(ON.text('SyncFenceWaitForever')); let syncFenceBtn2 = await driver.findComponent(ON.text('SyncFenceWaitForever')); // 点击'syncFenceWaitForever'按钮 - // await syncFenceBtn2.click(); 注释掉,因为点击按钮后,会直接退出应用(接口调用后,会直接退出应用,所以这里不进行点击操作) + await syncFenceBtn2.click(); await driver.delayMs(1000); hilog.info(0x0001, TAG, 'syncFenceWaitForever_001 end'); }) diff --git a/code/BasicFeature/Native/NdkNativeFence/ohosTest.md b/code/BasicFeature/Native/NdkNativeFence/ohosTest.md index 8e0836832c3f7142de4c8affdcf16aef2f26e700..196280944361f32d4e0baab8b481004ab1290d81 100755 --- a/code/BasicFeature/Native/NdkNativeFence/ohosTest.md +++ b/code/BasicFeature/Native/NdkNativeFence/ohosTest.md @@ -2,5 +2,5 @@ |:-------:|:------------:|:--------------:|:---------------:|:----:|:----:| | 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | | 主页展示 | 设备正常运行 | | 展示 SyncFenceWait 和 SyncFenceWaitForever 按钮 | 是 | Pass | -| 主页按钮点击 | 位于主页 | 点击 SyncFenceWait 按钮 | 等待3s后退出 | 是 | Pass | -| 主页按钮点击 | 位于主页 | 点击 SyncFenceWaitForever 按钮 | 等待5s后退出 | 是 | Pass | \ No newline at end of file +| 主页按钮点击 | 位于主页 | 点击 SyncFenceWait 按钮 | 有信号事件发生时,等待3s流程结束;无信号事件发生时,等待5s流程结束 | 是 | Pass | +| 主页按钮点击 | 位于主页 | 点击 SyncFenceWaitForever 按钮 | 有信号事件发生时,等待2s流程结束;无信号事件发生时,会永久阻塞 | 是 | Pass | \ No newline at end of file