1049e185fSopenharmony_ci/* 2049e185fSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd. 3049e185fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4049e185fSopenharmony_ci * you may not use this file except in compliance with the License. 5049e185fSopenharmony_ci * You may obtain a copy of the License at 6049e185fSopenharmony_ci * 7049e185fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8049e185fSopenharmony_ci * 9049e185fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10049e185fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11049e185fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12049e185fSopenharmony_ci * See the License for the specific language governing permissions and 13049e185fSopenharmony_ci * limitations under the License. 14049e185fSopenharmony_ci */ 15049e185fSopenharmony_ci 16049e185fSopenharmony_ci#ifndef WATCHDOG_H 17049e185fSopenharmony_ci#define WATCHDOG_H 18049e185fSopenharmony_ci 19049e185fSopenharmony_ci#include <atomic> 20049e185fSopenharmony_ci#include <condition_variable> 21049e185fSopenharmony_ci#include "media_dfx.h" 22049e185fSopenharmony_ci#include "media_log.h" 23049e185fSopenharmony_ci 24049e185fSopenharmony_cinamespace OHOS { 25049e185fSopenharmony_cinamespace Media { 26049e185fSopenharmony_ci/** 27049e185fSopenharmony_ci * More than timeoutMs_ If Notify() is not used to trigger the dog feeding action within, the Alarm() action 28049e185fSopenharmony_ci * will be triggered. When notify() is restored, the AlarmRecovery() action will be triggered. 29049e185fSopenharmony_ci * See interface details for specific usage. 30049e185fSopenharmony_ci */ 31049e185fSopenharmony_ciclass __attribute__((visibility("default"))) WatchDog { 32049e185fSopenharmony_cipublic: 33049e185fSopenharmony_ci WatchDog() = default; 34049e185fSopenharmony_ci explicit WatchDog(uint32_t timeoutMs) : timeoutMs_(timeoutMs) {}; 35049e185fSopenharmony_ci ~WatchDog(); 36049e185fSopenharmony_ci 37049e185fSopenharmony_ci /** 38049e185fSopenharmony_ci * Create a listening thread. Repeated calls do not take effect. 39049e185fSopenharmony_ci */ 40049e185fSopenharmony_ci void EnableWatchDog(); 41049e185fSopenharmony_ci 42049e185fSopenharmony_ci /** 43049e185fSopenharmony_ci * End and destroy the listening thread. 44049e185fSopenharmony_ci */ 45049e185fSopenharmony_ci void DisableWatchDog(); 46049e185fSopenharmony_ci 47049e185fSopenharmony_ci /** 48049e185fSopenharmony_ci * The listening thread enters the paused state (semaphore waiting). 49049e185fSopenharmony_ci */ 50049e185fSopenharmony_ci void PauseWatchDog(); 51049e185fSopenharmony_ci 52049e185fSopenharmony_ci /** 53049e185fSopenharmony_ci * The listening thread resumes running and starts a new round of timeout waiting. 54049e185fSopenharmony_ci */ 55049e185fSopenharmony_ci void ResumeWatchDog(); 56049e185fSopenharmony_ci 57049e185fSopenharmony_ci /** 58049e185fSopenharmony_ci * Watchdog feeding action. 59049e185fSopenharmony_ci * It needs to be called regularly within the timeoutMs_ time, otherwise Alarm() will be triggered. 60049e185fSopenharmony_ci * When the watchdog recovers, AlarmRecovery() will be triggered. 61049e185fSopenharmony_ci */ 62049e185fSopenharmony_ci void Notify(); 63049e185fSopenharmony_ci 64049e185fSopenharmony_ci /** 65049e185fSopenharmony_ci * This event will be triggered when the watchdog times out. A timeout will only be triggered once. 66049e185fSopenharmony_ci * Please inherit and override the interface. 67049e185fSopenharmony_ci */ 68049e185fSopenharmony_ci virtual void Alarm(); 69049e185fSopenharmony_ci 70049e185fSopenharmony_ci /** 71049e185fSopenharmony_ci * This event will be triggered when the watchdog is restored. 72049e185fSopenharmony_ci */ 73049e185fSopenharmony_ci virtual void AlarmRecovery(); 74049e185fSopenharmony_ci 75049e185fSopenharmony_ci /** 76049e185fSopenharmony_ci * The thread used to monitor the watchdog. 77049e185fSopenharmony_ci * The watchdog timeout will trigger the Alarm() action and enter the pause state, 78049e185fSopenharmony_ci * until Notify() triggers the AlarmRecovery() again, and then continue to run. 79049e185fSopenharmony_ci */ 80049e185fSopenharmony_ci void WatchDogThread(); 81049e185fSopenharmony_ci 82049e185fSopenharmony_ciprivate: 83049e185fSopenharmony_ci std::atomic<bool> disabling = false; 84049e185fSopenharmony_ci bool enable_ = false; 85049e185fSopenharmony_ci bool pause_ = false; 86049e185fSopenharmony_ci bool paused_ = false; 87049e185fSopenharmony_ci bool alarmed_ = false; 88049e185fSopenharmony_ci uint32_t timeoutMs_ = 1000; // Default 1000ms. 89049e185fSopenharmony_ci uint32_t count_ = 0; 90049e185fSopenharmony_ci std::condition_variable cond_; 91049e185fSopenharmony_ci std::mutex mutex_; 92049e185fSopenharmony_ci std::unique_ptr<std::thread> thread_; 93049e185fSopenharmony_ci}; 94049e185fSopenharmony_ci} // namespace Media 95049e185fSopenharmony_ci} // namespace OHOS 96049e185fSopenharmony_ci 97049e185fSopenharmony_ci#endif 98