• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Change TimeOut.
89      *
90      */
91     void ChangeTimeOut(const std::string& bundleName);
92 
93     /**
94      *
95      * @brief Set bundle info.
96      *
97      */
98     void SetBundleInfo(const std::string& bundleName, const std::string& bundleVersion);
99 
100     /**
101      *
102      * @brief Set whether thread is working in the background or not.
103      *
104      */
105     void SetBgWorkingThreadStatus(const bool isBgWorkingThread);
106 
107 private:
108     void Timer();
109     void ReportEvent();
110 
111     std::atomic_bool appMainThreadIsAlive_ = false;
112     std::atomic_bool stopWatchdog_ = false;
113     std::atomic_bool needReport_ = true;
114     std::atomic_bool isSixSecondEvent_ = false;
115     std::atomic_bool isInBackground_ = true;
116     std::atomic_bool isBgWorkingThread_ = false;
117     std::atomic_int backgroundReportCount_ = 0;
118     std::atomic_int watchdogReportCount_ = 0;
119     std::mutex cvMutex_;
120     std::condition_variable cvWatchdog_;
121     static std::shared_ptr<EventHandler> appMainHandler_;
122     int64_t lastWatchTime_ = 0;
123 };
124 }  // namespace AppExecFwk
125 }  // namespace OHOS
126 #endif  // OHOS_ABILITY_RUNTIME_WATCHDOG_H
127