1 /* 2 * Copyright (C) 2021-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 #ifndef POWERMGR_RUNNING_LOCK_H 17 #define POWERMGR_RUNNING_LOCK_H 18 19 #include <memory> 20 #include <mutex> 21 22 #include <iremote_object.h> 23 24 #include "ipower_mgr.h" 25 #include "running_lock_info.h" 26 27 namespace OHOS { 28 namespace PowerMgr { 29 class RunningLock { 30 public: 31 RunningLock(const wptr<IPowerMgr>& proxy, const std::string& name, RunningLockType type); 32 ~RunningLock(); 33 DISALLOW_COPY_AND_MOVE(RunningLock); 34 35 PowerErrors Init(); 36 PowerErrors Recover(const wptr<IPowerMgr>& proxy); 37 bool IsUsed(); 38 39 /** 40 * Acquires a runninglock. 41 * The parameter of last called will replace that of the previous called, and the effective parameter will 42 * be that of the last called. Eg : If call Lock(n) first then Lock(), a lasting lock will be hold, and 43 * need UnLock() to be called to release it. If else call Lock() first and then Lock(n), the parameter 'n' 44 * takes effect and it will be released in n ms automatiocly. 45 * @param timeOutMs timeOutMs this runninglock will be released in timeOutMs milliseconds. If it is called without 46 * parameter or parameter is 0 a lasting runninglock will be hold. 47 */ 48 ErrCode Lock(int32_t timeOutMs = -1); 49 50 /** 51 * Release the runninglock, no matter how many times Lock() was called. 52 * The release can be done by calling UnLock() only once. 53 */ 54 ErrCode UnLock(); 55 56 static constexpr uint32_t MAX_NAME_LEN = 256; 57 static constexpr uint32_t CREATE_WITH_SCREEN_ON = 0x10000000; 58 59 private: 60 PowerErrors Create(); 61 void Release(); 62 std::mutex mutex_; 63 RunningLockInfo runningLockInfo_; 64 sptr<IRemoteObject> token_; 65 wptr<IPowerMgr> proxy_; 66 bool CheckUsedNoLock(); 67 }; 68 } // namespace PowerMgr 69 } // namespace OHOS 70 #endif // POWERMGR_RUNNING_LOCK_H 71