1da853ecaSopenharmony_ci/*
2da853ecaSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3da853ecaSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4da853ecaSopenharmony_ci * you may not use this file except in compliance with the License.
5da853ecaSopenharmony_ci * You may obtain a copy of the License at
6da853ecaSopenharmony_ci *
7da853ecaSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8da853ecaSopenharmony_ci *
9da853ecaSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10da853ecaSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11da853ecaSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12da853ecaSopenharmony_ci * See the License for the specific language governing permissions and
13da853ecaSopenharmony_ci * limitations under the License.
14da853ecaSopenharmony_ci */
15da853ecaSopenharmony_ci
16da853ecaSopenharmony_ci#ifndef AVCODEC_XCOLLIE_H
17da853ecaSopenharmony_ci#define AVCODEC_XCOLLIE_H
18da853ecaSopenharmony_ci
19da853ecaSopenharmony_ci#include <string>
20da853ecaSopenharmony_ci#include <map>
21da853ecaSopenharmony_ci#include <mutex>
22da853ecaSopenharmony_ci
23da853ecaSopenharmony_cinamespace OHOS {
24da853ecaSopenharmony_cinamespace MediaAVCodec {
25da853ecaSopenharmony_ciclass __attribute__((visibility("default"))) AVCodecXCollie {
26da853ecaSopenharmony_cipublic:
27da853ecaSopenharmony_ci    static AVCodecXCollie &GetInstance();
28da853ecaSopenharmony_ci    uint64_t SetTimer(const std::string &name, bool isService, bool recovery, uint32_t timeout);
29da853ecaSopenharmony_ci    void CancelTimer(uint64_t index);
30da853ecaSopenharmony_ci    int32_t Dump(int32_t fd);
31da853ecaSopenharmony_ci    constexpr static uint32_t timerTimeout = 10;
32da853ecaSopenharmony_ciprivate:
33da853ecaSopenharmony_ci    AVCodecXCollie() = default;
34da853ecaSopenharmony_ci    ~AVCodecXCollie() = default;
35da853ecaSopenharmony_ci    void ServiceTimerCallback(void *data);
36da853ecaSopenharmony_ci    void ClientTimerCallback(void *data);
37da853ecaSopenharmony_ci
38da853ecaSopenharmony_ci    std::mutex mutex_;
39da853ecaSopenharmony_ci    uint64_t dumperIndex_ = 1;
40da853ecaSopenharmony_ci    std::map<int32_t, std::pair<int32_t, std::string>> dfxDumper_;
41da853ecaSopenharmony_ci    uint32_t threadDeadlockCount_ = 0;
42da853ecaSopenharmony_ci};
43da853ecaSopenharmony_ci
44da853ecaSopenharmony_ciclass __attribute__((visibility("hidden"))) AVCodecXcollieTimer {
45da853ecaSopenharmony_cipublic:
46da853ecaSopenharmony_ci    AVCodecXcollieTimer(const std::string &name, bool isService = true,
47da853ecaSopenharmony_ci        bool recovery = false, uint32_t timeout = 30)
48da853ecaSopenharmony_ci    {
49da853ecaSopenharmony_ci        index_ = AVCodecXCollie::GetInstance().SetTimer(name, isService, recovery, timeout);
50da853ecaSopenharmony_ci    };
51da853ecaSopenharmony_ci
52da853ecaSopenharmony_ci    ~AVCodecXcollieTimer()
53da853ecaSopenharmony_ci    {
54da853ecaSopenharmony_ci        AVCodecXCollie::GetInstance().CancelTimer(index_);
55da853ecaSopenharmony_ci    }
56da853ecaSopenharmony_ciprivate:
57da853ecaSopenharmony_ci    uint64_t index_ = 0;
58da853ecaSopenharmony_ci};
59da853ecaSopenharmony_ci
60da853ecaSopenharmony_ci#define COLLIE_LISTEN(statement, args...) { AVCodecXcollieTimer xCollie(args); statement; }
61da853ecaSopenharmony_ci#define CLIENT_COLLIE_LISTEN(statement, name) { AVCodecXcollieTimer xCollie(name, false, false, 30); statement; }
62da853ecaSopenharmony_ci} // namespace MediaAVCodec
63da853ecaSopenharmony_ci} // namespace OHOS
64da853ecaSopenharmony_ci#endif // AVCODEC_XCOLLIE_H