Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Atomic.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/Concepts.hpp"
23
24#include <atomic>
25#include <memory>
26
27namespace util {
28
32template <SomeNumberType NumberType>
33class Atomic {
34public:
35 using ValueType = NumberType;
36
37 Atomic() = default;
38
44 Atomic(ValueType const value) : value_(value)
45 {
46 }
47
48 ~Atomic() = default;
49
50 // Copy and move constructors and assignment operators are not allowed for atomics
51 Atomic(Atomic const&) = delete;
52 Atomic(Atomic&&) = delete;
53 Atomic&
54 operator=(Atomic const&) = delete;
55 Atomic&
56 operator=(Atomic&&) = delete;
57
63 void
64 add(ValueType const value)
65 {
66 if constexpr (std::is_integral_v<ValueType>) {
67 value_.fetch_add(value);
68 } else {
69#if __cpp_lib_atomic_float >= 201711L
70 value_.fetch_add(value);
71#else
72 // Workaround for atomic float not being supported by the standard library
73 // compare_exchange_weak returns false if the value is not exchanged and updates the current value
74 auto current = value_.load();
75 while (!value_.compare_exchange_weak(current, current + value)) {
76 }
77#endif
78 }
79 }
80
86 void
87 set(ValueType const value)
88 {
89 value_ = value;
90 }
91
97 ValueType
98 value() const
99 {
100 return value_;
101 }
102
103private:
104 std::atomic<ValueType> value_{0};
105};
106
107template <SomeNumberType NumberType>
108using AtomicPtr = std::unique_ptr<Atomic<NumberType>>;
109
110} // namespace util
Atomic wrapper for integral and floating point types.
Definition Atomic.hpp:33
void set(ValueType const value)
Update the current value to the new value.
Definition Atomic.hpp:87
void add(ValueType const value)
Add a value to the current value.
Definition Atomic.hpp:64
ValueType value() const
Get the current value.
Definition Atomic.hpp:98
Atomic(ValueType const value)
Construct a new Atomic object.
Definition Atomic.hpp:44
This namespace contains various utilities.
Definition AccountUtils.hpp:30