18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci#ifndef META_API_LOCKING_H
168bf80f4bSopenharmony_ci#define META_API_LOCKING_H
178bf80f4bSopenharmony_ci
188bf80f4bSopenharmony_ci#include <meta/interface/intf_lockable.h>
198bf80f4bSopenharmony_ci
208bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE()
218bf80f4bSopenharmony_ci
228bf80f4bSopenharmony_ci/**
238bf80f4bSopenharmony_ci * @brief Helper class to acquire write lock and release it end of the scope.
248bf80f4bSopenharmony_ci */
258bf80f4bSopenharmony_ciclass InterfaceLock {
268bf80f4bSopenharmony_cipublic:
278bf80f4bSopenharmony_ci    META_NO_COPY(InterfaceLock)
288bf80f4bSopenharmony_ci
298bf80f4bSopenharmony_ci    explicit InterfaceLock(LockType type, const CORE_NS::IInterface* t) : type_(type), p_(interface_cast<ILockable>(t))
308bf80f4bSopenharmony_ci    {
318bf80f4bSopenharmony_ci        if (p_) {
328bf80f4bSopenharmony_ci            if (type_ == LockType::UNIQUE_LOCK) {
338bf80f4bSopenharmony_ci                p_->Lock();
348bf80f4bSopenharmony_ci            } else if (type_ == LockType::SHARED_LOCK) {
358bf80f4bSopenharmony_ci                p_->LockShared();
368bf80f4bSopenharmony_ci            }
378bf80f4bSopenharmony_ci        }
388bf80f4bSopenharmony_ci    }
398bf80f4bSopenharmony_ci    explicit InterfaceLock(LockType type, const CORE_NS::IInterface& t) : InterfaceLock(type, &t) {}
408bf80f4bSopenharmony_ci    template<typename Type>
418bf80f4bSopenharmony_ci    explicit InterfaceLock(LockType type, const BASE_NS::shared_ptr<Type>& t) : InterfaceLock(type, t.get())
428bf80f4bSopenharmony_ci    {}
438bf80f4bSopenharmony_ci
448bf80f4bSopenharmony_ci    InterfaceLock(InterfaceLock&& l) noexcept : type_(l.type_), p_(l.p_)
458bf80f4bSopenharmony_ci    {
468bf80f4bSopenharmony_ci        l.p_ = nullptr;
478bf80f4bSopenharmony_ci    }
488bf80f4bSopenharmony_ci
498bf80f4bSopenharmony_ci    InterfaceLock& operator=(InterfaceLock&& l) noexcept
508bf80f4bSopenharmony_ci    {
518bf80f4bSopenharmony_ci        type_ = l.type_;
528bf80f4bSopenharmony_ci        p_ = l.p_;
538bf80f4bSopenharmony_ci        l.p_ = nullptr;
548bf80f4bSopenharmony_ci        return *this;
558bf80f4bSopenharmony_ci    }
568bf80f4bSopenharmony_ci
578bf80f4bSopenharmony_ci    ~InterfaceLock()
588bf80f4bSopenharmony_ci    {
598bf80f4bSopenharmony_ci        if (p_) {
608bf80f4bSopenharmony_ci            if (type_ == LockType::UNIQUE_LOCK) {
618bf80f4bSopenharmony_ci                p_->Unlock();
628bf80f4bSopenharmony_ci            } else if (type_ == LockType::SHARED_LOCK) {
638bf80f4bSopenharmony_ci                p_->UnlockShared();
648bf80f4bSopenharmony_ci            }
658bf80f4bSopenharmony_ci        }
668bf80f4bSopenharmony_ci    }
678bf80f4bSopenharmony_ci
688bf80f4bSopenharmony_ci    explicit operator bool() const
698bf80f4bSopenharmony_ci    {
708bf80f4bSopenharmony_ci        return p_ != nullptr;
718bf80f4bSopenharmony_ci    }
728bf80f4bSopenharmony_ci
738bf80f4bSopenharmony_ciprivate:
748bf80f4bSopenharmony_ci    LockType type_;
758bf80f4bSopenharmony_ci    const ILockable* p_;
768bf80f4bSopenharmony_ci};
778bf80f4bSopenharmony_ci
788bf80f4bSopenharmony_ci/**
798bf80f4bSopenharmony_ci * @brief Helper class to acquire write lock and release it end of the scope.
808bf80f4bSopenharmony_ci */
818bf80f4bSopenharmony_ciclass InterfaceUniqueLock {
828bf80f4bSopenharmony_cipublic:
838bf80f4bSopenharmony_ci    META_NO_COPY(InterfaceUniqueLock)
848bf80f4bSopenharmony_ci
858bf80f4bSopenharmony_ci    explicit InterfaceUniqueLock(const CORE_NS::IInterface* t) : p_(interface_cast<ILockable>(t))
868bf80f4bSopenharmony_ci    {
878bf80f4bSopenharmony_ci        if (p_) {
888bf80f4bSopenharmony_ci            p_->Lock();
898bf80f4bSopenharmony_ci        }
908bf80f4bSopenharmony_ci    }
918bf80f4bSopenharmony_ci    explicit InterfaceUniqueLock(const CORE_NS::IInterface& t) : InterfaceUniqueLock(&t) {}
928bf80f4bSopenharmony_ci    template<typename Type>
938bf80f4bSopenharmony_ci    explicit InterfaceUniqueLock(const BASE_NS::shared_ptr<Type>& t) : InterfaceUniqueLock(t.get())
948bf80f4bSopenharmony_ci    {}
958bf80f4bSopenharmony_ci
968bf80f4bSopenharmony_ci    InterfaceUniqueLock(InterfaceUniqueLock&& l) noexcept : p_(l.p_)
978bf80f4bSopenharmony_ci    {
988bf80f4bSopenharmony_ci        l.p_ = nullptr;
998bf80f4bSopenharmony_ci    }
1008bf80f4bSopenharmony_ci
1018bf80f4bSopenharmony_ci    InterfaceUniqueLock& operator=(InterfaceUniqueLock&& l) noexcept
1028bf80f4bSopenharmony_ci    {
1038bf80f4bSopenharmony_ci        p_ = l.p_;
1048bf80f4bSopenharmony_ci        l.p_ = nullptr;
1058bf80f4bSopenharmony_ci        return *this;
1068bf80f4bSopenharmony_ci    }
1078bf80f4bSopenharmony_ci
1088bf80f4bSopenharmony_ci    ~InterfaceUniqueLock()
1098bf80f4bSopenharmony_ci    {
1108bf80f4bSopenharmony_ci        if (p_) {
1118bf80f4bSopenharmony_ci            p_->Unlock();
1128bf80f4bSopenharmony_ci        }
1138bf80f4bSopenharmony_ci    }
1148bf80f4bSopenharmony_ci
1158bf80f4bSopenharmony_ci    explicit operator bool() const
1168bf80f4bSopenharmony_ci    {
1178bf80f4bSopenharmony_ci        return p_ != nullptr;
1188bf80f4bSopenharmony_ci    }
1198bf80f4bSopenharmony_ci
1208bf80f4bSopenharmony_ciprivate:
1218bf80f4bSopenharmony_ci    const ILockable* p_;
1228bf80f4bSopenharmony_ci};
1238bf80f4bSopenharmony_ci
1248bf80f4bSopenharmony_ci/**
1258bf80f4bSopenharmony_ci * @brief Helper class to acquire read lock and release it end of the scope.
1268bf80f4bSopenharmony_ci */
1278bf80f4bSopenharmony_ciclass InterfaceSharedLock {
1288bf80f4bSopenharmony_cipublic:
1298bf80f4bSopenharmony_ci    META_NO_COPY(InterfaceSharedLock)
1308bf80f4bSopenharmony_ci
1318bf80f4bSopenharmony_ci    explicit InterfaceSharedLock(const CORE_NS::IInterface* t) : p_(interface_cast<ILockable>(t))
1328bf80f4bSopenharmony_ci    {
1338bf80f4bSopenharmony_ci        if (p_) {
1348bf80f4bSopenharmony_ci            p_->LockShared();
1358bf80f4bSopenharmony_ci        }
1368bf80f4bSopenharmony_ci    }
1378bf80f4bSopenharmony_ci    explicit InterfaceSharedLock(const CORE_NS::IInterface& t) : InterfaceSharedLock(&t) {}
1388bf80f4bSopenharmony_ci    template<typename Type>
1398bf80f4bSopenharmony_ci    explicit InterfaceSharedLock(const BASE_NS::shared_ptr<Type>& t) : InterfaceSharedLock(t.get())
1408bf80f4bSopenharmony_ci    {}
1418bf80f4bSopenharmony_ci
1428bf80f4bSopenharmony_ci    InterfaceSharedLock(InterfaceSharedLock&& l) noexcept : p_(l.p_)
1438bf80f4bSopenharmony_ci    {
1448bf80f4bSopenharmony_ci        l.p_ = nullptr;
1458bf80f4bSopenharmony_ci    }
1468bf80f4bSopenharmony_ci
1478bf80f4bSopenharmony_ci    InterfaceSharedLock& operator=(InterfaceSharedLock&& l) noexcept
1488bf80f4bSopenharmony_ci    {
1498bf80f4bSopenharmony_ci        p_ = l.p_;
1508bf80f4bSopenharmony_ci        l.p_ = nullptr;
1518bf80f4bSopenharmony_ci        return *this;
1528bf80f4bSopenharmony_ci    }
1538bf80f4bSopenharmony_ci
1548bf80f4bSopenharmony_ci    ~InterfaceSharedLock()
1558bf80f4bSopenharmony_ci    {
1568bf80f4bSopenharmony_ci        if (p_) {
1578bf80f4bSopenharmony_ci            p_->UnlockShared();
1588bf80f4bSopenharmony_ci        }
1598bf80f4bSopenharmony_ci    }
1608bf80f4bSopenharmony_ci
1618bf80f4bSopenharmony_ci    explicit operator bool() const
1628bf80f4bSopenharmony_ci    {
1638bf80f4bSopenharmony_ci        return p_ != nullptr;
1648bf80f4bSopenharmony_ci    }
1658bf80f4bSopenharmony_ci
1668bf80f4bSopenharmony_ciprivate:
1678bf80f4bSopenharmony_ci    const ILockable* p_;
1688bf80f4bSopenharmony_ci};
1698bf80f4bSopenharmony_ci
1708bf80f4bSopenharmony_ciMETA_END_NAMESPACE()
1718bf80f4bSopenharmony_ci
1728bf80f4bSopenharmony_ci#endif
173