Fix atomics and make it compile

This commit is contained in:
Curle 2021-07-15 04:43:46 +01:00
parent 138c026b9a
commit 770e040383
9 changed files with 67 additions and 61 deletions

View File

@ -106,8 +106,8 @@ void DrawEditor(EditorState* currentState);
void DrawHeader();
void DrawBackground();
void DrawTextArea();
void DrawTextLine();
void DrawText();
void DrawLine();
void DrawMenus();
void DrawBoxes();
void DrawBoxShadows();

View File

@ -55,11 +55,11 @@ typedef struct {
} __attribute__((packed)) psf_t;
size_t KernelAddr;
size_t KernelEnd;
size_t MemoryPages;
size_t FreeMemorySize;
size_t FullMemorySize;
extern size_t KernelAddr;
extern size_t KernelEnd;
extern size_t MemoryPages;
extern size_t FreeMemorySize;
extern size_t FullMemorySize;
void SetupExtensions();
@ -81,7 +81,7 @@ int ParseKernelHeader(size_t InitrdPtr);
int Main();
void Exit();
void Exit(int code);
void SomethingWentWrong(const char* Message);

View File

@ -1,9 +1,4 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <kernel/system/descriptors.h>
/************************
@ -13,8 +8,9 @@ extern "C" {
#define PAUSE __asm__ __volatile__("pause")
uint8_t kbdSBuffer[8];
uint8_t InputBuffer[128];
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
uint8_t ACK;
@ -24,7 +20,7 @@ typedef struct {
uint8_t Error;
} KBD_FLAGS;
KBD_FLAGS KbdFlags;
extern KBD_FLAGS KbdFlags;
DESC_TBL ReadGDT(void);

View File

@ -1,8 +1,6 @@
#pragma once
#include <stdbool.h>
#include <stdatomic.h>
// TODO: <atomic> for cplusplus.
#include <stddef.h>
/************************
*** Team Kitty, 2020 ***
@ -25,8 +23,8 @@ extern "C" {
*/
typedef struct {
atomic_size_t NowServing;
atomic_size_t NextTicket;
size_t NowServing;
size_t NextTicket;
} ticketlock_t;
#define NEW_TICKETLOCK() (ticketlock_t{0})

View File

