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 #define HST_LOG_TAG "AutoLock" 17 #include <memory> 18 #include "osal/task/autolock.h" 19 #include <utility> 20 namespace OHOS { 21 namespace Media { AutoLock(Mutex & mutex)22AutoLock::AutoLock(Mutex& mutex) : mutex_(&mutex) 23 { 24 mutex_->lock(); 25 } 26 ~AutoLock()27AutoLock::~AutoLock() 28 { 29 if (mutex_ != nullptr) { 30 mutex_->unlock(); 31 mutex_ = nullptr; 32 } 33 } 34 AutoLock(AutoLock && other)35AutoLock::AutoLock(AutoLock&& other) noexcept 36 { 37 mutex_ = nullptr; 38 *this = std::move(other); 39 } 40 41 operator =(AutoLock && other)42AutoLock& AutoLock::operator=(AutoLock&& other) noexcept 43 { 44 if (this != &other) { 45 if (mutex_) { 46 mutex_->unlock(); 47 } 48 mutex_ = other.mutex_; 49 other.mutex_ = nullptr; 50 } 51 return *this; 52 } 53 } // namespace Media 54 } // namespace OHOS