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