mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2024-12-19 17:02:14 +00:00
pf2: implement critical sections
This commit is contained in:
parent
7418c80e7f
commit
dd6c9e1de1
6 changed files with 269 additions and 0 deletions
|
@ -22,3 +22,4 @@
|
||||||
#include <vapours/svc.hpp>
|
#include <vapours/svc.hpp>
|
||||||
|
|
||||||
#include <vapours/prfile2/prfile2_common.hpp>
|
#include <vapours/prfile2/prfile2_common.hpp>
|
||||||
|
#include <vapours/prfile2/prfile2_system.hpp>
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <vapours/prfile2/pf/prfile2_pf_config.hpp>
|
#include <vapours/prfile2/pf/prfile2_pf_config.hpp>
|
||||||
#include <vapours/prfile2/pf/prfile2_pf_types.hpp>
|
#include <vapours/prfile2/pf/prfile2_pf_types.hpp>
|
||||||
|
#include <vapours/prfile2/prfile2_critical_section.hpp>
|
||||||
|
|
||||||
namespace ams::prfile2::pf {
|
namespace ams::prfile2::pf {
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
* 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/prfile2/pf/prfile2_pf_config.hpp>
|
||||||
|
|
||||||
|
namespace ams::prfile2 {
|
||||||
|
|
||||||
|
struct CriticalSection {
|
||||||
|
enum State {
|
||||||
|
State_NotInitialized = 0,
|
||||||
|
State_Initialized = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Resource;
|
||||||
|
|
||||||
|
u8 state;
|
||||||
|
int lock_count;
|
||||||
|
Resource *resource;
|
||||||
|
u64 owner;
|
||||||
|
};
|
||||||
|
|
||||||
|
void InitializeCriticalSection(CriticalSection *cs);
|
||||||
|
void FinalizeCriticalSection(CriticalSection *cs);
|
||||||
|
|
||||||
|
void EnterCriticalSection(CriticalSection *cs);
|
||||||
|
void ExitCriticalSection(CriticalSection *cs);
|
||||||
|
|
||||||
|
class ScopedCriticalSection {
|
||||||
|
private:
|
||||||
|
CriticalSection *cs;
|
||||||
|
public:
|
||||||
|
ALWAYS_INLINE ScopedCriticalSection(CriticalSection *c) : cs(c) { EnterCriticalSection(this->cs); }
|
||||||
|
ALWAYS_INLINE ScopedCriticalSection(CriticalSection &c) : ScopedCriticalSection(std::addressof(c)) { /* ... */ }
|
||||||
|
|
||||||
|
ALWAYS_INLINE ~ScopedCriticalSection() { ExitCriticalSection(this->cs); }
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
/*
|
||||||
|
* 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/prfile2/prfile2_common.hpp>
|
||||||
|
|
||||||
|
namespace ams::prfile2::system {
|
||||||
|
|
||||||
|
void Initialize();
|
||||||
|
|
||||||
|
pf::Error GetCurrentContextId(u64 *out);
|
||||||
|
|
||||||
|
}
|
143
libraries/libvapours/source/prfile2/prfile2_critical_section.cpp
Normal file
143
libraries/libvapours/source/prfile2/prfile2_critical_section.cpp
Normal file
|
@ -0,0 +1,143 @@
|
||||||
|
/*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
#if defined(ATMOSPHERE_IS_STRATOSPHERE)
|
||||||
|
#include <stratosphere.hpp>
|
||||||
|
#elif defined(ATMOSPHERE_IS_MESOSPHERE)
|
||||||
|
#include <mesosphere.hpp>
|
||||||
|
#elif defined(ATMOSPHERE_IS_EXOSPHERE)
|
||||||
|
#include <exosphere.hpp>
|
||||||
|
#else
|
||||||
|
#include <vapours.hpp>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace ams::prfile2 {
|
||||||
|
|
||||||
|
#if defined(AMS_PRFILE2_THREAD_SAFE)
|
||||||
|
|
||||||
|
struct CriticalSection::Resource {
|
||||||
|
os::MutexType mutex;
|
||||||
|
bool in_use;
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
constexpr inline const auto NumCriticalSectionResources = 5 /* TODO: NumVolumes */;
|
||||||
|
|
||||||
|
constinit os::SdkMutex g_crit_resource_mutex;
|
||||||
|
constinit CriticalSection::Resource g_crit_resources[NumCriticalSectionResources];
|
||||||
|
|
||||||
|
CriticalSection::Resource *AllocateCriticalSectionResource() {
|
||||||
|
std::scoped_lock lk(g_crit_resource_mutex);
|
||||||
|
|
||||||
|
for (auto &resource : g_crit_resources) {
|
||||||
|
if (!resource.in_use) {
|
||||||
|
resource.in_use = true;
|
||||||
|
return std::addressof(resource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AMS_ABORT("Failed to allocate critical section resource");
|
||||||
|
}
|
||||||
|
|
||||||
|
void AcquireCriticalSectionResource(CriticalSection::Resource *resource) {
|
||||||
|
return os::LockMutex(std::addressof(resource->mutex));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReleaseCriticalSectionResource(CriticalSection::Resource *resource) {
|
||||||
|
return os::UnlockMutex(std::addressof(resource->mutex));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeCriticalSection(CriticalSection *cs) {
|
||||||
|
/* Check pre-condition. */
|
||||||
|
AMS_ASSERT(cs->state == CriticalSection::State_NotInitialized);
|
||||||
|
|
||||||
|
/* Perform initialization. */
|
||||||
|
if (cs->state == CriticalSection::State_NotInitialized) {
|
||||||
|
cs->resource = AllocateCriticalSectionResource();
|
||||||
|
cs->owner = 0;
|
||||||
|
cs->state = CriticalSection::State_Initialized;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FinalizeCriticalSection(CriticalSection *cs) {
|
||||||
|
/* Check pre-condition. */
|
||||||
|
AMS_ASSERT(cs->state == CriticalSection::State_Initialized);
|
||||||
|
|
||||||
|
/* TODO */
|
||||||
|
AMS_UNUSED(cs);
|
||||||
|
AMS_ABORT("prfile2::FinalizeCriticalSection");
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnterCriticalSection(CriticalSection *cs) {
|
||||||
|
/* Check pre-condition. */
|
||||||
|
AMS_ASSERT(cs->state == CriticalSection::State_Initialized);
|
||||||
|
|
||||||
|
if (AMS_LIKELY(cs->lock_count == 0)) {
|
||||||
|
/* Acquire the lock .*/
|
||||||
|
AcquireCriticalSectionResource(cs->resource);
|
||||||
|
system::GetCurrentContextId(std::addressof(cs->owner));
|
||||||
|
} else {
|
||||||
|
/* Get the current context id. */
|
||||||
|
u64 current_context_id;
|
||||||
|
system::GetCurrentContextId(std::addressof(current_context_id));
|
||||||
|
|
||||||
|
/* If the lock isn't already held, acquire it. */
|
||||||
|
if (cs->owner != current_context_id) {
|
||||||
|
AcquireCriticalSectionResource(cs->resource);
|
||||||
|
cs->owner = current_context_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Increment the lock count. */
|
||||||
|
++cs->lock_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ExitCriticalSection(CriticalSection *cs) {
|
||||||
|
/* Check pre-condition. */
|
||||||
|
AMS_ASSERT(cs->state == CriticalSection::State_Initialized);
|
||||||
|
|
||||||
|
/* Unlock. */
|
||||||
|
if ((--cs->lock_count) == 0) {
|
||||||
|
cs->owner = 0;
|
||||||
|
ReleaseCriticalSectionResource(cs->resource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
void InitializeCriticalSection(CriticalSection *cs) {
|
||||||
|
AMS_UNUSED(cs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void FinalizeCriticalSection(CriticalSection *cs) {
|
||||||
|
AMS_UNUSED(cs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnterCriticalSection(CriticalSection *cs) {
|
||||||
|
AMS_UNUSED(cs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ExitCriticalSection(CriticalSection *cs) {
|
||||||
|
AMS_UNUSED(cs);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
48
libraries/libvapours/source/prfile2/prfile2_system.cpp
Normal file
48
libraries/libvapours/source/prfile2/prfile2_system.cpp
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
#if defined(ATMOSPHERE_IS_STRATOSPHERE)
|
||||||
|
#include <stratosphere.hpp>
|
||||||
|
#elif defined(ATMOSPHERE_IS_MESOSPHERE)
|
||||||
|
#include <mesosphere.hpp>
|
||||||
|
#elif defined(ATMOSPHERE_IS_EXOSPHERE)
|
||||||
|
#include <exosphere.hpp>
|
||||||
|
#else
|
||||||
|
#include <vapours.hpp>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace ams::prfile2::system {
|
||||||
|
|
||||||
|
void Initialize() {
|
||||||
|
/* ... */
|
||||||
|
}
|
||||||
|
|
||||||
|
pf::Error GetCurrentContextId(u64 *out) {
|
||||||
|
/* Check that out isn't null. */
|
||||||
|
if (out == nullptr) {
|
||||||
|
return static_cast<pf::Error>(-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set the output. */
|
||||||
|
#if defined(AMS_PRFILE2_THREAD_SAFE)
|
||||||
|
*out = reinterpret_cast<u64>(os::GetCurrentThread());
|
||||||
|
#else
|
||||||
|
*out = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return pf::Error_Ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue