diff options
| author | Amlal <amlalelmahrouss@icloud.com> | 2024-05-11 06:43:54 +0000 |
|---|---|---|
| committer | Amlal <amlalelmahrouss@icloud.com> | 2024-05-11 06:43:54 +0000 |
| commit | ca675beb41dba8d7d16c5793b55d1672f38be3b4 (patch) | |
| tree | c995ada42729ac2059a0ed87a4539d1a7e10b14a /Kernel/KernelKit/LockDelegate.hpp | |
| parent | 2b4a4792abf51487ab4a16106f9376f43acf381a (diff) | |
| parent | bc57a29a24b98b00ba17710ba84ec2188ab73504 (diff) | |
Merged in MHR-23 (pull request #12)
MHR-23: Merge work.
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 |
