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 bool IsUsed(); 37 38 /** 39 * Acquires a runninglock. 40 * The parameter of last called will replace that of the previous called, and the effective parameter will 41 * be that of the last called. Eg : If call Lock(n) first then Lock(), a lasting lock will be hold, and 42 * need UnLock() to be called to release it. If else call Lock() first and then Lock(n), the parameter 'n' 43 * takes effect and it will be released in n ms automatiocly. 44 * @param timeOutMs timeOutMs this runninglock will be released in timeOutMs milliseconds. If it is called without 45 * parameter or parameter is 0 a lasting runninglock will be hold. 46 */ 47 ErrCode Lock(int32_t timeOutMs = -1); 48 49 /** 50 * Release the runninglock, no matter how many times Lock() was called. 51 * The release can be done by calling UnLock() only once. 52 */ 53 ErrCode UnLock(); 54 55 static constexpr uint32_t MAX_NAME_LEN = 256; 56 static constexpr uint32_t CREATE_WITH_SCREEN_ON = 0x10000000; 57 58 private: 59 PowerErrors Create(); 60 void Release(); 61 std::mutex mutex_; 62 RunningLockInfo runningLockInfo_; 63 sptr<IRemoteObject> token_; 64 wptr<IPowerMgr> proxy_; 65 bool CheckUsedNoLock(); 66 }; 67 } // namespace PowerMgr 68 } // namespace OHOS 69 #endif // POWERMGR_RUNNING_LOCK_H 70