• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 POWERMGR_IRUNNING_LOCK_ACTION_H
17 #define POWERMGR_IRUNNING_LOCK_ACTION_H
18 
19 #include <array>
20 #include <string>
21 
22 #include "power_common.h"
23 #include "running_lock_info.h"
24 
25 namespace OHOS {
26 namespace PowerMgr {
27 class IRunningLockAction {
28 public:
29     IRunningLockAction() = default;
30     virtual ~IRunningLockAction() = default;
31 
GetLockTag(RunningLockType type)32     static inline std::string GetLockTag(RunningLockType type)
33     {
34         return LOCK_TAGS[ToUnderlying(type)];
35     }
36 
37     virtual void Acquire(RunningLockType type);
38     virtual void Release(RunningLockType type);
39 
40     virtual void Lock(RunningLockType type, const std::string& tag) = 0;
41     virtual void Unlock(RunningLockType type, const std::string& tag) = 0;
42 
43 private:
44     class RunningLockDesc {
45     public:
IncRef()46         inline void IncRef()
47         {
48             refCnt++;
49         }
50 
DecRef()51         inline void DecRef()
52         {
53             refCnt--;
54         }
55 
IsRefNone()56         inline bool IsRefNone() const
57         {
58             return refCnt == 0;
59         }
60 
GetRefCnt()61         inline uint32_t GetRefCnt() const
62         {
63             return refCnt;
64         }
65 
66     private:
67         uint32_t refCnt{0};
68     };
69 
IsValidType(RunningLockType type)70     static inline bool IsValidType(RunningLockType type)
71     {
72         return type < RunningLockType::RUNNINGLOCK_BUTT;
73     }
74 
75     static const std::array<std::string, ToUnderlying(RunningLockType::RUNNINGLOCK_BUTT)> LOCK_TAGS;
76 
77     std::array<RunningLockDesc, ToUnderlying(RunningLockType::RUNNINGLOCK_BUTT)> lockDescs_;
78 };
79 } // namespace PowerMgr
80 } // namespace OHOS
81 #endif // POWERMGR_IRUNNING_LOCK_ACTION_H
82