From b4962c8c7b94fce2e67a00671de87fa96fdbb659 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Tue, 3 Dec 2024 08:00:26 +0000 Subject: add mutex to linked_list_allocator --- .../arch/memory/heap/linked_list_allocator.hpp | 8 ++-- .../include/arch/memory/heap/memory_block.hpp | 3 +- arch/x86_64/include/arch/shared/mutex.hpp | 46 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 arch/x86_64/include/arch/shared/mutex.hpp (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp b/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp index e77602c..49217d5 100644 --- a/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp +++ b/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp @@ -2,6 +2,7 @@ #define TEACHOS_ARCH_X86_64_MEMORY_HEAP_LINKED_LIST_ALLOCATOR_HPP #include "arch/memory/heap/memory_block.hpp" +#include "arch/shared/mutex.hpp" namespace teachos::arch::memory::heap { @@ -96,9 +97,10 @@ namespace teachos::arch::memory::heap */ auto clear_memory_block_header(void * pointer) -> void; - std::size_t heap_start; ///< Start of the allocatable heap area - std::size_t heap_end; ///< End of the allocatable heap area - std::atomic first; ///< First free entry in our memory + std::size_t heap_start; ///< Start of the allocatable heap area + std::size_t heap_end; ///< End of the allocatable heap area + memory_block * first; ///< First free entry in our memory + shared::mutex mutex; }; } // namespace teachos::arch::memory::heap diff --git a/arch/x86_64/include/arch/memory/heap/memory_block.hpp b/arch/x86_64/include/arch/memory/heap/memory_block.hpp index 1fbbfd5..c48d0cd 100644 --- a/arch/x86_64/include/arch/memory/heap/memory_block.hpp +++ b/arch/x86_64/include/arch/memory/heap/memory_block.hpp @@ -1,7 +1,6 @@ #ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_MEMORY_BLOCK_HPP #define TEACHOS_ARCH_X86_64_MEMORY_HEAP_MEMORY_BLOCK_HPP -#include #include namespace teachos::arch::memory::heap @@ -22,7 +21,7 @@ namespace teachos::arch::memory::heap std::size_t size; ///< Amount of free memory this hole contains, has to always be atleast 16 bytes to hold the size ///< variable and the pointer to the next hole. - std::atomic next; ///< Optional pointer to the next free memory, holds nullptr if there is none. + memory_block * next; ///< Optional pointer to the next free memory, holds nullptr if there is none. }; } // namespace teachos::arch::memory::heap diff --git a/arch/x86_64/include/arch/shared/mutex.hpp b/arch/x86_64/include/arch/shared/mutex.hpp new file mode 100644 index 0000000..d874dd8 --- /dev/null +++ b/arch/x86_64/include/arch/shared/mutex.hpp @@ -0,0 +1,46 @@ +#ifndef TEACHOS_ARCH_X86_64_MUTEX_HPP +#define TEACHOS_ARCH_X86_64_MUTEX_HPP + +#include + +namespace teachos::arch::shared +{ + struct mutex + { + mutex() = default; + ~mutex() = default; + + mutex(const mutex &) = delete; + mutex & operator=(const mutex &) = delete; + + /** + * @brief Lock the mutex (blocks if not available) + */ + void lock() + { + while (true) + { + if (!locked.exchange(true, std::memory_order_acquire)) + { + return; + } + } + } + + /** + * @brief Try to lock the mutex (non-blocking) + * + * @return true if lock has been acquired and false otherwise + */ + bool try_lock() { return !locked.exchange(true, std::memory_order_acquire); } + + /** + * @brief Unlock the mutex + */ + void unlock() { locked.store(false, std::memory_order_release); } + + private: + std::atomic locked{false}; + }; +} // namespace teachos::arch::shared +#endif // TEACHOS_ARCH_X86_64_MUTEX_HPP \ No newline at end of file -- cgit v1.2.3