Implemented SmartCriticalSection

This commit is contained in:
Alex 2022-10-21 03:49:12 +03:00
parent 2f7b871aa0
commit 5d41d36bd8
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
9 changed files with 59 additions and 30 deletions

View File

@ -4,7 +4,7 @@
#include <printf.h> #include <printf.h>
#include <lock.hpp> #include <lock.hpp>
NEWLOCK(DebuggerLock); NewLock(DebuggerLock);
using namespace UniversalAsynchronousReceiverTransmitter; using namespace UniversalAsynchronousReceiverTransmitter;
@ -57,7 +57,7 @@ namespace SysDbg
void WriteLine(DebugLevel Level, const char *File, int Line, const char *Function, const char *Format, ...) void WriteLine(DebugLevel Level, const char *File, int Line, const char *Function, const char *Format, ...)
{ {
// SMARTLOCK(DebuggerLock); // SmartLock(DebuggerLock);
WritePrefix(Level, File, Line, Function); WritePrefix(Level, File, Line, Function);
va_list args; va_list args;
va_start(args, Format); va_start(args, Format);

View File

@ -6,25 +6,25 @@ namespace Memory
{ {
uint64_t Physical::GetTotalMemory() uint64_t Physical::GetTotalMemory()
{ {
SMARTLOCK(this->MemoryLock); SmartLock(this->MemoryLock);
return this->TotalMemory; return this->TotalMemory;
} }
uint64_t Physical::GetFreeMemory() uint64_t Physical::GetFreeMemory()
{ {
SMARTLOCK(this->MemoryLock); SmartLock(this->MemoryLock);
return this->FreeMemory; return this->FreeMemory;
} }
uint64_t Physical::GetReservedMemory() uint64_t Physical::GetReservedMemory()
{ {
SMARTLOCK(this->MemoryLock); SmartLock(this->MemoryLock);
return this->ReservedMemory; return this->ReservedMemory;
} }
uint64_t Physical::GetUsedMemory() uint64_t Physical::GetUsedMemory()
{ {
SMARTLOCK(this->MemoryLock); SmartLock(this->MemoryLock);
return this->UsedMemory; return this->UsedMemory;
} }
@ -58,7 +58,7 @@ namespace Memory
void *Physical::RequestPage() void *Physical::RequestPage()
{ {
SMARTLOCK(this->MemoryLock); SmartLock(this->MemoryLock);
for (; PageBitmapIndex < PageBitmap.Size * 8; PageBitmapIndex++) for (; PageBitmapIndex < PageBitmap.Size * 8; PageBitmapIndex++)
{ {
if (PageBitmap[PageBitmapIndex] == true) if (PageBitmap[PageBitmapIndex] == true)
@ -81,7 +81,7 @@ namespace Memory
void *Physical::RequestPages(uint64_t Count) void *Physical::RequestPages(uint64_t Count)
{ {
SMARTLOCK(this->MemoryLock); SmartLock(this->MemoryLock);
for (; PageBitmapIndex < PageBitmap.Size * 8; PageBitmapIndex++) for (; PageBitmapIndex < PageBitmap.Size * 8; PageBitmapIndex++)
{ {
if (PageBitmap[PageBitmapIndex] == true) if (PageBitmap[PageBitmapIndex] == true)
@ -119,7 +119,7 @@ namespace Memory
void Physical::FreePage(void *Address) void Physical::FreePage(void *Address)
{ {
SMARTLOCK(this->MemoryLock); SmartLock(this->MemoryLock);
if (Address == nullptr) if (Address == nullptr)
{ {
warn("Null pointer passed to FreePage."); warn("Null pointer passed to FreePage.");
@ -228,7 +228,7 @@ namespace Memory
void Physical::Init(BootInfo *Info) void Physical::Init(BootInfo *Info)
{ {
SMARTLOCK(this->MemoryLock); SmartLock(this->MemoryLock);
void *LargestFreeMemorySegment = nullptr; void *LargestFreeMemorySegment = nullptr;
uint64_t LargestFreeMemorySegmentSize = 0; uint64_t LargestFreeMemorySegmentSize = 0;
uint64_t MemorySize = Info->Memory.Size; uint64_t MemorySize = Info->Memory.Size;

View File

@ -7,7 +7,7 @@ namespace Memory
{ {
void Virtual::Map(void *VirtualAddress, void *PhysicalAddress, uint64_t Flags) void Virtual::Map(void *VirtualAddress, void *PhysicalAddress, uint64_t Flags)
{ {
SMARTLOCK(this->MemoryLock); SmartLock(this->MemoryLock);
if (!this->Table) if (!this->Table)
{ {
error("No page table"); error("No page table");
@ -85,7 +85,7 @@ namespace Memory
void Virtual::Unmap(void *VirtualAddress) void Virtual::Unmap(void *VirtualAddress)
{ {
SMARTLOCK(this->MemoryLock); SmartLock(this->MemoryLock);
if (!this->Table) if (!this->Table)
{ {
error("No page table"); error("No page table");

View File

@ -7,13 +7,13 @@ extern uint64_t _binary_Files_ter_powerline_v12n_psf_start;
extern uint64_t _binary_Files_ter_powerline_v12n_psf_end; extern uint64_t _binary_Files_ter_powerline_v12n_psf_end;
extern uint64_t _binary_Files_ter_powerline_v12n_psf_size; extern uint64_t _binary_Files_ter_powerline_v12n_psf_size;
NEWLOCK(PrintLock); NewLock(PrintLock);
namespace Video namespace Video
{ {
char Display::Print(char Char, int Index, bool WriteToUART) char Display::Print(char Char, int Index, bool WriteToUART)
{ {
SMARTLOCK(PrintLock); SmartLock(PrintLock);
if (this->ColorIteration) if (this->ColorIteration)
{ {

View File

@ -12,7 +12,7 @@
#include <cargs.h> #include <cargs.h>
#include <io.h> #include <io.h>
NEWLOCK(KernelLock); NewLock(KernelLock);
BootInfo *bInfo = nullptr; BootInfo *bInfo = nullptr;
Video::Display *Display = nullptr; Video::Display *Display = nullptr;
@ -29,7 +29,7 @@ extern "C" void putchar(char c) { Display->Print(c, 0); }
EXTERNC void KPrint(const char *Format, ...) EXTERNC void KPrint(const char *Format, ...)
{ {
SMARTLOCK(KernelLock); SmartLock(KernelLock);
Time tm = ReadClock(); Time tm = ReadClock();
printf_("\eCCCCCC[\e00AEFF%02ld:%02ld:%02ld\eCCCCCC] ", tm.Hour, tm.Minute, tm.Second); printf_("\eCCCCCC[\e00AEFF%02ld:%02ld:%02ld\eCCCCCC] ", tm.Hour, tm.Minute, tm.Second);
va_list args; va_list args;

View File

@ -2,7 +2,7 @@
#include <lock.hpp> #include <lock.hpp>
#include <memory.hpp> #include <memory.hpp>
NEWLOCK(liballocLock); NewLock(liballocLock);
EXTERNC int liballoc_lock() { return liballocLock.Lock(); } EXTERNC int liballoc_lock() { return liballocLock.Lock(); }
EXTERNC int liballoc_unlock() { return liballocLock.Unlock(); } EXTERNC int liballoc_unlock() { return liballocLock.Unlock(); }

View File

@ -1,11 +1,15 @@
#ifndef __FENNIX_KERNEL_LOCK_H__ #ifndef __FENNIX_KERNEL_LOCK_H__
#define __FENNIX_KERNEL_LOCK_H__ #define __FENNIX_KERNEL_LOCK_H__
/*
TODO: Add deadlock detection.
*/
#include <types.h> #include <types.h>
#include <cpu.hpp> #include <cpu.hpp>
#ifdef __cplusplus #ifdef __cplusplus
/** @brief Please use this macro to create a new lock. */
class LockClass class LockClass
{ {
private: private:
@ -28,26 +32,50 @@ public:
return 0; return 0;
} }
}; };
/** @brief Please use this macro to create a new smart lock. */
#define NEWLOCK(Name) LockClass Name class SmartLockClass
class SmartLock
{ {
private: private:
LockClass *LockPointer = nullptr; LockClass *LockPointer = nullptr;
public: public:
SmartLock(LockClass &Lock) SmartLockClass(LockClass &Lock)
{ {
this->LockPointer = &Lock; this->LockPointer = &Lock;
this->LockPointer->Lock(); this->LockPointer->Lock();
} }
~SmartLock() { this->LockPointer->Unlock(); } ~SmartLockClass() { this->LockPointer->Unlock(); }
};
/** @brief Please use this macro to create a new smart critical section lock. */
class SmartCriticalSectionClass
{
private:
LockClass *LockPointer = nullptr;
bool InterruptsEnabled = false;
public:
SmartCriticalSectionClass(LockClass &Lock)
{
if (CPU::Interrupts(CPU::Check))
InterruptsEnabled = true;
CPU::Interrupts(CPU::Disable);
this->LockPointer = &Lock;
this->LockPointer->Lock();
}
~SmartCriticalSectionClass()
{
if (InterruptsEnabled)
CPU::Interrupts(CPU::Enable);
this->LockPointer->Unlock();
}
}; };
#define SL_CONCAT(x, y) x##y /** @brief Create a new lock (can be used with SmartCriticalSection). */
#define SMARTLOCK(LockClassName) SmartLock SL_CONCAT(lock##_, __COUNTER__)(LockClassName) #define NewLock(Name) LockClass Name
/** @brief Simple lock that is automatically released when the scope ends. */
#endif #define SmartLock(LockClassName) SmartLockClass CONCAT(lock##_, __COUNTER__)(LockClassName)
/** @brief Simple critical section that is automatically released when the scope ends and interrupts are restored if they were enabled. */
#define SmartCriticalSection(LockClassName) SmartCriticalSectionClass CONCAT(lock##_, __COUNTER__)(LockClassName)
#endif // __cplusplus
#endif // !__FENNIX_KERNEL_LOCK_H__ #endif // !__FENNIX_KERNEL_LOCK_H__

View File

@ -232,7 +232,7 @@ namespace Memory
class Physical class Physical
{ {
private: private:
NEWLOCK(MemoryLock); NewLock(MemoryLock);
uint64_t TotalMemory = 0; uint64_t TotalMemory = 0;
uint64_t FreeMemory = 0; uint64_t FreeMemory = 0;
@ -358,7 +358,7 @@ namespace Memory
class Virtual class Virtual
{ {
private: private:
NEWLOCK(MemoryLock); NewLock(MemoryLock);
PageTable *Table = nullptr; PageTable *Table = nullptr;
class PageMapIndexer class PageMapIndexer

View File

@ -33,6 +33,7 @@
#endif #endif
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
#define CONCAT(x, y) x##y
#ifndef __va_list__ #ifndef __va_list__
typedef __builtin_va_list va_list; typedef __builtin_va_list va_list;