aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/include
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86_64/include')
-rw-r--r--arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp8
-rw-r--r--arch/x86_64/include/arch/memory/heap/memory_block.hpp3
-rw-r--r--arch/x86_64/include/arch/shared/mutex.hpp46
3 files changed, 52 insertions, 5 deletions
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<memory_block *> 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 <atomic>
#include <cstdint>
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<memory_block *> 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 <atomic>
+
+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<bool> locked{false};
+ };
+} // namespace teachos::arch::shared
+#endif // TEACHOS_ARCH_X86_64_MUTEX_HPP \ No newline at end of file