1 /*
2 * Copyright (c) 2021-2022 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 "actions/irunning_lock_action.h"
17
18 #include "power_log.h"
19
20 using namespace std;
21
22 namespace OHOS {
23 namespace PowerMgr {
24 const array<string, ToUnderlying(RunningLockType::RUNNINGLOCK_BUTT)> IRunningLockAction::LOCK_TAGS {
25 "OHOSPowerMgr.Screen", "OHOSPowerMgr.Background", "OHOSPowerMgr.Proximity",
26 };
27
Acquire(RunningLockType type)28 void IRunningLockAction::Acquire(RunningLockType type)
29 {
30 if (!IsValidType(type)) {
31 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Invalid runninglock type");
32 return;
33 }
34
35 auto t = ToUnderlying(type);
36 RunningLockDesc& desc = lockDescs_[t];
37 if (desc.IsRefNone()) {
38 Lock(type, GetLockTag(type));
39 }
40 desc.IncRef();
41 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Acquire runninglock, type: %{public}u, refCnt: %{public}u", t,
42 desc.GetRefCnt());
43 }
44
Release(RunningLockType type)45 void IRunningLockAction::Release(RunningLockType type)
46 {
47 if (!IsValidType(type)) {
48 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Invalid runninglock type");
49 return;
50 }
51
52 auto t = ToUnderlying(type);
53 RunningLockDesc& desc = lockDescs_[t];
54 if (desc.IsRefNone()) {
55 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Invalid refCnt of wakelock: %{public}u", t);
56 return;
57 }
58
59 desc.DecRef();
60 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Release runninglock, type: %{public}u, refCnt: %{public}u", t,
61 desc.GetRefCnt());
62 if (desc.IsRefNone()) {
63 Unlock(type, GetLockTag(type));
64 }
65 }
66 } // namespace PowerMgr
67 } // namespace OHOS
68