mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-05-31 06:48:22 -04:00
os: implement ReadWriteLock
This commit is contained in:
parent
6eb77e69c4
commit
97cba5e881
20 changed files with 941 additions and 66 deletions
|
@ -19,12 +19,18 @@
|
|||
|
||||
namespace ams::os::impl {
|
||||
|
||||
class ReadWriteLockImpl;
|
||||
#if defined(ATMOSPHERE_OS_HORIZON)
|
||||
class ReadWriteLockHorizonImpl;
|
||||
#endif
|
||||
|
||||
class InternalConditionVariableImpl;
|
||||
|
||||
class InternalCriticalSectionImpl {
|
||||
private:
|
||||
friend class ReadWriteLockImpl;
|
||||
#if defined(ATMOSPHERE_OS_HORIZON)
|
||||
friend class ReadWriteLockHorizonImpl;
|
||||
#endif
|
||||
|
||||
friend class InternalConditionVariableImpl;
|
||||
private:
|
||||
u32 thread_handle;
|
||||
|
|
|
@ -15,7 +15,9 @@
|
|||
*/
|
||||
|
||||
#pragma once
|
||||
#include "os_common_types.hpp"
|
||||
#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 {
|
||||
|
||||
|
@ -23,67 +25,83 @@ namespace ams::os {
|
|||
NON_COPYABLE(ReadWriteLock);
|
||||
NON_MOVEABLE(ReadWriteLock);
|
||||
private:
|
||||
::RwLock r;
|
||||
ReadWriteLockType rw_lock;
|
||||
public:
|
||||
ReadWriteLock() {
|
||||
rwlockInit(&this->r);
|
||||
}
|
||||
constexpr explicit ReadWriteLock() : rw_lock{{}, 0, ::ams::os::ReadWriteLockType::State_Initialized, nullptr, 0, {}, {}} { /* ... */ }
|
||||
|
||||
bool IsWriteLockHeldByCurrentThread() const {
|
||||
return rwlockIsWriteLockHeldByCurrentThread(const_cast<::RwLock *>(&this->r));
|
||||
}
|
||||
|
||||
bool IsLockOwner() const {
|
||||
return rwlockIsOwnedByCurrentThread(const_cast<::RwLock *>(&this->r));
|
||||
}
|
||||
~ReadWriteLock() { os::FinalizeReadWriteLock(std::addressof(this->rw_lock)); }
|
||||
|
||||
void AcquireReadLock() {
|
||||
rwlockReadLock(&this->r);
|
||||
}
|
||||
|
||||
void ReleaseReadLock() {
|
||||
rwlockReadUnlock(&this->r);
|
||||
return os::AcquireReadLock(std::addressof(this->rw_lock));
|
||||
}
|
||||
|
||||
bool TryAcquireReadLock() {
|
||||
return rwlockTryReadLock(&this->r);
|
||||
return os::TryAcquireReadLock(std::addressof(this->rw_lock));
|
||||
}
|
||||
|
||||
void ReleaseReadLock() {
|
||||
return os::ReleaseReadLock(std::addressof(this->rw_lock));
|
||||
}
|
||||
|
||||
void AcquireWriteLock() {
|
||||
rwlockWriteLock(&this->r);
|
||||
}
|
||||
|
||||
void ReleaseWriteLock() {
|
||||
rwlockWriteUnlock(&this->r);
|
||||
return os::AcquireWriteLock(std::addressof(this->rw_lock));
|
||||
}
|
||||
|
||||
bool TryAcquireWriteLock() {
|
||||
return rwlockTryWriteLock(&this->r);
|
||||
return os::TryAcquireWriteLock(std::addressof(this->rw_lock));
|
||||
}
|
||||
|
||||
void ReleaseWriteLock() {
|
||||
return os::ReleaseWriteLock(std::addressof(this->rw_lock));
|
||||
}
|
||||
|
||||
bool IsReadLockHeld() const {
|
||||
return os::IsReadLockHeld(std::addressof(this->rw_lock));
|
||||
}
|
||||
|
||||
bool IsWriteLockHeldByCurrentThread() const {
|
||||
return os::IsWriteLockHeldByCurrentThread(std::addressof(this->rw_lock));
|
||||
}
|
||||
|
||||
bool IsLockOwner() const {
|
||||
return os::IsReadWriteLockOwnerThread(std::addressof(this->rw_lock));
|
||||
}
|
||||
|
||||
void lock_shared() {
|
||||
this->AcquireReadLock();
|
||||
}
|
||||
|
||||
void unlock_shared() {
|
||||
this->ReleaseReadLock();
|
||||
return this->AcquireReadLock();
|
||||
}
|
||||
|
||||
bool try_lock_shared() {
|
||||
return this->TryAcquireReadLock();
|
||||
}
|
||||
|
||||
void lock() {
|
||||
this->AcquireWriteLock();
|
||||
void unlock_shared() {
|
||||
return this->ReleaseReadLock();
|
||||
}
|
||||
|
||||
void unlock() {
|
||||
this->ReleaseWriteLock();
|
||||
void lock() {
|
||||
return this->AcquireWriteLock();
|
||||
}
|
||||
|
||||
bool try_lock() {
|
||||
return this->TryAcquireWriteLock();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020 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
|
||||
#include <vapours.hpp>
|
||||
#include <stratosphere/os/os_rw_lock_common.hpp>
|
||||
|
||||
namespace ams::os {
|
||||
|
||||
struct ReadWriteLockType;
|
||||
|
||||
void InitalizeReadWriteLock(ReadWriteLockType *rw_lock);
|
||||
void FinalizeReadWriteLock(ReadWriteLockType *rw_lock);
|
||||
|
||||
void AcquireReadLock(ReadWriteLockType *rw_lock);
|
||||
bool TryAcquireReadLock(ReadWriteLockType *rw_lock);
|
||||
void ReleaseReadLock(ReadWriteLockType *rw_lock);
|
||||
|
||||
void AcquireWriteLock(ReadWriteLockType *rw_lock);
|
||||
bool TryAcquireWriteLock(ReadWriteLockType *rw_lock);
|
||||
void ReleaseWriteLock(ReadWriteLockType *rw_lock);
|
||||
|
||||
bool IsReadLockHeld(const ReadWriteLockType *rw_lock);
|
||||
bool IsWriteLockHeldByCurrentThread(const ReadWriteLockType *rw_lock);
|
||||
bool IsReadWriteLockOwnerThread(const ReadWriteLockType *rw_lock);
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020 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
|
||||
#include <vapours.hpp>
|
||||
|
||||
namespace ams::os {
|
||||
|
||||
constexpr inline s32 ReadWriteLockCountMax = (1 << (BITSIZEOF(u16) - 1)) - 1;
|
||||
constexpr inline s32 ReadWriteLockWaiterCountMax = (1 << BITSIZEOF(u8)) - 1;
|
||||
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020 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
|
||||
#include <vapours.hpp>
|
||||
#include <stratosphere/os/impl/os_internal_critical_section.hpp>
|
||||
#include <stratosphere/os/impl/os_internal_condition_variable.hpp>
|
||||
|
||||
namespace ams::os {
|
||||
|
||||
struct ThreadType;
|
||||
|
||||
struct ReadWriteLockType {
|
||||
enum State {
|
||||
State_NotInitialized = 0,
|
||||
State_Initialized = 1,
|
||||
};
|
||||
|
||||
struct LockCount {
|
||||
union {
|
||||
s32 _arr[sizeof(impl::InternalCriticalSectionStorage) / sizeof(s32)];
|
||||
impl::InternalCriticalSectionStorage cs_storage;
|
||||
};
|
||||
util::BitPack32 counter;
|
||||
};
|
||||
static_assert(std::is_pod<LockCount>::value);
|
||||
static_assert(std::is_trivial<LockCount>::value);
|
||||
|
||||
union {
|
||||
struct {
|
||||
LockCount c;
|
||||
u32 write_lock_count;
|
||||
} aligned;
|
||||
struct {
|
||||
u32 write_lock_count;
|
||||
LockCount c;
|
||||
} not_aligned;
|
||||
} lock_count;
|
||||
|
||||
u32 reserved_1;
|
||||
|
||||
u8 state;
|
||||
ThreadType *owner_thread;
|
||||
u32 reserved_2;
|
||||
|
||||
union {
|
||||
s32 _arr[sizeof(impl::InternalConditionVariableStorage) / sizeof(s32)];
|
||||
impl::InternalConditionVariableStorage _storage;
|
||||
} cv_read_lock;
|
||||
union {
|
||||
s32 _arr[sizeof(impl::InternalConditionVariableStorage) / sizeof(s32)];
|
||||
impl::InternalConditionVariableStorage _storage;
|
||||
} cv_write_lock;
|
||||
};
|
||||
static_assert(std::is_trivial<ReadWriteLockType>::value);
|
||||
|
||||
}
|
|
@ -22,6 +22,7 @@ namespace ams::os {
|
|||
|
||||
/* Tick API. */
|
||||
Tick GetSystemTick();
|
||||
Tick GetSystemTickOrdered();
|
||||
s64 GetSystemTickFrequency();
|
||||
TimeSpan ConvertToTimeSpan(Tick tick);
|
||||
Tick ConvertToTick(TimeSpan ts);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue