Atmosphere/libraries/libmesosphere/source/kern_k_server_port.cpp

157 lines
4.5 KiB
C++
Raw Normal View History

2020-03-14 07:12:47 +01:00
/*
* Copyright (c) Atmosphère-NX
2020-03-14 07:12:47 +01:00
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mesosphere.hpp>
namespace ams::kern {
void KServerPort::Initialize(KPort *parent) {
/* Set member variables. */
m_parent = parent;
2020-03-14 07:12:47 +01:00
}
bool KServerPort::IsLight() const {
return this->GetParent()->IsLight();
}
void KServerPort::CleanupSessions() {
/* Ensure our preconditions are met. */
2020-08-04 03:11:13 +02:00
if (this->IsLight()) {
MESOSPHERE_ASSERT(m_session_list.empty());
2020-08-04 03:11:13 +02:00
} else {
MESOSPHERE_ASSERT(m_light_session_list.empty());
2020-08-04 03:11:13 +02:00
}
2020-03-14 07:12:47 +01:00
/* Cleanup the session list. */
while (true) {
/* Get the last session in the list. */
2020-03-14 07:12:47 +01:00
KServerSession *session = nullptr;
{
KScopedSchedulerLock sl;
if (!m_session_list.empty()) {
session = std::addressof(m_session_list.front());
m_session_list.pop_front();
2020-03-14 07:12:47 +01:00
}
}
/* Close the session. */
if (session != nullptr) {
session->Close();
} else {
break;
}
}
/* Cleanup the light session list. */
while (true) {
/* Get the last session in the list. */
2020-03-14 07:12:47 +01:00
KLightServerSession *session = nullptr;
{
KScopedSchedulerLock sl;
if (!m_light_session_list.empty()) {
session = std::addressof(m_light_session_list.front());
m_light_session_list.pop_front();
2020-03-14 07:12:47 +01:00
}
}
/* Close the session. */
if (session != nullptr) {
session->Close();
} else {
break;
}
}
}
void KServerPort::Destroy() {
/* Note with our parent that we're closed. */
2020-12-26 05:11:34 +01:00
m_parent->OnServerClosed();
2020-03-14 07:12:47 +01:00
/* Perform necessary cleanup of our session lists. */
this->CleanupSessions();
/* Close our reference to our parent. */
m_parent->Close();
2020-03-14 07:12:47 +01:00
}
bool KServerPort::IsSignaled() const {
MESOSPHERE_ASSERT_THIS();
if (this->IsLight()) {
return !m_light_session_list.empty();
2020-03-14 07:12:47 +01:00
} else {
return !m_session_list.empty();
2020-07-09 23:49:51 +02:00
}
}
void KServerPort::EnqueueSession(KServerSession *session) {
MESOSPHERE_ASSERT_THIS();
MESOSPHERE_ASSERT(!this->IsLight());
KScopedSchedulerLock sl;
/* Add the session to our queue. */
m_session_list.push_back(*session);
if (m_session_list.size() == 1) {
2020-07-09 23:49:51 +02:00
this->NotifyAvailable();
}
}
void KServerPort::EnqueueSession(KLightServerSession *session) {
MESOSPHERE_ASSERT_THIS();
MESOSPHERE_ASSERT(this->IsLight());
KScopedSchedulerLock sl;
/* Add the session to our queue. */
m_light_session_list.push_back(*session);
if (m_light_session_list.size() == 1) {
2020-07-09 23:49:51 +02:00
this->NotifyAvailable();
2020-03-14 07:12:47 +01:00
}
}
2020-07-10 02:49:33 +02:00
KServerSession *KServerPort::AcceptSession() {
MESOSPHERE_ASSERT_THIS();
MESOSPHERE_ASSERT(!this->IsLight());
KScopedSchedulerLock sl;
/* Return the first session in the list. */
if (m_session_list.empty()) {
2020-07-10 02:49:33 +02:00
return nullptr;
}
KServerSession *session = std::addressof(m_session_list.front());
m_session_list.pop_front();
2020-07-10 02:49:33 +02:00
return session;
}
KLightServerSession *KServerPort::AcceptLightSession() {
MESOSPHERE_ASSERT_THIS();
MESOSPHERE_ASSERT(this->IsLight());
KScopedSchedulerLock sl;
/* Return the first session in the list. */
if (m_light_session_list.empty()) {
2020-07-10 02:49:33 +02:00
return nullptr;
}
KLightServerSession *session = std::addressof(m_light_session_list.front());
m_light_session_list.pop_front();
2020-07-10 02:49:33 +02:00
return session;
}
2020-03-14 07:12:47 +01:00
}