1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
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 MONITOR_SERVICE_H
17 #define MONITOR_SERVICE_H
18 
19 #include <list>
20 #include <map>
21 #include <thread>
22 #include "monitor_server_object.h"
23 #include "nocopyable.h"
24 
25 namespace OHOS {
26 namespace Media {
27 class MonitorServer : public NoCopyable {
28 public:
29     MonitorServer();
30     ~MonitorServer();
31 
32     static MonitorServer &GetInstance();
33     int32_t Click(int32_t pid);
34     int32_t EnableMonitor(int32_t pid);
35     int32_t DisableMonitor(int32_t pid);
36 
37     int32_t RegisterObj(int32_t pid, wptr<MonitorServerObject> obj);
38     int32_t CancellationObj(int32_t pid, wptr<MonitorServerObject> obj);
39 
40     int32_t OnClientDie(int32_t pid);
41     int32_t Dump(int32_t fd, bool needDetail);
42 
43 private:
44     uint64_t GetTimeMS();
45     void MonitorThread();
46     int32_t GetWaitTime();
47     int32_t GetObjListByPid(int32_t pid, std::list<wptr<MonitorServerObject>> &list);
48     int32_t ObjCtrl(std::list<wptr<MonitorServerObject>> &recoveryList,
49         std::list<wptr<MonitorServerObject>> &abnormalList);
50 
51     struct TimeInfo {
TimeInfoTimeInfo52         TimeInfo(int32_t t, bool flag)
53             : time(t), triggerFlag(flag), alarmed(false)
54         {
55         }
56         int32_t time;
57         bool triggerFlag;
58         bool alarmed;
59     };
60 
61     bool waitingAgain_ = false;
62     std::map<int32_t, std::list<wptr<MonitorServerObject>>> objListMap_;
63     std::multimap<int32_t, TimeInfo> timesMap_;
64     std::mutex mutex_;
65     std::mutex thredMutex_;
66     std::unique_ptr<std::thread> thread_ = nullptr;
67     bool threadRunning_ = false;
68     std::condition_variable cond_;
69 };
70 } // namespace Media
71 } // namespace OHOS
72 #endif // MONITOR_SERVICE_H
73