summaryrefslogtreecommitdiffhomepage
path: root/Private/NewKit/LockDelegate.hpp
blob: c6c1ce0c3abcbec7913f506c2b89579251e648bf (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
/*
 *	========================================================
 *
 *	HCore
 * 	Copyright Mahrouss Logic, all rights reserved.
 *
 * 	========================================================
 */

#pragma once

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

#define kLockDone (200U)    /* job is done */
#define kLockTimedOut (100U) /* has timed out */

namespace HCore
{
template <Size N>
class LockDelegate final
{
  public:
    LockDelegate() = delete;

  public:
    explicit LockDelegate(Boolean *expr)
    {
        auto spin = 0U;
        while (spin != N)
        {
            if (*expr)
            {
                m_LockStatus | kLockDone;
                break;
            }
        }

        if (spin == N)
            m_LockStatus | kLockTimedOut;
    }

    ~LockDelegate() = default;

    LockDelegate &operator=(const LockDelegate &) = delete;
    LockDelegate(const LockDelegate &) = delete;

    bool Done()
    {
        return m_LockStatus[kLockDone] == kLockDone;
    }
    bool HasTimedOut()
    {
        return m_LockStatus[kLockTimedOut] != kLockTimedOut;
    }

  private:
    Atom<UInt> m_LockStatus;
};
} // namespace HCore