From 387c3736d4cc271bd873ea1a37de23b86c6f5830 Mon Sep 17 00:00:00 2001 From: BianYafei Date: Wed, 30 Nov 2022 13:34:30 +0800 Subject: [PATCH] NetConnClientFuzzTest shell cmd timeout bug fix Signed-off-by: BianYafei Change-Id: Ica1552fd389f78010302a166894373ab14fe2c3f --- services/common/include/timer.h | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/services/common/include/timer.h b/services/common/include/timer.h index 3c3ed09c7..a636893b6 100755 --- a/services/common/include/timer.h +++ b/services/common/include/timer.h @@ -28,6 +28,7 @@ namespace OHOS { namespace NetManagerStandard { +static constexpr const int TIMER_MAX_INTERVAL_MS = 50; class Timer { public: Timer() : stopStatus_(true), tryStopFlag_(false) {} @@ -52,7 +53,7 @@ public: stopStatus_ = false; std::thread([this, interval, taskFun]() { while (!tryStopFlag_) { - std::this_thread::sleep_for(std::chrono::milliseconds(interval)); + OneTiming(interval); if (!tryStopFlag_) { taskFun(); } @@ -72,11 +73,9 @@ public: NETMGR_LOG_D("start once thread..."); stopStatus_ = false; std::thread([this, interval, taskFun]() { + OneTiming(interval); if (!tryStopFlag_) { - std::this_thread::sleep_for(std::chrono::milliseconds(interval)); - if (!tryStopFlag_) { - taskFun(); - } + taskFun(); } std::lock_guard locker(mutex_); stopStatus_ = true; @@ -99,6 +98,25 @@ public: } } +private: + void OneTiming(int time) + { + int repeatCount = (time > TIMER_MAX_INTERVAL_MS) ? (time / TIMER_MAX_INTERVAL_MS) : 0; + int remainTime = (time > TIMER_MAX_INTERVAL_MS) ? (time % TIMER_MAX_INTERVAL_MS) : time; + while (!tryStopFlag_) { + if (repeatCount > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(TIMER_MAX_INTERVAL_MS)); + } else { + if (remainTime) { + std::this_thread::sleep_for(std::chrono::milliseconds(remainTime)); + remainTime = 0; + } + break; + } + repeatCount--; + } + } + private: std::atomic stopStatus_; std::atomic tryStopFlag_; -- Gitee