stos: ke: Add initial support for spinlocks
Signed-off-by: Chloe M. <chloe@mensia.org>
This commit is contained in:
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026, Chloe M.
|
||||||
|
* Provided under the BSD-3 clause.
|
||||||
|
*
|
||||||
|
* Description: Spinlock primitive
|
||||||
|
* Author: Chloe M.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _KE_SPINLOCK_H_
|
||||||
|
#define _KE_SPINLOCK_H_ 1
|
||||||
|
|
||||||
|
#include <stdef.h>
|
||||||
|
#include <hal/intr.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Represents a spinlock that may be acquired for mutual
|
||||||
|
* exclusion of threads.
|
||||||
|
*
|
||||||
|
* @Data: Spinlock data (must be zero on init)
|
||||||
|
*/
|
||||||
|
typedef struct {
|
||||||
|
UQUAD Data;
|
||||||
|
} SPINLOCK;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initialize a spinlock
|
||||||
|
*
|
||||||
|
* @Lock: Spinlock to initialize
|
||||||
|
* @Name: Name of spinlock
|
||||||
|
*/
|
||||||
|
VOID KeSpinLockInit(SPINLOCK *Lock, const CHAR *Name);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Acquire a spinlock
|
||||||
|
*
|
||||||
|
* @Lock: Lock to acquire
|
||||||
|
* @IrqMut: If true, mututate IRQ state upon acquisition
|
||||||
|
*/
|
||||||
|
VOID KeSpinLockAcquire(SPINLOCK *Lock, BOOLEAN IrqMut);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Release a spinlock
|
||||||
|
*
|
||||||
|
* @Lock: Spinlock to release
|
||||||
|
*/
|
||||||
|
VOID KeSpinLockRelease(SPINLOCK *Lock);
|
||||||
|
|
||||||
|
#endif /* !_KE_SPINLOCK_H_ */
|
||||||
@@ -17,6 +17,7 @@ CFILES += $(shell find ../drivers -name "*.c")
|
|||||||
CFILES += $(shell find ../mm -name "*.c")
|
CFILES += $(shell find ../mm -name "*.c")
|
||||||
CFILES += $(shell find ../ex -name "*.c")
|
CFILES += $(shell find ../ex -name "*.c")
|
||||||
CFILES += $(shell find bpal -name "*.c")
|
CFILES += $(shell find bpal -name "*.c")
|
||||||
|
CFILES += $(shell find synch -name "*.c")
|
||||||
DFILES = $(CFILES:.c=.d)
|
DFILES = $(CFILES:.c=.d)
|
||||||
OFILES = $(CFILES:.c=.o)
|
OFILES = $(CFILES:.c=.o)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026, Chloe M.
|
||||||
|
* Provided under the BSD-3 clause.
|
||||||
|
*
|
||||||
|
* Description: Spinlock primitive
|
||||||
|
* Author: Chloe M.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <ke/spinlock.h>
|
||||||
|
#include <hal/prim.h>
|
||||||
|
|
||||||
|
VOID KeSpinLockInit(SPINLOCK *Lock, const CHAR *Name)
|
||||||
|
{
|
||||||
|
if (Lock == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Lock->Data = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
VOID
|
||||||
|
KeSpinLockAcquire(SPINLOCK *Lock, BOOLEAN IrqMut)
|
||||||
|
{
|
||||||
|
IRQL LastIrql;
|
||||||
|
|
||||||
|
if (Lock == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IrqMut) {
|
||||||
|
LastIrql = HalRaiseIrql(IRQL_CRITICAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Acquire the lock */
|
||||||
|
while (HalCpuAswap(&Lock->Data, 1)) {
|
||||||
|
HalCpuSpinWait();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IrqMut) {
|
||||||
|
HalLowerIrql(LastIrql);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VOID
|
||||||
|
KeSpinLockRelease(SPINLOCK *Lock)
|
||||||
|
{
|
||||||
|
if (Lock == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
HalCpuAswap(&Lock->Data, 0);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user