• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 RELIABILITY_WATCHDOG_H
17 #define RELIABILITY_WATCHDOG_H
18 
19 #include <string>
20 #include "event_handler.h"
21 #include "singleton.h"
22 
23 using Task = std::function<void()>;
24 using TimeOutCallback = std::function<void(const std::string &name, int waitState)>;
25 namespace OHOS {
26 namespace HiviewDFX {
27 class Watchdog : public Singleton<Watchdog> {
28     DECLARE_SINGLETON(Watchdog);
29     static const uint64_t WATCHDOG_TIMEVAL = 30000;
30 public:
31 
32     /**
33      * Add handler to watchdog thread with customized check interval
34      *
35      * @param name, the name of handler check task
36      * @param handler, the handler to be checked periodically
37      * @param timeOutCallback, callback when timeout
38      * @param interval, the period in millisecond
39      * @return 0 if added
40      *
41      */
42     int AddThread(const std::string &name, std::shared_ptr<AppExecFwk::EventHandler> handler,
43         TimeOutCallback timeOutCallback = nullptr, uint64_t interval = WATCHDOG_TIMEVAL);
44 
45     /**
46      * Add handler to watchdog thread with customized check interval
47      *
48      * @param name, the name of handler check task
49      * @param handler, the handler to be checked periodically
50      * @param interval, the period in millisecond
51      * @return 0 if added
52      *
53      */
54     int AddThread(const std::string &name, std::shared_ptr<AppExecFwk::EventHandler> handler,  uint64_t interval);
55 
56     /**
57      * Run a onshot task in shared watchdog thread, the submitted task should never be time consuming
58      *
59      * @param name, task name
60      * @param task, a short functiona
61      * @param delay, delay a few millisecond to run the task
62      *
63      */
64     void RunOneShotTask(const std::string& name, Task&& task, uint64_t delay = 0);
65 
66     /**
67      * Run a periodical task in shared watchdog thread
68      *
69      * @param name, task name
70      * @param task, a short functiona
71      * @param interval, the millisecond interval of the periodical task
72      * @param delay, delay a few millisecond to first run the task
73      *
74      */
75     void RunPeriodicalTask(const std::string& name, Task&& task, uint64_t interval, uint64_t delay = 0);
76 
77     /**
78      * stop watchdog thread and wait for cleanup synchronously
79      *
80      */
81     void StopWatchdog();
82 
83     /**
84      * watchdog adapt ffrt and not influence ipc full, hungtask delete watchdog, merge xcollie and watchdog.
85      *
86      */
87     void InitFfrtWatchdog();
88 };
89 } // end of namespace HiviewDFX
90 } // end of namespace OHOS
91 #endif
92 
93