1049e185fSopenharmony_ci/*
2049e185fSopenharmony_ci * Copyright (C) 2022 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 PLAYER_XCOLLIE_H
17049e185fSopenharmony_ci#define PLAYER_XCOLLIE_H
18049e185fSopenharmony_ci
19049e185fSopenharmony_ci#include <string>
20049e185fSopenharmony_ci#include <map>
21049e185fSopenharmony_ci#include <mutex>
22049e185fSopenharmony_ci
23049e185fSopenharmony_cinamespace OHOS {
24049e185fSopenharmony_cinamespace Media {
25049e185fSopenharmony_ciclass __attribute__((visibility("default"))) PlayerXCollie {
26049e185fSopenharmony_cipublic:
27049e185fSopenharmony_ci    static PlayerXCollie &GetInstance();
28049e185fSopenharmony_ci    int32_t SetTimer(const std::string &name, bool recovery = false, uint32_t timeout = 10); // 10s
29049e185fSopenharmony_ci    int32_t SetTimerByLog(const std::string &name, uint32_t timeout = 10); // 10s
30049e185fSopenharmony_ci    void CancelTimer(int32_t id);
31049e185fSopenharmony_ci    int32_t Dump(int32_t fd);
32049e185fSopenharmony_ci    constexpr static uint32_t timerTimeout = 10;
33049e185fSopenharmony_ciprivate:
34049e185fSopenharmony_ci    PlayerXCollie() = default;
35049e185fSopenharmony_ci    ~PlayerXCollie();
36049e185fSopenharmony_ci    void TimerCallback(void *data);
37049e185fSopenharmony_ci
38049e185fSopenharmony_ci    std::mutex mutex_;
39049e185fSopenharmony_ci    std::map<int32_t, std::string> dfxDumper_;
40049e185fSopenharmony_ci    uint32_t threadDeadlockCount_ = 0;
41049e185fSopenharmony_ci};
42049e185fSopenharmony_ci
43049e185fSopenharmony_ciclass __attribute__((visibility("hidden"))) XcollieTimer {
44049e185fSopenharmony_cipublic:
45049e185fSopenharmony_ci    XcollieTimer(const std::string &name, bool recovery = false, uint32_t timeout = 10)
46049e185fSopenharmony_ci    {
47049e185fSopenharmony_ci        id_ = PlayerXCollie::GetInstance().SetTimer(name, recovery, timeout);
48049e185fSopenharmony_ci    };
49049e185fSopenharmony_ci
50049e185fSopenharmony_ci    XcollieTimer(const std::string &name, uint32_t timeout)
51049e185fSopenharmony_ci    {
52049e185fSopenharmony_ci        id_ = PlayerXCollie::GetInstance().SetTimerByLog(name, timeout);
53049e185fSopenharmony_ci    }
54049e185fSopenharmony_ci
55049e185fSopenharmony_ci    ~XcollieTimer()
56049e185fSopenharmony_ci    {
57049e185fSopenharmony_ci        PlayerXCollie::GetInstance().CancelTimer(id_);
58049e185fSopenharmony_ci    }
59049e185fSopenharmony_ciprivate:
60049e185fSopenharmony_ci    int32_t id_ = 0;
61049e185fSopenharmony_ci};
62049e185fSopenharmony_ci
63049e185fSopenharmony_ci#define LISTENER(statement, args...) { OHOS::Media::XcollieTimer xCollie(args); statement; }
64049e185fSopenharmony_ci} // namespace Media
65049e185fSopenharmony_ci} // namespace OHOS
66049e185fSopenharmony_ci#endif