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.hpp"
8#include "assert.hpp"
9#include "cast.hpp"
10#include <atomic>
11#include <cstdint>
12
13namespace hi::inline v1 {
14
40public:
41 constexpr wfree_idle_count() noexcept = default;
42 ~wfree_idle_count() = default;
43 wfree_idle_count(wfree_idle_count const&) = delete;
45 wfree_idle_count& operator=(wfree_idle_count const&) = delete;
46 wfree_idle_count& operator=(wfree_idle_count&&) = delete;
47
52 [[nodiscard]] hi_force_inline bool is_locked() const noexcept
53 {
54 return to_bool(_lock_count.load(std::memory_order::relaxed));
55 }
56
61 hi_force_inline void lock() noexcept
62 {
63 auto lock_count = _lock_count.fetch_add(1, std::memory_order::acquire);
65 }
66
71 hi_force_inline void unlock() noexcept
72 {
73 auto lock_count = _lock_count.fetch_sub(1, std::memory_order::release);
74 hi_axiom(lock_count != 0);
75 if (lock_count == 1) {
76 // No one is locking, increment the idle count.
77 _version.fetch_add(1, std::memory_order::relaxed);
78 }
79 }
80
85 hi_force_inline uint64_t operator*() const noexcept
86 {
87 return _version.load(std::memory_order::acquire);
88 }
89
90private:
91 std::atomic<uint64_t> _version = 0;
92 std::atomic<uint64_t> _lock_count = 0;
93};
94
95} // namespace hi::inline v1
Utilities to assert and bound check.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
Utilities used by the HikoGUI library itself.
DOXYGEN BUG.
Definition algorithm.hpp:15
Counts how many times a critical section was idle.
Definition wfree_idle_count.hpp:39
hi_force_inline void lock() noexcept
Start the critical section.
Definition wfree_idle_count.hpp:61
hi_force_inline bool is_locked() const noexcept
Check if the critical section is locked.
Definition wfree_idle_count.hpp:52
hi_force_inline uint64_t operator*() const noexcept
Get the current idle-count.
Definition wfree_idle_count.hpp:85
hi_force_inline void unlock() noexcept
End the critical section.
Definition wfree_idle_count.hpp:71