1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-18 09:56:40 +00:00
Atmosphere/libraries/libstratosphere/source/os/os_rw_lock.cpp

103 lines
4.2 KiB
C++
Raw Normal View History

2020-04-14 01:07:06 +01:00
/*
* Copyright (c) Atmosphère-NX
2020-04-14 01:07:06 +01: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/>.
*/
#include <stratosphere.hpp>
#include "impl/os_thread_manager.hpp"
#include "impl/os_rw_lock_impl.hpp"
namespace ams::os {
void InitalizeReaderWriterLock(ReaderWriterLockType *rw_lock) {
2020-04-14 01:07:06 +01:00
/* Create objects. */
util::ConstructAt(impl::GetLockCount(rw_lock).cs_storage);
util::ConstructAt(rw_lock->cv_read_lock._storage);
util::ConstructAt(rw_lock->cv_write_lock._storage);
2020-04-14 01:07:06 +01:00
/* Set member variables. */
impl::ClearReadLockCount(impl::GetLockCount(rw_lock));
impl::ClearWriteLocked(impl::GetLockCount(rw_lock));
impl::ClearReadLockWaiterCount(impl::GetLockCount(rw_lock));
impl::ClearWriteLockWaiterCount(impl::GetLockCount(rw_lock));
impl::ClearWriteLockCount(rw_lock);
rw_lock->owner_thread = nullptr;
/* Mark initialized. */
rw_lock->state = ReaderWriterLockType::State_Initialized;
2020-04-14 01:07:06 +01:00
}
void FinalizeReaderWriterLock(ReaderWriterLockType *rw_lock) {
AMS_ASSERT(rw_lock->state == ReaderWriterLockType::State_Initialized);
2020-04-14 01:07:06 +01:00
/* Don't allow finalizing a locked lock. */
AMS_ASSERT(impl::GetReadLockCount(impl::GetLockCount(rw_lock)) == 0);
AMS_ASSERT(impl::GetWriteLocked(impl::GetLockCount(rw_lock)) == 0);
/* Mark not initialized. */
rw_lock->state = ReaderWriterLockType::State_NotInitialized;
2020-04-14 01:07:06 +01:00
/* Destroy objects. */
util::DestroyAt(rw_lock->cv_write_lock._storage);
util::DestroyAt(rw_lock->cv_read_lock._storage);
util::DestroyAt(impl::GetLockCount(rw_lock).cs_storage);
2020-04-14 01:07:06 +01:00
}
void AcquireReadLock(ReaderWriterLockType *rw_lock) {
AMS_ASSERT(rw_lock->state == ReaderWriterLockType::State_Initialized);
return impl::ReaderWriterLockImpl::AcquireReadLock(rw_lock);
2020-04-14 01:07:06 +01:00
}
bool TryAcquireReadLock(ReaderWriterLockType *rw_lock) {
AMS_ASSERT(rw_lock->state == ReaderWriterLockType::State_Initialized);
return impl::ReaderWriterLockImpl::TryAcquireReadLock(rw_lock);
2020-04-14 01:07:06 +01:00
}
void ReleaseReadLock(ReaderWriterLockType *rw_lock) {
AMS_ASSERT(rw_lock->state == ReaderWriterLockType::State_Initialized);
return impl::ReaderWriterLockImpl::ReleaseReadLock(rw_lock);
2020-04-14 01:07:06 +01:00
}
void AcquireWriteLock(ReaderWriterLockType *rw_lock) {
AMS_ASSERT(rw_lock->state == ReaderWriterLockType::State_Initialized);
return impl::ReaderWriterLockImpl::AcquireWriteLock(rw_lock);
2020-04-14 01:07:06 +01:00
}
bool TryAcquireWriteLock(ReaderWriterLockType *rw_lock) {
AMS_ASSERT(rw_lock->state == ReaderWriterLockType::State_Initialized);
return impl::ReaderWriterLockImpl::TryAcquireWriteLock(rw_lock);
2020-04-14 01:07:06 +01:00
}
void ReleaseWriteLock(ReaderWriterLockType *rw_lock) {
AMS_ASSERT(rw_lock->state == ReaderWriterLockType::State_Initialized);
2020-04-14 01:07:06 +01:00
AMS_ABORT_UNLESS(rw_lock->owner_thread == impl::GetCurrentThread());
return impl::ReaderWriterLockImpl::ReleaseWriteLock(rw_lock);
2020-04-14 01:07:06 +01:00
}
bool IsReadLockHeld(const ReaderWriterLockType *rw_lock) {
AMS_ASSERT(rw_lock->state == ReaderWriterLockType::State_Initialized);
2020-04-14 01:07:06 +01:00
return impl::GetReadLockCount(impl::GetLockCount(rw_lock)) != 0;
}
bool IsWriteLockHeldByCurrentThread(const ReaderWriterLockType *rw_lock) {
AMS_ASSERT(rw_lock->state == ReaderWriterLockType::State_Initialized);
2020-04-14 01:07:06 +01:00
return rw_lock->owner_thread == impl::GetCurrentThread() && impl::GetWriteLockCount(*rw_lock) != 0;
}
bool IsReaderWriterLockOwnerThread(const ReaderWriterLockType *rw_lock) {
AMS_ASSERT(rw_lock->state == ReaderWriterLockType::State_Initialized);
2020-04-14 01:07:06 +01:00
return rw_lock->owner_thread == impl::GetCurrentThread();
}
}