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 AVCODEC_XCOLLIE_H
17 #define AVCODEC_XCOLLIE_H
18 
19 #include <string>
20 #include <map>
21 #include <mutex>
22 
23 namespace OHOS {
24 namespace MediaAVCodec {
25 class __attribute__((visibility("default"))) AVCodecXCollie {
26 public:
27     static AVCodecXCollie &GetInstance();
28     uint64_t SetTimer(const std::string &name, bool isService, bool recovery, uint32_t timeout);
29     void CancelTimer(uint64_t index);
30     int32_t Dump(int32_t fd);
31     constexpr static uint32_t timerTimeout = 10;
32 private:
33     AVCodecXCollie() = default;
34     ~AVCodecXCollie() = default;
35     void ServiceTimerCallback(void *data);
36     void ClientTimerCallback(void *data);
37 
38     std::mutex mutex_;
39     uint64_t dumperIndex_ = 1;
40     std::map<int32_t, std::pair<int32_t, std::string>> dfxDumper_;
41     uint32_t threadDeadlockCount_ = 0;
42 };
43 
44 class __attribute__((visibility("hidden"))) AVCodecXcollieTimer {
45 public:
46     AVCodecXcollieTimer(const std::string &name, bool isService = true,
47         bool recovery = false, uint32_t timeout = 30)
48     {
49         index_ = AVCodecXCollie::GetInstance().SetTimer(name, isService, recovery, timeout);
50     };
51 
~AVCodecXcollieTimer()52     ~AVCodecXcollieTimer()
53     {
54         AVCodecXCollie::GetInstance().CancelTimer(index_);
55     }
56 private:
57     uint64_t index_ = 0;
58 };
59 
60 #define COLLIE_LISTEN(statement, args...) { AVCodecXcollieTimer xCollie(args); statement; }
61 #define CLIENT_COLLIE_LISTEN(statement, name) { AVCodecXcollieTimer xCollie(name, false, false, 30); statement; }
62 } // namespace MediaAVCodec
63 } // namespace OHOS
64 #endif // AVCODEC_XCOLLIE_H