Implement KCriticalSection (mostly)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include <mesosphere/core/KSynchronizationObject.hpp>
|
||||
#include <mesosphere/core/Result.hpp>
|
||||
#include <mesosphere/threading/KScheduler.hpp>
|
||||
#include <mesosphere/threading/KScopedCriticalSection.hpp>
|
||||
#include <mesosphere/threading/KThread.hpp>
|
||||
|
||||
#include <mutex>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <mesosphere/processes/KWritableEvent.hpp>
|
||||
#include <mesosphere/processes/KReadableEvent.hpp>
|
||||
#include <mesosphere/processes/KEvent.hpp>
|
||||
#include <mesosphere/threading/KScheduler.hpp>
|
||||
#include <mesosphere/threading/KScopedCriticalSection.hpp>
|
||||
|
||||
namespace mesosphere
|
||||
{
|
||||
@@ -18,12 +18,12 @@ KReadableEvent::~KReadableEvent()
|
||||
Result KReadableEvent::Signal()
|
||||
{
|
||||
KScopedCriticalSection criticalSection{};
|
||||
|
||||
|
||||
if (!this->isSignaled) {
|
||||
this->isSignaled = true;
|
||||
NotifyWaiters();
|
||||
}
|
||||
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include <mesosphere/threading/KConditionVariable.hpp>
|
||||
#include <mesosphere/threading/KScheduler.hpp>
|
||||
#include <mesosphere/threading/KScopedCriticalSection.hpp>
|
||||
#include <mesosphere/core/KCoreContext.hpp>
|
||||
|
||||
namespace mesosphere
|
||||
|
||||
45
mesosphere/source/threading/KCriticalSection.cpp
Normal file
45
mesosphere/source/threading/KCriticalSection.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <mesosphere/threading/KCriticalSection.hpp>
|
||||
#include <mesosphere/threading/KScheduler.hpp>
|
||||
#include <mesosphere/core/KCoreContext.hpp>
|
||||
|
||||
namespace mesosphere
|
||||
{
|
||||
|
||||
bool KCriticalSection::try_lock()
|
||||
{
|
||||
KThread *curThread = KCoreContext::GetCurrentInstance().GetCurrentThread();
|
||||
if (curThread == lockingThread) {
|
||||
++lockCount;
|
||||
return true;
|
||||
} else if (KInterruptSpinLock<false>::try_lock()) {
|
||||
lockingThread = curThread;
|
||||
lockCount = 1;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void KCriticalSection::lock()
|
||||
{
|
||||
KThread *curThread = KCoreContext::GetCurrentInstance().GetCurrentThread();
|
||||
if (curThread == lockingThread) {
|
||||
++lockCount;
|
||||
} else {
|
||||
KInterruptSpinLock<false>::lock();
|
||||
lockingThread = curThread;
|
||||
lockCount = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void KCriticalSection::unlock()
|
||||
{
|
||||
if (--lockCount == 0) {
|
||||
lockingThread = nullptr;
|
||||
KScheduler::HandleCriticalSectionLeave();
|
||||
} else {
|
||||
KInterruptSpinLock<false>::unlock();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <mesosphere/threading/KMutex.hpp>
|
||||
#include <mesosphere/threading/KThread.hpp>
|
||||
#include <mesosphere/threading/KScheduler.hpp>
|
||||
#include <mesosphere/threading/KScopedCriticalSection.hpp>
|
||||
|
||||
namespace mesosphere
|
||||
{
|
||||
|
||||
@@ -341,4 +341,15 @@ void KScheduler::YieldCurrentThreadAndWaitForLoadBalancing()
|
||||
cctx.GetScheduler()->DoYieldOperation(Global::YieldThreadAndWaitForLoadBalancing, *cctx.GetCurrentThread());
|
||||
}
|
||||
|
||||
void KScheduler::HandleCriticalSectionLeave()
|
||||
{
|
||||
if (KScheduler::Global::reselectionRequired) {
|
||||
KScheduler::Global::SelectThreads();
|
||||
}
|
||||
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
|
||||
// TODO: check which cores needs ctx switches, sent interrupts and/or ctx switch ourselves
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include <mesosphere/threading/KThread.hpp>
|
||||
#include <mesosphere/threading/KScheduler.hpp>
|
||||
#include <mesosphere/threading/KScopedCriticalSection.hpp>
|
||||
|
||||
namespace mesosphere
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user