aboutsummaryrefslogblamecommitdiffstats
path: root/library/cpp/monlib/metrics/atomics_array.h
blob: 876c167938bd997749c09e60df9da33b2d60381b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11



                                
                 


                                           
                                                  

                                                
                                                               


                                                      
                                                
                                                                  




                                      
                                                     
                                                
                                                                       
         




                                                               

                                                
                                                                           



                        
                                                

                     
#pragma once

#include <util/generic/ptr.h>
#include <util/generic/vector.h>

#include <atomic>

namespace NMonitoring {
    class TAtomicsArray {
    public:
        explicit TAtomicsArray(size_t size)
            : Values_(new std::atomic<ui64>[size])
            , Size_(size)
        {
            for (size_t i = 0; i < Size_; i++) {
                Values_[i].store(0, std::memory_order_relaxed);
            }
        }

        ui64 operator[](size_t index) const noexcept {
            Y_DEBUG_ABORT_UNLESS(index < Size_);
            return Values_[index].load(std::memory_order_relaxed);
        }

        size_t Size() const noexcept {
            return Size_;
        }

        void Add(size_t index, ui64 count) noexcept {
            Y_DEBUG_ABORT_UNLESS(index < Size_);
            Values_[index].fetch_add(count, std::memory_order_relaxed);
        }

        void Reset() noexcept {
            for (size_t i = 0; i < Size_; i++) {
                Values_[i].store(0, std::memory_order_relaxed);
            }
        }

        TVector<ui64> Copy() const {
            TVector<ui64> copy(Reserve(Size_));
            for (size_t i = 0; i < Size_; i++) {
                copy.push_back(Values_[i].load(std::memory_order_relaxed));
            }
            return copy;
        }

    private:
        TArrayHolder<std::atomic<ui64>> Values_;
        size_t Size_;
    };
}