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 using XCollieCallback = std::function<void (void *)>; 28 namespace OHOS { 29 namespace HiviewDFX { 30 class WatchdogTask { 31 static int64_t curId; 32 public: 33 WatchdogTask(std::string name, std::shared_ptr<AppExecFwk::EventHandler> handler, 34 TimeOutCallback timeOutCallback, uint64_t interval); 35 WatchdogTask(std::string name, Task&& task, uint64_t delay, uint64_t interval, bool isOneshot); 36 WatchdogTask(std::string name, unsigned int timeout, XCollieCallback func, void *arg, unsigned int flag); WatchdogTask()37 WatchdogTask() 38 : name(""), 39 task(nullptr), 40 timeOutCallback(nullptr), 41 checker(nullptr), 42 checkInterval(0), 43 nextTickTime(0), 44 isTaskScheduled(false), 45 isOneshotTask(false) {}; ~WatchdogTask()46 ~WatchdogTask() {}; 47 48 bool operator<(const WatchdogTask &obj) const 49 { 50 // as we use std::priority_queue, the event with smaller target time will be in the top of the queue 51 return (this->nextTickTime > obj.nextTickTime); 52 } 53 54 void Run(uint64_t now); 55 void RunHandlerCheckerTask(); 56 void SendEvent(const std::string &msg, const std::string &eventName) const; 57 void SendXCollieEvent(const std::string &timerName, const std::string &keyMsg) const; 58 void DoCallback(); 59 60 int EvaluateCheckerState(); 61 std::string GetBlockDescription(uint64_t interval); 62 std::string name; 63 Task task; 64 TimeOutCallback timeOutCallback; 65 std::shared_ptr<HandlerChecker> checker; 66 int64_t id; 67 uint64_t timeout; 68 XCollieCallback func; 69 void *arg; 70 unsigned int flag; 71 uint64_t checkInterval; 72 uint64_t nextTickTime; 73 bool isTaskScheduled; 74 bool isOneshotTask; 75 }; 76 } // end of namespace HiviewDFX 77 } // end of namespace OHOS 78 #endif 79