summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/KernelKit/LockDelegate.h
blob: a657caa0b37d24f6d8f26653da4516ae1ba4452c (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
65
66
67
68
69
/* -------------------------------------------

	Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved.

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

#pragma once

#include <NewKit/Atom.h>
#include <NewKit/Defines.h>

namespace NeOS
{
	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 NeOS