NeKernel dev
Loading...
Searching...
No Matches
Semaphore.h
Go to the documentation of this file.
1/* ========================================
2
3 Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
4
5======================================== */
6
7#pragma once
8
12
14#include <KernelKit/Timer.h>
15#include <NeKit/Config.h>
16
17#define kSemaphoreOwnerIndex (0U)
18#define kSemaphoreCountIndex (1U)
19
20#define kSemaphoreCount (2U)
21
22#define kSemaphoreIncrementOwner(sem) (sem[kSemaphoreOwnerIndex]++)
23#define kSemaphoreDecrementOwner(sem) (sem[kSemaphoreOwnerIndex]--)
24
25namespace Kernel {
28
30inline BOOL rtl_sem_is_valid(const SemaphoreArr& sem, UInt64 owner = 0) {
31 return sem[kSemaphoreOwnerIndex] == owner && sem[kSemaphoreCountIndex] > 0;
32}
33
38 sem[kSemaphoreOwnerIndex] = 0;
39 sem[kSemaphoreCountIndex] = 0;
40
41 return TRUE;
42}
43
49 if (!owner) {
51 return FALSE; // Invalid owner
52 }
53
54 sem[kSemaphoreOwnerIndex] = owner;
55 sem[kSemaphoreCountIndex] = 0;
56
57 return TRUE;
58}
59
65inline BOOL rtl_sem_wait(SemaphoreArr& sem, UInt64 owner, UInt64 timeout,
66 BOOL* condition = nullptr) {
67 if (!rtl_sem_is_valid(sem, owner)) {
68 return FALSE;
69 }
70
71 if (timeout <= 0) {
73
74 return FALSE;
75 }
76
77 if (!condition || *condition) {
78 if (sem[kSemaphoreCountIndex] == 0) {
80 return FALSE;
81 }
82
85
86 return TRUE;
87 }
88
89 HardwareTimer timer(timeout);
90 BOOL ret = timer.Wait();
91
92 if (ret) {
93 if (!condition || *condition) {
94 if (sem[kSemaphoreCountIndex] == 0) {
96 return FALSE;
97 }
98
101
102 return TRUE;
103 }
104 }
105
107
108 return FALSE; // Failed to acquire semaphore
109}
110} // namespace Kernel
#define TRUE
#define FALSE
#define BOOL
#define err_global_get()
Definition KPC.h:25
#define kSemaphoreOwnerIndex
Definition Semaphore.h:17
#define kSemaphoreCountIndex
Definition Semaphore.h:18
#define kSemaphoreCount
Definition Semaphore.h:20
Definition Timer.h:47
BOOL Wait() override
Wait for the timer to stop spinning.
Definition HalTimer.cc:82
UPS inline definitions.
Definition Device.h:12
BOOL rtl_sem_wait(SemaphoreArr &sem, UInt64 owner, UInt64 timeout, BOOL *condition=nullptr)
Waits for the semaphore to be available, blocking until it is.
Definition Semaphore.h:65
constexpr KPCError kErrorSuccess
Definition KPC.h:32
UInt64 SemaphoreArr[kSemaphoreCount]
Semaphore structure used for synchronization.
Definition Semaphore.h:27
BOOL rtl_sem_acquire(SemaphoreArr &sem, UInt64 owner)
Initializes the semaphore with an owner and a count of zero.
Definition Semaphore.h:48
BOOL rtl_sem_is_valid(const SemaphoreArr &sem, UInt64 owner=0)
Checks if the semaphore is valid.
Definition Semaphore.h:30
constexpr KPCError kErrorUnavailable
Definition KPC.h:70
constexpr KPCError kErrorInvalidData
Definition KPC.h:56
BOOL rtl_sem_release(SemaphoreArr &sem)
Releases the semaphore, resetting its owner and count.
Definition Semaphore.h:37
constexpr KPCError kErrorTimeout
Definition KPC.h:68
__UINT64_TYPE__ UInt64
Definition Config.h:48