From be3550d3825fc8c5024d2932f2fa18c9d95851cf Mon Sep 17 00:00:00 2001 From: TuxSH Date: Mon, 12 Nov 2018 09:54:22 +0100 Subject: [PATCH] meso: Implement KLightServerSession dtor --- .../processes/KLightClientSession.hpp | 1 - .../processes/KLightServerSession.hpp | 7 +++-- .../mesosphere/processes/KLightSession.hpp | 8 +++++ .../include/mesosphere/threading/KThread.hpp | 10 ++++++- .../source/processes/KLightClientSession.cpp | 4 ++- .../source/processes/KLightServerSession.cpp | 29 ++++++++++++++++++- mesosphere/source/processes/KLightSession.cpp | 9 ++++-- mesosphere/source/threading/KThread.cpp | 2 +- 8 files changed, 60 insertions(+), 10 deletions(-) diff --git a/mesosphere/include/mesosphere/processes/KLightClientSession.hpp b/mesosphere/include/mesosphere/processes/KLightClientSession.hpp index c43157a49..2366718eb 100644 --- a/mesosphere/include/mesosphere/processes/KLightClientSession.hpp +++ b/mesosphere/include/mesosphere/processes/KLightClientSession.hpp @@ -21,7 +21,6 @@ class KLightClientSession final : public KAutoObject, public IClient currentSender{}, currentReceiver{}; - bool isRemoteActive = false; + SharedPtr currentSender{}; + KThread *currentReceiver = nullptr; }; MESOSPHERE_AUTO_OBJECT_DEFINE_INCREF(LightServerSession); diff --git a/mesosphere/include/mesosphere/processes/KLightSession.hpp b/mesosphere/include/mesosphere/processes/KLightSession.hpp index b3a4fdb2c..d80860682 100644 --- a/mesosphere/include/mesosphere/processes/KLightSession.hpp +++ b/mesosphere/include/mesosphere/processes/KLightSession.hpp @@ -24,6 +24,14 @@ class KLightSession final : virtual ~KLightSession(); Result Initialize(); + + private: + friend class KLightClientSession; + friend class KLightServerSession; + + void Terminate(bool fromServer); + bool isClientAlive = false; + bool isServerAlive = false; }; MESOSPHERE_AUTO_OBJECT_DEFINE_INCREF(LightSession); diff --git a/mesosphere/include/mesosphere/threading/KThread.hpp b/mesosphere/include/mesosphere/threading/KThread.hpp index 686171923..be7749eed 100644 --- a/mesosphere/include/mesosphere/threading/KThread.hpp +++ b/mesosphere/include/mesosphere/threading/KThread.hpp @@ -242,6 +242,14 @@ class KThread final : void CancelKernelSync(); /// Takes effect immediately void CancelKernelSync(Result res); + /// Needs to be in kernel sync + bool IsInKernelSync() const { return currentWaitList != nullptr; } + + /// User sync + constexpr bool IsWaitingSync() const { return isWaitingSync; } + void SetWaitingSync(bool isWaitingSync) { this->isWaitingSync = isWaitingSync; } + constexpr bool IsSyncCancelled() const { return isSyncCancelled; } + void SetSyncCancelled(bool isSyncCancelled) { this->isSyncCancelled = isSyncCancelled; } /// Takes effect when critical section is left void HandleSyncObjectSignaled(KSynchronizationObject *syncObj); @@ -295,7 +303,7 @@ private: uint basePriority = 64, priority = 64; int currentCoreId = -1; ulong affinityMask = 0; - bool cancelled = false; + bool isSyncCancelled = false; bool isWaitingSync = false; uiptr wantedMutex = 0; KThread *wantedMutexOwner = nullptr; diff --git a/mesosphere/source/processes/KLightClientSession.cpp b/mesosphere/source/processes/KLightClientSession.cpp index 491f22fbc..05a480633 100644 --- a/mesosphere/source/processes/KLightClientSession.cpp +++ b/mesosphere/source/processes/KLightClientSession.cpp @@ -1,11 +1,13 @@ #include +#include +#include namespace mesosphere { KLightClientSession::~KLightClientSession() { - //TODO + parent->Terminate(false); } diff --git a/mesosphere/source/processes/KLightServerSession.cpp b/mesosphere/source/processes/KLightServerSession.cpp index c0229a2ec..481278e7d 100644 --- a/mesosphere/source/processes/KLightServerSession.cpp +++ b/mesosphere/source/processes/KLightServerSession.cpp @@ -1,11 +1,13 @@ #include +#include +#include namespace mesosphere { KLightServerSession::~KLightServerSession() { - //TODO + Terminate(true); } bool KLightServerSession::IsSignaled() const @@ -13,5 +15,30 @@ bool KLightServerSession::IsSignaled() const return false; // TODO } +void KLightServerSession::Terminate(bool fromServer) +{ + SharedPtr curSender{std::move(currentSender)}; + { + KScopedCriticalSection critsec{}; + if (fromServer) { + parent->isServerAlive = false; // buggy in official kernel -- where it sets it outside of critical section + } else { + parent->isClientAlive = false; + } + if (curSender != nullptr) { + kassert(curSender->GetSchedulingStatus() == KThread::SchedulingStatus::Paused && curSender->IsInKernelSync()); + curSender->CancelKernelSync(ResultKernelConnectionClosed()); //TODO check + currentSender = nullptr; + currentReceiver = nullptr; + } + + for (auto &&sender : senderThreads) { + kassert(sender.GetSchedulingStatus() == KThread::SchedulingStatus::Paused && sender.IsInKernelSync()); + sender.CancelKernelSync(ResultKernelConnectionClosed()); //TODO check + } + + KThread::ResumeAllFromKernelSync(receiverThreads); + } +} } diff --git a/mesosphere/source/processes/KLightSession.cpp b/mesosphere/source/processes/KLightSession.cpp index 8af8bf055..12858c867 100644 --- a/mesosphere/source/processes/KLightSession.cpp +++ b/mesosphere/source/processes/KLightSession.cpp @@ -11,11 +11,16 @@ KLightSession::~KLightSession() Result KLightSession::Initialize() { SetClientServerParent(); - client.isRemoteActive = true; - server.isRemoteActive = true; + isClientAlive = true; + isServerAlive = true; SetResourceOwner(KCoreContext::GetCurrentInstance().GetCurrentProcess()); return ResultSuccess(); } +void KLightSession::Terminate(bool fromServer) +{ + server.Terminate(fromServer); +} + } diff --git a/mesosphere/source/threading/KThread.cpp b/mesosphere/source/threading/KThread.cpp index cd0a2da42..775684a77 100644 --- a/mesosphere/source/threading/KThread.cpp +++ b/mesosphere/source/threading/KThread.cpp @@ -151,7 +151,7 @@ Result KThread::WaitSynchronizationImpl(int &outId, KSynchronizationObject **syn if (IsDying()) { return ResultKernelThreadTerminating(); } - if (cancelled) { + if (isSyncCancelled) { return ResultKernelCancelled(); }