Implement basic kernel spinlock mechanism
All checks were successful
Builds / ExectOS (amd64) (push) Successful in 29s
Builds / ExectOS (i686) (push) Successful in 27s

This commit is contained in:
Rafal Kupiec 2024-02-05 22:07:39 +01:00
parent badb16e37d
commit 99abcd63d1
Signed by: belliash
GPG Key ID: 4E829243E0CFE6B4
3 changed files with 102 additions and 0 deletions

View File

@ -16,6 +16,10 @@
/* Kernel services routines forward references */ /* Kernel services routines forward references */
XTFASTCALL
VOID
KeAcquireSpinLock(IN OUT PKSPIN_LOCK SpinLock);
XTAPI XTAPI
VOID VOID
KeInitializeApc(IN PKAPC Apc, KeInitializeApc(IN PKAPC Apc,
@ -69,6 +73,10 @@ KeReleaseSemaphore(IN PKSEMAPHORE Semaphore,
IN LONG Adjustment, IN LONG Adjustment,
IN BOOLEAN Wait); IN BOOLEAN Wait);
XTFASTCALL
VOID
KeReleaseSpinLock(IN OUT PKSPIN_LOCK SpinLock);
XTAPI XTAPI
VOID VOID
KeSetTargetProcessorDpc(IN PKDPC Dpc, KeSetTargetProcessorDpc(IN PKDPC Dpc,

View File

@ -13,6 +13,10 @@
/* Kernel services routines forward references */ /* Kernel services routines forward references */
XTFASTCALL
VOID
KeAcquireQueuedSpinLock(IN KSPIN_LOCK_QUEUE_LEVEL LockLevel);
XTAPI XTAPI
VOID VOID
KeClearEvent(IN PKEVENT Event); KeClearEvent(IN PKEVENT Event);
@ -71,6 +75,10 @@ XTFASTCALL
KRUNLEVEL KRUNLEVEL
KeRaiseRunLevel(IN KRUNLEVEL RunLevel); KeRaiseRunLevel(IN KRUNLEVEL RunLevel);
XTFASTCALL
VOID
KeReleaseQueuedSpinLock(IN KSPIN_LOCK_QUEUE_LEVEL LockLevel);
XTAPI XTAPI
LONG LONG
KeSetEvent(IN PKEVENT Event, KeSetEvent(IN PKEVENT Event,

View File

@ -9,6 +9,53 @@
#include <xtos.h> #include <xtos.h>
/**
* Acquires a specified queued spinlock.
*
* @param LockLevel
* Supplies the queued spinlock level.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTFASTCALL
VOID
KeAcquireQueuedSpinLock(IN KSPIN_LOCK_QUEUE_LEVEL LockLevel)
{
/* Acquire the queued spinlock */
KeAcquireSpinLock(KeGetCurrentProcessorControlBlock()->LockQueue[LockLevel].Lock);
}
/**
* Acquires a kernel spin lock.
*
* @param SpinLock
* Supplies a pointer to the kernel spin lock.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTFASTCALL
VOID
KeAcquireSpinLock(IN OUT PKSPIN_LOCK SpinLock)
{
/* Try to acquire the lock */
while(RtlAtomicBitTestAndSet((PLONG)SpinLock, 0))
{
/* Wait until locked is cleared */
while(*(VOLATILE PKSPIN_LOCK)SpinLock & 1)
{
/* Yield processor and keep waiting */
ArYieldProcessor();
}
}
/* Add an explicit memory barrier */
ArReadWriteBarrier();
}
/** /**
* Initializes a kernel spinlock object. * Initializes a kernel spinlock object.
* *
@ -26,3 +73,42 @@ KeInitializeSpinLock(IN PKSPIN_LOCK SpinLock)
/* Zero initialize spinlock */ /* Zero initialize spinlock */
*SpinLock = 0; *SpinLock = 0;
} }
/**
* Releases a queued spinlock.
*
* @param LockLevel
* Supplies the queued spinlock level.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTFASTCALL
VOID
KeReleaseQueuedSpinLock(IN KSPIN_LOCK_QUEUE_LEVEL LockLevel)
{
/* Clear the lock */
KeReleaseSpinLock(KeGetCurrentProcessorControlBlock()->LockQueue[LockLevel].Lock);
}
/**
* Releases a kernel spin lock.
*
* @param SpinLock
* Supplies a pointer to the kernel spin lock.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTFASTCALL
VOID
KeReleaseSpinLock(IN OUT PKSPIN_LOCK SpinLock)
{
/* Clear the lock */
RtlAtomicAnd32((PLONG)SpinLock, 0);
/* Add an explicit memory barrier */
ArReadWriteBarrier();
}