summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_server_port.cpp
blob: bb6632f58ecc0c3a343044ec34a0ee465238d2d4 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <tuple>
#include "common/assert.h"
#include "core/hle/kernel/k_client_port.h"
#include "core/hle/kernel/k_port.h"
#include "core/hle/kernel/k_scheduler.h"
#include "core/hle/kernel/k_server_port.h"
#include "core/hle/kernel/k_server_session.h"
#include "core/hle/kernel/k_thread.h"

namespace Kernel {

KServerPort::KServerPort(KernelCore& kernel) : KSynchronizationObject{kernel} {}
KServerPort::~KServerPort() = default;

void KServerPort::Initialize(KPort* parent) {
    // Set member variables.
    m_parent = parent;
}

bool KServerPort::IsLight() const {
    return this->GetParent()->IsLight();
}

void KServerPort::CleanupSessions() {
    // Ensure our preconditions are met.
    if (this->IsLight()) {
        ASSERT(m_session_list.empty());
    } else {
        ASSERT(m_light_session_list.empty());
    }

    // Cleanup the session list.
    while (true) {
        // Get the last session in the list.
        KServerSession* session = nullptr;
        {
            KScopedSchedulerLock sl{m_kernel};
            if (!m_session_list.empty()) {
                session = std::addressof(m_session_list.front());
                m_session_list.pop_front();
            }
        }

        // Close the session.
        if (session != nullptr) {
            session->Close();
        } else {
            break;
        }
    }

    // Cleanup the light session list.
    while (true) {
        // Get the last session in the list.
        KLightServerSession* session = nullptr;
        {
            KScopedSchedulerLock sl{m_kernel};
            if (!m_light_session_list.empty()) {
                session = std::addressof(m_light_session_list.front());
                m_light_session_list.pop_front();
            }
        }

        // Close the session.
        if (session != nullptr) {
            session->Close();
        } else {
            break;
        }
    }
}

void KServerPort::Destroy() {
    // Note with our parent that we're closed.
    m_parent->OnServerClosed();

    // Perform necessary cleanup of our session lists.
    this->CleanupSessions();

    // Close our reference to our parent.
    m_parent->Close();
}

bool KServerPort::IsSignaled() const {
    if (this->IsLight()) {
        return !m_light_session_list.empty();
    } else {
        return !m_session_list.empty();
    }
}

void KServerPort::EnqueueSession(KServerSession* session) {
    ASSERT(!this->IsLight());

    KScopedSchedulerLock sl{m_kernel};

    // Add the session to our queue.
    m_session_list.push_back(*session);
    if (m_session_list.size() == 1) {
        this->NotifyAvailable();
    }
}

void KServerPort::EnqueueSession(KLightServerSession* session) {
    ASSERT(this->IsLight());

    KScopedSchedulerLock sl{m_kernel};

    // Add the session to our queue.
    m_light_session_list.push_back(*session);
    if (m_light_session_list.size() == 1) {
        this->NotifyAvailable();
    }
}

KServerSession* KServerPort::AcceptSession() {
    ASSERT(!this->IsLight());

    KScopedSchedulerLock sl{m_kernel};

    // Return the first session in the list.
    if (m_session_list.empty()) {
        return nullptr;
    }

    KServerSession* session = std::addressof(m_session_list.front());
    m_session_list.pop_front();
    return session;
}

KLightServerSession* KServerPort::AcceptLightSession() {
    ASSERT(this->IsLight());

    KScopedSchedulerLock sl{m_kernel};

    // Return the first session in the list.
    if (m_light_session_list.empty()) {
        return nullptr;
    }

    KLightServerSession* session = std::addressof(m_light_session_list.front());
    m_light_session_list.pop_front();
    return session;
}

} // namespace Kernel