summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/KernelKit/Semaphore.h
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-07-21 08:51:41 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2025-07-21 09:07:20 +0100
commit4e61e22c3da59b259741e57298725330791aed3e (patch)
treeb81e078defa58d1fcae570fa72b403606ae596b8 /dev/kernel/KernelKit/Semaphore.h
parent1e12163836e649da0d67fc8f17bc9a415554efe4 (diff)
feat: NeKernel Semaphore API.
includes: - New semaphore API for the kernel, we also make use of the HardwareTimer class here. - Defined header only for now. - New HAL HW Timer API in AMD64. Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/kernel/KernelKit/Semaphore.h')
-rw-r--r--dev/kernel/KernelKit/Semaphore.h79
1 files changed, 78 insertions, 1 deletions
diff --git a/dev/kernel/KernelKit/Semaphore.h b/dev/kernel/KernelKit/Semaphore.h
index a1b5ecad..7fa05008 100644
--- a/dev/kernel/KernelKit/Semaphore.h
+++ b/dev/kernel/KernelKit/Semaphore.h
@@ -6,10 +6,87 @@
#pragma once
+/// @author Amlal El Mahrouss
+/// @file Semaphore.h
+/// @brief Semaphore structure and functions for synchronization in the kernel.
+
#include <CompilerKit/CompilerKit.h>
#include <KernelKit/Timer.h>
#include <NeKit/Defines.h>
+#define kSemaphoreOwnerIndex (0)
+#define kSemaphoreCountIndex (1)
+
+#define kSemaphoreCount (2)
+
namespace Kernel {
-typedef Int64 Semaphore;
+/// @brief Semaphore structure used for synchronization.
+typedef UInt64 Semaphore[kSemaphoreCount];
+
+/// @brief Checks if the semaphore is valid.
+inline BOOL rtl_sem_is_valid(const Semaphore& sem) {
+ return sem[kSemaphoreOwnerIndex] != 0 || sem[kSemaphoreCountIndex] > 0;
+}
+
+/// @brief Releases the semaphore, resetting its owner and count.
+/// @param sem
+/// @return
+inline BOOL rtl_sem_release(Semaphore& sem) {
+ sem[kSemaphoreOwnerIndex] = 0;
+ sem[kSemaphoreCountIndex] = 0;
+
+ return TRUE;
+}
+
+/// @brief Initializes the semaphore with an owner and a count of zero.
+/// @param sem
+/// @param owner
+/// @return
+inline BOOL rtl_sem_init(Semaphore& sem, Int64 owner) {
+ if (!owner || sem[kSemaphoreOwnerIndex] > 0) {
+ return FALSE;
+ }
+
+ sem[kSemaphoreOwnerIndex] = owner;
+ sem[kSemaphoreCountIndex] = 0;
+
+ return TRUE;
+}
+
+/// @brief Waits for the semaphore to be available, blocking until it is.
+/// @param sem
+/// @param timeout
+/// @return
+inline BOOL rtl_sem_wait(Semaphore& sem, Int64 timeout) {
+ if (!rtl_sem_is_valid(sem)) {
+ return FALSE;
+ }
+
+ if (timeout <= 0) {
+ err_global_get() = kErrorNetworkTimeout;
+
+ return FALSE;
+ }
+
+ if (sem[kSemaphoreCountIndex] > 0) {
+ err_global_get() = kErrorSuccess;
+ sem[kSemaphoreCountIndex]--;
+
+ return TRUE;
+ }
+
+ HardwareTimer timer(timeout);
+ timer.Wait();
+
+ if (sem[kSemaphoreCountIndex] > 0) {
+ err_global_get() = kErrorSuccess;
+ sem[kSemaphoreCountIndex]--;
+
+ return TRUE;
+ }
+
+ err_global_get() = kErrorNetworkTimeout;
+
+ return FALSE; // Failed to acquire semaphore
+} // namespace Kernel
} // namespace Kernel \ No newline at end of file