mirror of
https://github.com/Atmosphere-NX/Atmosphere
synced 2025-01-03 11:11:14 +00:00
htc: add RpcTaskQueue/RpcTaskIdFreeList
This commit is contained in:
parent
82757cd1b4
commit
1867c31f63
7 changed files with 214 additions and 16 deletions
|
@ -27,6 +27,9 @@ namespace ams::htc::server::rpc {
|
|||
|
||||
constinit os::SdkMutex g_rpc_mutex;
|
||||
|
||||
constinit RpcTaskIdFreeList g_task_id_free_list;
|
||||
constinit RpcTaskTable g_task_table;
|
||||
|
||||
}
|
||||
|
||||
RpcClient::RpcClient(driver::IDriver *driver, htclow::ChannelId channel)
|
||||
|
@ -36,17 +39,18 @@ namespace ams::htc::server::rpc {
|
|||
m_receive_thread_stack(g_receive_thread_stack),
|
||||
m_send_thread_stack(g_send_thread_stack),
|
||||
m_mutex(g_rpc_mutex),
|
||||
m_task_table(),
|
||||
m_task_id_free_list(g_task_id_free_list),
|
||||
m_task_table(g_task_table),
|
||||
m_task_active(),
|
||||
m_task_queue(),
|
||||
m_cancelled(false),
|
||||
m_thread_running(false)
|
||||
{
|
||||
/* Initialize all events. */
|
||||
for (size_t i = 0; i < MaxTaskCount; ++i) {
|
||||
os::InitializeEvent(std::addressof(m_5F8_events[i]), false, os::EventClearMode_AutoClear);
|
||||
os::InitializeEvent(std::addressof(m_1138_events[i]), false, os::EventClearMode_AutoClear);
|
||||
for (size_t i = 0; i < MaxRpcCount; ++i) {
|
||||
os::InitializeEvent(std::addressof(m_receive_buffer_available_events[i]), false, os::EventClearMode_AutoClear);
|
||||
os::InitializeEvent(std::addressof(m_send_buffer_available_events[i]), false, os::EventClearMode_AutoClear);
|
||||
}
|
||||
|
||||
/* TODO: Clear all of m_3C0 array to zero. */
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,10 +17,15 @@
|
|||
#include <stratosphere.hpp>
|
||||
#include "../driver/htc_i_driver.hpp"
|
||||
#include "htc_rpc_task_table.hpp"
|
||||
#include "htc_rpc_task_queue.hpp"
|
||||
#include "htc_rpc_task_id_free_list.hpp"
|
||||
|
||||
namespace ams::htc::server::rpc {
|
||||
|
||||
class RpcClient {
|
||||
private:
|
||||
/* TODO: where is this value coming from, again? */
|
||||
static constexpr size_t BufferSize = 0xE400;
|
||||
private:
|
||||
u64 m_00;
|
||||
driver::IDriver *m_driver;
|
||||
|
@ -30,14 +35,16 @@ namespace ams::htc::server::rpc {
|
|||
os::ThreadType m_receive_thread;
|
||||
os::ThreadType m_send_thread;
|
||||
os::SdkMutex &m_mutex;
|
||||
/* TODO: m_task_id_free_list */
|
||||
RpcTaskTable m_task_table;
|
||||
/* TODO: m_3C0[MaxTaskCount] */
|
||||
/* TODO: m_rpc_task_queue */
|
||||
RpcTaskIdFreeList &m_task_id_free_list;
|
||||
RpcTaskTable &m_task_table;
|
||||
bool m_task_active[MaxRpcCount];
|
||||
RpcTaskQueue m_task_queue;
|
||||
bool m_cancelled;
|
||||
bool m_thread_running;
|
||||
os::EventType m_5F8_events[MaxTaskCount];
|
||||
os::EventType m_1138_events[MaxTaskCount];
|
||||
os::EventType m_receive_buffer_available_events[MaxRpcCount];
|
||||
os::EventType m_send_buffer_available_events[MaxRpcCount];
|
||||
u8 m_receive_buffer[BufferSize];
|
||||
u8 m_send_buffer[BufferSize];
|
||||
public:
|
||||
RpcClient(driver::IDriver *driver, htclow::ChannelId channel);
|
||||
};
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* 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 <stratosphere.hpp>
|
||||
#include "htc_rpc_tasks.hpp"
|
||||
#include "htc_htcmisc_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htc::server::rpc {
|
||||
|
||||
class RpcTaskIdFreeList {
|
||||
private:
|
||||
u32 m_task_ids[MaxRpcCount];
|
||||
u32 m_offset;
|
||||
u32 m_free_count;
|
||||
public:
|
||||
constexpr RpcTaskIdFreeList() : m_task_ids(), m_offset(0), m_free_count(MaxRpcCount) {
|
||||
for (auto i = 0; i < static_cast<int>(MaxRpcCount); ++i) {
|
||||
m_task_ids[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
Result Allocate(u32 *out) {
|
||||
/* Check that we have free tasks. */
|
||||
R_UNLESS(m_free_count > 0, htc::ResultOutOfRpcTask());
|
||||
|
||||
/* Get index. */
|
||||
const auto index = m_offset;
|
||||
m_free_count = (m_free_count + 1) % MaxRpcCount;
|
||||
--m_free_count;
|
||||
|
||||
/* Get the task id. */
|
||||
*out = m_task_ids[index];
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
void Free(u32 task_id) {
|
||||
/* Check pre-conditions. */
|
||||
AMS_ASSERT(m_free_count < static_cast<int>(MaxRpcCount));
|
||||
|
||||
/* Determine index. */
|
||||
const auto index = ((m_free_count++) + m_offset) % MaxRpcCount;
|
||||
|
||||
/* Set the task id. */
|
||||
m_task_ids[index] = task_id;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
/*
|
||||
* 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 <stratosphere.hpp>
|
||||
#include "htc_rpc_tasks.hpp"
|
||||
#include "htc_htcmisc_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htc::server::rpc {
|
||||
|
||||
class RpcTaskQueue {
|
||||
private:
|
||||
u32 m_task_ids[MaxRpcCount];
|
||||
PacketCategory m_task_categories[MaxRpcCount];
|
||||
int m_offset;
|
||||
int m_count;
|
||||
os::SdkConditionVariable m_cv;
|
||||
os::SdkMutex m_mutex;
|
||||
bool m_cancelled;
|
||||
public:
|
||||
constexpr RpcTaskQueue() = default;
|
||||
|
||||
void Initialize() {
|
||||
m_offset = 0;
|
||||
m_count = 0;
|
||||
m_cancelled = false;
|
||||
}
|
||||
|
||||
void Finalize() {
|
||||
m_offset = 0;
|
||||
m_count = 0;
|
||||
}
|
||||
|
||||
void Cancel() {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
/* Cancel ourselves. */
|
||||
m_cancelled = true;
|
||||
|
||||
/* Signal to consumers/producers. */
|
||||
m_cv.Signal();
|
||||
}
|
||||
|
||||
void Add(u32 task_id, PacketCategory category) {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
/* Check pre-conditions. */
|
||||
AMS_ASSERT(m_count < static_cast<int>(MaxRpcCount));
|
||||
|
||||
/* Determine index. */
|
||||
const auto index = ((m_count++) + m_offset) % MaxRpcCount;
|
||||
|
||||
/* Set task. */
|
||||
m_task_ids[index] = task_id;
|
||||
m_task_categories[index] = category;
|
||||
|
||||
/* Signal. */
|
||||
if (m_count > 0) {
|
||||
m_cv.Signal();
|
||||
}
|
||||
}
|
||||
|
||||
Result Take(u32 *out_id, PacketCategory *out_category) {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
/* Wait until we can take. */
|
||||
while (m_count == 0 && !m_cancelled) {
|
||||
m_cv.Wait(m_mutex);
|
||||
}
|
||||
|
||||
/* Check that we're not cancelled. */
|
||||
R_UNLESS(!m_cancelled, htc::ResultCancelled());
|
||||
|
||||
/* Determine index. */
|
||||
const auto index = m_offset;
|
||||
|
||||
/* Advance the queue. */
|
||||
m_offset = (m_offset + 1) % MaxRpcCount;
|
||||
--m_count;
|
||||
|
||||
/* Return the task info. */
|
||||
*out_id = m_task_ids[index];
|
||||
*out_category = m_task_categories[index];
|
||||
return ResultSuccess();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
|
@ -36,8 +36,8 @@ namespace ams::htc::server::rpc {
|
|||
static constexpr size_t MaxTaskSize = 0xA000;
|
||||
using TaskStorage = typename std::aligned_storage<MaxTaskSize, alignof(void *)>::type;
|
||||
private:
|
||||
bool m_valid[MaxTaskCount];
|
||||
TaskStorage m_storages[MaxTaskCount];
|
||||
bool m_valid[MaxRpcCount];
|
||||
TaskStorage m_storages[MaxRpcCount];
|
||||
private:
|
||||
template<typename T>
|
||||
ALWAYS_INLINE T *GetPointer(u32 index) {
|
||||
|
@ -47,7 +47,7 @@ namespace ams::htc::server::rpc {
|
|||
}
|
||||
|
||||
ALWAYS_INLINE bool IsValid(u32 index) {
|
||||
return index < MaxTaskCount && m_valid[index];
|
||||
return index < MaxRpcCount && m_valid[index];
|
||||
}
|
||||
public:
|
||||
constexpr RpcTaskTable() = default;
|
||||
|
|
|
@ -18,7 +18,25 @@
|
|||
|
||||
namespace ams::htc::server::rpc {
|
||||
|
||||
constexpr inline size_t MaxTaskCount = 0x48;
|
||||
constexpr inline size_t MaxRpcCount = 0x48;
|
||||
|
||||
enum class PacketCategory : s16 {
|
||||
Request = 0,
|
||||
Response = 1,
|
||||
Notification = 2,
|
||||
};
|
||||
|
||||
struct RpcPacket {
|
||||
s16 protocol;
|
||||
s16 version;
|
||||
PacketCategory category;
|
||||
u16 type;
|
||||
s64 body_size;
|
||||
u32 task_id;
|
||||
u64 params[5];
|
||||
u8 data[];
|
||||
};
|
||||
static_assert(sizeof(RpcPacket) == 0x40);
|
||||
|
||||
enum class RpcTaskCancelReason {
|
||||
None = 0,
|
||||
|
|
|
@ -23,6 +23,11 @@ namespace ams::htc {
|
|||
R_DEFINE_ERROR_RESULT(ConnectionFailure, 1);
|
||||
R_DEFINE_ERROR_RESULT(NotFound, 2);
|
||||
R_DEFINE_ERROR_RESULT(NotEnoughBuffer, 3);
|
||||
|
||||
R_DEFINE_ERROR_RESULT(Cancelled, 101);
|
||||
|
||||
R_DEFINE_ERROR_RESULT(Unknown, 1023);
|
||||
|
||||
R_DEFINE_ERROR_RESULT(OutOfRpcTask, 2102);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue