• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "ipc/critical_manager.h"
17 
18 #include "app_log_tag_wrapper.h"
19 #include "mem_mgr_client.h"
20 #include "system_ability_definition.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 namespace {
25 constexpr uint32_t DELAY_MILLI_SECONDS = 5 * 1000;
26 constexpr const char* TASK_NAME = "CriticalTask";
27 }
28 
29 std::mutex CriticalManager::mutex_;
30 
CriticalManager()31 CriticalManager::CriticalManager()
32 {
33     delayedTaskMgr_ = std::make_shared<SingleDelayedTaskMgr>(TASK_NAME, DELAY_MILLI_SECONDS);
34 }
35 
SetMemMgrStatus(bool started)36 void CriticalManager::SetMemMgrStatus(bool started)
37 {
38     std::lock_guard<std::mutex> lock(mutex_);
39     memMgrStarted_ = started;
40 }
41 
BeforeRequest()42 void CriticalManager::BeforeRequest()
43 {
44     std::lock_guard<std::mutex> lock(mutex_);
45     LOG_D(BMS_TAG_INSTALLD, "before counter: %{public}d, memmgr: %{public}d, critical: %{public}d",
46         counter_, memMgrStarted_, critical_);
47     counter_++;
48     if (memMgrStarted_ && !critical_ && counter_ > 0) {
49         LOG_I(BMS_TAG_INSTALLD, "SetCritical true");
50         Memory::MemMgrClient::GetInstance().SetCritical(
51             getpid(), true, INSTALLD_SERVICE_ID);
52         critical_ = true;
53     }
54 }
55 
AfterRequest()56 void CriticalManager::AfterRequest()
57 {
58     std::lock_guard<std::mutex> lock(mutex_);
59     LOG_D(BMS_TAG_INSTALLD, "after counter: %{public}d, memmgr: %{public}d, critical: %{public}d",
60         counter_, memMgrStarted_, critical_);
61     counter_--;
62     auto delayTask = []() {
63         std::lock_guard<std::mutex> lock(mutex_);
64         if (memMgrStarted_ && critical_ && counter_ == 0) {
65             LOG_I(BMS_TAG_INSTALLD, "SetCritical false");
66             Memory::MemMgrClient::GetInstance().SetCritical(
67                 getpid(), false, INSTALLD_SERVICE_ID);
68             critical_ = false;
69         }
70     };
71     delayedTaskMgr_->ScheduleDelayedTask(delayTask);
72 }
73 
74 }  // namespace AppExecFwk
75 }  // namespace OHOS
76