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 {
38 val_.store(desired, std::memory_order_relaxed);
41 T Load()
const noexcept {
return val_.load(std::memory_order_relaxed); }
43 operator T()
const noexcept {
return Load(); }
45 RelaxedCounter& operator++()
noexcept {
46 val_.fetch_add(1, std::memory_order_relaxed);
50 T operator++(
int)
noexcept {
51 return val_.fetch_add(1, std::memory_order_relaxed);
54 RelaxedCounter& operator--()
noexcept {
55 val_.fetch_sub(1, std::memory_order_relaxed);
59 T operator--(
int)
noexcept {
60 return val_.fetch_sub(1, std::memory_order_relaxed);
63 RelaxedCounter& operator+=(T arg)
noexcept {
64 val_.fetch_add(arg, std::memory_order_relaxed);
68 RelaxedCounter& operator-=(T arg)
noexcept {
69 val_.fetch_sub(arg, std::memory_order_relaxed);
74 static_assert(std::atomic<T>::is_always_lock_free);
76 std::atomic<T> val_{T{}};
80void DumpMetric(Writer& writer,
const RelaxedCounter<T>& value) {
81 writer = value.Load();