• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "hdf_base.h"
18 #include "system_operation.h"
19 #include "power_hdf_log.h"
20 
21 namespace OHOS {
22 namespace HDI {
23 namespace Power {
24 namespace V1_2 {
Increase(const RunningLockInfo & info)25 int32_t RunningLockCounter::Increase(const RunningLockInfo &info)
26 {
27     auto iterator = runninglockInfos_.find(info.name);
28     if (iterator != runninglockInfos_.end()) {
29         if (info.timeoutMs < 0) {
30             // Lock counter increase failed, runninglock is exist and timeout < 0
31             HDF_LOGW("LCIF,N=%{public}s exist", info.name.c_str());
32             return HDF_FAILURE;
33         }
34         iterator->second.timeoutMs = info.timeoutMs;
35         return HDF_SUCCESS;
36     }
37     ++counter_;
38     if (counter_ == 1) {
39         SystemOperation::WriteWakeLock(tag_);
40     }
41     runninglockInfos_.emplace(info.name, info);
42     return HDF_SUCCESS;
43 }
44 
Decrease(const RunningLockInfo & info)45 int32_t RunningLockCounter::Decrease(const RunningLockInfo &info)
46 {
47     auto iterator = runninglockInfos_.find(info.name);
48     if (iterator == runninglockInfos_.end()) {
49         // Runninglock is not exist, no need to decrease lock counter
50         HDF_LOGW("RL N=%{public}s no exist", 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 
Clean()61 void RunningLockCounter::Clean()
62 {
63     runninglockInfos_.clear();
64 }
65 
66 } // namespace V1_2
67 } // namespace Power
68 } // namespace HDI
69 } // namespace OHOS
70