• 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 #ifdef ABILITY_RUNTIME_HITRACE_ENABLE
26 #include "hitrace/hitracechain.h"
27 #endif
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 constexpr uint32_t INI_TIMER_FIRST_SECOND = 10000;
32 constexpr const char* MAIN_THREAD_TIMEOUT_TASK = "MAIN_THREAD_TIMEOUT_TASK";
33 class Watchdog {
34 public:
35     Watchdog();
36     ~Watchdog();
37 
38     /**
39      *
40      * @brief Init the Watchdog.
41      *
42      * @param mainHandler The handler of main thread.
43      */
44     void Init(const std::shared_ptr<EventHandler> mainHandler);
45 
46     /**
47      *
48      * @brief Stop the mainthread function of watchdog.
49      *
50      */
51     void Stop();
52 
53     /**
54      *
55      * @brief Set the state of main thread.
56      *
57      * @param appMainThreadState The state of main thread.
58      */
59     void SetAppMainThreadState(const bool appMainThreadState);
60 
61     /**
62      *
63      * @brief Set whether app is in the background or not
64      *
65      */
66     void SetBackgroundStatus(const bool isInBackground);
67 
68     /**
69      *
70      * @brief Allow report the main thread timeout event.
71      *
72      */
73     void AllowReportEvent();
74 
75     /**
76      *
77      * @brief Get StopWatchdog flag.
78      *
79      */
80     bool IsStopWatchdog();
81 
82     /**
83      *
84      * @brief Check and reset the main thread state.
85      *
86      */
87     bool IsReportEvent();
88 
89       /**
90      *
91      * @brief Change TimeOut.
92      *
93      */
94     void ChangeTimeOut(const std::string& bundleName);
95 
96     /**
97      *
98      * @brief Set bundle info.
99      *
100      */
101     void SetBundleInfo(const std::string& bundleName, const std::string& bundleVersion);
102 
103     /**
104      *
105      * @brief Set whether thread is working in the background or not.
106      *
107      */
108     void SetBgWorkingThreadStatus(const bool isBgWorkingThread);
109 
110 private:
111     void Timer();
112     void ReportEvent();
113 #ifdef ABILITY_RUNTIME_HITRACE_ENABLE
114     void SetHiTraceChainId();
115 
116     static OHOS::HiviewDFX::HiTraceId* hitraceId_;
117 #endif
118 
119     std::atomic_bool appMainThreadIsAlive_ = false;
120     std::atomic_bool stopWatchdog_ = false;
121     std::atomic_bool needReport_ = true;
122     std::atomic_bool isSixSecondEvent_ = false;
123     std::atomic_bool isInBackground_ = true;
124     std::atomic_bool isBgWorkingThread_ = false;
125     std::atomic_int watchdogReportCount_ = 0;
126     std::mutex cvMutex_;
127     std::condition_variable cvWatchdog_;
128     static std::shared_ptr<EventHandler> appMainHandler_;
129     int64_t lastWatchTime_ = 0;
130 };
131 }  // namespace AppExecFwk
132 }  // namespace OHOS
133 #endif  // OHOS_ABILITY_RUNTIME_WATCHDOG_H
134