8#include <userver/utils/statistics/fwd.hpp>
10USERVER_NAMESPACE_BEGIN
12namespace utils::statistics {
16class RelaxedCounter final {
20 constexpr RelaxedCounter()
noexcept =
default;
21 constexpr RelaxedCounter(T desired)
noexcept : val_(desired) {}
23 RelaxedCounter(
const RelaxedCounter& other)
noexcept : val_(other.Load()) {}
25 RelaxedCounter& operator=(
const RelaxedCounter& other)
noexcept {
26 if (
this == &other)
return *
this;
32 RelaxedCounter& operator=(T desired)
noexcept {
37 void Store(T desired)
noexcept { val_.store(desired, std::memory_order_relaxed); }
39 T Load()
const noexcept {
return val_.load(std::memory_order_relaxed); }
41 operator T()
const noexcept {
return Load(); }
43 RelaxedCounter& operator++()
noexcept {
44 val_.fetch_add(1, std::memory_order_relaxed);
48 T operator++(
int)
noexcept {
return val_.fetch_add(1, std::memory_order_relaxed); }
50 RelaxedCounter& operator--()
noexcept {
51 val_.fetch_sub(1, std::memory_order_relaxed);
55 T operator--(
int)
noexcept {
return val_.fetch_sub(1, std::memory_order_relaxed); }
57 RelaxedCounter& operator+=(T arg)
noexcept {
58 val_.fetch_add(arg, std::memory_order_relaxed);
62 RelaxedCounter& operator-=(T arg)
noexcept {
63 val_.fetch_sub(arg, std::memory_order_relaxed);
68 static_assert(std::atomic<T>::is_always_lock_free);
70 std::atomic<T> val_{T{}};
74void DumpMetric(Writer& writer,
const RelaxedCounter<T>& value) {
75 writer = value.Load();