From 57921ca1d52dd3c3c988bf198b903c2da42f391a Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 16 Mar 2025 16:42:09 +0100 Subject: ADD: BinaryMutex class, and reworking Semaphore class. Signed-off-by: Amlal El Mahrouss --- dev/Kernel/src/BinaryMutex.cc | 74 +++++++++++++++++++++++++++++++++++++++++++ dev/Kernel/src/Semaphore.cc | 71 ----------------------------------------- 2 files changed, 74 insertions(+), 71 deletions(-) create mode 100644 dev/Kernel/src/BinaryMutex.cc delete mode 100644 dev/Kernel/src/Semaphore.cc (limited to 'dev/Kernel/src') diff --git a/dev/Kernel/src/BinaryMutex.cc b/dev/Kernel/src/BinaryMutex.cc new file mode 100644 index 00000000..4fdaa8f4 --- /dev/null +++ b/dev/Kernel/src/BinaryMutex.cc @@ -0,0 +1,74 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. + +------------------------------------------- */ + +#include +#include + +namespace NeOS +{ + /***********************************************************************************/ + /// @brief Unlocks the semaphore. + /***********************************************************************************/ + Bool BinaryMutex::Unlock() noexcept + { + if (fLockingProcess) + { + fLockingProcess = UserProcess(); + fLockingProcess.Status = ProcessStatusKind::kFrozen; + return Yes; + } + + return No; + } + + /***********************************************************************************/ + /// @brief Locks process in the semaphore. + /***********************************************************************************/ + Bool BinaryMutex::Lock(UserProcess& process) + { + if (!process || fLockingProcess) + return No; + + fLockingProcess = process; + + return Yes; + } + + /***********************************************************************************/ + /// @brief Checks if process is locked. + /***********************************************************************************/ + Bool BinaryMutex::IsLocked() const + { + return fLockingProcess.Status == ProcessStatusKind::kRunning; + } + + /***********************************************************************************/ + /// @brief Try lock or wait. + /***********************************************************************************/ + Bool BinaryMutex::LockOrWait(UserProcess& process, TimerInterface* timer) + { + if (timer == nullptr) + return No; + + this->Lock(process); + + timer->Wait(); + + return this->Lock(process); + } + + /***********************************************************************************/ + /// @brief Wait for process **sec** until we check if it's free. + /// @param sec seconds. + /***********************************************************************************/ + BOOL BinaryMutex::WaitForProcess(const Int16& sec) noexcept + { + HardwareTimer hw_timer(rtl_seconds(sec)); + hw_timer.Wait(); + + return !this->IsLocked(); + } +} // namespace NeOS diff --git a/dev/Kernel/src/Semaphore.cc b/dev/Kernel/src/Semaphore.cc deleted file mode 100644 index 20dd89e6..00000000 --- a/dev/Kernel/src/Semaphore.cc +++ /dev/null @@ -1,71 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved. - -------------------------------------------- */ - -#include -#include - -namespace NeOS -{ - /***********************************************************************************/ - /// @brief Unlocks the semaphore. - /***********************************************************************************/ - Bool Semaphore::Unlock() noexcept - { - if (fLockingProcess) - { - fLockingProcess = UserProcess(); - fLockingProcess.Status = ProcessStatusKind::kFrozen; - return Yes; - } - - return No; - } - - /***********************************************************************************/ - /// @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.Status == ProcessStatusKind::kRunning; - } - - /***********************************************************************************/ - /// @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 NeOS -- cgit v1.2.3