diff --git a/frameworks/kits/appkit/native/app/include/watchdog.h b/frameworks/kits/appkit/native/app/include/watchdog.h index 63f1afd0bff26300925cf0cbfa838572d1f82140..f8883511eff8f635aa65bfc5b5a3117073b1c8e4 100644 --- a/frameworks/kits/appkit/native/app/include/watchdog.h +++ b/frameworks/kits/appkit/native/app/include/watchdog.h @@ -25,9 +25,8 @@ namespace OHOS { namespace AppExecFwk { const uint32_t MAIN_THREAD_IS_ALIVE = 0; const uint32_t MAIN_THREAD_TIMEOUT_TIME = 3000; -const uint32_t INI_ZERO = 0; -const uint32_t INI_TIMER_FIRST_SECOND = 10; -const uint32_t INI_TIMER_SECOND = 3; +const uint32_t INI_TIMER_FIRST_SECOND = 10000; +const uint32_t INI_TIMER_SECOND = 3000; const std::string MAIN_THREAD_IS_ALIVE_MSG = "MAIN_THREAD_IS_ALIVE"; class WatchDog : public EventHandler { public: @@ -42,7 +41,7 @@ public: * */ void ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer &event) override; - static void Timer(int sig); + bool Timer(); /** * @@ -77,6 +76,8 @@ public: static bool GetAppMainThreadState(); private: + std::atomic_bool stopWatchDog_ = false; + std::shared_ptr watchDogThread_ = nullptr; std::shared_ptr watchDogRunner_; static bool appMainThreadIsAlive_; static std::shared_ptr appMainHandler_; diff --git a/frameworks/kits/appkit/native/app/src/main_thread.cpp b/frameworks/kits/appkit/native/app/src/main_thread.cpp index 868b04c2eb8a613ff5c61fb57f7b1cee244c26c6..63cd02f62b1a06acb2aad96b57656d530098579d 100644 --- a/frameworks/kits/appkit/native/app/src/main_thread.cpp +++ b/frameworks/kits/appkit/native/app/src/main_thread.cpp @@ -1342,10 +1342,10 @@ void MainThread::Init(const std::shared_ptr &runner, const std::sha APP_LOGE("MainThread::Init WatchDog postTask task failed"); } TaskTimeoutDetected(); - /* + watchDogHandler_->Init(mainHandler_, watchDogHandler_); APP_LOGI("MainThread:Init before CreateRunner."); - */ + TaskHandlerClient::GetInstance()->CreateRunner(); APP_LOGI("MainThread:Init after CreateRunner."); APP_LOGI("MainThread:Init end."); diff --git a/frameworks/kits/appkit/native/app/src/watchdog.cpp b/frameworks/kits/appkit/native/app/src/watchdog.cpp index 4b25197344d1a199447cd181a5c7a3e1db7f59d8..a109e5a5101d64ec7e68481184e8490d73da7b5f 100644 --- a/frameworks/kits/appkit/native/app/src/watchdog.cpp +++ b/frameworks/kits/appkit/native/app/src/watchdog.cpp @@ -50,33 +50,31 @@ void WatchDog::ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer &event) void WatchDog::Init(const std::shared_ptr &mainHandler, const std::shared_ptr &watchDogHandler) { - APP_LOGI("watchdog is run !"); WatchDog::appMainHandler_ = mainHandler; WatchDog::currentHandler_ = watchDogHandler; - struct itimerval tick; - tick.it_value.tv_sec = INI_TIMER_FIRST_SECOND; - tick.it_value.tv_usec = INI_ZERO; - tick.it_interval.tv_sec = INI_TIMER_SECOND; - tick.it_interval.tv_usec = INI_ZERO; - - if (signal(SIGALRM, &WatchDog::Timer) == SIG_ERR) { - APP_LOGE("WatchDog::Timer signal fail."); - } - - if (setitimer(ITIMER_REAL, &tick, NULL) < INI_ZERO) { - APP_LOGE("Init WatchDog timer failed"); + if (watchDogThread_ == nullptr) { + watchDogThread_ = std::make_shared(&WatchDog::Timer, this); + APP_LOGI("Watchdog is running!"); } } void WatchDog::Stop() { APP_LOGI("Watchdog is stop !"); + stopWatchDog_.store(true); + if (watchDogThread_ != nullptr && watchDogThread_->joinable()) { + watchDogThread_->join(); + watchDogThread_ = nullptr; + } if (watchDogRunner_) { watchDogRunner_.reset(); } if (currentHandler_) { currentHandler_.reset(); } + if (appMainHandler_) { + appMainHandler_.reset(); + } } std::shared_ptr WatchDog::GetCurrentHandler() @@ -89,14 +87,19 @@ bool WatchDog::GetAppMainThreadState() return appMainThreadIsAlive_; } -void WatchDog::Timer(int sig) +bool WatchDog::Timer() { - auto timeoutTask1 = [&]() { - appMainThreadIsAlive_ = false; - APP_LOGI("Waring : main thread is not response!"); - }; - currentHandler_->PostTask(timeoutTask1, MAIN_THREAD_IS_ALIVE_MSG, MAIN_THREAD_TIMEOUT_TIME); - appMainHandler_->SendEvent(MAIN_THREAD_IS_ALIVE); + std::this_thread::sleep_for(std::chrono::milliseconds(INI_TIMER_FIRST_SECOND)); + while (!stopWatchDog_) { + std::this_thread::sleep_for(std::chrono::milliseconds(INI_TIMER_SECOND)); + auto timeoutTask1 = [&]() { + appMainThreadIsAlive_ = false; + APP_LOGI("Waring : main thread is not response!"); + }; + currentHandler_->PostTask(timeoutTask1, MAIN_THREAD_IS_ALIVE_MSG, MAIN_THREAD_TIMEOUT_TIME); + appMainHandler_->SendEvent(MAIN_THREAD_IS_ALIVE); + } + return true; } } // namespace AppExecFwk } // namespace OHOS