This commit is contained in:
uosfz 2025-02-07 16:57:12 +01:00
parent 57ec3792e9
commit 74e76cad7a
Signed by: uosfz
SSH key fingerprint: SHA256:FlktuluyhTQg3jHZNLKwxOOC5hbfrUXM0tz3IA3lGJo
3 changed files with 25 additions and 0 deletions

View file

@ -18,6 +18,7 @@ KERNEL_SOURCES := \
src/kernel.c \
src/output.c \
src/pci.c \
src/sync.c \
$(KERNEL_SOURCES_$(ARCH)) \
# end of kernel sources list

13
include/sync.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef KARLOS_SYNC_H
#define KARLOS_SYNC_H
#include "stdatomic.h"
struct spinlock {
atomic_int lock;
};
void spin_lock(struct spinlock *s);
void spin_unlock(struct spinlock *s);
#endif

11
src/sync.c Normal file
View file

@ -0,0 +1,11 @@
#include "sync.h"
void spin_lock(struct spinlock *s) {
while (atomic_exchange_explicit(&s->lock, 1, memory_order_acquire)) {
// spin
}
}
void spin_unlock(struct spinlock *s) {
atomic_store_explicit(&s->lock, 0, memory_order_release);
}