Move spinlock to separate folder

This commit is contained in:
2025-08-13 22:19:11 +02:00
parent d4f06b4538
commit ce6b17d72b
7 changed files with 50 additions and 39 deletions

View File

@ -19,6 +19,7 @@ SRCFILES := $(wildcard *.c) \
$(wildcard bitmap/*.c) \
$(wildcard pmm/*.c) \
$(wildcard bootinfo/*.c) \
$(wildcard spinlock/*.c) \
$(wildcard hal/*.c) \
$(wildcard hal/$(ARCH)/*.c) \
$(wildcard hal/$(ARCH)/*.S) \

View File

@ -13,8 +13,8 @@ void kmain(void) {
}
bootinfo_init();
pmm_init();
hal_init();
pmm_init();
kprintf(BANNER_TEXT "\n");

View File

@ -1,17 +1,17 @@
#include <stddef.h>
#include <limine.h>
#include "pmm.h"
#include "spinlock.h"
#include "kprintf.h"
#include "bitmap/bitmap.h"
#include "bootinfo/bootinfo.h"
#include "spinlock/spinlock.h"
#define _DIV_ROUNDUP(num, div) ((num + div - 1) / div)
PhysMem PHYS_MEM;
void pmm_init(void) {
PHYS_MEM.spinlock = SPINLOCK_INIT();
spinlock_init(&PHYS_MEM.spinlock);
BitMap *bm = &PHYS_MEM.self;
bm->init = false;
@ -53,9 +53,9 @@ void pmm_init(void) {
}
void *pmm_alloc(size_t pages) {
SPINLOCK_ACQUIRE(&PHYS_MEM.spinlock);
spinlock_acquire(&PHYS_MEM.spinlock);
uintptr_t phys = (uintptr_t)bitmap_alloc(&PHYS_MEM.self, pages);
SPINLOCK_RELEASE(&PHYS_MEM.spinlock);
spinlock_release(&PHYS_MEM.spinlock);
if (!phys) {
ERR("hal/pmm", "phys memory ran out\n");
@ -65,7 +65,7 @@ void *pmm_alloc(size_t pages) {
}
void pmm_free(uintptr_t ptr, size_t pages) {
SPINLOCK_ACQUIRE(&PHYS_MEM.spinlock);
spinlock_acquire(&PHYS_MEM.spinlock);
bitmap_markregion(&PHYS_MEM.self, (void *)ptr, pages * BITMAP_BLOCK_SIZE, 0);
SPINLOCK_RELEASE(&PHYS_MEM.spinlock);
spinlock_release(&PHYS_MEM.spinlock);
}

View File

@ -3,8 +3,8 @@
#include <stddef.h>
#include <stdint.h>
#include "spinlock.h"
#include "bitmap/bitmap.h"
#include "spinlock/spinlock.h"
typedef struct {
SpinLock spinlock;

View File

@ -1,31 +0,0 @@
#ifndef SPINLOCK_SPINLOCK_H_
#define SPINLOCK_SPINLOCK_H_
#include <stdatomic.h>
#include "hal/hal.h"
typedef atomic_bool SpinLock;
// Spin more efficiently - cpu dependant
#if defined(__x86_64__)
# define SPINLOCK_HINT() asm volatile("pause")
#else
# define SPINLOCK_HINT()
#endif
#define SPINLOCK_ACQUIRE(sl) \
do { \
bool __unlocked = false; \
while (!atomic_compare_exchange_weak((sl), &__unlocked, true)) { \
SPINLOCK_HINT(); \
} \
} while(0)
#define SPINLOCK_RELEASE(sl) \
do { \
atomic_store((sl), false); \
} while(0)
#define SPINLOCK_INIT() false
#endif // SPINLOCK_SPINLOCK_H_

View File

@ -0,0 +1,20 @@
#include <stdatomic.h>
#include <stdint.h>
#include "spinlock.h"
void spinlock_init(SpinLock *sl) {
atomic_store(&sl->lock, false);
}
void spinlock_acquire(SpinLock *sl) {
bool unlocked = false;
while (!atomic_compare_exchange_weak(&sl->lock, &unlocked, true)) {
unlocked = false;
SPINLOCK_HINT();
}
}
void spinlock_release(SpinLock *sl) {
atomic_store(&sl->lock, false);
}

View File

@ -0,0 +1,21 @@
#ifndef SPINLOCK_SPINLOCK_H_
#define SPINLOCK_SPINLOCK_H_
#include <stdatomic.h>
#include <stdint.h>
#include "hal/hal.h"
typedef struct { atomic_bool lock; } SpinLock;
// Spin more efficiently - cpu dependant
#if defined(__x86_64__)
# define SPINLOCK_HINT() asm volatile("pause")
#else
# define SPINLOCK_HINT()
#endif
void spinlock_init(SpinLock *sl);
void spinlock_acquire(SpinLock *sl);
void spinlock_release(SpinLock *sl);
#endif // SPINLOCK_SPINLOCK_H_