From 610f91d87152cbe48d3054fcf437d8239da6ef35 Mon Sep 17 00:00:00 2001 From: Amlal Date: Sat, 21 Dec 2024 21:59:13 +0100 Subject: IMP: :boom: Breaking changes some checks are needed to be done. Signed-off-by: Amlal --- dev/Kernel/src/Semaphore.cc | 72 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 dev/Kernel/src/Semaphore.cc (limited to 'dev/Kernel/src/Semaphore.cc') diff --git a/dev/Kernel/src/Semaphore.cc b/dev/Kernel/src/Semaphore.cc new file mode 100644 index 00000000..804feaa8 --- /dev/null +++ b/dev/Kernel/src/Semaphore.cc @@ -0,0 +1,72 @@ +/* ------------------------------------------- + + Copyright (C) 2024, TQ B.V, all rights reserved. + +------------------------------------------- */ + +#include +#include + +namespace Kernel +{ + /***********************************************************************************/ + /// @brief Unlocks process out of the semaphore. + /***********************************************************************************/ + Bool Semaphore::Unlock() noexcept + { + if (fLockingProcess) + fLockingProcess = nullptr; + 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 (process == nullptr) + return No; + + 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 -- cgit v1.2.3