-
Notifications
You must be signed in to change notification settings - Fork 4
/
in_fake_critical_section.hpp
73 lines (55 loc) · 2.51 KB
/
in_fake_critical_section.hpp
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
70
71
72
73
// Copyright 2019-2021 Laurynas Biveinis
#ifndef UNODB_DETAIL_IN_FAKE_CRITICAL_SECTION_HPP
#define UNODB_DETAIL_IN_FAKE_CRITICAL_SECTION_HPP
#include "global.hpp" // IWYU pragma: keep
#include <cstddef>
#include <type_traits>
namespace unodb {
// Provide access to T with in_critical_section<T>-like interface, except that
// loads and stores are direct instead of relaxed atomic. It enables having a
// common templatized implementation of single-threaded and OLC node algorithms.
template <typename T>
class [[nodiscard]] in_fake_critical_section final {
public:
constexpr in_fake_critical_section() noexcept = default;
// cppcheck-suppress noExplicitConstructor
// NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
constexpr in_fake_critical_section(T value_) noexcept : value{value_} {}
constexpr in_fake_critical_section(const in_fake_critical_section<T> &) =
default;
constexpr in_fake_critical_section(in_fake_critical_section<T> &&) noexcept =
default;
~in_fake_critical_section() noexcept = default;
// Enable when needed
in_fake_critical_section &operator=(in_fake_critical_section &&) = delete;
// Return nothing as we never chain assignments for now.
// NOLINTNEXTLINE(cppcoreguidelines-c-copy-assignment-signature,misc-unconventional-assign-operator)
constexpr void operator=(T new_value) noexcept { value = new_value; }
// NOLINTNEXTLINE(cppcoreguidelines-c-copy-assignment-signature,misc-unconventional-assign-operator)
constexpr void operator=(in_fake_critical_section<T> new_value) noexcept {
value = new_value;
}
constexpr void operator++() noexcept { ++value; }
constexpr void operator--() noexcept { --value; }
// NOLINTNEXTLINE(cert-dcl21-cpp)
constexpr T operator--(int) noexcept { return value--; }
template <typename T_ = T,
typename = std::enable_if_t<!std::is_integral_v<T_>>>
[[nodiscard, gnu::pure]] constexpr auto operator==(
std::nullptr_t) const noexcept {
return value == nullptr;
}
template <typename T_ = T,
typename = std::enable_if_t<!std::is_integral_v<T_>>>
[[nodiscard, gnu::pure]] constexpr auto operator!=(
std::nullptr_t) const noexcept {
return value != nullptr;
}
// NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
[[nodiscard]] constexpr operator T() const noexcept { return value; }
[[nodiscard]] constexpr T load() const noexcept { return value; }
private:
T value;
};
} // namespace unodb
#endif // UNODB_DETAIL_IN_FAKE_CRITICAL_SECTION_HPP