فهرست منبع

feat(lock): impl. spin lock

greatbridf 2 سال پیش
والد
کامیت
aa5b5749c4
2فایلهای تغییر یافته به همراه36 افزوده شده و 0 حذف شده
  1. 1 0
      CMakeLists.txt
  2. 35 0
      include/types/lock.h

+ 1 - 0
CMakeLists.txt

@@ -83,6 +83,7 @@ set(KERNEL_MAIN_SOURCES src/kernel_main.c
                         include/types/allocator.hpp
                         include/types/cplusplus.hpp
                         include/types/list.hpp
+                        include/types/lock.h
                         include/types/string.hpp
                         include/types/vector.hpp
                         include/kernel_main.h

+ 35 - 0
include/types/lock.h

@@ -0,0 +1,35 @@
+#pragma once
+
+#include <types/stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+static inline void spin_lock(uint32_t* lock_addr)
+{
+    asm volatile(
+            "_spin:\n\t\
+             movl $1, %%eax\n\t\
+             xchgl %%eax, (%0)\n\t\
+             test $0, %%eax\n\t\
+             jne _spin\n\t\
+            "
+            : "=r" (lock_addr)
+            : "0"  (lock_addr)
+            : "eax", "memory"
+            );
+}
+
+static inline void spin_unlock(uint32_t* lock_addr)
+{
+    asm volatile("movl $0, %%eax\nxchgl %%eax, (%0)"
+                 :
+                 : "r"  (lock_addr)
+                 : "eax", "memory"
+                 );
+}
+
+#ifdef __cplusplus
+}
+#endif