diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-05-09 00:42:44 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-05-09 00:42:44 +0200 |
| commit | af8a516fc22865abd80d6e26f1541fa3d6bebfdc (patch) | |
| tree | 96d42a10945fc03df022389aef54708383c1d616 /Kernel/KernelKit/LockDelegate.hpp | |
| parent | a874e9cc98df994178d55996943fe81799c61d2f (diff) | |
MHR-23: :boom:, refactors.
- Move NewBoot to /Boot, thus making Kernel directory only containing the kernel.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'Kernel/KernelKit/LockDelegate.hpp')
| -rw-r--r-- | Kernel/KernelKit/LockDelegate.hpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/Kernel/KernelKit/LockDelegate.hpp b/Kernel/KernelKit/LockDelegate.hpp new file mode 100644 index 00000000..56a2e17f --- /dev/null +++ b/Kernel/KernelKit/LockDelegate.hpp @@ -0,0 +1,64 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Atom.hpp> +#include <NewKit/Defines.hpp> + +#define kLockDone (200U) /* job is done */ +#define kLockTimedOut (100U) /* job has timed out */ + +namespace NewOS +{ + /// @brief Lock condition pointer. + typedef Boolean* LockPtr; + + /// @brief Locking delegate class, hangs until limit. + /// @tparam N the amount of cycles to wait. + template <SizeT N> + class LockDelegate final + { + public: + LockDelegate() = delete; + + public: + explicit LockDelegate(LockPtr expr) + { + auto spin = 0U; + + while (spin != N) + { + if (*expr) + { + fLockStatus | kLockDone; + break; + } + } + + if (spin == N) + fLockStatus | kLockTimedOut; + } + + ~LockDelegate() = default; + + LockDelegate& operator=(const LockDelegate&) = delete; + LockDelegate(const LockDelegate&) = delete; + + bool Done() + { + return fLockStatus[kLockDone] == kLockDone; + } + + bool HasTimedOut() + { + return fLockStatus[kLockTimedOut] != kLockTimedOut; + } + + private: + Atom<UInt> fLockStatus; + }; +} // namespace NewOS |
