Move spinlock to separate folder
This commit is contained in:
20
kernel/spinlock/spinlock.c
Normal file
20
kernel/spinlock/spinlock.c
Normal 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);
|
||||
}
|
||||
|
21
kernel/spinlock/spinlock.h
Normal file
21
kernel/spinlock/spinlock.h
Normal 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_
|
Reference in New Issue
Block a user