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

@ -35,6 +35,9 @@ MESOSPHERE_AUTO_OBJECT_FW_DECL(InterruptEvent);
MESOSPHERE_AUTO_OBJECT_FW_DECL(LightSession);
MESOSPHERE_AUTO_OBJECT_FW_DECL(LightClientSession);
MESOSPHERE_AUTO_OBJECT_FW_DECL(LightServerSession);
MESOSPHERE_AUTO_OBJECT_FW_DECL(Port);
MESOSPHERE_AUTO_OBJECT_FW_DECL(ClientPort);
MESOSPHERE_AUTO_OBJECT_FW_DECL(ServerPort);
class KAutoObject {
public:

View file

@ -97,6 +97,8 @@ class ResultError : public ResultBase<ResultError<module, description>> {
#define DEFINE_RESULT(module, name, description) class Result##module##name final : public ResultError<ResultModule::module, description> {}
DEFINE_RESULT(Kernel, OutOfSessions, 7);
DEFINE_RESULT(Kernel, InvalidCapabilityDescriptor, 14);
DEFINE_RESULT(Kernel, NotImplemented, 33);

View file

@ -40,6 +40,15 @@ auto MakeObjectRaw(Args&& ...args)
if constexpr (std::is_base_of_v<ISetAllocated<T>, T>) {
obj->AddToAllocatedSet();
}
} else {
if constexpr (std::is_base_of_v<IClientServerParentTag, T>) {
delete &obj->GetClient();
delete &obj->GetServer();
} else {
delete obj;
}
obj = nullptr;
}
cleanup:
if (doReslimitCleanup) {

View file

@ -1,10 +1,9 @@
#pragma once
#include <mesosphere/core/KSynchronizationObject.hpp>
#include <mesosphere/core/util.hpp>
#include <mesosphere/core/Result.hpp>
#include <mesosphere/interfaces/IClient.hpp>
#include <mesosphere/threading/KThread.hpp>
#include <tuple>
namespace mesosphere
{
@ -24,12 +23,16 @@ class KClientPort final :
virtual bool IsSignaled() const override;
std::tuple<Result, SharedPtr<KLightClientSession>> ConnectLight();
private:
friend class KPort;
std::atomic<int> numSessions{0}, currentCapacity{0}, maxSessions{0};
std::atomic<int> numSessions{0};
std::atomic<int> peakNumNormalSessions{0};
int maxSessions = 0;
};
MESOSPHERE_AUTO_OBJECT_DEFINE_INCREF(ClientPort);
}
}

View file

@ -24,7 +24,7 @@ class KLightClientSession final : public KAutoObject, public IClient<KLightSessi
private:
friend class KLightSession;
KClientPort *parentPort = nullptr;
SharedPtr<KClientPort> parentClientPort = nullptr;
};
MESOSPHERE_AUTO_OBJECT_DEFINE_INCREF(LightClientSession);

View file

@ -11,6 +11,8 @@
namespace mesosphere
{
class KPort;
struct LightSessionRequest {
s32 cmdId;
u32 data[6];
@ -28,7 +30,7 @@ class KLightSession final :
virtual ~KLightSession();
Result Initialize();
Result Initialize(KPort *parentPort = nullptr);
private:
friend class KLightClientSession;

View file

@ -6,7 +6,7 @@
#include <mesosphere/interfaces/ISetAllocated.hpp>
#include <mesosphere/processes/KClientPort.hpp>
#include <mesosphere/processes/KServerPort.hpp>
#include <mesosphere/processes/KLightServerSession.hpp>
#include <mesosphere/processes/KLightSession.hpp>
namespace mesosphere
{
@ -23,12 +23,12 @@ class KPort final :
Result Initialize(int maxSessions, bool isLight);
Result AddLightServerSession(KLightServerSession &lightServerSession);
private:
friend class KClientPort;
friend class KServerPort;
Result AddServerSession(KLightServerSession &lightServerSession);
bool isClientAlive = false;
bool isServerAlive = false;
bool isLight = false;
@ -36,4 +36,4 @@ class KPort final :
MESOSPHERE_AUTO_OBJECT_DEFINE_INCREF(Port);
}
}

View file

@ -23,11 +23,11 @@ class KServerPort final :
private:
friend class KPort;
Result AddServerSession(KLightServerSession &lightServerSession);
Result AddLightServerSession(KLightServerSession &lightServerSession);
KLightServerSession::List lightServerSessions{};
};
MESOSPHERE_AUTO_OBJECT_DEFINE_INCREF(ServerPort);
}
}

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();