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