Atmosphere/libraries/libstratosphere/include/stratosphere/os/os_rw_lock.hpp

113 lines
3.6 KiB
C++
Raw Normal View History

/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
2020-04-14 02:07:06 +02:00
#include <stratosphere/os/os_rw_lock_common.hpp>
#include <stratosphere/os/os_rw_lock_types.hpp>
#include <stratosphere/os/os_rw_lock_api.hpp>
namespace ams::os {
class ReaderWriterLock {
NON_COPYABLE(ReaderWriterLock);
NON_MOVEABLE(ReaderWriterLock);
private:
2021-10-10 09:14:06 +02:00
ReaderWriterLockType m_rw_lock;
public:
constexpr explicit ReaderWriterLock() : m_rw_lock {{ { MakeConstantInitializedLockCount(), 0 } }, 0, ::ams::os::ReaderWriterLockType::State_Initialized, nullptr, 0, {AMS_OS_INTERNAL_CONDITION_VARIABLE_IMPL_CONSTANT_INITIALIZER}, {AMS_OS_INTERNAL_CONDITION_VARIABLE_IMPL_CONSTANT_INITIALIZER} } {
/* ... */
}
constexpr ~ReaderWriterLock() {
if (!std::is_constant_evaluated()) {
os::FinalizeReaderWriterLock(std::addressof(m_rw_lock));
}
}
2020-04-14 02:07:06 +02:00
void AcquireReadLock() {
2021-10-10 09:14:06 +02:00
return os::AcquireReadLock(std::addressof(m_rw_lock));
}
2020-04-14 02:07:06 +02:00
bool TryAcquireReadLock() {
2021-10-10 09:14:06 +02:00
return os::TryAcquireReadLock(std::addressof(m_rw_lock));
}
void ReleaseReadLock() {
2021-10-10 09:14:06 +02:00
return os::ReleaseReadLock(std::addressof(m_rw_lock));
}
2020-04-14 02:07:06 +02:00
void AcquireWriteLock() {
2021-10-10 09:14:06 +02:00
return os::AcquireWriteLock(std::addressof(m_rw_lock));
}
2020-04-14 02:07:06 +02:00
bool TryAcquireWriteLock() {
2021-10-10 09:14:06 +02:00
return os::TryAcquireWriteLock(std::addressof(m_rw_lock));
}
void ReleaseWriteLock() {
2021-10-10 09:14:06 +02:00
return os::ReleaseWriteLock(std::addressof(m_rw_lock));
}
2020-04-14 02:07:06 +02:00
bool IsReadLockHeld() const {
2021-10-10 09:14:06 +02:00
return os::IsReadLockHeld(std::addressof(m_rw_lock));
}
2020-04-14 02:07:06 +02:00
bool IsWriteLockHeldByCurrentThread() const {
2021-10-10 09:14:06 +02:00
return os::IsWriteLockHeldByCurrentThread(std::addressof(m_rw_lock));
}
2020-04-14 02:07:06 +02:00
bool IsLockOwner() const {
2021-10-10 09:14:06 +02:00
return os::IsReaderWriterLockOwnerThread(std::addressof(m_rw_lock));
2020-04-14 02:07:06 +02:00
}
void lock_shared() {
return this->AcquireReadLock();
}
bool try_lock_shared() {
return this->TryAcquireReadLock();
}
2020-04-14 02:07:06 +02:00
void unlock_shared() {
return this->ReleaseReadLock();
}
2020-04-14 02:07:06 +02:00
void lock() {
return this->AcquireWriteLock();
}
bool try_lock() {
return this->TryAcquireWriteLock();
}
2020-04-14 02:07:06 +02:00
void unlock() {
return this->ReleaseWriteLock();
}
operator ReaderWriterLockType &() {
2021-10-10 09:14:06 +02:00
return m_rw_lock;
2020-04-14 02:07:06 +02:00
}
operator const ReaderWriterLockType &() const {
2021-10-10 09:14:06 +02:00
return m_rw_lock;
2020-04-14 02:07:06 +02:00
}
ReaderWriterLockType *GetBase() {
2021-10-10 09:14:06 +02:00
return std::addressof(m_rw_lock);
2020-04-14 02:07:06 +02:00
}
};
}