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
|
|
|
|
|
2023-02-19 08:44:54 -05:00
|
|
|
#include <list>
|
2023-02-18 16:26:48 -05:00
|
|
|
#include <mutex>
|
2024-02-07 12:09:26 -05:00
|
|
|
#include <optional>
|
2023-02-18 16:26:48 -05:00
|
|
|
#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"
|
2024-02-07 12:09:26 -05:00
|
|
|
#include "core/hle/service/os/multi_wait.h"
|
2024-02-06 23:09:43 -05:00
|
|
|
#include "core/hle/service/os/mutex.h"
|
2023-02-18 16:26:48 -05:00
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
class System;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
class KEvent;
|
|
|
|
class KServerPort;
|
|
|
|
class KServerSession;
|
|
|
|
} // namespace Kernel
|
|
|
|
|
|
|
|
namespace Service {
|
|
|
|
|
2024-02-07 12:09:26 -05:00
|
|
|
class Port;
|
|
|
|
class Session;
|
|
|
|
|
2023-02-18 16:26:48 -05:00
|
|
|
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:
|
2024-02-07 12:09:26 -05:00
|
|
|
void LinkToDeferredList(MultiWaitHolder* holder);
|
|
|
|
void LinkDeferred();
|
|
|
|
MultiWaitHolder* WaitSignaled();
|
|
|
|
Result Process(MultiWaitHolder* holder);
|
|
|
|
bool WaitAndProcessImpl();
|
2023-02-18 16:26:48 -05:00
|
|
|
Result LoopProcessImpl();
|
2024-02-07 12:09:26 -05:00
|
|
|
|
|
|
|
Result OnPortEvent(Port* port);
|
|
|
|
Result OnSessionEvent(Session* session);
|
|
|
|
Result OnDeferralEvent();
|
|
|
|
Result CompleteSyncRequest(Session* session);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void DestroySession(Session* session);
|
2023-02-18 16:26:48 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
Core::System& m_system;
|
2024-02-07 12:09:26 -05:00
|
|
|
Mutex m_selection_mutex;
|
2023-02-18 16:26:48 -05:00
|
|
|
|
2024-02-07 12:09:26 -05:00
|
|
|
// Events
|
|
|
|
Kernel::KEvent* m_wakeup_event{};
|
2023-02-19 08:44:54 -05:00
|
|
|
Kernel::KEvent* m_deferral_event{};
|
|
|
|
|
2024-02-07 12:09:26 -05:00
|
|
|
// Deferred wait list
|
|
|
|
std::mutex m_deferred_list_mutex{};
|
|
|
|
MultiWait m_deferred_list{};
|
|
|
|
|
|
|
|
// Guest state tracking
|
|
|
|
MultiWait m_multi_wait{};
|
|
|
|
Common::IntrusiveListBaseTraits<Port>::ListType m_servers{};
|
|
|
|
Common::IntrusiveListBaseTraits<Session>::ListType m_sessions{};
|
|
|
|
std::list<Session*> m_deferred_sessions{};
|
|
|
|
std::optional<MultiWaitHolder> m_wakeup_holder{};
|
|
|
|
std::optional<MultiWaitHolder> m_deferral_holder{};
|
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
|