@ -19,11 +19,12 @@ extern "C" {
static bool KernelLoaded = false;
address_space_t KernelAddressSpace;
size_t KernelAddr = (size_t) &LoadAddr;
size_t KernelEnd = (size_t) &end;
address_space_t KernelAddressSpace;
void PrintPressedChar(KeyboardData data);
int CharPrinterCallbackID;
void TrackInternalBuffer(KeyboardData data);
@ -145,7 +146,6 @@ void SomethingWentWrong(const char* Message) {
void Exit(int ExitCode) {
SerialPrintf("Kernel stopped with code %x\r\n", ExitCode);
}
#ifdef __cplusplus

View File

@ -1,27 +1,29 @@
#include <kernel/chroma.h>
#include <lainlib/lainlib.h>
#include <lainlib/mutex/ticketlock.h>
#define PAUSE __asm__ __volatile__("pause")
#ifdef __cplusplus
extern "C" {
#endif
void TicketLock(ticketlock_t* Lock) {
size_t Ticket = atomic_fetch_add_explicit(&Lock->NextTicket, 1, memory_order_relaxed);
while(atomic_load_explicit(&Lock->NowServing, memory_order_acquire) != Ticket) {
size_t Ticket = __atomic_fetch_add(&Lock->NextTicket, 1, __ATOMIC_RELAXED);
__sync_synchronize();
while(__atomic_load_8(&Lock->NowServing, __ATOMIC_ACQUIRE) != Ticket) {
PAUSE;
}
}
bool TicketAttemptLock(ticketlock_t* Lock) {
size_t Ticket = atomic_load_explicit(&Lock->NowServing, memory_order_relaxed);
return atomic_compare_exchange_strong_explicit(&Lock->NowServing, &Ticket, Ticket + 1, memory_order_acquire, memory_order_relaxed);
size_t Ticket = __atomic_load_8(&Lock->NowServing, __ATOMIC_RELAXED);
__sync_synchronize();
return __atomic_compare_exchange_8(&Lock->NowServing, &Ticket, Ticket + 1, false, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
}
void TicketUnlock(ticketlock_t* Lock) {
size_t NextTicket = atomic_load_explicit(&Lock->NowServing, memory_order_relaxed) + 1;
atomic_store_explicit(&Lock->NowServing, NextTicket, memory_order_release);
size_t NextTicket = __atomic_load_8(&Lock->NowServing, __ATOMIC_RELAXED) + 1;
__sync_synchronize();
__atomic_store_8(&Lock->NowServing, NextTicket, __ATOMIC_RELEASE);
}
#ifdef __cplusplus

View File

@ -20,6 +20,8 @@ extern "C" {
*
*/
KBD_FLAGS KbdFlags;
char keys[128] = {
0, 27,
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',

View File

@ -32,6 +32,10 @@ extern "C" {
uint8_t* MemoryStart;
size_t MemoryBuckets;
size_t MemoryPages = 0;
size_t FreeMemorySize = 0;
size_t FullMemorySize = 0;
static buddy_t LowBuddy = {
.MaxOrder = 32,
.Base = (directptr_t) DIRECT_REGION,
@ -155,9 +159,6 @@ static directptr_t BuddyAllocate(buddy_t* Buddy, size_t Size) {
void InitMemoryManager() {
SerialPrintf("[ Mem] Counting memory..\r\n");
FreeMemorySize = 0;
FullMemorySize = 0;
size_t MemMapEntryCount = 0;
MMapEnt* MemMap = &bootldr.mmap;

View File

@ -18,7 +18,7 @@ extern "C" {
* This file also provides SerialPrintf.
*/
static void NumToStr(char* Buffer, size_t Num, size_t Base) {
void NumToStr(char* Buffer, size_t Num, size_t Base) {
size_t Temp, i = 0, j = 0;
do {
@ -54,7 +54,7 @@ int SerialPrintf(const char* Format, ...) {
switch(*Format) {
case 'c':
case 'c': {
Format++;
char c = (char) va_arg(Parameters, int);
@ -63,7 +63,8 @@ int SerialPrintf(const char* Format, ...) {
CharsWritten++;
break;
case 's':
}
case 's': {
Format++;
const char* Str = va_arg(Parameters, char*);
@ -76,10 +77,11 @@ int SerialPrintf(const char* Format, ...) {
CharsWritten += Len;
break;
}
case 'd':
case 'u':
case 'p':
case 'x':
case 'x': {
Num = va_arg(Parameters, size_t);
Base = 0;
@ -93,12 +95,13 @@ int SerialPrintf(const char* Format, ...) {
Format++;
NumToStr(BufferStr, Num, Base);
Len = strlen(BufferStr);
size_t Len = strlen(BufferStr);
WriteSerialString(BufferStr, Len);
CharsWritten += Len;
break;
}
//case 'p':
//uint8_t Base = 16;
//size_t Dest = (uintptr_t) va_arg(Parameters, void*);
@ -117,7 +120,7 @@ int SerialPrintf(const char* Format, ...) {
return CharsWritten;
}
size_t ParseEnglishColor(const char* Name);
size_t ParseEnglishColor(char* Name);
size_t ParseHexColor(const char* Name, bool bgFlag);
int Printf(const char* Format, ...) {
@ -137,7 +140,7 @@ int Printf(const char* Format, ...) {
Format++;
switch(*Format) {
case 'c':
case 'c': {
Format++;
char c = (char) va_arg(Parameters, int);
@ -146,7 +149,8 @@ int Printf(const char* Format, ...) {
CharsWritten++;
break;
case 's':
}
case 's': {
Format++;
const char* Str = va_arg(Parameters, char*);
@ -159,10 +163,11 @@ int Printf(const char* Format, ...) {
CharsWritten += Len;
break;
}
case 'd':
case 'u':
case 'p':
case 'x':
case 'x': {
Num = va_arg(Parameters, size_t);
Base = 0;
@ -176,12 +181,13 @@ int Printf(const char* Format, ...) {
Format++;
NumToStr(BufferStr, Num, Base);
Len = strlen(BufferStr);
size_t Len = strlen(BufferStr);
WriteString(BufferStr);
CharsWritten += Len;
break;
}
//case 'p':
//uint8_t Base = 16;
//size_t Dest = (uintptr_t) va_arg(Parameters, void*);
@ -194,22 +200,22 @@ int Printf(const char* Format, ...) {
Format++; // Skip backslash
switch(*Format) {
case '$':
case '$': {
// COLOR
Format ++; // Skip $ and {
size_t ContentLength = 0;
size_t Color = 0;
bool bgFlag = false;
switch(*Format) {
case '[':
bgFlag = true;
[[fallthrough]];
// bg
case '{':
// fg
Format++;
if(*Format == '<') {
Color = ParseEnglishColor(++Format);
Color = ParseEnglishColor((char*) ++Format);
Format++;
} else {
Color = ParseHexColor(++Format, bgFlag);
@ -227,6 +233,7 @@ int Printf(const char* Format, ...) {
Format++;
break;
}
case 'f':
// FORMAT
break;
@ -244,24 +251,24 @@ int Printf(const char* Format, ...) {
}
size_t ParseEnglishColor(const char* Stream) {
if(strcmp("red", Stream))
size_t ParseEnglishColor(char* Stream) {
if(strcmp(Stream, "red"))
return 0xFF0000;
if(strcmp("green", Stream))
if(strcmp(Stream, "green"))
return 0xFF00;
if(strcmp("blue", Stream))
if(strcmp(Stream, "blue"))
return 0xFF;
if(strcmp("yellow", Stream))
if(strcmp(Stream, "yellow"))
return 0xFFFF00;
if(strcmp("cyan", Stream))
if(strcmp(Stream, "cyan"))
return 0xFFFF;
if(strcmp("magenta", Stream))
if(strcmp(Stream, "magenta"))
return 0xFF00FF;
if(strcmp("beans", Stream))
if(strcmp(Stream, "beans"))
return 0xAA11CC;
if(strcmp("forgeb", Stream))
if(strcmp(Stream, "forgeb"))
return 0x1E2D42;
if(strcmp("forgey", Stream))
if(strcmp(Stream, "forgey"))
return 0xE0A969;
return 0xFFFFFF;
}
@ -270,8 +277,8 @@ size_t ParseHexColor(const char* Stream, bool bgFlag) {
size_t val = 0;
char c;
while ((*Stream != bgFlag ? ']' : '}') && (c = *Stream++)) {
char v = (c & 0xF) + (c >> 6) | ((c >> 3) & 0x8);
while ((*Stream != (bgFlag ? ']' : '}')) && (c = *Stream++)) {
char v = ((c & 0xF) + (c >> 6)) | ((c >> 3) & 0x8);
val = (val << 4) | (size_t) v;
}