aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/neh/pipequeue.h
blob: bed8d44bd2c2decbaa1c5ce5ee2a3424c9de5f86 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#pragma once

#include "lfqueue.h"

#include <library/cpp/coroutine/engine/impl.h>
#include <library/cpp/coroutine/engine/network.h>
#include <library/cpp/deprecated/atomic/atomic.h>
#include <util/system/pipe.h>

#ifdef _linux_
#include <sys/eventfd.h>
#endif

#if defined(_bionic_) && !defined(EFD_SEMAPHORE)
#define EFD_SEMAPHORE 1
#endif

namespace NNeh {
#ifdef _linux_
    class TSemaphoreEventFd {
    public:
        inline TSemaphoreEventFd() {
            F_ = eventfd(0, EFD_NONBLOCK | EFD_SEMAPHORE);
            if (F_ < 0) {
                ythrow TFileError() << "failed to create a eventfd";
            }
        }

        inline ~TSemaphoreEventFd() {
            close(F_);
        }

        inline size_t Acquire(TCont* c) {
            ui64 ev;
            return NCoro::ReadI(c, F_, &ev, sizeof ev).Processed();
        }

        inline void Release() {
            const static ui64 ev(1);
            (void)write(F_, &ev, sizeof ev);
        }

    private:
        int F_;
    };
#endif

    class TSemaphorePipe {
    public:
        inline TSemaphorePipe() {
            TPipeHandle::Pipe(S_[0], S_[1]);

            SetNonBlock(S_[0]);
            SetNonBlock(S_[1]);
        }

        inline size_t Acquire(TCont* c) {
            char ch;
            return NCoro::ReadI(c, S_[0], &ch, 1).Processed();
        }

        inline size_t Acquire(TCont* c, char* buff, size_t buflen) {
            return NCoro::ReadI(c, S_[0], buff, buflen).Processed();
        }

        inline void Release() {
            char ch = 13;
            S_[1].Write(&ch, 1);
        }

    private:
        TPipeHandle S_[2];
    };

    class TPipeQueueBase {
    public:
        inline void Enqueue(void* job) {
            Q_.Enqueue(job);
            S_.Release();
        }

        inline void* Dequeue(TCont* c, char* ch, size_t buflen) {
            void* ret = nullptr;

            while (!Q_.Dequeue(&ret) && S_.Acquire(c, ch, buflen)) {
            }

            return ret;
        }

        inline void* Dequeue() noexcept {
            void* ret = nullptr;

            Q_.Dequeue(&ret);

            return ret;
        }

    private:
        TLockFreeQueue<void*> Q_;
        TSemaphorePipe S_;
    };

    template <class T, size_t buflen = 1>
    class TPipeQueue {
    public:
        template <class TPtr>
        inline void EnqueueSafe(TPtr req) {
            Enqueue(req.Get());
            req.Release();
        }

        inline void Enqueue(T* req) {
            Q_.Enqueue(req);
        }

        template <class TPtr>
        inline void DequeueSafe(TCont* c, TPtr& ret) {
            ret.Reset(Dequeue(c));
        }

        inline T* Dequeue(TCont* c) {
            char ch[buflen];

            return (T*)Q_.Dequeue(c, ch, sizeof(ch));
        }

    protected:
        TPipeQueueBase Q_;
    };

    //optimized for avoiding unnecessary usage semaphore + use eventfd on linux
    template <class T>
    struct TOneConsumerPipeQueue {
        inline TOneConsumerPipeQueue()
            : Signaled_(0)
            , SkipWait_(0)
        {
        }

        inline void Enqueue(T* job) {
            Q_.Enqueue(job);

            AtomicSet(SkipWait_, 1);
            if (AtomicCas(&Signaled_, 1, 0)) {
                S_.Release();
            }
        }

        inline T* Dequeue(TCont* c) {
            T* ret = nullptr;

            while (!Q_.Dequeue(&ret)) {
                AtomicSet(Signaled_, 0);
                if (!AtomicCas(&SkipWait_, 0, 1)) {
                    if (!S_.Acquire(c)) {
                        break;
                    }
                }
                AtomicSet(Signaled_, 1);
            }

            return ret;
        }

        template <class TPtr>
        inline void EnqueueSafe(TPtr req) {
            Enqueue(req.Get());
            Y_UNUSED(req.Release());
        }

        template <class TPtr>
        inline void DequeueSafe(TCont* c, TPtr& ret) {
            ret.Reset(Dequeue(c));
        }

    protected:
        TLockFreeQueue<T*> Q_;
#ifdef _linux_
        TSemaphoreEventFd S_;
#else
        TSemaphorePipe S_;
#endif
        TAtomic Signaled_;
        TAtomic SkipWait_;
    };

    template <class T, size_t buflen = 1>
    struct TAutoPipeQueue: public TPipeQueue<T, buflen> {
        ~TAutoPipeQueue() {
            while (T* t = (T*)TPipeQueue<T, buflen>::Q_.Dequeue()) {
                delete t;
            }
        }
    };

    template <class T>
    struct TAutoOneConsumerPipeQueue: public TOneConsumerPipeQueue<T> {
        ~TAutoOneConsumerPipeQueue() {
            T* ret = nullptr;

            while (TOneConsumerPipeQueue<T>::Q_.Dequeue(&ret)) {
                delete ret;
            }
        }
    };
}