HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
wfree_idle_count.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../utility/module.hpp"
8#include <atomic>
9#include <cstdint>
10
11namespace hi::inline v1 {
12
38public:
39 constexpr wfree_idle_count() noexcept = default;
40 ~wfree_idle_count() = default;
41 wfree_idle_count(wfree_idle_count const&) = delete;
43 wfree_idle_count& operator=(wfree_idle_count const&) = delete;
44 wfree_idle_count& operator=(wfree_idle_count&&) = delete;
45
50 [[nodiscard]] hi_force_inline bool is_locked() const noexcept
51 {
52 return to_bool(_lock_count.load(std::memory_order::relaxed));
53 }
54
59 hi_force_inline void lock() noexcept
60 {
61 hilet lock_count = _lock_count.fetch_add(1, std::memory_order::acquire);
63 }
64
69 hi_force_inline void unlock() noexcept
70 {
71 auto lock_count = _lock_count.fetch_sub(1, std::memory_order::release);
72 hi_axiom(lock_count != 0);
73 if (lock_count == 1) {
74 // No one is locking, increment the idle count.
75 _version.fetch_add(1, std::memory_order::relaxed);
76 }
77 }
78
83 hi_force_inline uint64_t operator*() const noexcept
84 {
85 return _version.load(std::memory_order::acquire);
86 }
87
88private:
89 std::atomic<uint64_t> _version = 0;
90 std::atomic<uint64_t> _lock_count = 0;
91};
92
93} // namespace hi::inline v1
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
Counts how many times a critical section was idle.
Definition wfree_idle_count.hpp:37
hi_force_inline void lock() noexcept
Start the critical section.
Definition wfree_idle_count.hpp:59
hi_force_inline bool is_locked() const noexcept
Check if the critical section is locked.
Definition wfree_idle_count.hpp:50
hi_force_inline uint64_t operator*() const noexcept
Get the current idle-count.
Definition wfree_idle_count.hpp:83
hi_force_inline void unlock() noexcept
End the critical section.
Definition wfree_idle_count.hpp:69