1 /*
2  * Copyright (C) 2023 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 WATCHDOG_H
17 #define WATCHDOG_H
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include "media_dfx.h"
22 #include "media_log.h"
23 
24 namespace OHOS {
25 namespace Media {
26 /**
27  * More than timeoutMs_ If Notify() is not used to trigger the dog feeding action within, the Alarm() action
28  * will be triggered. When notify() is restored, the AlarmRecovery() action will be triggered.
29  * See interface details for specific usage.
30  */
31 class __attribute__((visibility("default"))) WatchDog {
32 public:
33     WatchDog() = default;
WatchDog(uint32_t timeoutMs)34     explicit WatchDog(uint32_t timeoutMs) : timeoutMs_(timeoutMs) {};
35     ~WatchDog();
36 
37     /**
38      * Create a listening thread. Repeated calls do not take effect.
39      */
40     void EnableWatchDog();
41 
42     /**
43      * End and destroy the listening thread.
44      */
45     void DisableWatchDog();
46 
47     /**
48      * The listening thread enters the paused state (semaphore waiting).
49      */
50     void PauseWatchDog();
51 
52     /**
53      * The listening thread resumes running and starts a new round of timeout waiting.
54      */
55     void ResumeWatchDog();
56 
57     /**
58      * Watchdog feeding action.
59      * It needs to be called regularly within the timeoutMs_ time, otherwise Alarm() will be triggered.
60      * When the watchdog recovers, AlarmRecovery() will be triggered.
61      */
62     void Notify();
63 
64     /**
65      * This event will be triggered when the watchdog times out. A timeout will only be triggered once.
66      * Please inherit and override the interface.
67      */
68     virtual void Alarm();
69 
70     /**
71      * This event will be triggered when the watchdog is restored.
72      */
73     virtual void AlarmRecovery();
74 
75     /**
76      * The thread used to monitor the watchdog.
77      * The watchdog timeout will trigger the Alarm() action and enter the pause state,
78      * until Notify() triggers the AlarmRecovery() again, and then continue to run.
79      */
80     void WatchDogThread();
81 
82 private:
83     std::atomic<bool> disabling = false;
84     bool enable_ = false;
85     bool pause_ = false;
86     bool paused_ = false;
87     bool alarmed_ = false;
88     uint32_t timeoutMs_ = 1000; // Default 1000ms.
89     uint32_t count_ = 0;
90     std::condition_variable cond_;
91     std::mutex mutex_;
92     std::unique_ptr<std::thread> thread_;
93 };
94 } // namespace Media
95 } // namespace OHOS
96 
97 #endif
98