meso: Implement ConnectLight

This commit is contained in:
TuxSH
2018-11-13 01:14:55 +01:00
committed by Michael Scire
parent efe7325af3
commit ad879ca327
12 changed files with 73 additions and 18 deletions

View File

@@ -2,6 +2,7 @@
#include <mesosphere/threading/KScopedCriticalSection.hpp>
#include <mesosphere/threading/KThread.hpp>
#include <mesosphere/core/KCoreContext.hpp>
#include <mesosphere/core/make_object.hpp>
namespace mesosphere
{
@@ -14,7 +15,30 @@ KClientPort::~KClientPort()
bool KClientPort::IsSignaled() const
{
return false; // TODO
return numSessions.load() < maxSessions;
}
std::tuple<Result, SharedPtr<KLightClientSession>> KClientPort::ConnectLight()
{
using RetType = std::tuple<Result, SharedPtr<KLightClientSession>>;
// Official kernel first checks reslimit then session max count. We will do the opposite.
int curCount = numSessions.load();
while (curCount < maxSessions || !numSessions.compare_exchange_weak(curCount, curCount + 1));
if (curCount >= maxSessions) {
return RetType{ResultKernelOutOfSessions(), nullptr};
}
auto [res, serverSession, clientSession] = MakeObject<KLightSession>(parent.get());
if (res.IsSuccess()) {
serverSession.detach(); // Lifetime is now managed my KServerPort session list
return RetType{ResultSuccess(), clientSession};
} else {
if (numSessions.fetch_sub(1) == maxSessions) {
NotifyWaiters();
}
return RetType{res, nullptr};
}
}
}

View File

@@ -1,4 +1,5 @@
#include <mesosphere/processes/KLightSession.hpp>
#include <mesosphere/processes/KPort.hpp>
#include <mesosphere/core/KCoreContext.hpp>
namespace mesosphere
@@ -8,14 +9,24 @@ KLightSession::~KLightSession()
{
}
Result KLightSession::Initialize()
Result KLightSession::Initialize(KPort *parentPort)
{
SetClientServerParent();
isClientAlive = true;
isServerAlive = true;
SetResourceOwner(KCoreContext::GetCurrentInstance().GetCurrentProcess());
return ResultSuccess();
if (parentPort == nullptr) {
return ResultSuccess();
} else {
// Another difference with official kernel: if adding the session fails, the session isn't added to allocator set (since it'll be deleted right away).
Result res = parentPort->AddLightServerSession(server);
if (res.IsSuccess()) {
client.parentClientPort = &parentPort->GetClient();
}
return res;
}
}
void KLightSession::Terminate(bool fromServer)

View File

@@ -21,13 +21,13 @@ Result KPort::Initialize(int maxSessions, bool isLight)
return ResultSuccess();
}
Result KPort::AddServerSession(KLightServerSession &lightServerSession)
Result KPort::AddLightServerSession(KLightServerSession &lightServerSession)
{
KScopedCriticalSection critsec{};
if (isClientAlive || isServerAlive) {
return ResultKernelConnectionRefused();
} else {
return server.AddServerSession(lightServerSession);
return server.AddLightServerSession(lightServerSession);
}
}

View File

@@ -10,6 +10,7 @@ KServerPort::~KServerPort()
{
KScopedCriticalSection critsec{};
parent->isServerAlive = false;
// TODO
}
bool KServerPort::IsSignaled() const
@@ -21,7 +22,7 @@ bool KServerPort::IsSignaled() const
}
}
Result KServerPort::AddServerSession(KLightServerSession &lightServerSession)
Result KServerPort::AddLightServerSession(KLightServerSession &lightServerSession)
{
KScopedCriticalSection critsec{};
bool wasEmpty = lightServerSessions.empty();