summaryrefslogtreecommitdiffhomepage
path: root/dev/Kernel/src/Semaphore.cc
diff options
context:
space:
mode:
authorAmlal <amlal.elmahrouss@icloud.com>2025-01-24 10:38:36 +0100
committerAmlal <amlal.elmahrouss@icloud.com>2025-01-24 10:38:36 +0100
commit7b4bd3577a31d0f0adc7371840642791ae1567f4 (patch)
tree1a8afc973aaa739d0d763315cad2fd376d1cea9c /dev/Kernel/src/Semaphore.cc
ADD: Open version, with important changes kept out.
Signed-off-by: Amlal <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev/Kernel/src/Semaphore.cc')
-rw-r--r--dev/Kernel/src/Semaphore.cc69
1 files changed, 69 insertions, 0 deletions
diff --git a/dev/Kernel/src/Semaphore.cc b/dev/Kernel/src/Semaphore.cc
new file mode 100644
index 00000000..b82efbfe
--- /dev/null
+++ b/dev/Kernel/src/Semaphore.cc
@@ -0,0 +1,69 @@
+/* -------------------------------------------
+
+ Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved.
+
+------------------------------------------- */
+
+#include <KernelKit/UserProcessScheduler.h>
+#include <KernelKit/Semaphore.h>
+
+namespace Kernel
+{
+ /***********************************************************************************/
+ /// @brief Unlocks process out of the semaphore.
+ /***********************************************************************************/
+ Bool Semaphore::Unlock() noexcept
+ {
+ if (fLockingProcess)
+ fLockingProcess = UserProcess();
+ else
+ return No;
+
+ return Yes;
+ }
+
+ /***********************************************************************************/
+ /// @brief Locks process in the semaphore.
+ /***********************************************************************************/
+ Bool Semaphore::Lock(UserProcess& process)
+ {
+ if (!process || fLockingProcess)
+ return No;
+
+ fLockingProcess = process;
+
+ return Yes;
+ }
+
+ /***********************************************************************************/
+ /// @brief Checks if process is locked.
+ /***********************************************************************************/
+ Bool Semaphore::IsLocked() const
+ {
+ return fLockingProcess;
+ }
+
+ /***********************************************************************************/
+ /// @brief Try lock or wait.
+ /***********************************************************************************/
+ Bool Semaphore::LockOrWait(UserProcess& process, TimerInterface* timer)
+ {
+ if (timer == nullptr)
+ return No;
+
+ this->Lock(process);
+
+ timer->Wait();
+
+ return this->Lock(process);
+ }
+
+ /***********************************************************************************/
+ /// @brief Wait for process to be free.
+ /***********************************************************************************/
+ Void Semaphore::WaitForProcess() noexcept
+ {
+ while (fLockingProcess)
+ ;
+ }
+} // namespace Kernel