summaryrefslogtreecommitdiffhomepage
path: root/Kernel/KernelKit/LockDelegate.hpp
blob: 0bf8104e15dbed5f601dee5e815bbc6cbfd61777 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* -------------------------------------------

	Copyright Zeta Electronics Corporation

------------------------------------------- */

#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