HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
unfair_mutex_intf.hpp
1// Copyright Take Vos 2020-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
9#pragma once
10
11#include "../utility/utility.hpp"
12#include "../macros.hpp"
13#include <atomic>
14#include <memory>
15
16hi_export_module(hikogui.concurrency.unfair_mutex : intf);
17
18hi_export namespace hi { inline namespace v1 {
19
37template<bool UseDeadLockDetector>
39public:
40 constexpr unfair_mutex_impl() noexcept {}
41 unfair_mutex_impl(unfair_mutex_impl const&) = delete;
43 unfair_mutex_impl& operator=(unfair_mutex_impl const&) = delete;
44 unfair_mutex_impl& operator=(unfair_mutex_impl&&) = delete;
45
47
48 bool is_locked() const noexcept;
49
50 void lock() noexcept;
51
59 [[nodiscard]] bool try_lock() noexcept;
60
61 void unlock() noexcept;
62
63private:
64 /*
65 * semaphore value:
66 * 0 - Unlocked, no other thread is waiting.
67 * 1 - Locked, no other thread is waiting.
68 * 2 - Locked, zero or more threads are waiting.
69 */
70 std::atomic_unsigned_lock_free semaphore = 0;
71 using semaphore_value_type = typename decltype(semaphore)::value_type;
72
73 bool holds_invariant() const noexcept;
74
75 void lock_contended(semaphore_value_type expected) noexcept;
76};
77
78#ifndef NDEBUG
80#else
82#endif
83
84}} // namespace hi::v1
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
An unfair mutex This is a fast implementation of a mutex which does not fairly arbitrate between mult...
Definition unfair_mutex_intf.hpp:38
bool try_lock() noexcept
When try_lock() is called from a thread that already owns the lock it will return false.
Definition unfair_mutex_impl.hpp:216