summaryrefslogtreecommitdiffhomepage
path: root/dev/Kernel/KernelKit/LockDelegate.h
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/KernelKit/LockDelegate.h
ADD: Open version, with important changes kept out.
Signed-off-by: Amlal <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev/Kernel/KernelKit/LockDelegate.h')
-rw-r--r--dev/Kernel/KernelKit/LockDelegate.h69
1 files changed, 69 insertions, 0 deletions
diff --git a/dev/Kernel/KernelKit/LockDelegate.h b/dev/Kernel/KernelKit/LockDelegate.h
new file mode 100644
index 00000000..2ee4bb25
--- /dev/null
+++ b/dev/Kernel/KernelKit/LockDelegate.h
@@ -0,0 +1,69 @@
+/* -------------------------------------------
+
+ Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved.
+
+------------------------------------------- */
+
+#pragma once
+
+#include <NewKit/Atom.h>
+#include <NewKit/Defines.h>
+
+namespace Kernel
+{
+ enum
+ {
+ kLockDone = 200,
+ kLockTimedOut,
+ };
+
+ /// @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;
+ }
+
+ ++spin;
+ }
+
+ 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 Kernel