summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/server_manager.h
blob: fdb8af2ff167895991c822f7e9665d8223f9b844 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <atomic>
#include <functional>
#include <list>
#include <map>
#include <mutex>
#include <string_view>
#include <vector>

#include "common/polyfill_thread.h"
#include "core/hle/result.h"
#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 HLERequestContext;
class SessionRequestHandler;
class SessionRequestManager;

class ServerManager {
public:
    explicit ServerManager(Core::System& system);
    ~ServerManager();

    Result RegisterSession(Kernel::KServerSession* session,
                           std::shared_ptr<SessionRequestManager> manager);
    Result RegisterNamedService(const std::string& service_name,
                                std::shared_ptr<SessionRequestHandler>&& handler,
                                u32 max_sessions = 64);
    Result ManageNamedPort(const std::string& service_name,
                           std::shared_ptr<SessionRequestHandler>&& handler, u32 max_sessions = 64);
    Result ManageDeferral(Kernel::KEvent** out_event);

    Result LoopProcess();
    void StartAdditionalHostThreads(const char* name, size_t num_threads);

    static void RunServer(std::unique_ptr<ServerManager>&& server);

private:
    struct RequestState;

    Result LoopProcessImpl();
    Result WaitAndProcessImpl();
    Result OnPortEvent(Kernel::KServerPort* port, std::shared_ptr<SessionRequestHandler>&& handler);
    Result OnSessionEvent(Kernel::KServerSession* session,
                          std::shared_ptr<SessionRequestManager>&& manager);
    Result OnDeferralEvent(std::list<RequestState>&& deferrals);
    Result CompleteSyncRequest(RequestState&& state);

private:
    Core::System& m_system;
    Mutex m_serve_mutex;
    std::mutex m_list_mutex;

    // Guest state tracking
    std::map<Kernel::KServerPort*, std::shared_ptr<SessionRequestHandler>> m_ports{};
    std::map<Kernel::KServerSession*, std::shared_ptr<SessionRequestManager>> m_sessions{};
    Kernel::KEvent* m_event{};
    Kernel::KEvent* m_deferral_event{};

    // Deferral tracking
    struct RequestState {
        Kernel::KServerSession* session;
        std::shared_ptr<HLERequestContext> context;
        std::shared_ptr<SessionRequestManager> manager;
    };
    std::list<RequestState> m_deferrals{};

    // Host state tracking
    std::atomic<bool> m_stopped{};
    std::vector<std::jthread> m_threads{};
    std::stop_source m_stop_source{};
};

} // namespace Service