kern: SvcConnectToNamedPort

This commit is contained in:
Michael Scire 2020-07-09 14:49:51 -07:00
parent a2eb93fde8
commit 7400a8ff68
15 changed files with 376 additions and 15 deletions

View file

@ -25,6 +25,19 @@ namespace ams::kern {
this->max_sessions = max_sessions;
}
void KClientPort::OnSessionFinalized() {
KScopedSchedulerLock sl;
const auto prev = this->num_sessions--;
if (prev == this->max_sessions) {
this->NotifyAvailable();
}
}
void KClientPort::OnServerClosed() {
MESOSPHERE_ASSERT_THIS();
}
bool KClientPort::IsLight() const {
return this->GetParent()->IsLight();
}
@ -43,4 +56,71 @@ namespace ams::kern {
return this->num_sessions < this->max_sessions;
}
Result KClientPort::CreateSession(KClientSession **out) {
MESOSPHERE_ASSERT_THIS();
/* Reserve a new session from the resource limit. */
KScopedResourceReservation session_reservation(GetCurrentProcessPointer(), ams::svc::LimitableResource_SessionCountMax);
R_UNLESS(session_reservation.Succeeded(), svc::ResultLimitReached());
/* Update the session counts. */
{
/* Atomically increment the number of sessions. */
s32 new_sessions;
{
const auto max = this->max_sessions;
auto cur_sessions = this->num_sessions.load(std::memory_order_acquire);
do {
R_UNLESS(cur_sessions < max, svc::ResultOutOfSessions());
new_sessions = cur_sessions + 1;
} while (!this->num_sessions.compare_exchange_weak(cur_sessions, new_sessions, std::memory_order_relaxed));
}
/* Atomically update the peak session tracking. */
{
auto peak = this->peak_sessions.load(std::memory_order_acquire);
do {
if (peak >= new_sessions) {
break;
}
} while (!this->peak_sessions.compare_exchange_weak(peak, new_sessions, std::memory_order_relaxed));
}
}
/* Create a new session. */
KSession *session = KSession::Create();
if (session == nullptr) {
/* Decrement the session count. */
const auto prev = this->num_sessions--;
if (prev == this->max_sessions) {
this->NotifyAvailable();
}
return svc::ResultOutOfResource();
}
/* Initialize the session. */
session->Initialize(this, this->parent->GetName());
/* Commit the session reservation. */
session_reservation.Commit();
/* Register the session. */
KSession::Register(session);
auto session_guard = SCOPE_GUARD {
session->GetClientSession().Close();
session->GetServerSession().Close();
};
/* Enqueue the session with our parent. */
R_TRY(this->parent->EnqueueSession(std::addressof(session->GetServerSession())));
/* We succeeded, so set the output. */
session_guard.Cancel();
*out = std::addressof(session->GetClientSession());
return ResultSuccess();
}
}