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