1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_ABILITY_RUNTIME_WATCHDOG_H 17 #define OHOS_ABILITY_RUNTIME_WATCHDOG_H 18 19 #include <string> 20 #include <mutex> 21 #include <thread> 22 #include "event_handler.h" 23 #include "inner_event.h" 24 #include "application_impl.h" 25 26 namespace OHOS { 27 namespace AppExecFwk { 28 constexpr uint32_t INI_TIMER_FIRST_SECOND = 10000; 29 constexpr const char* MAIN_THREAD_TIMEOUT_TASK = "MAIN_THREAD_TIMEOUT_TASK"; 30 class Watchdog { 31 public: 32 Watchdog(); 33 ~Watchdog(); 34 35 /** 36 * 37 * @brief Init the Watchdog. 38 * 39 * @param mainHandler The handler of main thread. 40 */ 41 void Init(const std::shared_ptr<EventHandler> mainHandler); 42 43 /** 44 * 45 * @brief Stop the mainthread function of watchdog. 46 * 47 */ 48 void Stop(); 49 50 /** 51 * 52 * @brief Set the state of main thread. 53 * 54 * @param appMainThreadState The state of main thread. 55 */ 56 void SetAppMainThreadState(const bool appMainThreadState); 57 58 /** 59 * 60 * @brief Set whether app is in the background or not 61 * 62 */ 63 void SetBackgroundStatus(const bool isInBackground); 64 65 /** 66 * 67 * @brief Allow report the main thread timeout event. 68 * 69 */ 70 void AllowReportEvent(); 71 72 /** 73 * 74 * @brief Get StopWatchdog flag. 75 * 76 */ 77 bool IsStopWatchdog(); 78 79 /** 80 * 81 * @brief Check and reset the main thread state. 82 * 83 */ 84 bool IsReportEvent(); 85 86 /** 87 * 88 * @brief Set bundle info. 89 * 90 */ 91 void SetBundleInfo(const std::string& bundleName, const std::string& bundleVersion); 92 93 /** 94 * 95 * @brief Set whether thread is working in the background or not. 96 * 97 */ 98 void SetBgWorkingThreadStatus(const bool isBgWorkingThread); 99 100 private: 101 void Timer(); 102 void ReportEvent(); 103 104 std::atomic_bool appMainThreadIsAlive_ = false; 105 std::atomic_bool stopWatchdog_ = false; 106 std::atomic_bool needReport_ = true; 107 std::atomic_bool isSixSecondEvent_ = false; 108 std::atomic_bool isInBackground_ = true; 109 std::atomic_bool isBgWorkingThread_ = false; 110 std::atomic_int backgroundReportCount_ = 0; 111 std::atomic_int watchdogReportCount_ = 0; 112 std::mutex cvMutex_; 113 std::condition_variable cvWatchdog_; 114 static std::shared_ptr<EventHandler> appMainHandler_; 115 int64_t lastWatchTime_ = 0; 116 }; 117 } // namespace AppExecFwk 118 } // namespace OHOS 119 #endif // OHOS_ABILITY_RUNTIME_WATCHDOG_H 120