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 RELIABILITY_WATCHDOG_TASK_H 17 #define RELIABILITY_WATCHDOG_TASK_H 18 19 #include <functional> 20 #include <string> 21 22 #include "event_handler.h" 23 #include "handler_checker.h" 24 25 using Task = std::function<void()>; 26 using TimeOutCallback = std::function<void(const std::string &name, int waitState)>; 27 namespace OHOS { 28 namespace HiviewDFX { 29 class WatchdogTask { 30 public: 31 WatchdogTask(std::string name, std::shared_ptr<AppExecFwk::EventHandler> handler, 32 TimeOutCallback timeOutCallback, uint64_t interval); 33 WatchdogTask(std::string name, Task&& task, uint64_t delay, uint64_t interval, bool isOneshot); WatchdogTask()34 WatchdogTask() 35 : name(""), 36 task(nullptr), 37 timeOutCallback(nullptr), 38 checker(nullptr), 39 checkInterval(0), 40 nextTickTime(0), 41 isTaskScheduled(false), 42 isOneshotTask(false) {}; ~WatchdogTask()43 ~WatchdogTask() {}; 44 45 bool operator<(const WatchdogTask &obj) const 46 { 47 // as we use std::priority_queue, the event with smaller target time will be in the top of the queue 48 return (this->nextTickTime > obj.nextTickTime); 49 } 50 51 void Run(uint64_t now); 52 void RunHandlerCheckerTask(); 53 void SendEvent(const std::string &msg, const std::string &eventName) const; 54 55 int EvaluateCheckerState(); 56 std::string GetBlockDescription(uint64_t interval); 57 std::string name; 58 Task task; 59 TimeOutCallback timeOutCallback; 60 std::shared_ptr<HandlerChecker> checker; 61 uint64_t checkInterval; 62 uint64_t nextTickTime; 63 bool isTaskScheduled; 64 bool isOneshotTask; 65 }; 66 } // end of namespace HiviewDFX 67 } // end of namespace OHOS 68 #endif 69