summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_session.h
blob: c6ead403b66c5f2b9e1ecdaa0109e2f2defc518f (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
91
92
93
94
95
96
97
98
99
100
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <atomic>
#include <string>

#include "core/hle/kernel/k_client_session.h"
#include "core/hle/kernel/k_server_session.h"
#include "core/hle/kernel/slab_helpers.h"

namespace Kernel {

class SessionRequestManager;

class KSession final : public KAutoObjectWithSlabHeapAndContainer<KSession, KAutoObjectWithList> {
    KERNEL_AUTOOBJECT_TRAITS(KSession, KAutoObject);

public:
    explicit KSession(KernelCore& kernel_);
    ~KSession() override;

    void Initialize(KClientPort* port_, const std::string& name_,
                    std::shared_ptr<SessionRequestManager> manager_ = nullptr);

    void Finalize() override;

    bool IsInitialized() const override {
        return initialized;
    }

    uintptr_t GetPostDestroyArgument() const override {
        return reinterpret_cast<uintptr_t>(process);
    }

    static void PostDestroy(uintptr_t arg);

    void OnServerClosed();

    void OnClientClosed();

    bool IsServerClosed() const {
        return this->GetState() != State::Normal;
    }

    bool IsClientClosed() const {
        return this->GetState() != State::Normal;
    }

    KClientSession& GetClientSession() {
        return client;
    }

    KServerSession& GetServerSession() {
        return server;
    }

    const KClientSession& GetClientSession() const {
        return client;
    }

    const KServerSession& GetServerSession() const {
        return server;
    }

    const KClientPort* GetParent() const {
        return port;
    }

    KClientPort* GetParent() {
        return port;
    }

private:
    enum class State : u8 {
        Invalid = 0,
        Normal = 1,
        ClientClosed = 2,
        ServerClosed = 3,
    };

    void SetState(State state) {
        atomic_state = static_cast<u8>(state);
    }

    State GetState() const {
        return static_cast<State>(atomic_state.load(std::memory_order_relaxed));
    }

    KServerSession server;
    KClientSession client;
    std::atomic<std::underlying_type_t<State>> atomic_state{
        static_cast<std::underlying_type_t<State>>(State::Invalid)};
    KClientPort* port{};
    KProcess* process{};
    bool initialized{};
};

} // namespace Kernel