2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-12-09 11:57:37 +00:00
|
|
|
*
|
|
|
|
* 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 01:07:06 +01: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>
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
namespace ams::os {
|
|
|
|
|
|
|
|
class ReadWriteLock {
|
|
|
|
NON_COPYABLE(ReadWriteLock);
|
|
|
|
NON_MOVEABLE(ReadWriteLock);
|
|
|
|
private:
|
2020-04-14 01:07:06 +01:00
|
|
|
ReadWriteLockType rw_lock;
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2020-04-14 01:07:06 +01:00
|
|
|
constexpr explicit ReadWriteLock() : rw_lock{{}, 0, ::ams::os::ReadWriteLockType::State_Initialized, nullptr, 0, {}, {}} { /* ... */ }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-04-14 01:07:06 +01:00
|
|
|
~ReadWriteLock() { os::FinalizeReadWriteLock(std::addressof(this->rw_lock)); }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-04-14 01:07:06 +01:00
|
|
|
void AcquireReadLock() {
|
|
|
|
return os::AcquireReadLock(std::addressof(this->rw_lock));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 01:07:06 +01:00
|
|
|
bool TryAcquireReadLock() {
|
|
|
|
return os::TryAcquireReadLock(std::addressof(this->rw_lock));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseReadLock() {
|
2020-04-14 01:07:06 +01:00
|
|
|
return os::ReleaseReadLock(std::addressof(this->rw_lock));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 01:07:06 +01:00
|
|
|
void AcquireWriteLock() {
|
|
|
|
return os::AcquireWriteLock(std::addressof(this->rw_lock));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 01:07:06 +01:00
|
|
|
bool TryAcquireWriteLock() {
|
|
|
|
return os::TryAcquireWriteLock(std::addressof(this->rw_lock));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseWriteLock() {
|
2020-04-14 01:07:06 +01:00
|
|
|
return os::ReleaseWriteLock(std::addressof(this->rw_lock));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 01:07:06 +01:00
|
|
|
bool IsReadLockHeld() const {
|
|
|
|
return os::IsReadLockHeld(std::addressof(this->rw_lock));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 01:07:06 +01:00
|
|
|
bool IsWriteLockHeldByCurrentThread() const {
|
|
|
|
return os::IsWriteLockHeldByCurrentThread(std::addressof(this->rw_lock));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 01:07:06 +01:00
|
|
|
bool IsLockOwner() const {
|
|
|
|
return os::IsReadWriteLockOwnerThread(std::addressof(this->rw_lock));
|
|
|
|
}
|
|
|
|
|
|
|
|
void lock_shared() {
|
|
|
|
return this->AcquireReadLock();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool try_lock_shared() {
|
|
|
|
return this->TryAcquireReadLock();
|
|
|
|
}
|
|
|
|
|
2020-04-14 01:07:06 +01:00
|
|
|
void unlock_shared() {
|
|
|
|
return this->ReleaseReadLock();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 01:07:06 +01:00
|
|
|
void lock() {
|
|
|
|
return this->AcquireWriteLock();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool try_lock() {
|
|
|
|
return this->TryAcquireWriteLock();
|
|
|
|
}
|
2020-04-14 01:07:06 +01:00
|
|
|
|
|
|
|
void unlock() {
|
|
|
|
return this->ReleaseWriteLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
operator ReadWriteLockType &() {
|
|
|
|
return this->rw_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator const ReadWriteLockType &() const {
|
|
|
|
return this->rw_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadWriteLockType *GetBase() {
|
|
|
|
return std::addressof(this->rw_lock);
|
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|