libstrat: namespace hossynch.hpp

This commit is contained in:
Michael Scire
2019-09-24 03:15:36 -07:00
committed by SciresM
parent 73d904036d
commit bb223eb5ae
57 changed files with 923 additions and 773 deletions

View File

@@ -27,7 +27,7 @@ namespace sts::cfg {
constexpr u64 InitialProcessIdMaxDeprecated = 0x50;
/* Privileged process globals. */
HosMutex g_lock;
os::Mutex g_lock;
bool g_got_privileged_process_status = false;
u64 g_min_initial_process_id = 0, g_max_initial_process_id = 0;
u64 g_cur_process_id = 0;
@@ -69,7 +69,7 @@ namespace sts::cfg {
/* Privileged Process utilities. */
bool IsInitialProcess() {
std::scoped_lock<HosMutex> lk(g_lock);
std::scoped_lock lk(g_lock);
/* If we've not detected, do detection. */
if (!g_got_privileged_process_status) {
@@ -81,7 +81,7 @@ namespace sts::cfg {
}
void GetInitialProcessRange(u64 *out_min, u64 *out_max) {
std::scoped_lock<HosMutex> lk(g_lock);
std::scoped_lock lk(g_lock);
/* If we've not detected, do detection. */
if (!g_got_privileged_process_status) {

View File

@@ -33,7 +33,7 @@ namespace sts::cfg {
constexpr size_t NumRequiredServicesForSdCardAccess = util::size(RequiredServicesForSdCardAccess);
/* SD card globals. */
HosMutex g_sd_card_lock;
os::Mutex g_sd_card_lock;
bool g_sd_card_initialized = false;
FsFileSystem g_sd_card_filesystem = {};
@@ -64,7 +64,7 @@ namespace sts::cfg {
/* SD card utilities. */
bool IsSdCardInitialized() {
std::scoped_lock<HosMutex> lk(g_sd_card_lock);
std::scoped_lock lk(g_sd_card_lock);
if (!g_sd_card_initialized) {
if (R_SUCCEEDED(TryInitializeSdCard())) {
@@ -75,7 +75,7 @@ namespace sts::cfg {
}
void WaitSdCardInitialized() {
std::scoped_lock<HosMutex> lk(g_sd_card_lock);
std::scoped_lock lk(g_sd_card_lock);
InitializeSdCard();
}

View File

@@ -24,7 +24,7 @@ namespace sts::hid {
namespace {
/* Global lock. */
HosMutex g_hid_lock;
os::Mutex g_hid_lock;
bool g_initialized_hid = false;
/* Helper. */
@@ -53,7 +53,7 @@ namespace sts::hid {
}
Result GetKeysHeld(u64 *out) {
std::scoped_lock<HosMutex> lk(g_hid_lock);
std::scoped_lock lk(g_hid_lock);
R_TRY(EnsureHidInitialized());

View File

@@ -1,235 +0,0 @@
/*
* Copyright (c) 2018-2019 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/>.
*/
#include <mutex>
#include <switch.h>
#include <stratosphere.hpp>
void HosMessageQueue::Send(uintptr_t data) {
/* Acquire mutex, wait sendable. */
std::scoped_lock<HosMutex> lock(this->queue_lock);
while (this->IsFull()) {
this->cv_not_full.Wait(&this->queue_lock);
}
/* Send, signal. */
this->SendInternal(data);
this->cv_not_empty.WakeAll();
}
bool HosMessageQueue::TrySend(uintptr_t data) {
std::scoped_lock<HosMutex> lock(this->queue_lock);
if (this->IsFull()) {
return false;
}
/* Send, signal. */
this->SendInternal(data);
this->cv_not_empty.WakeAll();
return true;
}
bool HosMessageQueue::TimedSend(uintptr_t data, u64 timeout) {
std::scoped_lock<HosMutex> lock(this->queue_lock);
TimeoutHelper timeout_helper(timeout);
while (this->IsFull()) {
if (timeout_helper.TimedOut()) {
return false;
}
this->cv_not_full.TimedWait(timeout, &this->queue_lock);
}
/* Send, signal. */
this->SendInternal(data);
this->cv_not_empty.WakeAll();
return true;
}
void HosMessageQueue::SendNext(uintptr_t data) {
/* Acquire mutex, wait sendable. */
std::scoped_lock<HosMutex> lock(this->queue_lock);
while (this->IsFull()) {
this->cv_not_full.Wait(&this->queue_lock);
}
/* Send, signal. */
this->SendNextInternal(data);
this->cv_not_empty.WakeAll();
}
bool HosMessageQueue::TrySendNext(uintptr_t data) {
std::scoped_lock<HosMutex> lock(this->queue_lock);
if (this->IsFull()) {
return false;
}
/* Send, signal. */
this->SendNextInternal(data);
this->cv_not_empty.WakeAll();
return true;
}
bool HosMessageQueue::TimedSendNext(uintptr_t data, u64 timeout) {
std::scoped_lock<HosMutex> lock(this->queue_lock);
TimeoutHelper timeout_helper(timeout);
while (this->IsFull()) {
if (timeout_helper.TimedOut()) {
return false;
}
this->cv_not_full.TimedWait(timeout, &this->queue_lock);
}
/* Send, signal. */
this->SendNextInternal(data);
this->cv_not_empty.WakeAll();
return true;
}
void HosMessageQueue::Receive(uintptr_t *out) {
/* Acquire mutex, wait receivable. */
std::scoped_lock<HosMutex> lock(this->queue_lock);
while (this->IsEmpty()) {
this->cv_not_empty.Wait(&this->queue_lock);
}
/* Receive, signal. */
*out = this->ReceiveInternal();
this->cv_not_full.WakeAll();
}
bool HosMessageQueue::TryReceive(uintptr_t *out) {
/* Acquire mutex, wait receivable. */
std::scoped_lock<HosMutex> lock(this->queue_lock);
if (this->IsEmpty()) {
return false;
}
/* Receive, signal. */
*out = this->ReceiveInternal();
this->cv_not_full.WakeAll();
return true;
}
bool HosMessageQueue::TimedReceive(uintptr_t *out, u64 timeout) {
std::scoped_lock<HosMutex> lock(this->queue_lock);
TimeoutHelper timeout_helper(timeout);
while (this->IsEmpty()) {
if (timeout_helper.TimedOut()) {
return false;
}
this->cv_not_empty.TimedWait(timeout, &this->queue_lock);
}
/* Receive, signal. */
*out = this->ReceiveInternal();
this->cv_not_full.WakeAll();
return true;
}
void HosMessageQueue::Peek(uintptr_t *out) {
/* Acquire mutex, wait receivable. */
std::scoped_lock<HosMutex> lock(this->queue_lock);
while (this->IsEmpty()) {
this->cv_not_empty.Wait(&this->queue_lock);
}
/* Peek. */
*out = this->PeekInternal();
}
bool HosMessageQueue::TryPeek(uintptr_t *out) {
/* Acquire mutex, wait receivable. */
std::scoped_lock<HosMutex> lock(this->queue_lock);
if (this->IsEmpty()) {
return false;
}
/* Peek. */
*out = this->PeekInternal();
return true;
}
bool HosMessageQueue::TimedPeek(uintptr_t *out, u64 timeout) {
std::scoped_lock<HosMutex> lock(this->queue_lock);
TimeoutHelper timeout_helper(timeout);
while (this->IsEmpty()) {
if (timeout_helper.TimedOut()) {
return false;
}
this->cv_not_empty.TimedWait(timeout, &this->queue_lock);
}
/* Peek. */
*out = this->PeekInternal();
return true;
}
void HosMessageQueue::SendInternal(uintptr_t data) {
/* Ensure we don't corrupt the queue, but this should never happen. */
if (this->count >= this->capacity) {
std::abort();
}
/* Write data to tail of queue. */
this->buffer[(this->count++ + this->offset) % this->capacity] = data;
}
void HosMessageQueue::SendNextInternal(uintptr_t data) {
/* Ensure we don't corrupt the queue, but this should never happen. */
if (this->count >= this->capacity) {
std::abort();
}
/* Write data to head of queue. */
this->offset = (this->offset + this->capacity - 1) % this->capacity;
this->buffer[this->offset] = data;
this->count++;
}
uintptr_t HosMessageQueue::ReceiveInternal() {
/* Ensure we don't corrupt the queue, but this should never happen. */
if (this->count == 0) {
std::abort();
}
uintptr_t data = this->buffer[this->offset];
this->offset = (this->offset + 1) % this->capacity;
this->count--;
return data;
}
uintptr_t HosMessageQueue::PeekInternal() {
/* Ensure we don't corrupt the queue, but this should never happen. */
if (this->count == 0) {
std::abort();
}
return this->buffer[this->offset];
}

View File

@@ -18,8 +18,8 @@
#include <switch.h>
#include <stratosphere.hpp>
static HosMutex g_server_query_mutex;
static HosThread g_server_query_manager_thread;
static sts::os::Mutex g_server_query_mutex;
static sts::os::Thread g_server_query_manager_thread;
static SessionManagerBase *g_server_query_manager = nullptr;
static void ServerQueryManagerThreadFunc(void *arg) {
@@ -27,7 +27,7 @@ static void ServerQueryManagerThreadFunc(void *arg) {
}
void RegisterMitmServerQueryHandle(Handle query_h, ServiceObjectHolder &&service) {
std::scoped_lock<HosMutex> lock(g_server_query_mutex);
std::scoped_lock lock(g_server_query_mutex);
const bool exists = g_server_query_manager != nullptr;
if (!exists) {

View File

@@ -0,0 +1,235 @@
/*
* Copyright (c) 2018-2019 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/>.
*/
#include <stratosphere.hpp>
namespace sts::os {
void MessageQueue::SendInternal(uintptr_t data) {
/* Ensure we don't corrupt the queue, but this should never happen. */
if (this->count >= this->capacity) {
std::abort();
}
/* Write data to tail of queue. */
this->buffer[(this->count++ + this->offset) % this->capacity] = data;
}
void MessageQueue::SendNextInternal(uintptr_t data) {
/* Ensure we don't corrupt the queue, but this should never happen. */
if (this->count >= this->capacity) {
std::abort();
}
/* Write data to head of queue. */
this->offset = (this->offset + this->capacity - 1) % this->capacity;
this->buffer[this->offset] = data;
this->count++;
}
uintptr_t MessageQueue::ReceiveInternal() {
/* Ensure we don't corrupt the queue, but this should never happen. */
if (this->count == 0) {
std::abort();
}
uintptr_t data = this->buffer[this->offset];
this->offset = (this->offset + 1) % this->capacity;
this->count--;
return data;
}
uintptr_t MessageQueue::PeekInternal() {
/* Ensure we don't corrupt the queue, but this should never happen. */
if (this->count == 0) {
std::abort();
}
return this->buffer[this->offset];
}
void MessageQueue::Send(uintptr_t data) {
/* Acquire mutex, wait sendable. */
std::scoped_lock lock(this->queue_lock);
while (this->IsFull()) {
this->cv_not_full.Wait(&this->queue_lock);
}
/* Send, signal. */
this->SendInternal(data);
this->cv_not_empty.WakeAll();
}
bool MessageQueue::TrySend(uintptr_t data) {
std::scoped_lock lock(this->queue_lock);
if (this->IsFull()) {
return false;
}
/* Send, signal. */
this->SendInternal(data);
this->cv_not_empty.WakeAll();
return true;
}
bool MessageQueue::TimedSend(uintptr_t data, u64 timeout) {
std::scoped_lock lock(this->queue_lock);
TimeoutHelper timeout_helper(timeout);
while (this->IsFull()) {
if (timeout_helper.TimedOut()) {
return false;
}
this->cv_not_full.TimedWait(&this->queue_lock, timeout);
}
/* Send, signal. */
this->SendInternal(data);
this->cv_not_empty.WakeAll();
return true;
}
void MessageQueue::SendNext(uintptr_t data) {
/* Acquire mutex, wait sendable. */
std::scoped_lock lock(this->queue_lock);
while (this->IsFull()) {
this->cv_not_full.Wait(&this->queue_lock);
}
/* Send, signal. */
this->SendNextInternal(data);
this->cv_not_empty.WakeAll();
}
bool MessageQueue::TrySendNext(uintptr_t data) {
std::scoped_lock lock(this->queue_lock);
if (this->IsFull()) {
return false;
}
/* Send, signal. */
this->SendNextInternal(data);
this->cv_not_empty.WakeAll();
return true;
}
bool MessageQueue::TimedSendNext(uintptr_t data, u64 timeout) {
std::scoped_lock lock(this->queue_lock);
TimeoutHelper timeout_helper(timeout);
while (this->IsFull()) {
if (timeout_helper.TimedOut()) {
return false;
}
this->cv_not_full.TimedWait(&this->queue_lock, timeout);
}
/* Send, signal. */
this->SendNextInternal(data);
this->cv_not_empty.WakeAll();
return true;
}
void MessageQueue::Receive(uintptr_t *out) {
/* Acquire mutex, wait receivable. */
std::scoped_lock lock(this->queue_lock);
while (this->IsEmpty()) {
this->cv_not_empty.Wait(&this->queue_lock);
}
/* Receive, signal. */
*out = this->ReceiveInternal();
this->cv_not_full.WakeAll();
}
bool MessageQueue::TryReceive(uintptr_t *out) {
/* Acquire mutex, wait receivable. */
std::scoped_lock lock(this->queue_lock);
if (this->IsEmpty()) {
return false;
}
/* Receive, signal. */
*out = this->ReceiveInternal();
this->cv_not_full.WakeAll();
return true;
}
bool MessageQueue::TimedReceive(uintptr_t *out, u64 timeout) {
std::scoped_lock lock(this->queue_lock);
TimeoutHelper timeout_helper(timeout);
while (this->IsEmpty()) {
if (timeout_helper.TimedOut()) {
return false;
}
this->cv_not_empty.TimedWait(&this->queue_lock, timeout);
}
/* Receive, signal. */
*out = this->ReceiveInternal();
this->cv_not_full.WakeAll();
return true;
}
void MessageQueue::Peek(uintptr_t *out) {
/* Acquire mutex, wait receivable. */
std::scoped_lock lock(this->queue_lock);
while (this->IsEmpty()) {
this->cv_not_empty.Wait(&this->queue_lock);
}
/* Peek. */
*out = this->PeekInternal();
}
bool MessageQueue::TryPeek(uintptr_t *out) {
/* Acquire mutex, wait receivable. */
std::scoped_lock lock(this->queue_lock);
if (this->IsEmpty()) {
return false;
}
/* Peek. */
*out = this->PeekInternal();
return true;
}
bool MessageQueue::TimedPeek(uintptr_t *out, u64 timeout) {
std::scoped_lock lock(this->queue_lock);
TimeoutHelper timeout_helper(timeout);
while (this->IsEmpty()) {
if (timeout_helper.TimedOut()) {
return false;
}
this->cv_not_empty.TimedWait(&this->queue_lock, timeout);
}
/* Peek. */
*out = this->PeekInternal();
return true;
}
}

View File

@@ -25,26 +25,26 @@ namespace sts::pm::info {
namespace {
/* Global lock. */
HosMutex g_info_lock;
os::Mutex g_info_lock;
std::set<u64> g_cached_launched_titles;
}
/* Information API. */
Result GetTitleId(ncm::TitleId *out_title_id, u64 process_id) {
std::scoped_lock<HosMutex> lk(g_info_lock);
std::scoped_lock lk(g_info_lock);
return pminfoGetTitleId(reinterpret_cast<u64 *>(out_title_id), process_id);
}
Result GetProcessId(u64 *out_process_id, ncm::TitleId title_id) {
std::scoped_lock<HosMutex> lk(g_info_lock);
std::scoped_lock lk(g_info_lock);
return pminfoAtmosphereGetProcessId(out_process_id, static_cast<u64>(title_id));
}
Result WEAK HasLaunchedTitle(bool *out, ncm::TitleId title_id) {
std::scoped_lock<HosMutex> lk(g_info_lock);
std::scoped_lock lk(g_info_lock);
if (g_cached_launched_titles.find(static_cast<u64>(title_id)) != g_cached_launched_titles.end()) {
*out = true;

View File

@@ -21,17 +21,17 @@ namespace sts::sm::impl {
namespace {
/* Globals. */
HosRecursiveMutex g_user_session_mutex;
HosRecursiveMutex g_mitm_session_mutex;
os::RecursiveMutex g_user_session_mutex;
os::RecursiveMutex g_mitm_session_mutex;
}
/* Utilities. */
HosRecursiveMutex &GetUserSessionMutex() {
os::RecursiveMutex &GetUserSessionMutex() {
return g_user_session_mutex;
}
HosRecursiveMutex &GetMitmSessionMutex() {
os::RecursiveMutex &GetMitmSessionMutex() {
return g_mitm_session_mutex;
}

View File

@@ -25,12 +25,12 @@
namespace sts::sm::impl {
/* Utilities. */
HosRecursiveMutex &GetUserSessionMutex();
HosRecursiveMutex &GetMitmSessionMutex();
os::RecursiveMutex &GetUserSessionMutex();
os::RecursiveMutex &GetMitmSessionMutex();
template<typename F>
Result DoWithUserSession(F f) {
std::scoped_lock<HosRecursiveMutex &> lk(GetUserSessionMutex());
std::scoped_lock<os::RecursiveMutex &> lk(GetUserSessionMutex());
{
R_ASSERT(smInitialize());
ON_SCOPE_EXIT { smExit(); };
@@ -41,7 +41,7 @@ namespace sts::sm::impl {
template<typename F>
Result DoWithMitmSession(F f) {
std::scoped_lock<HosRecursiveMutex &> lk(GetMitmSessionMutex());
std::scoped_lock<os::RecursiveMutex &> lk(GetMitmSessionMutex());
{
R_ASSERT(smAtmosphereMitmInitialize());
ON_SCOPE_EXIT { smAtmosphereMitmExit(); };

View File

@@ -17,14 +17,8 @@
#include <switch.h>
#include <stratosphere.hpp>
static HosRecursiveMutex g_sm_session_lock;
static HosRecursiveMutex g_sm_mitm_session_lock;
static sts::os::RecursiveMutex g_sm_session_lock;
HosRecursiveMutex &GetSmSessionMutex() {
sts::os::RecursiveMutex &GetSmSessionMutex() {
return g_sm_session_lock;
}
HosRecursiveMutex &GetSmMitmSessionMutex() {
return g_sm_mitm_session_lock;
}