spinlock
This commit is contained in:
parent
57ec3792e9
commit
74e76cad7a
3 changed files with 25 additions and 0 deletions
1
Makefile
1
Makefile
|
|
@ -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
13
include/sync.h
Normal 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
11
src/sync.c
Normal 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);
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue