• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         auto ret = runningLock_->Lock(TimeoutMs);
43         isLocked_ = true;
44         AUDIO_INFO_LOG("Lock runninglock, ret: %{public}d", ret);
45         return ret;
46     }
47 
UnLock()48     auto UnLock()
49     {
50         AUDIO_INFO_LOG("AudioRunningLockManager::UnLock in");
51         Trace traceUnlock("AudioRunningLockManager:UnLock");
52         std::lock_guard<std::mutex> lock(mutex_);
53         isLocked_ = false;
54         currentAppsUid_ = {};
55         lastAppsUid_ = {};
56 
57         Trace traceUpdateWorkSource("AudioRunningLockManager:runningLock_->UpdateWorkSource");
58         auto ret = runningLock_->UpdateWorkSource({});
59         AUDIO_INFO_LOG("UpdateWorkSource ret: %{public}d", ret);
60         Trace traceRunningUnlock("AudioRunningLockManager:runningLock_->UnLock");
61         ret = runningLock_->UnLock();
62         AUDIO_INFO_LOG("Unlock runninglock, ret: %{public}d", ret);
63         return ret;
64     }
65 
66     template<typename U>
UpdateAppsUid(const U & itBegin,const U & itEnd)67     int32_t UpdateAppsUid(const U &itBegin, const U &itEnd)
68     {
69         Trace trace("AudioRunningLockManager:UpdateAppsUid");
70         std::lock_guard<std::mutex> lock(mutex_);
71         std::unordered_set<int32_t> appsUidSet(itBegin, itEnd);
72 
73         currentAppsUid_ = std::move(appsUidSet);
74         return SUCCESS;
75     }
76 
UpdateAppsUidToPowerMgr()77     int32_t UpdateAppsUidToPowerMgr()
78     {
79         Trace trace("AudioRunningLockManager:UpdateAppsUidToPowerMgr");
80         std::lock_guard<std::mutex> lock(mutex_);
81         if (!isLocked_) {
82             return SUCCESS;
83         }
84         std::vector<int32_t> appsUid;
85         if (currentAppsUid_ == lastAppsUid_) {
86             return SUCCESS;
87         }
88         lastAppsUid_ = currentAppsUid_;
89         appsUid.insert(appsUid.end(), currentAppsUid_.begin(), currentAppsUid_.end());
90 
91         std::string appsUidInfo;
92         for (auto uid : appsUid) {
93             appsUidInfo += (std::to_string(uid) + ',');
94         }
95 
96         Trace traceUpdateWorkSource("AudioRunningLockManager:runningLock_->UpdateWorkSource");
97         auto ret = runningLock_->UpdateWorkSource(appsUid);
98         AUDIO_INFO_LOG("UpdateWorkSource size: %{public}zu [%{public}s], ret: %{public}d",
99             appsUid.size(), appsUidInfo.c_str(), ret);
100         return ret;
101     }
102 
103 private:
104     std::shared_ptr<T> runningLock_ = nullptr;
105     std::mutex mutex_;
106     std::unordered_set<int32_t> currentAppsUid_;
107     std::unordered_set<int32_t> lastAppsUid_;
108     std::atomic<bool> isLocked_ = false;
109 };
110 
111 }  // namespace AudioStandard
112 }  // namespace OHOS
113 
114 #endif // AUDIO_RUNNING_LOCK_MANAGER_H