Implement KCriticalSection (mostly)

This commit is contained in:
TuxSH
2018-11-08 01:04:06 +01:00
committed by Michael Scire
parent 4238d2e97f
commit 0fb40d1ef5
12 changed files with 115 additions and 21 deletions

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