1 /*
2 * Copyright (c) 2023 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 "running_lock_counter.h"
17
18 #include "hdf_base.h"
19 #include "system_operation.h"
20 #include "hdf_log.h"
21
22 namespace OHOS {
23 namespace HDI {
24 namespace Power {
25 namespace V1_1 {
Increase(const RunningLockInfo & info)26 int32_t RunningLockCounter::Increase(const RunningLockInfo &info)
27 {
28 auto iterator = runninglockInfos_.find(info.name);
29 if (iterator != runninglockInfos_.end()) {
30 if (info.timeoutMs < 0) {
31 HDF_LOGW("Lock counter increase failed, runninglock name=%{public}s is exist and timeout < 0",
32 info.name.c_str());
33 return HDF_FAILURE;
34 }
35 iterator->second.timeoutMs = info.timeoutMs;
36 return HDF_SUCCESS;
37 }
38 ++counter_;
39 if (counter_ == 1) {
40 SystemOperation::WriteWakeLock(tag_);
41 }
42 runninglockInfos_.emplace(info.name, info);
43 return HDF_SUCCESS;
44 }
45
Decrease(const RunningLockInfo & info)46 int32_t RunningLockCounter::Decrease(const RunningLockInfo &info)
47 {
48 auto iterator = runninglockInfos_.find(info.name);
49 if (iterator == runninglockInfos_.end()) {
50 HDF_LOGW("Runninglock name=%{public}s is not exist, no need to decrease lock counter", info.name.c_str());
51 return HDF_ERR_NOT_SUPPORT;
52 }
53 --counter_;
54 if (counter_ == 0) {
55 SystemOperation::WriteWakeUnlock(tag_);
56 }
57 runninglockInfos_.erase(info.name);
58 return HDF_SUCCESS;
59 }
60 } // namespace V1_1
61 } // namespace Power
62 } // namespace HDI
63 } // namespace OHOS