aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/neh/lfqueue.h
blob: c957047a996e63f7dd87b1b1ab1c561453940692 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once

#include <util/thread/lfqueue.h>
#include <util/generic/ptr.h>

namespace NNeh {
    template <class T>
    class TAutoLockFreeQueue {
        struct TCounter : TAtomicCounter {
            inline void IncCount(const T* const&) {
                Inc();
            }

            inline void DecCount(const T* const&) {
                Dec();
            }
        };

    public:
        typedef TAutoPtr<T> TRef;

        inline ~TAutoLockFreeQueue() {
            TRef tmp;

            while (Dequeue(&tmp)) {
            }
        }

        inline bool Dequeue(TRef* t) {
            T* res = nullptr;

            if (Q_.Dequeue(&res)) {
                t->Reset(res);

                return true;
            }

            return false;
        }

        inline void Enqueue(TRef& t) {
            Q_.Enqueue(t.Get());
            Y_UNUSED(t.Release());
        }

        inline size_t Size() {
            return Q_.GetCounter().Val();
        }

    private:
        TLockFreeQueue<T*, TCounter> Q_;
    };
}