Implement KSynchronizationObject

This commit is contained in:
TuxSH 2018-11-04 18:51:27 +01:00 committed by Michael Scire
parent 1684e1d35c
commit 7fde5fbe40
6 changed files with 82 additions and 3 deletions

View file

@ -6,7 +6,7 @@
#include <initializer_list>
#include <functional>
#include <type_traits>
#include <mesosphere/core/util.h>
#include <mesosphere/core/util.hpp>
#include <mesosphere/interfaces/ISlabAllocated.hpp>
namespace mesosphere

View file

@ -0,0 +1,26 @@
#pragma once
#include <mesosphere/core/KAutoObject.hpp>
#include <mesosphere/core/KLinkedList.hpp>
namespace mesosphere
{
class KSynchronizationObject : public KAutoObject {
public:
MESOSPHERE_AUTO_OBJECT_TRAITS(AutoObject, SynchronizationObject);
virtual ~KSynchronizationObject();
virtual bool IsSignaled() const = 0;
void Signal(); // Note: Signal() with !IsSignaled() is no-op
KLinkedList<KThread *>::const_iterator AddWaiter(KThread &thread);
void RemoveWaiter(KLinkedList<KThread *>::const_iterator it);
private:
KLinkedList<KThread *> waiters{};
};
}

View file

@ -3,6 +3,7 @@
#include <cstddef>
#include <cstdint>
#include <climits>
#include <chrono>
#include <boost/smart_ptr/intrusive_ptr.hpp>
#define MAX_CORES 4
@ -10,6 +11,8 @@
namespace mesosphere
{
using namespace std::chrono_literals;
using ushort = unsigned short;
using uint = unsigned int;
using ulong = unsigned long;

View file

@ -5,6 +5,7 @@
#include <mesosphere/core/util.hpp>
#include <mesosphere/core/Handle.hpp>
#include <mesosphere/core/Result.hpp>
#include <mesosphere/core/KSynchronizationObject.hpp>
#include <mesosphere/processes/KProcess.hpp>
#include <mesosphere/interfaces/IAlarmable.hpp>
#include <mesosphere/interfaces/ILimitedResource.hpp>
@ -206,6 +207,9 @@ class KThread final :
/// Takes effect immediately
void CancelKernelSync(Result res);
/// Takes effect when critical section is left
void HandleSyncObjectSignaled(KSynchronizationObject *syncObj);
constexpr size_t GetNumberOfKMutexWaiters() const { return numKernelMutexWaiters; }
constexpr uiptr GetWantedMutex() const { return wantedMutex; }
void SetWantedMutex(uiptr mtx) { wantedMutex = mtx; }
@ -244,8 +248,8 @@ private:
MutexWaitList mutexWaitList{};
size_t numKernelMutexWaiters = 0;
Handle syncResultHandle{};
Result syncResult = ResultSuccess();
KSynchronizationObject *signaledSyncObject = nullptr;
Result syncResult = ResultSuccess{};
u64 lastScheduledTime = 0;
};

View file

@ -0,0 +1,37 @@
#include <mesosphere/core/KSynchronizationObject.hpp>
#include <mesosphere/core/Result.hpp>
#include <mesosphere/core/KCoreContext.hpp>
#include <mesosphere/threading/KScheduler.hpp>
#include <mesosphere/threading/KThread.hpp>
#include <mutex>
namespace mesosphere
{
KSynchronizationObject::~KSynchronizationObject()
{
}
void KSynchronizationObject::Signal()
{
std::lock_guard criticalSection{KScheduler::GetCriticalSection()};
if (IsSignaled()) {
for (auto &&waiter : waiters) {
waiter->HandleSyncObjectSignaled(this);
}
}
}
KLinkedList<KThread *>::const_iterator KSynchronizationObject::AddWaiter(KThread &thread)
{
return waiters.insert(waiters.end(), &thread);
}
void KSynchronizationObject::RemoveWaiter(KLinkedList<KThread *>::const_iterator it)
{
waiters.erase(it);
}
}

View file

@ -120,6 +120,15 @@ void KThread::CancelKernelSync(Result res)
CancelKernelSync();
}
void KThread::HandleSyncObjectSignaled(KSynchronizationObject *syncObj)
{
if (GetSchedulingStatus() == SchedulingStatus::Paused) {
signaledSyncObject = syncObj;
syncResult = ResultSuccess{};
Reschedule(SchedulingStatus::Running);
}
}
void KThread::AddToMutexWaitList(KThread &thread)
{
// TODO: check&increment numKernelMutexWaiters