summaryrefslogtreecommitdiffhomepage
path: root/dev/zka/KernelKit/LockDelegate.h
diff options
context:
space:
mode:
Diffstat (limited to 'dev/zka/KernelKit/LockDelegate.h')
-rw-r--r--dev/zka/KernelKit/LockDelegate.h67
1 files changed, 67 insertions, 0 deletions
diff --git a/dev/zka/KernelKit/LockDelegate.h b/dev/zka/KernelKit/LockDelegate.h
new file mode 100644
index 00000000..8f7e9a42
--- /dev/null
+++ b/dev/zka/KernelKit/LockDelegate.h
@@ -0,0 +1,67 @@
+/* -------------------------------------------
+
+ Copyright ZKA Web Services Co.
+
+------------------------------------------- */
+
+#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;
+ }
+ }
+
+ 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