2023-02-18 16:26:48 -05:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <functional>
|
2023-02-19 08:44:54 -05:00
|
|
|
#include <list>
|
2023-02-18 16:26:48 -05:00
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "common/polyfill_thread.h"
|
2023-06-20 11:41:38 -04:00
|
|
|
#include "common/thread.h"
|
2023-02-18 16:26:48 -05:00
|
|
|
#include "core/hle/result.h"
|
2023-12-09 11:22:42 -05:00
|
|
|
#include "core/hle/service/hle_ipc.h"
|
2023-02-18 16:26:48 -05:00
|
|
|
#include "core/hle/service/mutex.h"
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
class System;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
class KEvent;
|
|
|
|
class KServerPort;
|
|
|
|
class KServerSession;
|
|
|
|
class KSynchronizationObject;
|
|
|
|
} // namespace Kernel
|
|
|
|
|
|
|
|
namespace Service {
|
|
|
|
|
|
|
|
class ServerManager {
|
|
|
|
public:
|
|
|
|
explicit ServerManager(Core::System& system);
|
|
|
|
~ServerManager();
|
|
|
|
|
|
|
|
Result RegisterSession(Kernel::KServerSession* session,
|
2023-02-19 14:42:12 -05:00
|
|
|
std::shared_ptr<SessionRequestManager> manager);
|
2023-12-09 11:22:42 -05:00
|
|
|
Result RegisterNamedService(const std::string& service_name,
|
|
|
|
SessionRequestHandlerFactory&& handler_factory,
|
|
|
|
u32 max_sessions = 64);
|
2023-02-18 16:26:48 -05:00
|
|
|
Result RegisterNamedService(const std::string& service_name,
|
2023-02-19 14:42:12 -05:00
|
|
|
std::shared_ptr<SessionRequestHandler>&& handler,
|
2023-02-18 16:26:48 -05:00
|
|
|
u32 max_sessions = 64);
|
|
|
|
Result ManageNamedPort(const std::string& service_name,
|
2023-12-09 11:22:42 -05:00
|
|
|
SessionRequestHandlerFactory&& handler_factory, u32 max_sessions = 64);
|
2023-02-19 08:44:54 -05:00
|
|
|
Result ManageDeferral(Kernel::KEvent** out_event);
|
2023-02-18 16:26:48 -05:00
|
|
|
|
|
|
|
Result LoopProcess();
|
|
|
|
void StartAdditionalHostThreads(const char* name, size_t num_threads);
|
|
|
|
|
|
|
|
static void RunServer(std::unique_ptr<ServerManager>&& server);
|
|
|
|
|
|
|
|
private:
|
2023-02-19 08:44:54 -05:00
|
|
|
struct RequestState;
|
|
|
|
|
2023-02-18 16:26:48 -05:00
|
|
|
Result LoopProcessImpl();
|
|
|
|
Result WaitAndProcessImpl();
|
2023-12-09 11:22:42 -05:00
|
|
|
Result OnPortEvent(Kernel::KServerPort* port, SessionRequestHandlerFactory&& handler_factory);
|
2023-02-18 16:26:48 -05:00
|
|
|
Result OnSessionEvent(Kernel::KServerSession* session,
|
2023-02-19 14:42:12 -05:00
|
|
|
std::shared_ptr<SessionRequestManager>&& manager);
|
2023-02-19 08:44:54 -05:00
|
|
|
Result OnDeferralEvent(std::list<RequestState>&& deferrals);
|
|
|
|
Result CompleteSyncRequest(RequestState&& state);
|
2023-02-18 16:26:48 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
Core::System& m_system;
|
|
|
|
Mutex m_serve_mutex;
|
|
|
|
std::mutex m_list_mutex;
|
|
|
|
|
|
|
|
// Guest state tracking
|
2023-12-09 11:22:42 -05:00
|
|
|
std::map<Kernel::KServerPort*, SessionRequestHandlerFactory> m_ports{};
|
2023-02-19 14:42:12 -05:00
|
|
|
std::map<Kernel::KServerSession*, std::shared_ptr<SessionRequestManager>> m_sessions{};
|
2023-02-18 16:26:48 -05:00
|
|
|
Kernel::KEvent* m_event{};
|
2023-02-19 08:44:54 -05:00
|
|
|
Kernel::KEvent* m_deferral_event{};
|
|
|
|
|
|
|
|
// Deferral tracking
|
|
|
|
struct RequestState {
|
|
|
|
Kernel::KServerSession* session;
|
2023-02-19 14:42:12 -05:00
|
|
|
std::shared_ptr<HLERequestContext> context;
|
|
|
|
std::shared_ptr<SessionRequestManager> manager;
|
2023-02-19 08:44:54 -05:00
|
|
|
};
|
|
|
|
std::list<RequestState> m_deferrals{};
|
2023-02-18 16:26:48 -05:00
|
|
|
|
|
|
|
// Host state tracking
|
2023-06-20 11:41:38 -04:00
|
|
|
Common::Event m_stopped{};
|
2023-02-18 16:26:48 -05:00
|
|
|
std::vector<std::jthread> m_threads{};
|
|
|
|
std::stop_source m_stop_source{};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Service
|