1 /*
2  * Copyright (c) 2021 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 HOS_CAMERA_WATCHDOG_H
17 #define HOS_CAMERA_WATCHDOG_H
18 
19 #include <condition_variable>
20 #include <memory>
21 #include <mutex>
22 #include <thread>
23 
24 namespace OHOS::Camera {
25 class WatchDog {
26 public:
27     WatchDog();
28     void Init(int ms, std::function<void()> executor, bool isKill = false);
29     ~WatchDog();
30 
31 private:
32     void WaitForWakeUP();
33     void KillProcess();
34 
35 private:
36     int timeMs_ = 0;
37     std::function<void()> executor_ = nullptr;
38     std::condition_variable cv_;
39     std::mutex lock_;
40     std::unique_ptr<std::thread> handleThread_ = nullptr;
41     bool terminate_ = false;
42     bool isKill_ = false;
43 };
44 
45 #define WATCHDOG_TIMEOUT 10000
46 
47 #define PLACE_A_WATCHDOG(t, f, k) do { \
48     WatchDog _dog;                \
49     _dog.Init(t, f, k); \
50 } while (0)
51 
52 #define PLACE_A_WATCHDOG_DEFAULT_TIME(f, k) PLACE_A_WATCHDOG(WATCHDOG_TIMEOUT, f, k)
53 #define PLACE_A_SELFKILL_WATCHDOG           PLACE_A_WATCHDOG_DEFAULT_TIME(nullptr, true)
54 #define PLACE_A_NOKILL_WATCHDOG(f)          PLACE_A_WATCHDOG_DEFAULT_TIME(f, false);
55 } // namespace OHOS::Camera
56 
57 #endif
58