From 03f97d94aeb02cef242a6f08ab7579ab537eb0e6 Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Thu, 25 Jun 2026 23:33:09 +0200 Subject: [PATCH] Add executive resource synchronization functions --- xtoskrnl/CMakeLists.txt | 1 + xtoskrnl/ex/data.cc | 9 ++ xtoskrnl/ex/resource.cc | 169 +++++++++++++++++++++++++++++++ xtoskrnl/includes/ex.hh | 1 + xtoskrnl/includes/ex/resource.hh | 33 ++++++ 5 files changed, 213 insertions(+) create mode 100644 xtoskrnl/ex/resource.cc create mode 100644 xtoskrnl/includes/ex/resource.hh diff --git a/xtoskrnl/CMakeLists.txt b/xtoskrnl/CMakeLists.txt index 95e148a..b9ed2f5 100644 --- a/xtoskrnl/CMakeLists.txt +++ b/xtoskrnl/CMakeLists.txt @@ -17,6 +17,7 @@ list(APPEND XTOSKRNL_SOURCE ${XTOSKRNL_SOURCE_DIR}/ex/data.cc ${XTOSKRNL_SOURCE_DIR}/ex/exports.cc ${XTOSKRNL_SOURCE_DIR}/ex/laslist.cc + ${XTOSKRNL_SOURCE_DIR}/ex/resource.cc ${XTOSKRNL_SOURCE_DIR}/ex/rundown.cc ${XTOSKRNL_SOURCE_DIR}/hl/${ARCH}/cpu.cc ${XTOSKRNL_SOURCE_DIR}/hl/${ARCH}/firmware.cc diff --git a/xtoskrnl/ex/data.cc b/xtoskrnl/ex/data.cc index 1e796a3..36e021a 100644 --- a/xtoskrnl/ex/data.cc +++ b/xtoskrnl/ex/data.cc @@ -32,3 +32,12 @@ LIST_ENTRY EX::LookasideList::PoolLookasideListHead; /* Tracks dynamic system lookaside lists */ LIST_ENTRY EX::LookasideList::SystemLookasideListHead; + +/* Specifies the default timeout interval for resource wait operations */ +LARGE_INTEGER EX::Resources::ResourcesTimeOut; + +/* Tracks all initialized structures currently active in the system */ +LIST_ENTRY EX::Resources::SystemResourcesList; + +/* The global spinlock that protects the system resources list */ +KSPIN_LOCK EX::Resources::SystemResourcesLock; diff --git a/xtoskrnl/ex/resource.cc b/xtoskrnl/ex/resource.cc new file mode 100644 index 0000000..a1115be --- /dev/null +++ b/xtoskrnl/ex/resource.cc @@ -0,0 +1,169 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/ex/resource.cc + * DESCRIPTION: Exclusive resource synchronization support + * DEVELOPERS: Aiken Harris + */ + +#include + + +/** + * Deletes a system resource and frees its associated memory allocations. + * + * @param Resource + * Supplies a pointer to the resource to be deleted. + * + * @return This routine returns a status code indicating the success or failure of the operation. + * + * @since XT 1.0 + */ +XTAPI +XTSTATUS +EX::Resources::DeleteResource(IN PERESOURCE Resource) +{ + /* Start a guarded code block */ + { + /* Raise runlevel to DISPATCH level and acquire system resources lock */ + KE::RaiseRunLevel RunLevel(DISPATCH_LEVEL); + KE::QueuedSpinLockGuard Guard(&SystemResourcesLock); + + /* Remove the resource from the system resource list */ + RTL::LinkedList::RemoveEntryList(&Resource->SystemResourcesList); + } + + /* Check if exclusive waiters event exists */ + if(Resource->ExclusiveWaiters) + { + /* Free the exclusive waiters event */ + MM::Allocator::FreePool(Resource->ExclusiveWaiters); + } + + /* Check if owner table exists */ + if(Resource->OwnerTable) + { + /* Free the owner table */ + MM::Allocator::FreePool(Resource->OwnerTable); + } + + /* Check if shared waiters semaphore exists */ + if(Resource->SharedWaiters) + { + /* Free the shared waiters semaphore */ + MM::Allocator::FreePool(Resource->SharedWaiters); + } + + /* Return success */ + return STATUS_SUCCESS; +} + +/** + * Initializes a system resource object. + * + * @param Resource + * Supplies a pointer to the resource structure to be initialized. + * + * @return This routine returns a status code indicating the success or failure of the operation. + * + * @since XT 1.0 + */ +XTAPI +XTSTATUS +EX::Resources::InitializeResource(IN PERESOURCE Resource) +{ + /* Clear the entire resource structure */ + RTL::Memory::ZeroMemory(Resource, sizeof(ERESOURCE)); + + /* Initialize the spinlock used for managing this resource */ + KE::SpinLock::InitializeSpinLock(&Resource->SpinLock); + + /* Raise runlevel to DISPATCH level and acquire the global system resources lock */ + KE::RaiseRunLevel RunLevel(DISPATCH_LEVEL); + KE::QueuedSpinLockGuard Guard(&SystemResourcesLock); + + /* Insert the resource into the global tracking list */ + RTL::LinkedList::InsertTailList(&SystemResourcesList, &Resource->SystemResourcesList); + + /* Return success */ + return STATUS_SUCCESS; +} + +/** + * Initializes the global system resource structures and synchronization primitives. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +EX::Resources::InitializeSystemResources(VOID) +{ + /* Set the default timeout for resource operations to 4 seconds relative to now */ + ResourcesTimeOut.QuadPart = -40000000LL; + + /* Initialize the global system resources list and its protecting spinlock */ + RTL::LinkedList::InitializeListHead(&SystemResourcesList); + KE::SpinLock::InitializeSpinLock(&SystemResourcesLock); +} + +/** + * Reinitializes an existing resource object, resetting its state and synchronization primitives. + * + * @param Resource + * Supplies a pointer to the resource structure to be reinitialized. + * + * @return This routine returns a status code indicating the success or failure of the operation. + * + * @since XT 1.0 + */ +XTAPI +XTSTATUS +EX::Resources::ReinitializeResource(IN PERESOURCE Resource) +{ + ULONG Index; + + /* Zero the active count and flags */ + Resource->ActiveCount = 0; + Resource->ActiveEntries = 0; + Resource->Flag = 0; + + /* Zero the contention count and counters */ + Resource->ContentionCount = 0; + Resource->NumberOfSharedWaiters = 0; + Resource->NumberOfExclusiveWaiters = 0; + + /* Zero the owner entry */ + Resource->OwnerEntry.OwnerThread = 0; + Resource->OwnerEntry.TableSize = 0; + + /* Check if exclusive waiters event exists */ + if(Resource->ExclusiveWaiters) + { + /* Reinitialize the event */ + KE::Event::InitializeEvent(Resource->ExclusiveWaiters, SynchronizationEvent, FALSE); + } + + /* Check if owner table exists */ + if(Resource->OwnerTable) + { + /* Iterate over the owner table */ + for(Index = 1; Index < Resource->OwnerTable->TableSize; Index++) + { + /* Zero the owner table */ + Resource->OwnerTable[Index].OwnerCount = 0; + Resource->OwnerTable[Index].OwnerThread = 0; + } + } + + /* Check if shared waiters semaphore exists */ + if(Resource->SharedWaiters) + { + /* Reinitialize the semaphore */ + KE::Semaphore::InitializeSemaphore(Resource->SharedWaiters, 0, MAXLONG); + } + + /* Return success */ + return STATUS_SUCCESS; +} diff --git a/xtoskrnl/includes/ex.hh b/xtoskrnl/includes/ex.hh index c29ac45..3b3be5d 100644 --- a/xtoskrnl/includes/ex.hh +++ b/xtoskrnl/includes/ex.hh @@ -12,6 +12,7 @@ #include #include +#include #include #endif /* __XTOSKRNL_EX_HH */ diff --git a/xtoskrnl/includes/ex/resource.hh b/xtoskrnl/includes/ex/resource.hh new file mode 100644 index 0000000..1492da4 --- /dev/null +++ b/xtoskrnl/includes/ex/resource.hh @@ -0,0 +1,33 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/includes/ex/resource.hh + * DESCRIPTION: Exclusive resource synchronization support + * DEVELOPERS: Aiken Harris + */ + +#ifndef __XTOSKRNL_EX_RESOURCE_HH +#define __XTOSKRNL_EX_RESOURCE_HH + +#include + + +/* Kernel Executive */ +namespace EX +{ + class Resources + { + private: + STATIC LARGE_INTEGER ResourcesTimeOut; + STATIC LIST_ENTRY SystemResourcesList; + STATIC KSPIN_LOCK SystemResourcesLock; + + public: + STATIC XTAPI XTSTATUS DeleteResource(IN PERESOURCE Resource); + STATIC XTAPI XTSTATUS InitializeResource(IN PERESOURCE Resource); + STATIC XTAPI VOID InitializeSystemResources(VOID); + STATIC XTAPI XTSTATUS ReinitializeResource(IN PERESOURCE Resource); + }; +} + +#endif /* __XTOSKRNL_EX_RESOURCES_HH */