// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) // Licensed under the Apache License, Version 2.0 (see LICENSE file) // Official repository: https://github.com/nekernel-org/nekernel #ifndef KERNELKIT_LOCKDELEGATE_H #define KERNELKIT_LOCKDELEGATE_H #include #include namespace Kernel { enum { kLockInvalid = 0, kLockDone = 200, kLockTimedOut = 300, kLockCount = 3, }; /// @brief Lock condition pointer. typedef Boolean* LockPtr; /// @brief Locking delegate class, hangs until limit. /// @tparam N the amount of cycles to wait. template 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 fLockStatus; }; } // namespace Kernel #endif