Kernel: Create CriticalScope and fix kmalloc
This disables interrupts for the current scope and restores them after the scope. This is used in kmalloc, since scheduler might call into kmalloc/kfree, but deadlock if some thread is currently trying to allocate. This allows us to use kmalloc in Scheduler.
This commit is contained in:
30
kernel/include/kernel/CriticalScope.h
Normal file
30
kernel/include/kernel/CriticalScope.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/NoCopyMove.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
class CriticalScope
|
||||
{
|
||||
BAN_NON_COPYABLE(CriticalScope);
|
||||
BAN_NON_MOVABLE(CriticalScope);
|
||||
|
||||
public:
|
||||
CriticalScope()
|
||||
{
|
||||
asm volatile("pushf; cli; pop %0" : "=r"(m_flags) :: "memory");
|
||||
}
|
||||
|
||||
~CriticalScope()
|
||||
{
|
||||
asm volatile("push %0; popf" :: "rm"(m_flags) : "memory", "cc");
|
||||
}
|
||||
|
||||
private:
|
||||
size_t m_flags;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -18,6 +18,4 @@ public:
|
||||
static InterruptController& get();
|
||||
};
|
||||
|
||||
uintptr_t disable_interrupts_and_get_flags();
|
||||
void restore_flags(uintptr_t);
|
||||
bool interrupts_enabled();
|
||||
Reference in New Issue
Block a user