aboutsummaryrefslogblamecommitdiffstats
path: root/library/cpp/messagebus/lfqueue_batch.h
blob: 8128d3154d0477f49fe9638cc0ae24b024feef2a (plain) (tree)
1
2
3
4
5
6
7
8
9
10
            
                                                         
 

                                
                                                                             
                           
                                                             
 



                               
                                                                      

                                     
                                                                         
                                                       
                                                                                                                 
                                                







                                                                
#pragma once

#include <library/cpp/messagebus/actor/temp_tls_vector.h>

#include <util/generic/vector.h>
#include <util/thread/lfstack.h>

template <typename T, template <typename, class> class TVectorType = TVector>
class TLockFreeQueueBatch {
private:
    TLockFreeStack<TVectorType<T, std::allocator<T>>*> Stack;

public:
    bool IsEmpty() {
        return Stack.IsEmpty();
    }

    void EnqueueAll(TAutoPtr<TVectorType<T, std::allocator<T>>> vec) {
        Stack.Enqueue(vec.Release());
    }

    void DequeueAllSingleConsumer(TVectorType<T, std::allocator<T>>* r) {
        TTempTlsVector<TVectorType<T, std::allocator<T>>*> vs;
        Stack.DequeueAllSingleConsumer(vs.GetVector());

        for (typename TVector<TVectorType<T, std::allocator<T>>*>::reverse_iterator i = vs.GetVector()->rbegin();
             i != vs.GetVector()->rend(); ++i) {
            if (i == vs.GetVector()->rend()) {
                r->swap(**i);
            } else {
                r->insert(r->end(), (*i)->begin(), (*i)->end());
            }
            delete *i;
        }
    }
};