1 /*
2  * Copyright (c) 2024 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 AUDIO_RUNNING_LOCK_MANAGER_H
17 #define AUDIO_RUNNING_LOCK_MANAGER_H
18 
19 #include <unordered_set>
20 #include <mutex>
21 #include <vector>
22 #include "audio_errors.h"
23 #include "audio_hdi_log.h"
24 #include "audio_utils.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 template<typename T>
29 class AudioRunningLockManager {
30 public:
AudioRunningLockManager(std::shared_ptr<T> runningLock)31     explicit AudioRunningLockManager(std::shared_ptr<T> runningLock) : runningLock_(runningLock)
32     {
33     }
34 
Lock(const int32_t TimeoutMs)35     auto Lock(const int32_t TimeoutMs)
36     {
37         Trace traceLock("AudioRunningLockManager:Lock");
38         std::lock_guard<std::mutex> lock(mutex_);
39         lastAppsUid_ = {};
40 
41         Trace traceRunningLock("AudioRunningLockManager:runningLock_->Lock");
42         WatchTimeout guard("PowerMgr Lock timeout");
43         auto ret = runningLock_->Lock(TimeoutMs);
44         guard.CheckCurrTimeout();
45         isLocked_ = true;
46         AUDIO_INFO_LOG("Lock runninglock, ret: %{public}d", ret);
47         return ret;
48     }
49 
UnLock()50     auto UnLock()
51     {
52         AUDIO_INFO_LOG("AudioRunningLockManager::UnLock in");
53         Trace traceUnlock("AudioRunningLockManager:UnLock");
54         std::lock_guard<std::mutex> lock(mutex_);
55         isLocked_ = false;
56         currentAppsUid_ = {};
57         lastAppsUid_ = {};
58 
59         Trace traceUpdateWorkSource("AudioRunningLockManager:runningLock_->UpdateWorkSource");
60         auto ret = runningLock_->UpdateWorkSource({});
61         AUDIO_INFO_LOG("UpdateWorkSource ret: %{public}d", ret);
62         Trace traceRunningUnlock("AudioRunningLockManager:runningLock_->UnLock");
63         ret = runningLock_->UnLock();
64         AUDIO_INFO_LOG("Unlock runninglock, ret: %{public}d", ret);
65         return ret;
66     }
67 
68     template<typename U>
UpdateAppsUid(const U & itBegin,const U & itEnd)69     int32_t UpdateAppsUid(const U &itBegin, const U &itEnd)
70     {
71         Trace trace("AudioRunningLockManager:UpdateAppsUid");
72         std::lock_guard<std::mutex> lock(mutex_);
73         std::unordered_set<int32_t> appsUidSet(itBegin, itEnd);
74 
75         currentAppsUid_ = std::move(appsUidSet);
76         return SUCCESS;
77     }
78 
UpdateAppsUidToPowerMgr()79     int32_t UpdateAppsUidToPowerMgr()
80     {
81         Trace trace("AudioRunningLockManager:UpdateAppsUidToPowerMgr");
82         std::lock_guard<std::mutex> lock(mutex_);
83         if (!isLocked_) {
84             return SUCCESS;
85         }
86         std::vector<int32_t> appsUid;
87         if (currentAppsUid_ == lastAppsUid_) {
88             return SUCCESS;
89         }
90         lastAppsUid_ = currentAppsUid_;
91         appsUid.insert(appsUid.end(), currentAppsUid_.begin(), currentAppsUid_.end());
92 
93         std::string appsUidInfo;
94         for (auto uid : appsUid) {
95             appsUidInfo += (std::to_string(uid) + ',');
96         }
97 
98         Trace traceUpdateWorkSource("AudioRunningLockManager:runningLock_->UpdateWorkSource");
99         auto ret = runningLock_->UpdateWorkSource(appsUid);
100         AUDIO_INFO_LOG("UpdateWorkSource size: %{public}zu [%{public}s], ret: %{public}d",
101             appsUid.size(), appsUidInfo.c_str(), ret);
102         return ret;
103     }
104 
105 private:
106     std::shared_ptr<T> runningLock_ = nullptr;
107     std::mutex mutex_;
108     std::unordered_set<int32_t> currentAppsUid_;
109     std::unordered_set<int32_t> lastAppsUid_;
110     std::atomic<bool> isLocked_ = false;
111 };
112 
113 }  // namespace AudioStandard
114 }  // namespace OHOS
115 
116 #endif // AUDIO_RUNNING_LOCK_MANAGER_H